Skip to content

[BUG] PPL date modifier function does not work as expected #3725

Closed

Description

@penghuo

What is the bug?
PPL now() with date modifier does not work as expected.

  • Run PPL and DSL queries at PST 2025-06-02 13:19:00. JVM timezone is America/Los_Angeles.
  • PPL query return empty results
###
POST {{baseUrl}}/_plugins/_ppl/
Content-Type: application/x-ndjson

{
    "query": "source=t0001 | where @timestamp >= date_sub(now(), INTERVAL 1 DAY) and @timestamp <= now()"
}

"datarows": [],
  • DSL query return expected result
###
POST {{baseUrl}}/t0001/_search
Content-Type: application/x-ndjson

{
  "query": {
      "range" : {
          "@timestamp" : {
              "gte" : "now-1d",
              "lte" : "now"
          }
      }
  }
}

"hits": {
    "hits": [
      {
        "_source": {
          "@timestamp": "2025-06-02T14:00:00"
        }
      },
      {
        "_source": {
          "@timestamp": "2025-06-02T15:00:00"
        }
      },
      {
        "_source": {
          "@timestamp": "2025-06-02T16:00:00"
        }
      }
    ]
  }

Do you have any additional context?

###
PUT {{baseUrl}}/t0001
Content-Type: application/x-ndjson

{
  "mappings": {
    "properties": {
      "@timestamp": { "type": "date" }
    }
  }
}

###
POST {{baseUrl}}/t0001/_bulk
Content-Type: application/x-ndjson

{"index": {}}
{"@timestamp": "2025-06-02T14:00:00"}
{"index": {}}
{"@timestamp": "2025-06-02T15:00:00"}
{"index": {}}
{"@timestamp": "2025-06-02T16:00:00"}

Activity

  1. added
    bug
    PPL
    on Jun 2, 2025
  2. penghuo commented on Jun 2, 2025

    @penghuo
    CollaboratorAuthor

    RCA

    The now() function retrieves the current instant as a LocalDateTime, but it constructs an ExprTimestampValue with the ZoneOffset.UTC. For example, if a query runs at 2025-06-02 13:19:00 in a JVM with the timezone America/Los_Angeles, the now() function incorrectly returns 2025-06-02 13:19:00 in UTC. Consequently, comparisons with @timestamp yield no results because all @timestamp values are considered later than 2025-06-02 13:19:00

  3. penghuo commented on Jun 13, 2025

    @penghuo
    CollaboratorAuthor

    Relate to #3599

  4. model-collapse commented on Jun 27, 2025

    @model-collapse

    This should be a production decision, let's involve more ppl to discuss.

  5. yuancu commented on Jul 2, 2025

    @yuancu
    Collaborator

    I'm a little confused why is the result of the DSL desired.

    • The current time is 2025-06-02 13:19:00
    • All timestamps in the index are later / greater than the current time
    • The query searches for timestamps smaller than now

    Shouldn't it return an empty list?

    Another test I tried locally

    • Time: Jul 2, 15:58, Shanghai Time (Both client & JVM)

    • Data

        @timestamp
      1 2025-07-02T14:00:00
      2 2025-07-02T15:00:00
      3 2025-07-02T16:00:00
    • PPL: source=t0001 | where @timestamp >= date_sub(now(), INTERVAL 1 DAY) and @timestamp <= now()

    • PPL result:

      {
        "schema": [
          {
            "name": "@timestamp",
            "type": "timestamp"
          }
        ],
        "datarows": [
          [
            "2025-07-02 14:00:00"
          ],
          [
            "2025-07-02 15:00:00"
          ]
        ],
        "total": 2,
        "size": 2
      }
    • DSL:

      {
        "query": {
            "range" : {
                "@timestamp" : {
                    "gte" : "now-1d",
                    "lte" : "now"
                }
            }
        }
      }
    • DSL result:

      {
        "hits": {
          "total": {
            "value": 0,
            "relation": "eq"
          },
          "hits": []
        }
      }

    Intuitively, the result of PPL is correct.

    About the UTC in ExprTimestampValue

    Time zones in PPL/OpenSearch are only relevant during the creation of a date, time, or timestamp. Once the value is created, the time zone information is discarded, and the timestamp is treated as a literal without any associated zone.

    For example, when you call now(), the timestamp is obtained in the JVM’s default time zone and converted to a LocalDateTime. If the JVM is set to UTC+8, now() at 13:00 results in the literal value July 2, 13:00. After this point, no time zone information is retained.

    However, there are cases — such as type conversions or formatting—where a time zone must be supplied. In these situations, UTC is used as a neutral default time zone.

  6. penghuo commented on Jul 2, 2025

    @penghuo
    CollaboratorAuthor

    In OpenSearch DSL, If we agree on following 2 concept

    1. now represent Epoch time, not impact by timezone.
    2. date value is stored as Epoch value.

    Then we can explain why 1st query return results, and 2nd query return empty results.

    1. run query in PST 2025-06-02 13:19:00 (UTC 2025-06-02 20:19:00), it return
        | @timestamp
      -- | --
      1 | 2025-06-02T14:00:00
      2 | 2025-06-02T15:00:00
      3 | 2025-06-02T16:00:00

    2. Run query in Jul 2, 15:58, Shanghai Time (UTC 2025-07-02 07:58:00), it should return empty results.

    Similaly in SPL, now() function is represented in UNIX time, or in seconds since Epoch time.

  7. yuancu commented on Jul 3, 2025

    @yuancu
    Collaborator

    Thank you for the explanation. I get it now.

    SPL2 uses unix time to circumvent the handling of time zone.

    However, there are still timezone involved in our representation of timestamps. In your example, for the DSL to return the expected results, there is a big assumption: the date/time stored in OpenSearch is in UTC time. Is there already such agreement? If so, we can also convert the time to UTC when storing and reading from OpenSearch.

    In the current implementation, the assumption is the date/time stored in OpenSearch are in JVM'm timezone. Therefore, 13:00 UTC+8 is stored as 13:00 directly.

  8. penghuo commented on Jul 3, 2025

    @penghuo
    CollaboratorAuthor

    In your example, for the DSL to return the expected results, there is a big assumption: the date/time stored in OpenSearch is in UTC time. Is there already such agreement? If so, we can also convert the time to UTC when storing and reading from OpenSearch.

    This is not assumption, Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.

  9. moved this from Not Started to In review in PPL Trackeron Jul 8, 2025
  10. yuancu commented on Jul 11, 2025

    @yuancu
    Collaborator

    @penghuo Is the solution in #3854 acceptable? It returns UTC time for almost all now-like functions.

  11. moved this from In review to Done in PPL Trackeron Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions