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-1510090: SNOW-1524103 Adding support for passing nil to variant columns #1170

Open
sahilsalim99 opened this issue Jun 27, 2024 · 5 comments
Assignees
Labels
enhancement The issue is a request for improvement or a new feature status-triage_done Initial triage done, will be further handled by the driver team

Comments

@sahilsalim99
Copy link

What is the current behavior?

My code
_, err = db.ExecContext(ctx, "INSERT INTO test_variant SELECT ?", nil)

The above results in

Error inserting into Snowflake: 002023 (22000): SQL compilation error:
Expression type does not match column data type, expecting VARIANT but got VARCHAR(16777216) for column VAR_COL

What is the desired behavior?

Being able to pass nil to variant columns while binding

How would this improve gosnowflake?

It would make it easier to work with variant columns.

The current workaround we're making use of involves wrapping it with a TRY_PARSE_JSON/PARSE_JSON :
_, err = db.ExecContext(ctx, "INSERT INTO test_variant SELECT TRY_PARSE_JSON(?)", nil)

@github-actions github-actions bot changed the title Adding support for passing nil to variant columns SNOW-1510090: Adding support for passing nil to variant columns Jun 27, 2024
@sfc-gh-dszmolka
Copy link
Contributor

hi there - as an alternative, would it be possible to try something like this #831 (comment) ?

@sfc-gh-dszmolka sfc-gh-dszmolka self-assigned this Jun 28, 2024
@sfc-gh-dszmolka sfc-gh-dszmolka added question Issue is a usage/other question rather than a bug status-triage_done Initial triage done, will be further handled by the driver team labels Jun 28, 2024
@sahilsalim99
Copy link
Author

For our use case we do not have info on the data type of the columns we're trying to insert into. Hence it would be great if passing nil to a variant column would work as it does for other data types.

@sfc-gh-dszmolka
Copy link
Contributor

sfc-gh-dszmolka commented Jul 5, 2024

tried one of the solutions for myself, which i suggested above

  1. create table test_db.public.go1170 (c1 variant);
...
	insertQuery := "INSERT INTO test_db.public.go1170 (c1) SELECT (?)"
	
	fmt.Printf("Inserting VARIANT into table: %v\n", insertQuery)
	_, err = conn.ExecContext(ctx, insertQuery,
	//	nil, --> this is indeed mapped to TEXT
		sql.NullInt64{}, // --> this is not
	)
	if err != nil {
		log.Fatalf("failed to run the query. %v, err: %v", insertQuery, err)
	}
  1. execute test script; relevant log snippets showing the input is not bound to TEXT (=VARCHAR) datatype anymore
INFO[0000]connection.go:301 gosnowflake.(*snowflakeConn).ExecContext Exec: "INSERT INTO test_db.public.go1170 (c1) SELECT (?)", [{ 1 {0 false}}] 
DEBU[0000]log.go:176 gosnowflake.(*defaultLogger).Debugf empty qcc                                    
INFO[0000]connection.go:118 gosnowflake.(*snowflakeConn).exec parameters: map[]                            
DEBU[0000]log.go:176 gosnowflake.(*defaultLogger).Debugf TYPE: sql.NullInt64, {0 false}               
INFO[0000]connection.go:127 gosnowflake.(*snowflakeConn).exec bindings: map[1:{FIXED <nil>  <nil>}]   
  1. then query the table on Snowsight GUI
select * from test_db.public.go1170 ;

C1
--
<null>

I was able to insert null value into VARIANT column without needing to pass it to PARSE_JSON() first.
Would this help you ?

@sahilsalim99
Copy link
Author

I was able to insert null value into VARIANT column without needing to pass it to PARSE_JSON() first. Would this help you ?

Currently we are inserting null in all other data types using nil.. would it be possible to replace nil with sql.NullInt64{} without running into any issues for all the other data types?

Also, are there plans to add support for nil binding for variant columns in the future?

@sfc-gh-pfus
Copy link
Collaborator

Hi @sahilsalim99 ! Variants are quite specific, because they can contain anything. It can be a string, an int or an object. If you just want to insert nil, we don't know what type is required.
We are planning to add support to binding nil variants, but this is still going to be something like:

sql.Exec("INSERT INTO test_variant VALUES (?)", DataTypeNilVariant, reflect.TypeOf(1))
sql.Exec("INSERT INTO test_variant VALUES (?)", DataTypeNilVariant, reflect.TypeOf(""))

@sfc-gh-dszmolka sfc-gh-dszmolka added enhancement The issue is a request for improvement or a new feature and removed question Issue is a usage/other question rather than a bug labels Jul 8, 2024
@sfc-gh-dszmolka sfc-gh-dszmolka changed the title SNOW-1510090: Adding support for passing nil to variant columns SNOW-1510090: SNOW-1524103 Adding support for passing nil to variant columns Jul 8, 2024
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 status-triage_done Initial triage done, will be further handled by the driver team
Projects
None yet
Development

No branches or pull requests

4 participants