Skip to content

Accept additional function argument literals for correct native query with pageable rewrite #2884

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

Closed
futzlig opened this issue Mar 23, 2023 · 1 comment
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: enhancement A general enhancement

Comments

@futzlig
Copy link

futzlig commented Mar 23, 2023

I'm migrating a project from Spring Boot 2.7.9 to 3.0.4. I have the following pageable, native query with a list_agg function:

    @Query(
            value = """
                    select s.id as id, s.name as name, gp.points
                    from specialist s
                    left join (select q.specialist_id, listagg(q.points, ';') as points from qualification q group by q.specialist_id) gp on gp.specialist_id = s.id
                    where name like :name
                    """
            , nativeQuery = true)
    Page<SpecialistOverview> overview(@Param("name") String name, Pageable pageable);

Calling this function results under Spring Boot 3.0.x in a DataIntegrityViolationException because the second parameter could not be bound. This ist the generated SQL:

select s.id as id, s.name as name, gp.points
from specialist s
left join (select q.specialist_id, listagg(q.points, ' fetch first ? rows only;') as points from qualification q group by q.specialist_id) gp on gp.specialist_id = s.id
where name like ?

As you can see the "fetch first ? rows only" section is now part of the delimter literal of the list_agg function.

Under Spring Boot 2.7.9 everything works as expected.

Attached projects to reproduce this anomality.

Hibernate6NativeQueryIssue.zip
PageableNativeQuerySpringBoot2.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 23, 2023
@gregturn
Copy link
Contributor

The problem lies in QueryUtils. It is being used to parse this native query, since JSqlParser is not on the classpath. One of the things it tries to do is process function aliases, which is contained in listagg(q.points, ';') as points, such that points would be captured as an alias.

Unfortunately, the ; character is not recognized on the current pattern's list of approved characters, so it stops right there and never sees points, thus causing the the processing to stop there. If you switch to something like ',', then the processing continues, because that is a recognized token.

A flaw in all this is the fact that the , token in the function parsing algorithm of QueryUtils is trying to spot arguments with the functional call. It's a little hard to read the regex to see how it would handle strings like ';' or ',' or whatever. I could simply add ; to the list of approve tokens, but I need to make sure this is done properly considering we don't have a parser for SQL.

gregturn added a commit that referenced this issue Mar 27, 2023
A native SQL function with ';' as an argument, e.g. listagg(a.b, ';'), will fail to find anything after it (like an alias). By adding ';' to the list of approved function tokens, queries with functional aliases will work properly.

Resolves #2884.
@gregturn gregturn added type: bug A general bug in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 27, 2023
@gregturn gregturn linked a pull request Mar 27, 2023 that will close this issue
mp911de pushed a commit that referenced this issue Mar 28, 2023
A native SQL function with ';' as an argument, e.g. listagg(a.b, ';'), will fail to find anything after it (like an alias). By adding ';' to the list of approved function tokens, queries with functional aliases will work properly.

Resolves #2884.
Original pull request: #2891
mp911de added a commit that referenced this issue Mar 28, 2023
Simplify tests.

See #2884
Original pull request: #2891
mp911de added a commit that referenced this issue Mar 28, 2023
Simplify tests.

See #2884
Original pull request: #2891
@mp911de mp911de added type: enhancement A general enhancement and removed type: bug A general bug labels Mar 28, 2023
@mp911de mp911de changed the title Native Pageable Query with list_agg Function generates syntactically wrong SQL Accept additional function argument literals for correct native query with pageable rewrite Mar 28, 2023
@mp911de mp911de added this to the 3.0.5 (2022.0.5) milestone Mar 28, 2023
@mp911de mp911de reopened this Mar 28, 2023
@mp911de mp911de closed this as completed Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants