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

Broken count query for native queries with table alias #2773

Closed
hpoettker opened this issue Jan 23, 2023 · 3 comments
Closed

Broken count query for native queries with table alias #2773

hpoettker opened this issue Jan 23, 2023 · 3 comments
Assignees
Labels
type: bug A general bug

Comments

@hpoettker
Copy link

hpoettker commented Jan 23, 2023

The following repository currently does not work with MySQL:

public interface SomeRepository extends CrudRepository<SomeEntity, Long> {

  @Query(
      value = "SELECT * FROM table_name some_alias",
      nativeQuery = true
  )
  Page<SomeEntity> search(Pageable pageable);

}

The repository worked fine with Spring Data JPA 2.6.3, has been broken since 2.6.4, and is still broken in 2.7.7.

The log on failure is

Hibernate: SELECT * FROM table_name some_alias ?
Hibernate: select count(some_alias) FROM table_name some_alias
WARN --- o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22
ERROR --- o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'some_alias' in 'field list'

The problem is the count query that should be select count(*) FROM table_name some_alias with * instead of the alias as parameter to COUNT.

The regression seems to have been introduced with #2389 as the PR contains a test case for the erroneous behavior.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 23, 2023
@mp911de mp911de self-assigned this Jan 25, 2023
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 25, 2023
@mp911de
Copy link
Member

mp911de commented Jan 25, 2023

The query rewrite doesn't distinguish between native an JPQL queries. In JPQL, this is a valid scheme to count on From level while SQL mandates columns or expressions. It's a bug that we need to fix.

schauder pushed a commit that referenced this issue Jan 30, 2023
De-duplicate code, use parametrized tests instead of test methods to verify individual fixtures. Ensure all variants are tested with JSQLparser and the default enhancer.

See #2773
Original pull request #2777
schauder pushed a commit that referenced this issue Jan 30, 2023
We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.

Closes #2773
Original pull request #2777
schauder pushed a commit that referenced this issue Jan 30, 2023
De-duplicate code, use parametrized tests instead of test methods to verify individual fixtures. Ensure all variants are tested with JSQLparser and the default enhancer.

See #2773
Original pull request #2777
@hpoettker
Copy link
Author

Thanks for implementing a fix so quickly after I created the issue!

As the issue is assigned to the milestone 2.7.8, this may be a stupid question, but will the fix be backported? I'm asking because the issue is closed but there is no respective commit on the branch 2.7.x.

mp911de added a commit that referenced this issue Feb 15, 2023
We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.

Closes #2773
Original pull request #2777
@mp911de
Copy link
Member

mp911de commented Feb 15, 2023

Thanks for reaching out. The missing backport was an oversight on our end. The backport is now in the 2.7.x branch and you should be able to verify the fix in the artifact that is being currently built by our CI.

@mp911de mp911de changed the title Broken count query for native MySQL queries with table alias Broken count query for native queries with table alias Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
3 participants