Skip to content

SQL: Fix QlIllegalArgumentException with non-foldable date range queries#142386

Merged
luigidellaquila merged 8 commits intoelastic:mainfrom
luigidellaquila:sql/fix_137365
Feb 23, 2026
Merged

SQL: Fix QlIllegalArgumentException with non-foldable date range queries#142386
luigidellaquila merged 8 commits intoelastic:mainfrom
luigidellaquila:sql/fix_137365

Conversation

@luigidellaquila
Copy link
Copy Markdown
Contributor

Fixing a NullPointerException due to the attempt to fold a range expression that is not foldable.
We'll translate it into a Painless script instead
Developed using AI-assisted tooling

Fixes: #137365

@luigidellaquila luigidellaquila added >bug :Analytics/SQL SQL querying auto-backport Automatically create backport pull requests when merged branch:9.2 branch:8.19 branch:9.3 labels Feb 12, 2026
@elasticsearchmachine elasticsearchmachine added Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.4.0 labels Feb 12, 2026
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @luigidellaquila, I've created a changelog YAML for you.

Copy link
Copy Markdown
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

Apart from my one comment related to the change, I would advise checking EQL code as well (since this code change is in common shared code between EQL and SQL) to see if/how this is affecting it. If necessary add tests for EQL as well. Thank you

return handler.wrapFunctionQuery(r, r.value(), () -> translate(r, handler));
}
// Fall back to script query for non-foldable bounds (e.g., DATE_ADD with field reference)
return new ScriptQuery(r.source(), r.asScript());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here don't you need to also call wrapIfNested?
Ie

Query q = new ScriptQuery(r.source(), r.asScript());
  return wrapIfNested(q, r.value());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for checking @astefan

I would advise checking EQL code as well

EQL is more restrictive than SQL, the optimizer can create Range objects, but only starting from expressions like field >= expression, and the right hand side expression is always checked and required to be foldable (if it's not, you'll get a Comparisons against fields are not (currently) supported).

Here don't you need to also call wrapIfNested?

That's a good point, let me check and add it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added wrapIfNested for correctness, but I think there is no practical way to make it happen in production. The verifier prevents functions from using nested fields, see https://github.com/elastic/elasticsearch/blob/main/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Verifier.java#L848, so I couldn't reproduce the original problem with a nested field.

Copy link
Copy Markdown
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

LGTM

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

💚 Backport successful

Status Branch Result
9.3
8.19
9.2

luigidellaquila added a commit to luigidellaquila/elasticsearch that referenced this pull request Feb 23, 2026
luigidellaquila added a commit to luigidellaquila/elasticsearch that referenced this pull request Feb 23, 2026
luigidellaquila added a commit to luigidellaquila/elasticsearch that referenced this pull request Feb 23, 2026
szybia added a commit to szybia/elasticsearch that referenced this pull request Feb 23, 2026
…on-sliced-reindex

* upstream/main: (110 commits)
  Add search task watchdog to log hot threads on slow search (elastic#142746)
  Fix return_intermediate_results query param on get async search results (elastic#142875)
  Mute org.elasticsearch.compute.operator.exchange.BatchDriverTests testSinglePageSingleBatch elastic#142895
  Cancel reindex body always has status (elastic#142766)
  Fix built-in roles sync losing updates (elastic#142433)
  ESQL: Clarify docs and add csv test for WHERE in STATS (elastic#133629)
  Fix and unmute ReindexResumeIT (elastic#142788)
  Fix broken release notes
  Mute org.elasticsearch.benchmark.vector.scorer.VectorScorerOSQBenchmarkTests testSingleScalarVsVectorized {p0=384 p1=4 p2=NIO p3=COSINE} elastic#142883
  ES|QL: fix Generative tests for commands that don't change the output schema (elastic#142864)
  Mute org.elasticsearch.benchmark.vector.scorer.VectorScorerOSQBenchmarkTests testSingleScalarVsVectorized {p0=1024 p1=1 p2=NIO p3=DOT_PRODUCT} elastic#142881
  SQL: Fix QlIllegalArgumentException with non-foldable date range queries (elastic#142386)
  Add more errors to the allowed_errors with github issue links (elastic#142862)
  ESQL: reapply "NDJSON datasource" (elastic#142855)
  Add implementation to update service settings method for Alibaba Cloud Search service (elastic#142738)
  Mute org.elasticsearch.snapshots.SnapshotShutdownIT testStartRemoveNodeButDoNotComplete elastic#142871
  Mute org.elasticsearch.snapshots.SnapshotShutdownIT testDeleteSnapshotWithPausedShardSnapshots elastic#142870
  Mute org.elasticsearch.snapshots.SnapshotShutdownIT testAbortSnapshotWhileRemovingNode elastic#142869
  Mute org.elasticsearch.snapshots.SnapshotShutdownIT testRemoveNodeDuringSnapshot elastic#142868
  ES|QL: Guard exponential_histogram TO_STRING against too large inputs (elastic#140718)
  ...
jdconrad pushed a commit to jdconrad/elasticsearch that referenced this pull request Feb 24, 2026
sidosera pushed a commit to sidosera/elasticsearch that referenced this pull request Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/SQL SQL querying auto-backport Automatically create backport pull requests when merged >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.19.13 v9.2.7 v9.3.2 v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL: QlIllegalArgumentException due to failed folding: Cannot determine value for DATE_ADD('days', -30, field)

3 participants