Skip to content

Allias put in order by clause #2626

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
ALekseiMokhov opened this issue Sep 2, 2022 · 4 comments
Closed

Allias put in order by clause #2626

ALekseiMokhov opened this issue Sep 2, 2022 · 4 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL revisit-after-parser-rewrite

Comments

@ALekseiMokhov
Copy link

ALekseiMokhov commented Sep 2, 2022

Good day! We face problems with migration from spring data jpa 2.5.7 to 2.5.8 and higher. Case: an interface projection query with sorting and pagination:

@Query("select` max(resource.name)             as resourceName," +
            " max(resource.id) as id," +
            " max(resource.description) as description," +
            " max(resource.uuid) as uuid," +
            " max(resource.type)        as type," +
            " max(resource.createdOn)  as createdOn," +
            " max(users.firstName)     as authorFirstName," +
            " max(users.lastName)      as authorLastName," +
            " max(file.version)         as version," +
            " max(file.comment)         as comment," +
            " file.deployed             as deployed," +
            " max(log.date)             as modifiedOn" +
            "where" +
            < predicate clause>
            + "and (cast(:startDate as date) is null or resource.latestLogRecord.date between cast(:startDate as date) and cast(:endDate as date)))" +
          "group by resource.id, file.deployed, log.author.firstName, file.comment ")
    Page<Model> getResourceModelFilter(Pageable pageable,
                                               @Param("workspaceId") Long workspaceId,
                                               @Param("name") @Nullable String name,
                                               @Param("types") @Nullable List<ResourceTypeEnum> types,
                                               @Param("comment") @Nullable String comment,
                                               @Param("modifiedBy") @Nullable String modifiedBy,
                                               @Param("startDate") @Nullable @Temporal Date StartDate,
                                               @Param("endDate") @Nullable @Temporal Date EndDate);

All method params are fields of our projection (Model)
and the problem appeares when we pass param to sort like
sortParam = new Sort.Order(sortParam.getDirection(), "uuid");
The result of generated log order by section:
order by lower(uuid) asc limit ?
while the previous version log shows different:
order by lower(col_3_0_) asc limit ?

Exception thrown notes that column 'uuid' is ambiguos, and manipulation with adding order by clause to the query do not change it.

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

There is backquote at the top and an excluded , so I'm not quite sure how to proceed. Is there a simplified version of this query for us to look at that repeats the bug you're seeing?

Could you include a link to a reproducer, staged on github, ideally using https://start.spring.io, H2 (or Testcontainers for PostGreSQL, etc.), and the version of Spring Boot aligned with your issue?

@gregturn gregturn added revisit-after-parser-rewrite in: query-parser Everything related to parsing JPQL or SQL and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 25, 2023
@gregturn
Copy link
Contributor

This appears to qualify for #2863, where the parser should recognize that uuid is an aliased column and NOT apply the FROM clauses' alias.

A test case to capture would be something like:

@Test
void orderByAliasedColumn() {

	Sort sortParam = Sort.by(Sort.Direction.DESC, "uuid");

	assertThat(createQueryFor("""
			select
				max(resource.name)             as resourceName,
				max(resource.id) as id,
				max(resource.description) as description,
				max(resource.uuid) as uuid,
				max(resource.type)        as type,
				max(resource.createdOn)  as createdOn,
				max(users.firstName)     as authorFirstName,
				max(users.lastName)      as authorLastName,
				max(file.version)         as version,
				max(file.comment)         as comment,
				file.deployed             as deployed,
				max(log.date)             as modifiedOn
			from Resource resource
			where (
				cast(:startDate as date) is null
				or resource.latestLogRecord.date between cast(:startDate as date) and cast(:endDate as date)
			)
			group by resource.id, file.deployed, log.author.firstName, file.comment
			""", sortParam)).isEqualToIgnoringWhitespace("""
			select
				max(resource.name)             as resourceName,
				max(resource.id) as id,
				max(resource.description) as description,
				max(resource.uuid) as uuid,
				max(resource.type)        as type,
				max(resource.createdOn)  as createdOn,
				max(users.firstName)     as authorFirstName,
				max(users.lastName)      as authorLastName,
				max(file.version)         as version,
				max(file.comment)         as comment,
				file.deployed             as deployed,
				max(log.date)             as modifiedOn
			from Resource resource
			where (
				cast(:startDate as date) is null
				or resource.latestLogRecord.date between cast(:startDate as date) and cast(:endDate as date)
			)
			group by resource.id, file.deployed, log.author.firstName, file.comment
			order by uuid desc
			""".trim());
}

The last line of the expected output is the key.

gregturn added a commit that referenced this issue Mar 15, 2023
If a projection of an HQL query is aliased, be that a function call or a simply alias, apply sorting should NOT result in that order parameter having the primary FROM clause's alias prefixed to.

Resolves #2863.
Related: #2626, #2322.
gregturn added a commit that referenced this issue Mar 15, 2023
If a projection of an HQL query is aliased, be that a function call or a simply alias, apply sorting should NOT result in that order parameter having the primary FROM clause's alias prefixed to.

Resolves #2863.
Related: #2626, #2322.
gregturn added a commit that referenced this issue Mar 15, 2023
If a projection of an HQL query is aliased, be that a function call or a simply alias, apply sorting should NOT result in that order parameter having the primary FROM clause's alias prefixed to.

Resolves #2863.
Related: #2626, #2322.
@gregturn gregturn linked a pull request Mar 27, 2023 that will close this issue
gregturn added a commit that referenced this issue Apr 5, 2023
If a projection of either an HQL or JPQL query is aliased, applied sorting should NOT result in that order parameter having the primary FROM clause's alias prefix to it. Same goes for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322.
Original pull request: #2865.
gregturn added a commit that referenced this issue Apr 5, 2023
If a projection of either an HQL or JPQL query is aliased, do NOT prefix the FROM clause's alias prefix to any relevant applied sorting. Same for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322.
Original pull request: #2865.
@gregturn
Copy link
Contributor

gregturn commented Apr 5, 2023

If you check 39e12ea, you should see your original query in there as a test case, verifying it now works.

Feel free to check out Spring Data JPA 3.1.0-SNAPSHOT and test it out for yourself.

gregturn added a commit that referenced this issue Apr 5, 2023
If a projection of either an HQL or JPQL query is aliased, do NOT prefix the FROM clause's alias prefix to any relevant applied sorting. Same for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322, #1655.
Original pull request: #2865.
@gregturn
Copy link
Contributor

Based on this being merged, I'm closing this ticket. If you run into more problems, feel free to open a new ticket.

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 revisit-after-parser-rewrite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants