Skip to content

Conversation

kssumin
Copy link

@kssumin kssumin commented Oct 18, 2025

Changes

This PR addresses issue #2856 by adding proper validation to QueryEnhancer implementations to prevent invalid operations on non-SELECT statements.

Summary

Previously, QueryEnhancer would silently return default values or apply inappropriate operations when attempting to:

  • Create count queries for INSERT/UPDATE/DELETE/MERGE statements
  • Apply sorting to INSERT/UPDATE/DELETE/MERGE statements

This behavior was problematic as it could lead to unexpected results or silent failures.

What Changed

  1. Statement Type Detection

    • Added StatementType enum to QueryInformation class
    • Implemented statement type detection using regex pattern matching in DefaultQueryEnhancer
    • Extended introspector classes (JpqlQueryIntrospector, HqlQueryIntrospector, EqlQueryIntrospector) to detect and propagate statement types
  2. Validation Logic

    • QueryEnhancer.rewrite(): Now throws IllegalStateException when attempting to apply sorting to non-SELECT statements
    • QueryEnhancer.createCountQueryFor(): Now throws IllegalStateException when attempting to create count queries for non-SELECT statements
    • Unsorted rewriting is still allowed for all statement types (no-op behavior)
  3. Affected Classes

    • DefaultQueryEnhancer: Added statement type detection and validation
    • JpaQueryEnhancer: Added validation for sorting and count queries
    • JSqlParserQueryEnhancer: Changed from returning original query to throwing exceptions
    • QueryInformation: Added StatementType enum and helper methods
    • HibernateQueryInformation: Extended to support statement type
  4. Test Coverage

    • Added test cases to QueryEnhancerUnitTests for JPQL queries:
      • INSERT/UPDATE/DELETE statements throwing exceptions for count queries
      • INSERT/UPDATE/DELETE statements throwing exceptions for sorting
      • Unsorted operations working for all statement types
    • Added test cases to JSqlParserQueryEnhancerUnitTests for native queries:
      • INSERT/UPDATE statements throwing exceptions for count queries and sorting
      • Unsorted operations working for all statement types

Closes: #2856

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

Add statement type detection to QueryEnhancer implementations to validate
operations. QueryEnhancer now throws IllegalStateException when attempting
to create count queries or apply sorting to INSERT, UPDATE, DELETE, or MERGE
statements, as these operations are only valid for SELECT queries.

Signed-off-by: kssumin <ksoomin25@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revisit QueryEnhancer exception handling

2 participants