databricks / databricks-sql-go

Golang database/sql driver for Databricks SQL.
Apache License 2.0
34 stars 37 forks source link

Issue while converting `nil` SQL parameter to `null` #193

Closed esdrasbeleza closed 2 months ago

esdrasbeleza commented 3 months ago

I was trying to use a query like this. I expected my query to return all results if it's not filtered, and filter the result if there's a filter.

SELECT *
FROM some_table
WHERE ISNULL(:mmCode) OR name = :mmCode

and then I pass sql.Named("mmCode", code) to my query. code is a *string. It can, of course, be nil.

Expected behaviour

Actual behaviour

I noticed that the library tries to use fmt.Sprintf("%s", param.Value) in parameters.go. If param.Value is nil, it becomes "%s(<nil>)" and my query won't work.

image

Workaround

I'm now manually creating my WHERE clause depending of the values I have for each filter parameter, but being able to use ISNULL would and inject nil parameters make my code shorter and cleaner.

I tried to write a PR myself, but I'm not familiar with the Spark parameters API.

kravets-levko commented 3 months ago

Hi @esdrasbeleza! Thank you for reporting this. Yes, NULL values need special handling (like it's done in Nodejs connector or in Python driver). However, seems that Go driver handles it differently (I'm not a Go expert and may be wrong, though). @andrefurlan-db can you please take a look?

esdrasbeleza commented 3 months ago

I tried to read the code for Node.js and Python and implement the same behaviour in Go. My previous code now seems to work as expected if I run this query:

query := `SELECT * FROM my_table
            WHERE ISNULL(:SOME_FIELD) OR SOME_FIELD = :SOME_FIELD
            LIMIT 10`
params := []interface{}{
    sql.Named("SOME_FIELD", nil),
}

Please check my PR! 🙏