felipepessoto commented on code in PR #13757:
URL: https://github.com/apache/gluten/pull/13757#discussion_r4701511479
##########
gluten-ut/test/src/test/scala/org/apache/gluten/extension/columnar/validator/GlutenTimestampNtzValidatorSuite.scala:
##########
@@ -172,6 +174,114 @@ class GlutenTimestampNtzValidatorSuite extends
SparkFunSuite {
}
}
+ test("operators passing NTZ values through are allowed when NTZ is
supported") {
+ val ts = AttributeReference("ts", TimestampNTZType)()
+ val ts2 = AttributeReference("ts2", TimestampNTZType)()
+ val left = InputPlan(Seq(id, ts))
+ val right = InputPlan(Seq(ts2))
+ val ntzLiteral = Literal(1704067200000000L, TimestampNTZType)
+ val rowFrame = SpecifiedWindowFrame(RowFrame, UnboundedPreceding,
CurrentRow)
+ val rangeFrame = SpecifiedWindowFrame(RangeFrame, UnboundedPreceding,
CurrentRow)
+ val plans = Seq[SparkPlan](
+ SortExec(Seq(SortOrder(ts, Descending)), global = true, left),
Review Comment:
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.
##########
gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/validator/Validators.scala:
##########
@@ -325,6 +330,75 @@ object Validators {
}
fail(s"${plan.nodeName} has TimestampNTZType in input/output schema")
}
+
+ /**
+ * Operators that move, sort, join or filter TimestampNTZ values without
NTZ-specific semantics.
+ * They are allowed when every expression touching NTZ passes
[[isSupportedNtzExpression]].
+ * Native validation still checks the functions and signatures they use,
e.g. comparisons
+ * between NTZ values.
+ */
+ private def isNtzPassThroughOperator(plan: SparkPlan): Boolean = plan
match {
+ case _: SortExec | _: FilterExec | _: TakeOrderedAndProjectExec => true
+ case _: SortMergeJoinExec | _: ShuffledHashJoinExec | _:
BroadcastHashJoinExec => true
+ case _: BroadcastNestedLoopJoinExec | _: CartesianProductExec => true
+ case _: BroadcastExchangeExec | _: ExpandExec | _: UnionExec | _:
CoalesceExec => true
+ case _: GlobalLimitExec | _: LocalLimitExec | _: CollectLimitExec => true
+ case _: WindowExec => true
+ case p => SparkShimLoader.getSparkShims.isWindowGroupLimitExec(p)
Review Comment:
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.
##########
gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/validator/Validators.scala:
##########
@@ -325,6 +330,75 @@ object Validators {
}
fail(s"${plan.nodeName} has TimestampNTZType in input/output schema")
}
+
+ /**
+ * Operators that move, sort, join or filter TimestampNTZ values without
NTZ-specific semantics.
+ * They are allowed when every expression touching NTZ passes
[[isSupportedNtzExpression]].
+ * Native validation still checks the functions and signatures they use,
e.g. comparisons
+ * between NTZ values.
+ */
+ private def isNtzPassThroughOperator(plan: SparkPlan): Boolean = plan
match {
+ case _: SortExec | _: FilterExec | _: TakeOrderedAndProjectExec => true
+ case _: SortMergeJoinExec | _: ShuffledHashJoinExec | _:
BroadcastHashJoinExec => true
+ case _: BroadcastNestedLoopJoinExec | _: CartesianProductExec => true
+ case _: BroadcastExchangeExec | _: ExpandExec | _: UnionExec | _:
CoalesceExec => true
+ case _: GlobalLimitExec | _: LocalLimitExec | _: CollectLimitExec => true
+ case _: WindowExec => true
+ case p => SparkShimLoader.getSparkShims.isWindowGroupLimitExec(p)
+ }
+
+ private def containsNtzType(dataType: DataType): Boolean = dataType match {
+ case TimestampNTZType => true
+ case st: StructType => st.exists(f => containsNtzType(f.dataType))
+ case at: ArrayType => containsNtzType(at.elementType)
+ case mt: MapType => containsNtzType(mt.keyType) ||
containsNtzType(mt.valueType)
+ case _ => false
+ }
+
+ private def hasNtzType(expression: Expression): Boolean = expression match
{
+ // These metadata nodes have no data type.
+ case _: WindowSpecDefinition | _: WindowFrame | _: PartitionSpec => false
+ case e =>
+ try {
+ containsNtzType(e.dataType)
+ } catch {
+ case NonFatal(_) => false
+ }
+ }
+
+ /**
+ * Returns true if every node of `expression` that produces or consumes
TimestampNTZ is one that
+ * only references, compares or conditionally passes NTZ values, or is an
NTZ function already
+ * supported natively.
+ */
+ private def isSupportedNtzExpression(expression: Expression): Boolean =
+ expression.find {
+ node => (hasNtzType(node) || node.children.exists(hasNtzType)) &&
!isSupportedNtzNode(node)
+ }.isEmpty
+
+ private def isSupportedNtzNode(expression: Expression): Boolean =
expression match {
+ case _: Attribute | _: BoundReference | _: Alias | _: Literal | _:
GetStructField => true
+ 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
Review Comment:
`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 is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]