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

Using Pagination with CTE query generates wrong countQuery #3752

Open
saadalmogren opened this issue Jan 24, 2025 · 2 comments
Open

Using Pagination with CTE query generates wrong countQuery #3752

saadalmogren opened this issue Jan 24, 2025 · 2 comments
Assignees
Labels
status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged

Comments

@saadalmogren
Copy link

Hello,
I am using the latest spring-data-jpa:3.4.2 with postgresql and faced the below issue:

using two CTE Queries with native query enabled as below:

@Query(value = """
            WITH credit_transactions AS (
                SELECT
                    *
                FROM transaction trx
                WHERE trx."CreditDebit" = 'CR' AND trx."Message Reference Id" > :transactionIdOffset
            ),
            debit_transactions AS (
                SELECT
                    *
                FROM transaction trx
                WHERE trx."CreditDebit" = 'DR' AND trx."Message Reference Id" > :transactionIdOffset
            )
            SELECT
                COALESCE(c."Message Reference Id", d."Message Reference Id") AS "Message Reference Id",
            FROM credit_transactions c
            FULL OUTER JOIN debit_transactions d
                ON c."Message Reference Id" = d."Message Reference Id" ORDER BY "Message Reference Id" ASC 
            """,
            nativeQuery = true)
    Page<Transaction> getTransactionsByOffset(@Param("transactionIdOffset") Long transactionIdOffset, Pageable pageable);

but hibernate executes the following query

WITH credit_transactions AS (select count(trx) FROM transaction trx
    WHERE trx."CreditDebit" = 'CR' AND trx."Message Reference Id" > ?
),
debit_transactions AS (
    SELECT
        *
    FROM transaction trx
    WHERE trx."CreditDebit" = 'DR' AND trx."Message Reference Id" > ?
)
SELECT
COALESCE(c."Message Reference Id", d."Message Reference Id") AS "Message Reference Id",
FROM credit_transactions c
FULL OUTER JOIN debit_transactions d
    ON c."Message Reference Id" = d."Message Reference Id"

and throws the following error:

Caused by: org.postgresql.util.PSQLException: ERROR: column c.Message Reference Id does not exist
  Hint: Perhaps you meant to reference the column "d.Message Reference Id".

CAUSE:
It changed the first CTE with:

WITH credit_transactions AS (select count(trx) FROM transaction trx
    WHERE trx."CreditDebit" = 'CR' AND trx."Message Reference Id" > ?
)

Workaround:
I was able to workaround the issue by specifying an explicit countQuery. Thanks to: #3726 (comment)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 24, 2025
@mp911de mp911de self-assigned this Jan 27, 2025
@mp911de
Copy link
Member

mp911de commented Jan 31, 2025

Spring Data requires JSqlParser for overly complex queries that use functions or more advanced features beyond a simple SELECT statement with regular column projections. See also our reference documentation on native query rewriting. Dropping JSqlParser onto the class path seems to generate the correct query. Care to verify?

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Jan 31, 2025
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants