Skip to content

Issue while converting nil SQL parameter to null #193

Closed

Description

@esdrasbeleza

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.

Activity

  1. kravets-levko commented on Mar 12, 2024

    @kravets-levko
    Contributor

    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?

  2. esdrasbeleza commented on Apr 1, 2024

    @esdrasbeleza
    ContributorAuthor

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

  3. added a commit that references this issue on Apr 16, 2024
    aeb5e5d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions