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-906944: Implement PreparedStatement.setArray #1506

Closed
mftruso opened this issue Sep 5, 2023 · 4 comments
Closed

SNOW-906944: Implement PreparedStatement.setArray #1506

mftruso opened this issue Sep 5, 2023 · 4 comments
Assignees
Labels
feature status-triage_done Initial triage done, will be further handled by the driver team

Comments

@mftruso
Copy link

mftruso commented Sep 5, 2023

What is the current behavior?

Not implemented, not documented. Arrays must be converted to a String and then parsed back into an Array in Snowflake.

String query = "call some_procedure(strtok_to_array(?, ','))"
 
List args = ["a", "b", "c"]
PreparedStatement ps = conn.prepareStatement(query)
ps.setString(0, args.join(','))

What is the desired behavior?

Have direct support for Arrays by implementing setArray

String query = "call some_procedure(?)"
 
List args = ["a", "b", "c"]
PreparedStatement ps = conn.prepareStatement(query)
ps.setArray(0, args)

How would this improve snowflake-jdbc?

Parity with java.sql.PreparedStatement. Tighter integration with snowflake types. Less String manipulation.

References, Other Background

A previous issue was created for this but never acknowledged: #382

@mftruso mftruso added the feature label Sep 5, 2023
@github-actions github-actions bot changed the title Implement PreparedStatement.setArray SNOW-906944: Implement PreparedStatement.setArray Sep 5, 2023
@sfc-gh-spanaite
Copy link
Contributor

@sfc-gh-anugupta FYI

@sfc-gh-dszmolka
Copy link
Contributor

hi and apologies it took so long - it looks like the functionality was implemented as part of structured types support , which change was just released yesterday with v3.16.0

@sfc-gh-dszmolka sfc-gh-dszmolka added the status-triage_done Initial triage done, will be further handled by the driver team label Apr 30, 2024
@nikammerlaan
Copy link

nikammerlaan commented May 23, 2024

Hey @sfc-gh-dszmolka, thank you for implementing! Unfortunately, I haven't been able to work out the intended way to use this. I created an Array instance from the connection using createArrayOf, but receive an opaque error from the server: Expression type does not match column data type, expecting ARRAY but got VARCHAR(1) for column {my column name} Additionally, I tried using setObject with a normal Java array, but did not have any success. Could you clarify how this is meant to be used?

@sfc-gh-dszmolka
Copy link
Contributor

hi @nikammerlaan double checked with the team implementing the feature and per their feedback, should work if the Structured Types (public preview) feature is enabled on the server-side. Considering its documentation, it says it should be already available for all accounts.

An example which has been tested and worked:

        SnowflakePreparedStatementV1 stmt =
            (SnowflakePreparedStatementV1)
                connection.prepareStatement(
                    "INSERT INTO array_of_integers (arrayInt) SELECT ?;"); ) {

      statement.execute(" CREATE OR REPLACE TABLE array_of_integers(arrayInt ARRAY(INTEGER))");

      Array array = connection.createArrayOf("INTEGER", new Integer[] {1, 2, 3});
      stmt.setArray(1, array);
      stmt.executeUpdate();

can you please test it and see how it works for you?
If you still have any errors, I advise to open a new issue and not use a closed one (which is resolved since the ask was to implement setArray which is available and implemented). Thank you in advance !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature status-triage_done Initial triage done, will be further handled by the driver team
Projects
None yet
Development

No branches or pull requests

5 participants