Skip to content

ES|QL: Validate TOP_SNIPPETS query argument is foldable#142763

Merged
carlosdelest merged 29 commits intoelastic:mainfrom
mridula-s109:topsnippet/foldable
Mar 12, 2026
Merged

ES|QL: Validate TOP_SNIPPETS query argument is foldable#142763
carlosdelest merged 29 commits intoelastic:mainfrom
mridula-s109:topsnippet/foldable

Conversation

@mridula-s109
Copy link
Copy Markdown
Contributor

@mridula-s109 mridula-s109 commented Feb 20, 2026

Implements compile-time validation for the TOP_SNIPPETS function to ensure that its query parameter is foldable (constant). This follows the same pattern as other full-text functions like Kql/FullTextFunction.

Changes:

TopSnippets.java: Added foldable query validation in resolveType() using Foldables.resolveTypeQuery(), following the Kql/FullTextFunction pattern
TopSnippetsTests.java: Updated assertion style per code review feedback
VerifierTests.java: Added analyzer-level verification tests for query foldability
top-snippets.csv-spec: Added integration tests for foldable query validation
230_folding.yml: Added YAML REST integration tests for folding
docs/changelog/142462.yaml: Added changelog entry

Fixes #142462

…on time

Implements compile-time validation for the TOP_SNIPPETS function to ensure
that its query parameter is foldable (constant). This follows the same
pattern as other full-text functions like Match.

Changes:
- TopSnippets.java: Implements PostOptimizationVerificationAware interface
  and adds postOptimizationVerification() method using Foldables.resolveTypeQuery()
- TopSnippetsValidationTests.java: New unit tests for validation logic
- TopSnippetsTests.java: Updated assertion style per code review feedback
- LogicalPlanOptimizerTests.java: Added post-optimization validation tests
- top-snippets.csv-spec: Added integration tests for foldable query validation
- docs/changelog/142462.yaml: Added changelog entry

Fixes elastic#142462
@mridula-s109 mridula-s109 self-assigned this Feb 20, 2026
@mridula-s109 mridula-s109 added >enhancement Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch :Search Relevance/ES|QL Search functionality in ES|QL v9.4.0 labels Feb 20, 2026
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @mridula-s109, I've created a changelog YAML for you.

@mridula-s109 mridula-s109 changed the title ES|QL: Validate TOP_SNIPPETS query argument is foldable at verification ES|QL: Validate TOP_SNIPPETS query argument is foldable Feb 20, 2026
@mridula-s109 mridula-s109 marked this pull request as ready for review February 23, 2026 19:01
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @mridula-s109, I've updated the changelog YAML for you.

Copy link
Copy Markdown
Member

@carlosdelest carlosdelest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Some minor questions about tests

- match: { values.0.2: "Alice Smith" }

---
TOP_SNIPPETS with foldable query:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need a capability for mixed clusters? Will this work when issued against an older cluster? 🤔

@mridula-s109
Copy link
Copy Markdown
Contributor Author

Thanks for the comments @carlosdelest and @ioanatia, i am looking into them, will rerequest review post addressing them.

@carlosdelest carlosdelest self-assigned this Mar 10, 2026
@carlosdelest carlosdelest requested a review from ioanatia March 12, 2026 09:21
as(registeredDomain.child(), EsRelation.class);
}

public void testTopSnippetsQueryMustBeFoldable() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially suggested to @mridula-s109 to add a similar test in VerifierTests, because we already had some tests that were checking for constant arguments.
If the validation is actually done post-optimization, it makes sense to have the test here.

@carlosdelest carlosdelest merged commit 47d658b into elastic:main Mar 12, 2026
36 checks passed
jfreden pushed a commit to jfreden/elasticsearch that referenced this pull request Mar 12, 2026
* ES|QL: Validate TOP_SNIPPETS query argument is foldable at verification time

Implements compile-time validation for the TOP_SNIPPETS function to ensure
that its query parameter is foldable (constant). This follows the same
pattern as other full-text functions like Match.

Changes:
- TopSnippets.java: Implements PostOptimizationVerificationAware interface
  and adds postOptimizationVerification() method using Foldables.resolveTypeQuery()
- TopSnippetsValidationTests.java: New unit tests for validation logic
- TopSnippetsTests.java: Updated assertion style per code review feedback
- LogicalPlanOptimizerTests.java: Added post-optimization validation tests
- top-snippets.csv-spec: Added integration tests for foldable query validation
- docs/changelog/142462.yaml: Added changelog entry

Fixes elastic#142462

* Update docs/changelog/142763.yaml

* [CI] Auto commit changes from spotless

* [CI] Auto commit changes from spotless

* Update 142763.yaml

* Delete docs/changelog/142462.yaml

* Removed validation test

# Conflicts:
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/TopSnippetsValidationTests.java

* Removed the constantQuery Test

* Moved validation to verification level

* tests added

* modified the test cases

* Update TopSnippets.java

* [CI] Auto commit changes from spotless

* fixed bug

* [CI] Auto commit changes from spotless

* Update docs/changelog/142763.yaml

* Add foldable validation to TOP_SNIPPETS query parameter

* [CI] Auto commit changes from spotless

* Fix YAML tests

* Fix tests

* Remove protected access

* Use postOptimizationVerification to let optimizer to fold constants before checking for Literals

* Move test to optimization tests as folding check happens there

---------

Co-authored-by: elasticsearchmachine <infra-root+elasticsearchmachine@elastic.co>
Co-authored-by: carlosdelest <carlos.delgado@elastic.co>
Co-authored-by: Carlos Delgado <6339205+carlosdelest@users.noreply.github.com>
michalborek pushed a commit to michalborek/elasticsearch that referenced this pull request Mar 23, 2026
* ES|QL: Validate TOP_SNIPPETS query argument is foldable at verification time

Implements compile-time validation for the TOP_SNIPPETS function to ensure
that its query parameter is foldable (constant). This follows the same
pattern as other full-text functions like Match.

Changes:
- TopSnippets.java: Implements PostOptimizationVerificationAware interface
  and adds postOptimizationVerification() method using Foldables.resolveTypeQuery()
- TopSnippetsValidationTests.java: New unit tests for validation logic
- TopSnippetsTests.java: Updated assertion style per code review feedback
- LogicalPlanOptimizerTests.java: Added post-optimization validation tests
- top-snippets.csv-spec: Added integration tests for foldable query validation
- docs/changelog/142462.yaml: Added changelog entry

Fixes elastic#142462

* Update docs/changelog/142763.yaml

* [CI] Auto commit changes from spotless

* [CI] Auto commit changes from spotless

* Update 142763.yaml

* Delete docs/changelog/142462.yaml

* Removed validation test

# Conflicts:
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/TopSnippetsValidationTests.java

* Removed the constantQuery Test

* Moved validation to verification level

* tests added

* modified the test cases

* Update TopSnippets.java

* [CI] Auto commit changes from spotless

* fixed bug

* [CI] Auto commit changes from spotless

* Update docs/changelog/142763.yaml

* Add foldable validation to TOP_SNIPPETS query parameter

* [CI] Auto commit changes from spotless

* Fix YAML tests

* Fix tests

* Remove protected access

* Use postOptimizationVerification to let optimizer to fold constants before checking for Literals

* Move test to optimization tests as folding check happens there

---------

Co-authored-by: elasticsearchmachine <infra-root+elasticsearchmachine@elastic.co>
Co-authored-by: carlosdelest <carlos.delgado@elastic.co>
Co-authored-by: Carlos Delgado <6339205+carlosdelest@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>enhancement :Search Relevance/ES|QL Search functionality in ES|QL Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ES|QL TOP_SNIPPETS: Validate query argument is foldable at verification time

4 participants