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

Support VALUES list in SELECT statements in NamedParameterJdbcTemplate #28921

Open
roookeee opened this issue Aug 3, 2022 · 3 comments
Open
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement

Comments

@roookeee
Copy link

roookeee commented Aug 3, 2022

Preamble: I have been sent here by @schauder in spring-data-relational in regards to spring-projects/spring-data-relational#1300.

Currently JdbcTemplate does not support SELECT statements that use the VALUES keyword. Here is an example from spring-data-jdbc that uses JdbcTemplate under the hood:

@Repository
interface MyRepository {
    @Query("SELECT * FROM my_entity WHERE id IN (VALUES (:ids))")
    List<MyEntity> myQuery(List<String> ids)
}

:ids is not properly expanded (it needs to put every list entry into ()) and thus generates wrong SQL.
I know this would be complex to support for complex objects types as IN-statements works with tuples in some databases but the simple, one-valued variant should be pretty straightforward.

At least PostgreSQL generates different plans for a simple IN vs IN VALUES clause, especially when the input list is big (>100) which perform quite differently (10-30% worse for us).
Using a VALUES list is also interesting when using CTE (WITH) to populate a temporary table with user provided input, this is not achievable with an IN statement.

Disclaimer: I haven't checked if VALUES expansion works in custom INSERTS

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 3, 2022
@poutsma poutsma added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Jan 30, 2023
@jhoeller
Copy link
Contributor

I suppose this works fine with regular positional parameters (where we do not parse the provided SQL ourselves), and you are effectively asking for NamedParameterJdbcTemplate support for VALUES, expanding a nested named parameter accordingly?

@jhoeller jhoeller changed the title JdbcTemplate: Missing support for VALUES list in SELECT statements NamedParameterJdbcTemplate: Missing support for VALUES list in SELECT statements Dec 18, 2023
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 18, 2023
@jhoeller jhoeller added this to the General Backlog milestone Dec 18, 2023
@schauder
Copy link
Contributor

schauder commented Jan 3, 2024

I suppose this works fine with regular positional parameters (where we do not parse the provided SQL ourselves), and you are effectively asking for NamedParameterJdbcTemplate support for VALUES, expanding a nested named parameter accordingly?

Yes, this is correct. Spring Data JDBC uses NamedParameterJdbcTemplate and it's named parameters exclusively.

@sbrannen sbrannen changed the title NamedParameterJdbcTemplate: Missing support for VALUES list in SELECT statements NamedParameterJdbcTemplate: Missing support for VALUES list in SELECT statements Jan 3, 2024
@sbrannen sbrannen changed the title NamedParameterJdbcTemplate: Missing support for VALUES list in SELECT statements Support VALUES list in SELECT statements in NamedParameterJdbcTemplate Jan 3, 2024
@bclozel bclozel changed the title Support VALUES list in SELECT statements in NamedParameterJdbcTemplate Support VALUES list in SELECT statements in NamedParameterJdbcTemplate Feb 14, 2024
@encircled
Copy link
Contributor

@jhoeller, I can prepare a PR for this, but I'm not sure if such a feature belongs in the framework, as it is not supported by jdbc statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants