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

SNOW-176486 How to insert a map[string]string in a variant column? #217

Open
vgrigoriu opened this issue Feb 1, 2019 · 5 comments
Open
Assignees
Labels
enhancement The issue is a request for improvement or a new feature jira copied to JIRA status-triage_done Initial triage done, will be further handled by the driver team

Comments

@vgrigoriu
Copy link

Issue description

I'm trying to insert a Go map in a Snowflake column of type VARIANT. I get an error saying failed to run query: sql: converting argument $2 type: unsupported type map[string]string, a map.

Example code

result, err := db.Exec("insert into SomeTable (some_column) values ($1);",
	map[string]string{"key": "value"})
if err != nil {
	log.Fatalf("failed to run query: %v", err)
}

Configuration

Driver version (or git SHA): latest

Go version: go1.10.1 darwin/amd64

Server version: 3.12.0

Client OS: macos

@smtakeda smtakeda added the jira copied to JIRA label Feb 5, 2019
@ChTimTsubasa ChTimTsubasa self-assigned this Mar 23, 2019
@ChTimTsubasa
Copy link
Contributor

Hi @vgrigoriu , the error is thrown from https://golang.org/src/database/sql/driver/types.go line 281. Which means using DB interface does not support map type.

Besides that, we don't support binding VARIANT on the golang driver for now. You can directly send a query containing the variant if that match you needs. See https://docs.snowflake.net/manuals/sql-reference/data-types-semistructured.html for more info on that.

We will keep this thread open and upgrade here once we support binding VARIANT type.

@smtakeda smtakeda added the enhancement The issue is a request for improvement or a new feature label Sep 30, 2019
@github-actions github-actions bot closed this as completed Jul 1, 2022
@sfc-gh-jfan sfc-gh-jfan reopened this Jul 1, 2022
@github-actions github-actions bot closed this as completed Jul 2, 2022
@sfc-gh-jfan sfc-gh-jfan reopened this Jul 7, 2022
@sfc-gh-dszmolka sfc-gh-dszmolka changed the title How to insert a map[string]string in a variant column? SNOW-176486 How to insert a map[string]string in a variant column? Mar 28, 2023
@sfc-gh-dszmolka
Copy link
Contributor

this is with the driver team already so reopened this issue to properly track the progress when it's any

@sfc-gh-dszmolka
Copy link
Contributor

small update: this turns out to be a new feature request, looking at https://github.com/snowflakedb/gosnowflake/blob/master/bind_uploader.go#L292

		// TODO SNOW-176486 variant, object, array

so thank you everyone for bearing with us while this gets put on the roadmap and eventually worked on.

The workaround will be converting map[string]string to a JSON string and inserting VARIANT data directly using "INSERT INTO ... SELECT":
https://docs.snowflake.com/en/sql-reference/data-types-semistructured#example-of-inserting-a-variant

Example:

	param := map[string]string{"key": "value"}
	jsonStr, _ := json.Marshal(param)

	db.Exec("create or replace table issue_296(c1 variant)")
	result, err := db.Exec(
		"INSERT INTO issue_296 (c1) SELECT TO_VARIANT(PARSE_JSON(?));",
		string(jsonStr),
	)

@mihaitodor
Copy link

@sfc-gh-dszmolka Thanks! Just curious, why SELECT TO_VARIANT(PARSE_JSON(?)) and not just SELECT PARSE_JSON(?)? It looks like the return type of PARSE_JSON is VARIANT

@sfc-gh-dszmolka
Copy link
Contributor

good catch, should be of no difference, but testing should prove or disprove this assumption

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue is a request for improvement or a new feature jira copied to JIRA status-triage_done Initial triage done, will be further handled by the driver team
Projects
None yet
Development

No branches or pull requests

9 participants