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

PostgreSQL mode: STRING_AGG with prepared statement parameter not working #3619

Closed
coder-hugo opened this issue Sep 5, 2022 · 4 comments · Fixed by #3620
Closed

PostgreSQL mode: STRING_AGG with prepared statement parameter not working #3619

coder-hugo opened this issue Sep 5, 2022 · 4 comments · Fixed by #3620

Comments

@coder-hugo
Copy link

In version 2 of the h2 database it's not possible anymore to use a prepared statement parameter for the separator of the STRING_AGG function (PostgreSQL mode).
In version 1 you could do the following:

PreparedStatement statement = connection.prepareStatement("select t1.id, string_agg(t2.value, ?) from t1 left join t2 on t2.t1_id = t1.id group by t1.id");
statement.setString(1, ",");

and you will receive a result that will look like this:

| 1 | v1,v2,v3 |
| 2 | v2,v5    |

If you do the same with version 2 of the h2 database, the result looks like this:

| 1 | v1v2v3 |
| 2 | v2v5   |

So somehow the comma gets lost in the prepared statement. If you don't use a prepared statement parameter for the separator but set it fixed to statement (string_agg(t2.value, ',')) you will receive the correct result. So in general the STRING_AGG function in the PostgresSQL mode seems to work.

@katzyn
Copy link
Contributor

katzyn commented Sep 5, 2022

This issue appears because PostgreSQL-specific STRING_AGG is translated to standard LISTAGG function supported by H2. But LISTAGG allows only character string literals as separators and H2 doesn't preserve the original expression here and takes its value immediately. Unfortunately, it means parameters cannot work here, so this implementation needs to be extended to support JDBC parameters too.

@coder-hugo
Copy link
Author

Thanks, for the fast reply. I just had a short look into the code, to check if I could provide a PR for this. But it looks like there is currently no aggregate function that supports JDBC parameters, so it will be a larger change. Am I right?

@katzyn
Copy link
Contributor

katzyn commented Sep 5, 2022

Almost all of them support any expressions including parameters. In H2, unlike in the most of JDBC drivers, parameters are special kind of expressions. But LISTAGG handles some arguments as direct values and not as expressions.

@coder-hugo
Copy link
Author

Thanks for the fast fix. Is there any schedule for the next release that will contain this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants