Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while converting nil SQL parameter to null #193

Closed
esdrasbeleza opened this issue Mar 12, 2024 · 2 comments · Fixed by #199
Closed

Issue while converting nil SQL parameter to null #193

esdrasbeleza opened this issue Mar 12, 2024 · 2 comments · Fixed by #199

Comments

@esdrasbeleza
Copy link
Contributor

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.

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
Copy link
Collaborator

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
Copy link
Contributor Author

esdrasbeleza commented 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:

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! 🙏

kravets-levko added a commit that referenced this issue Apr 16, 2024
…er to a query (#199)

- When a `sql.NamedValue` has the field `Value` set to `nil`, the
resulting `cli_service.TSparkParameter` will also have the value `nil`
instead of `*cli_service.TSparkParameterValue{StringValue:
*"%!s(<nil>"}`.
- Add the type `SqlVoid`, following the conventions used in the [NodeJS
connector](https://github.com/databricks/databricks-sql-nodejs/blob/main/lib/DBSQLParameter.ts#L43-L51)
and the [Python
driver](https://github.com/databricks/databricks-sql-python/blob/f6fd7a7956a4dbc78ad36b5e079fe8d74176a0f1/src/databricks/sql/parameters/native.py#L319-L323).

Fix #193.

---------

Signed-off-by: Esdras Beleza <esdras@esdrasbeleza.com>
Signed-off-by: Levko Kravets <levko.ne@gmail.com>
Signed-off-by: candiduslynx <candiduslynx@gmail.com>
Co-authored-by: Levko Kravets <levko.ne@gmail.com>
Co-authored-by: Mahdi Dibaiee <mdibaiee@pm.me>
Co-authored-by: Alex Shcherbakov <candiduslynx@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants