Skip to content

Conversation

yuancu
Copy link
Collaborator

@yuancu yuancu commented Sep 4, 2025

Description

Support time modifier in search command.

Examples:

source=any earliest='2020-12-10'
source=any earliest='2020-12-10' latest='2025-09-10 13:00:12'
source=any latest=now
source=logs earliest=-7d
source=logs earliest='-1d@d'

Queries with earliest and latest time modifiers will be converted to a query string query with comparison conditions on the implicit timestamp field @timestamp. For example, query source=time_test earliest=-1year latest='-50d@w3' is converted to the following DSL:

{
  "query": {
    "query_string": {
      "query": "(@timestamp:>=now-1y) AND (@timestamp:<=now-50d/w-5d)",
    }
  }
}

Work items

  • support absolute time like 2012-10-10
  • support relative time range like now, -30m, -2h@h, @w0
  • support chained time range offsets like -mon@mon+7d
  • Unix epoch time like 1 (UTC January 1, 1970 at 12:00:01 AM)

Related Issues

Resolves #4135

Implementation Walk-through

Implementation Details

  1. Time Modifier Processing

The core functionality is implemented in the visitTimeModifierValue and visitTimeModifierExpression methods in AstExpressionBuilder.java:

  • visitTimeModifierValue: Converts time values to OpenSearch date math expressions
  • visitTimeModifierExpression: Creates comparison conditions for @timestamp field in query string
  1. Time Format Conversion

The PR extends DateTimeUtils.java with the parseRelativeTime method, which transforms PPL time expressions to OpenSearch date math expressions.

  1. Query String Query Creation

Time modifiers are converted to search comparisons with a query string on the implicit @timestamp field, which is now defined as a constant in OpenSearchConstants.java. For example, search earliest=-10days@month latest=now() is converted to a query string query like the following:

{
  "query": {
    "query_string": {
      "query": "(@timestamp:>=-10d/M) AND (@timestamp:<=now)"
    }
  }
}

PPL Time Modifiers to OpenSearch Date Math Examples

  • Absolute Times

    PPL Time Modifier OpenSearch Date Math Explanation
    '2023-01-01' 2023-01-01 Simple date
    '2023-01-01 13:45:30' 2023-01-01T13:45:30Z Date with time
  • Relative Times

    PPL Time Modifier OpenSearch Date Math Explanation
    now now Current time
    -30s now-30s 30 seconds ago
    -1h now-1h 1 hour ago
    +1d now+1d 1 day in future
  • Snapping to Time Units (Using @)

    PPL Time Modifier OpenSearch Date Math Explanation
    -1d@d now-1d/d 1 day ago, rounded to day
    @h now/h Round to current hour
    @m now/M Round to current month
  • Week Days

    PPL Time Modifier OpenSearch Date Math Explanation
    @w0 /w-1d or /w+6d Previous Sunday (depending on current day)
    @w1 /w Start of week (Monday)
    @w4 /w+3d Thursday of current week
  • Special Cases

PPL Time Modifier OpenSearch Date Math Explanation
@q /M or /M-1M or /M-2M Start of current quarter
-2q -6M 2 quarters ago (6 months)
1234.567 1234567 Unix timestamp (seconds → milliseconds)
  • Complex Expressions
PPL Time Modifier OpenSearch Date Math Explanation
-1d+1y@mon now-1d+1y/M 1 day ago + 1 year, rounded to month
-3d@d-2h+10m now-3d/d-2h+10m 3 days ago, day start, minus 2 hours, plus 10 minutes

Limitations

Currently, the following formats has to be quoted:

  • chained offsets. e.g. -1day@month+1h or +1year-4day

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@yuancu yuancu added the enhancement New feature or request label Sep 4, 2025
@vamsimanohar
Copy link
Member

can you review this: #4152
I am changing the grammar for search expression and its functionality.
We might need to reevaluate the implementation.

@yuancu
Copy link
Collaborator Author

yuancu commented Sep 7, 2025

Hi @vamsimanohar, thank you for reminding! I think the functionality does not overlap, I'll re-implement this based on your grammar.

@vamsimanohar
Copy link
Member

@yuancu Few things to keep in mind.

We fundamentally want search command to be always push down and translate toquery_stringfunction in DSL which follows Lucene query syntax. Anything we add to search command should get translated to lucene query.

I think it should be possible with your changes.

@yuancu yuancu changed the title Support absolute time range in search command Support time modifiers in search command Sep 12, 2025
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Unit test search with absolute time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Rephrase timeRange and timeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Switch to earliest and latest udf

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Add a convert util

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Verify time correctness during coversion

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix quarter parsing bugs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix week snap parsing

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Remove old implementation that translates time modifier to time filter

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: "${hamcrest_version}"
testImplementation group: 'org.mockito', name: 'mockito-core', version: "${mockito_version}"
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: "${mockito_version}"
testImplementation group: 'org.opensearch', name: 'opensearch', version: "${opensearch_version}"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This dependency is added for the access of org.opensearch.common.time.DateMathParser -- I want to make sure that the parsed OpenSearch date math like now-1d/M-2M returns the intended instant for -1d@q

Copy link
Collaborator

Choose a reason for hiding this comment

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

parseRelativeTime() is called by PPL module only, right? if move parseRelativeTime to PPL module, does opensearch depedency still needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is only called by PPL module. If I still want to assure the correctness of the parsed instant, I'll have to move this dependency to the PPL module.

Is there any concern over this dependency? If it's for the performance, as the dependency is only test time, I assume there would be no harm to the runtime performance.

Copy link
Member

Choose a reason for hiding this comment

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

we want core modules to not depend on any opensearch dependencies. This helps in our future plans to publish ppl as a library or running sql engine as a separate service.

If we can avoid, its better to avoid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the explanation! I've removed the dependency and related tests.

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
@vamsimanohar
Copy link
Member

@yuancu Did we cover every usecase mentioned in the description? Lets try to close this PR by end of the day. I can stay late and get on a call with you for review.

You probably might need to refactor from this PR:#4334

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
@yuancu
Copy link
Collaborator Author

yuancu commented Sep 30, 2025

@yuancu Did we cover every usecase mentioned in the description? Lets try to close this PR by end of the day. I can stay late and get on a call with you for review.

You probably might need to refactor from this PR:#4334

Unit test and explaining IT covers all of them. I did not cover some relative time modifier in integration test because they either have something to do with the current time, which may be flaky to mock and test. I'll try to cover more of them in the integration test. E.g. there exists unknown latency after I write a timestamp and before the query is executed.

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
@yuancu yuancu requested a review from penghuo September 30, 2025 17:06
@vamsimanohar vamsimanohar enabled auto-merge (squash) September 30, 2025 17:55
@vamsimanohar vamsimanohar dismissed penghuo’s stale review September 30, 2025 17:58

The requested change has been addressed.

@vamsimanohar vamsimanohar merged commit e468513 into opensearch-project:main Sep 30, 2025
34 checks passed
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.19-dev failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Navigate to the root of your repository
cd $(git rev-parse --show-toplevel)
# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/sql/backport-2.19-dev 2.19-dev
# Navigate to the new working tree
pushd ../.worktrees/sql/backport-2.19-dev
# Create a new branch
git switch --create backport/backport-4224-to-2.19-dev
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 e4685137de681a5f513a88c681b406ad5a786c27
# Push it to GitHub
git push --set-upstream origin backport/backport-4224-to-2.19-dev
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/sql/backport-2.19-dev

Then, create a pull request where the base branch is 2.19-dev and the compare/head branch is backport/backport-4224-to-2.19-dev.

yuancu added a commit to yuancu/sql-plugin that referenced this pull request Oct 9, 2025
* Implement absolute time range in search command

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Unit test search with absolute time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Rephrase timeRange and timeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Switch to earliest and latest udf

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Add a convert util

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Verify time correctness during coversion

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix quarter parsing bugs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix week snap parsing

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Remove old implementation that translates time modifier to time filter

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix anomalyzed test & add a todo for an ignored test

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support now() in time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix time modifier explain ITs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support unixtimestamp (second) as a time modifier value

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update docs for search command with time modifiers

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Test accessing fields with name earliest and latest in search command

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update doctest in condition.rst due to the update in the implementation of earliest and latest conditions

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update PPLQueryDataAnonymizerTest.java

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update explain ITs to use yaml plan files

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update a link to OpenSearch exists

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support using timesnaps without quotes

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add a unit test for direct format

- additionally rename parseRelativeTime to resolveTimeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add support to ISO 8601 date format to time modifier, as it is now widely supported in PPL

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update syntax by reusing SPANLENGTH definition

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update explain IT for search with time modifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add integration tests for time modifiers

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Parse timestamp string with multiple parsers in a loop

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Remove opensearch test dependency from core module

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix unit tests

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Minor updates to explain limitations in search doc

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

---------

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
(cherry picked from commit e468513)
yuancu added a commit to yuancu/sql-plugin that referenced this pull request Oct 9, 2025
* Implement absolute time range in search command

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Unit test search with absolute time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Rephrase timeRange and timeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Switch to earliest and latest udf

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Add a convert util

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Verify time correctness during coversion

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix quarter parsing bugs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix week snap parsing

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Remove old implementation that translates time modifier to time filter

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix anomalyzed test & add a todo for an ignored test

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support now() in time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix time modifier explain ITs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support unixtimestamp (second) as a time modifier value

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update docs for search command with time modifiers

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Test accessing fields with name earliest and latest in search command

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update doctest in condition.rst due to the update in the implementation of earliest and latest conditions

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update PPLQueryDataAnonymizerTest.java

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update explain ITs to use yaml plan files

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update a link to OpenSearch exists

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support using timesnaps without quotes

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add a unit test for direct format

- additionally rename parseRelativeTime to resolveTimeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add support to ISO 8601 date format to time modifier, as it is now widely supported in PPL

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update syntax by reusing SPANLENGTH definition

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update explain IT for search with time modifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add integration tests for time modifiers

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Parse timestamp string with multiple parsers in a loop

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Remove opensearch test dependency from core module

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix unit tests

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Minor updates to explain limitations in search doc

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

---------

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
(cherry picked from commit e468513)
yuancu added a commit to yuancu/sql-plugin that referenced this pull request Oct 9, 2025
* Implement absolute time range in search command

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Unit test search with absolute time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Rephrase timeRange and timeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Switch to earliest and latest udf

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Add a convert util

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Verify time correctness during coversion

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix quarter parsing bugs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Fix week snap parsing

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

Remove old implementation that translates time modifier to time filter

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix anomalyzed test & add a todo for an ignored test

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support now() in time range

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix time modifier explain ITs

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support unixtimestamp (second) as a time modifier value

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update docs for search command with time modifiers

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Test accessing fields with name earliest and latest in search command

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update doctest in condition.rst due to the update in the implementation of earliest and latest conditions

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update PPLQueryDataAnonymizerTest.java

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update explain ITs to use yaml plan files

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update a link to OpenSearch exists

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Support using timesnaps without quotes

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add a unit test for direct format

- additionally rename parseRelativeTime to resolveTimeModifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add support to ISO 8601 date format to time modifier, as it is now widely supported in PPL

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update syntax by reusing SPANLENGTH definition

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update explain IT for search with time modifier

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Add integration tests for time modifiers

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Parse timestamp string with multiple parsers in a loop

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Remove opensearch test dependency from core module

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Fix unit tests

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Minor updates to explain limitations in search doc

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

---------

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
(cherry picked from commit e468513)
qianheng-aws pushed a commit that referenced this pull request Oct 10, 2025
* Implement absolute time range in search command



Unit test search with absolute time range



Rephrase timeRange and timeModifier



Switch to earliest and latest udf



Add a convert util



Verify time correctness during coversion



Fix quarter parsing bugs



Fix week snap parsing



Remove old implementation that translates time modifier to time filter



* Fix anomalyzed test & add a todo for an ignored test



* Support now() in time range



* Fix time modifier explain ITs



* Support unixtimestamp (second) as a time modifier value



* Update docs for search command with time modifiers



* Test accessing fields with name earliest and latest in search command



* Update doctest in condition.rst due to the update in the implementation of earliest and latest conditions



* Update PPLQueryDataAnonymizerTest.java



* Update explain ITs to use yaml plan files



* Update a link to OpenSearch exists



* Support using timesnaps without quotes



* Add a unit test for direct format

- additionally rename parseRelativeTime to resolveTimeModifier



* Add support to ISO 8601 date format to time modifier, as it is now widely supported in PPL



* Update syntax by reusing SPANLENGTH definition



* Update explain IT for search with time modifier



* Add integration tests for time modifiers



* Parse timestamp string with multiple parsers in a loop



* Remove opensearch test dependency from core module



* Fix unit tests



* Minor updates to explain limitations in search doc



---------


(cherry picked from commit e468513)

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
asifabashar added a commit to asifabashar/sql that referenced this pull request Oct 10, 2025
* main-apple: (218 commits)
  Add ignorePrometheus Flag for integTest and docTest (opensearch-project#4442)
  Create fab-radar.yml
  PPL `fillnull` command enhancement (opensearch-project#4421)
  reverting to _doc + _id (opensearch-project#4435)
  Support `multisearch` command in calcite (opensearch-project#4332)
  Add 3.3 release notes (opensearch-project#4422) (opensearch-project#4423)
  [SQL/PPL] Fix the `count(*)` and `dc(field)` to be capped at MAX_INTEGER opensearch-project#4416 (opensearch-project#4418)
  Change the default search sort tiebreaker to `_shard_doc` for PIT search (opensearch-project#4378)
  [Enhancement] Add error handling for known limitation of sql `JOIN` (opensearch-project#4344)
  Bugfix: SQL type mapping for legacy JDBC output (opensearch-project#3613)
  Version bump: 3.3 (opensearch-project#4417)
  Add max/min eval functions (opensearch-project#4333)
  Support time modifiers in search command  (opensearch-project#4224)
  Fix numbered token bug and make it optional output in patterns command (opensearch-project#4402)
  refactor span (opensearch-project#4334)
  Move release notes categories (opensearch-project#3818)
  [Doc] Enable doctest with Calcite (opensearch-project#4379)
  Mod function should return decimal instead of float when handle the operands are decimal literal (opensearch-project#4407)
  Scale of decimal literal should always be positive in Calcite (opensearch-project#4401)
  Enable Calcite by default and implicit fallback the unsupported commands (opensearch-project#4372)
  ...
@LantaoJin LantaoJin added the backport-manually Filed a PR to backport manually. label Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 2.19-dev backport-failed backport-manually Filed a PR to backport manually. enhancement New feature or request v3.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support earliest and latest in search command

5 participants