Skip to content

feat(sql): recognize dateadd predicates in time intrinsic filters#6666

Merged
bluestreak01 merged 3 commits intomasterfrom
puzpuzpuz_dateadd_intrinsics
Jan 19, 2026
Merged

feat(sql): recognize dateadd predicates in time intrinsic filters#6666
bluestreak01 merged 3 commits intomasterfrom
puzpuzpuz_dateadd_intrinsics

Conversation

@puzpuzpuz
Copy link
Copy Markdown
Contributor

@puzpuzpuz puzpuzpuz commented Jan 19, 2026

Closes #6506

dateadd() calls are now recognized in time intrinsics.

How It Works

When you write:

WHERE dateadd('m', 15, timestamp) = '2022-03-08T18:30:00.000Z'

The optimizer now transforms it to:

WHERE timestamp = dateadd('m', -15, '2022-03-08T18:30:00.000Z')

Which evaluates to:

WHERE timestamp = '2022-03-08T18:15:00.000Z'

This allows the existing interval-based partition pruning to work, significantly improving query performance by skipping irrelevant partitions.

Changes Made

Added AST rewriting step to WhereClauseParser to transform dateadd(period, stride, designated_ts) OP value into designated_ts OP dateadd(period, -stride, value):

  • rewriteDateaddTimestamp() - Entry point that recursively traverses AND-connected predicates
  • tryRewriteDateaddComparison() - Handles comparison operators (=, !=, <, <=, >, >=)
  • tryRewriteDateaddBetween() - Handles BETWEEN expressions

@puzpuzpuz puzpuzpuz self-assigned this Jan 19, 2026
@puzpuzpuz puzpuzpuz added Enhancement Enhance existing functionality SQL Issues or changes relating to SQL execution labels Jan 19, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 19, 2026

Walkthrough

Implements support for recognizing dateadd function calls on designated timestamp columns in WHERE clause predicates. Adds AST rewrite logic to transform dateadd-wrapped timestamp expressions into forms that existing time intrinsic analysis can recognize, enabling query optimization for filters like dateadd('m', 15, timestamp) = value.

Changes

Cohort / File(s) Summary
Time Intrinsics Enhancement
core/src/main/java/io/questdb/griffin/WhereClauseParser.java, core/src/main/java/io/questdb/griffin/SqlCodeGenerator.java
Adds comprehensive dateadd pattern detection and AST rewriting utilities. New helper methods identify dateadd-on-timestamp patterns, create inverse dateadd nodes with negated strides, and rewrite both comparison and BETWEEN contexts. Updates public extract(...) signature with @NotNull annotations and new parameters including expressionNodePool. Integrates rewrite logic into main extraction workflow via rewriteDateaddTimestamp(...).
Dateadd Intrinsics Test Coverage
core/src/test/java/io/questdb/test/griffin/ExplainPlanTest.java, core/src/test/java/io/questdb/test/griffin/WhereClauseParserTest.java
Adds test method testDateaddIntrinsic() validating explain plan output for dateadd intrinsics across multiple scenarios. Introduces extensive tests for dateadd transformations covering between, equals, greater-than, and less-than predicates with edge cases. Updates helper methods to accept ObjectPool parameter for expression node management.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • nwoolmer
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 19.15% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main feature: adding recognition of dateadd predicates in time intrinsic filters.
Description check ✅ Passed The description comprehensively explains the feature, shows concrete examples of the transformation, and outlines the implementation approach with specific method names.
Linked Issues check ✅ Passed The code changes fully address issue #6506 by implementing AST rewriting logic to recognize and transform dateadd predicates on designated timestamps, enabling partition pruning and improved query performance.
Out of Scope Changes check ✅ Passed All changes are directly related to the objective of recognizing dateadd predicates in time intrinsics, with modifications to WhereClauseParser for AST rewriting and comprehensive test coverage additions.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.40.5)
core/src/main/java/io/questdb/griffin/SqlCodeGenerator.java
core/src/test/java/io/questdb/test/griffin/ExplainPlanTest.java
core/src/test/java/io/questdb/test/griffin/WhereClauseParserTest.java

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@puzpuzpuz puzpuzpuz marked this pull request as ready for review January 19, 2026 12:24
@bluestreak01
Copy link
Copy Markdown
Member

@CodeRabbit review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 19, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 120 / 127 (94.49%)

file detail

path covered line new line coverage
🔵 io/questdb/griffin/WhereClauseParser.java 120 127 94.49%

@bluestreak01 bluestreak01 merged commit bb9e9bc into master Jan 19, 2026
45 checks passed
@bluestreak01 bluestreak01 deleted the puzpuzpuz_dateadd_intrinsics branch January 19, 2026 13:00
@puzpuzpuz
Copy link
Copy Markdown
Contributor Author

@bluestreak01 thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Enhance existing functionality SQL Issues or changes relating to SQL execution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recognize dateadd calls in time instrinsics filter

3 participants