Skip to content

[AMORO-4559][AMS] Support time functions in self-optimizing.filter - #4560

Open
owenmonn wants to merge 3 commits into
apache:masterfrom
owenmonn:filter-time-functions
Open

owenmonn wants to merge 3 commits into
apache:masterfrom
owenmonn:filter-time-functions

Conversation

@owenmonn

@owenmonn owenmonn commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Why are the changes needed?

Close #4559.

Based on #4558. Its commit is the first one here until #4558 is merged.

Brief change log

  • DateTimeExpressionEvaluator (new): evaluates the value side of the filter. It supports CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP (with or without ()), INTERVAL '3' HOUR, INTERVAL 3 HOUR and INTERVAL '3 HOUR' (YEAR to SECOND, and WEEK) with + and -, AT TIME ZONE '<zone>', and DATE_FORMAT(expression, pattern). Any other node is rejected with an UnsupportedOperationException naming it. The current time is read once per conversion from a Clock.
  • ExpressionUtil: converts the evaluated value for the column type, also when the value is on the left of a comparison or a BETWEEN bound.
  • docs/user-guides/configurations.md: the functions, and a note that the filter selects whole partitions.

Time zones:

  • CURRENT_DATE, CURRENT_TIME, and a date or time taken from CURRENT_TIMESTAMP use the AMS JVM zone.
  • CURRENT_TIMESTAMP is compared with timestamp columns as an instant. Values of a timestamp column without zone are read as UTC, as data expiration does.
  • CURRENT_TIMESTAMP AT TIME ZONE 'z' is the wall clock in z. A timestamp column without zone compares it with the stored value as is, a timestamp column with zone rejects it, and INTERVAL arithmetic after it follows z's daylight saving rules.

A STRING column accepts a literal or DATE_FORMAT. A pattern containing % is rejected.

How was this patch tested?

  • Add some test cases that check the changes thoroughly including negative and positive cases if possible

    TestDateTimeExpressionEvaluator and TestExpressionUtil. The clock is fixed in America/Los_Angeles, so the zone choices and daylight saving transitions are asserted regardless of the build machine's zone.

  • Add screenshots for manual tests if appropriate

  • Run test locally before making a pull request

    Also verified on an AMS built from these changes on 0.8.0. With the filter below on a table partitioned by string dt and hr, only partitions that ended more than two hours earlier were optimized. An hour later, after the next write, the partition that had crossed the boundary was optimized as well.

    dt < DATE_FORMAT(CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Seoul' - INTERVAL '2' HOUR, 'yyyy-MM-dd')
    OR (dt = DATE_FORMAT(CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Seoul' - INTERVAL '2' HOUR, 'yyyy-MM-dd')
        AND hr < DATE_FORMAT(CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Seoul' - INTERVAL '2' HOUR, 'HH'))

Documentation

  • Does this pull request introduce a new feature? yes
  • If yes, how is the feature documented? docs

jsqlparser wraps a parenthesized condition in a Parenthesis node, which the
converter rejected as unsupported, so a filter such as
a < 1 OR (a = 1 AND b < 2) failed. Unwrap it.
Let the filter use CURRENT_DATE, CURRENT_TIME and CURRENT_TIMESTAMP with
INTERVAL arithmetic, so the optimizing scope can leave out recently written
data without updating the property by a periodic job, for example
'self-optimizing.filter' = 'event_time < CURRENT_TIMESTAMP - INTERVAL ''3'' HOUR'.

- DateTimeExpressionEvaluator evaluates the value expression of the filter:
  CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP (with or without parentheses)
  and INTERVAL in the forms INTERVAL '3' HOUR, INTERVAL 3 HOUR and
  INTERVAL '3 HOUR' with the units YEAR, MONTH, WEEK, DAY, HOUR, MINUTE and
  SECOND, combined with + and -. Any other value expression is rejected; the
  caller still gets an IllegalArgumentException, now naming the unsupported
  node.
- ExpressionUtil converts the result for DATE, TIME and TIMESTAMP columns; a
  CURRENT_TIMESTAMP-based value can also be compared with a DATE or TIME
  column. The value can be on either side of a comparison or a bound of
  BETWEEN. A timestamp without zone is compared with CURRENT_TIMESTAMP in UTC,
  the convention data expiration uses for timestamp fields; CURRENT_DATE and
  CURRENT_TIME use the JVM zone.
- The filter is converted whenever the table is evaluated or planned, that is
  when a new snapshot is found, so CURRENT_* moves with the writes. The whole
  filter is converted with one current time taken from a Clock (the JVM clock
  in production), so several occurrences see the same instant and tests can
  fix the time and the zone.
- Tests for the evaluator and for the end-to-end conversion per column type.
  configurations.md documents the functions and that the filter selects whole
  partitions, so the condition belongs on a partition column that leaves out
  the partitions still being written.
Tables partitioned by string columns such as dt='20260930' could not use the
time functions.

- DATE_FORMAT(expression, pattern) formats a CURRENT_DATE / CURRENT_TIME /
  CURRENT_TIMESTAMP based value with a Java DateTimeFormatter pattern for a
  string column; a pattern with % (MySQL or Trino style) is rejected. A string
  column accepts a literal or DATE_FORMAT; other functions are rejected.
- CURRENT_TIMESTAMP AT TIME ZONE '<zone>' gives the wall-clock time of that
  zone and keeps the zone rules, so INTERVAL arithmetic follows daylight saving
  time. A timestamp column without zone compares it as stored (as if UTC), a
  DATE or TIME column takes its date or time, DATE_FORMAT formats it, and a
  column with zone rejects it since AT TIME ZONE gives a wall-clock time here.
  DATE_FORMAT and the comparison with DATE, TIME and timestamp-without-zone
  columns therefore do not depend on the JVM zone, for example
  dt < DATE_FORMAT(CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Seoul' - INTERVAL '1' DAY, 'yyyyMMdd').
- Tests for both, including daylight saving time; configurations.md updated.
@github-actions github-actions Bot added the type:docs Improvements or additions to documentation label Dec 2, 2026

This branch has not been deployed

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

Labels

type:docs Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support time functions in self-optimizing.filter

1 participant