Skip to content

JSqlParserQueryEnhancer removes WITH clauses for paginated and sorted native queries #3263

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
lumi77 opened this issue Dec 8, 2023 · 3 comments
Assignees
Labels
type: bug A general bug

Comments

@lumi77
Copy link

lumi77 commented Dec 8, 2023

Repository class:

@Repository
public interface ProjectsRepository extends JpaRepository<Project, Integer> {
    String QUERY = """
            WITH all_projects AS
            (
                SELECT *
                FROM projects
            )
            SELECT * FROM all_projects
            """;

    @Query(value = QUERY, nativeQuery = true)
    List<Project> findAllNativeQuery();

    @Query(value = QUERY, nativeQuery = true)
    Page<Project> findAllNativeQuery(Pageable pageable);
}

Test class for the repository

@DataJpaTest
class ProjectsRepositoryTest {
    @Autowired
    private ProjectsRepository projectsRepository;

    @Test
    void findAll() {
        var actual = projectsRepository.findAllNativeQuery();

        assertThat(actual).isEmpty();
    }

    @Test
    void findAllPageableUnsorted() {
        var actual = projectsRepository.findAllNativeQuery(PageRequest.of(0, 10));

        assertThat(actual.getContent()).isEmpty();
    }

    @Test
    void findAllPageableSorted() {
        var actual = projectsRepository.findAllNativeQuery(PageRequest.of(0, 10, Sort.Direction.ASC, "name"));

        assertThat(actual.getContent()).isEmpty();
    }
}

When JSqlParser is not on the class path, all three tests pass.

When JSqlParser is on the class path, the third tests fails with an exception:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement [Table "ALL_PROJECTS" not found; SQL statement:
SELECT * FROM all_projects ORDER BY name ASC fetch first ? rows only [42102-224]] 
[SELECT * FROM all_projects ORDER BY name ASC fetch first ? rows only]; SQL [SELECT * FROM all_projects ORDER BY name ASC fetch first ? rows only].

Both the printed query "SELECT * FROM all_projects ORDER BY name ASC fetch first ? rows only" and the error message "Table "ALL_PROJECTS" not found" imply that the WITH clause

WITH all_projects AS
           (
               SELECT *
               FROM projects
           )

has been lost while preprocessing the query in JSqlParserQueryEnhancer. This happens in public String applySorting(Sort sort, @Nullable String alias)

JSqlParserQueryEnhancerBug.zip.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 8, 2023
@christophstrobl
Copy link
Member

@lumi77 thanks for reporting and the reproducer!

@christophstrobl christophstrobl self-assigned this Dec 10, 2023
quaff added a commit to quaff/spring-data-jpa that referenced this issue Dec 11, 2023
@quaff
Copy link
Contributor

quaff commented Dec 11, 2023

It should be fixed by #3248.

@christophstrobl
Copy link
Member

Thanks @quaff for linking the PR. We're currently looking into ways to mitigate the issue without (major/minor) version bumps for the upcoming service release.

@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 12, 2023
christophstrobl added a commit that referenced this issue Dec 12, 2023
This commit makes sure to render the entire statement after applying the sort expression via the QueryEnhancer. Previously only parts, the actual statement body, had been considered.

Closes: #3263
Original Pull Request: #3264
christophstrobl added a commit that referenced this issue Dec 12, 2023
This commit makes sure to render the entire statement after applying the sort expression via the QueryEnhancer. Previously only parts, the actual statement body, had been considered.

Closes: #3263
Original Pull Request: #3264
@christophstrobl christophstrobl added this to the 3.1.7 (2023.0.7) milestone Dec 12, 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
Development

Successfully merging a pull request may close this issue.

4 participants