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

Passing a map as an argument to a query #42

Closed
bruth opened this issue Sep 28, 2022 · 6 comments
Closed

Passing a map as an argument to a query #42

bruth opened this issue Sep 28, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@bruth
Copy link

bruth commented Sep 28, 2022

Thank you for creating this library (and just merging #40 for static builds)!

I created a table with a column with a map datatype and when I attempt to insert a record using the Map type I am getting the error:

unsupported type duckdb.Map, a map

I noticed this line has a TODO. Is this for supporting additional data types as passed arguments to queries? Is it a matter at filling out more of these cases?

@marcboeker
Copy link
Owner

Thanks for reporting this. Do you have some sample code that I can use to probably add the missing data type. That would be great. Thanks!

@bruth
Copy link
Author

bruth commented Oct 8, 2022

Thanks for the response and sorry for the delay! I actually hope to have a PR opened up next week some time.

@marcboeker
Copy link
Owner

marcboeker commented Oct 8, 2022

No worries, I've already started with a PR. But the serialisation of the Map{"foo": "bar", "bam": "baz"} to the SQL representation map(['foo', 'bam'], ['bar', 'baz']) is not that trivial, as a map could contain another map as its key or value. For numericals and strings my implementation already works.

@marcboeker
Copy link
Owner

After I have racked my brain about serializing a map to a query parameter, I have checked the DuckDB APIs for prepared statements and have found no way to bind a map to a parameter. After thinking twice about it, I'm unsure what your use case is.
Could you please show me the query you want to use. This would help me to understand the problem better. Thanks!

@andyyu2004
Copy link
Contributor

Hey @marcboeker, I have a motivating example for the same issue except with a slice.

This fails with the error sql: converting argument $1 type: unsupported type []string, a slice of string.

func TestSliceParameter(t *testing.T) {
	db := openDB(t)
	defer db.Close()

	_, err := db.Exec("CREATE TABLE foo (x text)")
	require.NoError(t, err)

	_, err = db.Exec("INSERT INTO foo VALUES ('test')")
	require.NoError(t, err)

	_, err = db.Query("SELECT x FROM foo WHERE x = ANY($1)", []string{"test"})
	require.NoError(t, err)
}

@marcboeker
Copy link
Owner

Hi @andyyu2004, thanks for the example. As we're internally using the DuckDB parameter bind API, there is currently no function for binding slices.

A dirty hack would be to interpolate the placeholder with multiple placeholders an then iterating over the slice and binding each item separately. But I would highly suggest to avoid this, as it can get ugly quickly.

For these kind of use-cases I have used sqlx in the past. See the example under the "IN queries" section on this page. Maybe you could try this?

@marcboeker marcboeker added the enhancement New feature or request label May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants