Skip to content

[GLUTEN-11622][VL] Allow operators passing TimestampNTZ values through to run natively - #13757

Open
WangGuangxin wants to merge 3 commits into
apache:mainfrom
WangGuangxin:ntz-operator-validation
Open

WangGuangxin wants to merge 3 commits into
apache:mainfrom
WangGuangxin:ntz-operator-validation

Conversation

@WangGuangxin

@WangGuangxin WangGuangxin commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

TimestampNTZ validation is disabled by default (spark.gluten.sql.columnar.backend.velox.enableTimestampNtzValidation=false). Even so, FallbackByTimestampNTZ only allowed scans, aggregates, shuffle exchanges and some projects. Any other operator with TimestampNTZ in its input or output schema fell back with <Node> has TimestampNTZType in input/output schema. This included Sort, Filter, the joins, Window, WindowGroupLimit and TakeOrderedAndProject.

This PR allows those operators: Sort, Filter, TakeOrderedAndProject, the joins (SortMergeJoin, ShuffledHashJoin, BroadcastHashJoin, BroadcastNestedLoopJoin, CartesianProduct), BroadcastExchange, Expand, Union, Coalesce, the limits, Window and WindowGroupLimit. The condition is that every expression touching TimestampNTZ is one of:

  • an attribute reference, literal, comparison, IsNull/IsNotNull, or If/CaseWhen/Coalesce;
  • an NTZ function that is already supported natively: cast, hour/minute/second, timestampadd, convert_timezone, make_timestamp;
  • min/max/first/last/count, or lag/lead/nth_value/In/InSet.

Native validation still checks the functions and signatures used. For example, comparisons for which Velox has no (TIMESTAMP UTC, TIMESTAMP UTC) signature still fall back. Unsupported NTZ expressions (e.g. date_format on NTZ) and RANGE frames with value offsets over an NTZ order key are not offloaded.

Related issue: #11622

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

…h to run natively

With TimestampNTZ validation disabled (the default), FallbackByTimestampNTZ
still only allowed scans, aggregates, shuffle exchanges and some projects.
Any other operator with TimestampNTZ in its input or output schema fell
back, e.g. Sort, Filter, joins, Window, WindowGroupLimit and
TakeOrderedAndProject.

Allow these operators when every expression touching TimestampNTZ only
references, compares or conditionally passes NTZ values, or is an NTZ
function already supported natively (cast, hour/minute/second,
timestampadd, convert_timezone, make_timestamp, min/max/first/last/count,
lag/lead/nth_value). Native validation still checks the functions and
signatures used, so e.g. comparisons Velox can't evaluate yet still fall
back. RANGE frames with value offsets over an NTZ order key are not
offloaded.
A filter such as `ts_ntz IN (TIMESTAMP_NTZ '2023-10-01 12:34:57', ...)`
fell back with "Filter has TimestampNTZType in input/output schema".
IN and INSET were missing from the TimestampNTZ expressions that
pass-through operators may contain.

Allow In and InSet there. Native validation still checks Velox's `in`
signature, so the filter falls back until Velox registers `in` for
TIMESTAMP UTC.
@github-actions github-actions Bot added the CORE works for Gluten Core label Nov 24, 2026
@WangGuangxin
WangGuangxin force-pushed the ntz-operator-validation branch from 14602c5 to aced7a8 Compare November 24, 2026 11:25
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@WangGuangxin
WangGuangxin requested review from rui-mo and zhztheplayer and removed request for rui-mo November 24, 2026 12:04

@rui-mo rui-mo left a comment

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.

@WangGuangxin Thanks for the fix! Would you be interested in explicitly supporting the TimestampUTC type in the relevant Velox operators first, rather than relying on implicit support? If no code changes are needed, we could update the documentation and add relevant tests in Velox.

…tampNTZ to run natively

A window such as `rank() OVER (PARTITION BY k ORDER BY ts_ntz)` fell back
with "Window has TimestampNTZType in input/output schema", and so did the
WindowGroupLimit Spark plans for `rank() <= n`. Rank, DenseRank and
PercentRank take the window order keys as children, so the NTZ check
rejected them, while RowNumber, which has no children, passed.
`greatest`/`least` over NTZ values fell back the same way.

Allow Rank, DenseRank, PercentRank, Greatest and Least in the NTZ
expressions that pass-through operators may contain. They only compare
NTZ values. Native validation still checks Velox's `greatest`/`least`
signatures, so those fall back until Velox registers them for
TIMESTAMP UTC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@WangGuangxin

Copy link
Copy Markdown
Contributor Author

@WangGuangxin Thanks for the fix! Would you be interested in explicitly supporting the TimestampUTC type in the relevant Velox operators first, rather than relying on implicit support? If no code changes are needed, we could update the documentation and add relevant tests in Velox.

@rui-mo ok

@WangGuangxin

Copy link
Copy Markdown
Contributor Author

@rui-mo A feature gap is facebookincubator/velox#20647. please help review .
No more issues found in other operators/expressions, will add doc and UT to cover TimestampUTC type later

@felipepessoto

Copy link
Copy Markdown
Contributor

My Delta writer PR (#13512) also touches FallbackByTimestampNTZ for write statistics. I’d prefer to let this PR merge first. Then I’ll rebase mine on main, reuse the shared validation logic, and keep only what the Delta writer still needs.

One case I’ll keep covered on my side: for a struct containing both timestamp types, mixed.ltz IS NOT NULL can run natively, but IF(mixed.ltz IS NOT NULL, to_json(mixed), 'x') should still fall back if to_json(mixed) is unsupported. The null check shouldn’t make the whole expression eligible.

@felipepessoto felipepessoto left a comment

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.

Please fix the tied-rank case before merging. On Spark 4.1, the merge-base validator preserved all three input rows, but this revision returned only two with the same data and backend. The inline comments also cover the Delta fallback expectation and the missing Unix timestamp functions.

case _: BroadcastExchangeExec | _: ExpandExec | _: UnionExec | _: CoalesceExec => true
case _: GlobalLimitExec | _: LocalLimitExec | _: CollectLimitExec => true
case _: WindowExec => true
case p => SparkShimLoader.getSparkShims.isWindowGroupLimitExec(p)

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.

This admits a WindowGroupLimit that drops tied rows. On Spark 4.1, with two rows sharing the same ts_ntz, RANK() OVER (PARTITION BY ts_ntz ORDER BY ts_ntz) filtered to r = 1 kept both in Spark but only one in Velox. DENSE_RANK() does the same. SubstraitToVeloxPlan.cc removes sort keys already used for partitioning and then builds a limited RowNumberNode when none remain. Please keep this case on Spark or fix that lowering so only ROW_NUMBER truncates ties, and cover it with an executed query.

val rowFrame = SpecifiedWindowFrame(RowFrame, UnboundedPreceding, CurrentRow)
val rangeFrame = SpecifiedWindowFrame(RangeFrame, UnboundedPreceding, CurrentRow)
val plans = Seq[SparkPlan](
SortExec(Seq(SortOrder(ts, Descending)), global = true, left),

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.

The new sort behavior breaks an existing integration test. On Spark 4.1, VeloxDeltaSuite fails delta: TIMESTAMP_NTZ as partition column should fallback and return correct results: DeltaSuite.scala:890 sets noFallBack = false, which requires a fallback, but ORDER BY c1 now runs as a native sort over a native Delta scan. Please update the Velox expectation and keep the row comparison. The suite is shared with Bolt, so its expectation shouldn't change.

case _: SortOrder | _: IsNull | _: IsNotNull | _: BinaryComparison => true
case _: In | _: InSet | _: Greatest | _: Least => true
case _: If | _: CaseWhen | _: Coalesce => true
case _: Cast | _: Hour | _: Minute | _: Second | _: TimestampAdd | _: ConvertTimezone => true

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.

UnixTimestamp and ToUnixTimestamp are missing from this list. On Spark 4.1, SELECT unix_timestamp(ts_ntz), to_unix_timestamp(ts_ntz) uses a native projection, but WHERE unix_timestamp(ts_ntz) > 0 still runs as a Spark filter because this match rejects it. Please include both here, as the ProjectExec branch already does, and cover the predicate case.

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

CORE works for Gluten Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants