You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issue while converting nil SQL parameter to null #193
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
If name is nil, then it would be converted to NULL in SQL and trigger the first case in my WHERE clause.
If name is not nil, then it would become a string and trigger the second case in my WHERE clause.
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.
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.
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?
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),
}
Activity
kravets-levko commentedon Mar 12, 2024
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 commentedon Apr 1, 2024
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:
Please check my PR! 🙏
Fix Spark parameter creation when passing a `nil`-value named paramet…