spring-projects / spring-projects/spring-framework

Support VALUES list in SELECT statements in NamedParameterJdbcTemplate

Open
#28,921 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: data type: enhancement
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Preamble: I have been sent here by @schauder in spring-data-relational in regards to https://github.com/spring-projects/spring-data-relational/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with NamedParameterJdbcTemplate's collection-parameter expansion for the shown SELECT statement, then compare it with the JdbcTemplate behavior and the linked spring-data-relational context. Done means a single-valued VALUES list is expanded into valid SQL for the example while existing IN-list behavior remains intact, with the behavior verified by relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, sql
Domain
backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.