voonhous commented on code in PR #19405: URL: https://github.com/apache/hudi/pull/19405#discussion_r3690171860
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/TestCatalystExpressionOrderPreserving.scala: ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi + +import org.apache.spark.sql.catalyst.expressions.{Add, AttributeReference, BitwiseOr, Cast, DateAdd, DateSub, Divide, Exp, Expression, Literal, Log, Lower, Multiply, ShiftLeft, Sqrt, Upper} +import org.apache.spark.sql.types.{DateType, DoubleType, IntegerType, LongType, StringType} +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +/** + * Branch coverage for the order-preserving transformation matcher in + * [[org.apache.spark.sql.BaseHoodieCatalystExpressionUtils]]. This is what lets data skipping map a + * transformed column reference back to its source attribute, so each case is pinned to the exact + * source [[AttributeReference]] it must recover, and non-order-preserving shapes must not match. + */ +class TestCatalystExpressionOrderPreserving extends SparkAdapterSupport { + + private val intAttr = AttributeReference("i", IntegerType)() + private val strAttr = AttributeReference("s", StringType)() + private val dblAttr = AttributeReference("d", DoubleType)() + private val dateAttr = AttributeReference("dt", DateType)() + + private def matched(expr: Expression): Option[AttributeReference] = + sparkAdapter.getCatalystExpressionUtils.tryMatchAttributeOrderingPreservingTransformation(expr) + + @Test + def testIdentityAttributeMatches(): Unit = { + assertEquals(Some(intAttr), matched(intAttr)) + } + + @Test + def testArithmeticTransformationsPreserveOrdering(): Unit = { + assertEquals(Some(intAttr), matched(Add(intAttr, Literal(1)))) + assertEquals(Some(intAttr), matched(Add(Literal(1), intAttr))) + assertEquals(Some(intAttr), matched(Multiply(intAttr, Literal(2)))) + assertEquals(Some(intAttr), matched(Multiply(Literal(2), intAttr))) + assertEquals(Some(intAttr), matched(Divide(intAttr, Literal(2)))) + assertEquals(Some(intAttr), matched(BitwiseOr(intAttr, Literal(1)))) + assertEquals(Some(intAttr), matched(BitwiseOr(Literal(1), intAttr))) + assertEquals(Some(intAttr), matched(ShiftLeft(intAttr, Literal(1)))) + } + + @Test + def testUnaryMathAndStringTransformationsPreserveOrdering(): Unit = { + assertEquals(Some(dblAttr), matched(Exp(dblAttr))) + assertEquals(Some(dblAttr), matched(Log(dblAttr))) + assertEquals(Some(strAttr), matched(Upper(strAttr))) + assertEquals(Some(strAttr), matched(Lower(strAttr))) + } + + @Test + def testDateTransformationsPreserveOrdering(): Unit = { + assertEquals(Some(dateAttr), matched(DateAdd(dateAttr, Literal(1)))) + assertEquals(Some(dateAttr), matched(DateSub(dateAttr, Literal(1)))) + } + + @Test + def testUpCastPreservesOrderingButNumericToStringDoesNot(): Unit = { + // Widening a numeric column preserves ordering, so the source attribute is recovered. + assertEquals(Some(intAttr), matched(Cast(intAttr, LongType))) + // Casting a numeric column to string can reorder values, so it must not match. + assertEquals(None, matched(Cast(intAttr, StringType))) Review Comment: Both `Cast` cases pin the safe directions, but the discriminating one is missing: `HoodieSparkTypeUtils.isCastPreservingOrdering` only rejects `String<->Numeric` and returns `true` for everything else, including narrowing numeric casts. Adding ```scala val longAttr = AttributeReference("l", LongType)() assertEquals(None, matched(Cast(longAttr, IntegerType))) ``` fails today: the matcher recovers the attribute even though non-ANSI narrowing wraps around, and `DataSkippingUtils.translateIntoColumnStatsIndexFilterExpr` then rewrites min/max through the cast. Concrete failure: bigint col `a` in a file with values {1, 2147483647, 4294967297} (min=1, max=4294967297); the filter `cast(a as int) > 100` is translated to `cast(a_maxValue as int) > 100`, and `cast(4294967297L as int)` wraps to 1, so the file is pruned even though it holds a=2147483647 whose cast is 2147483647 -> silently missing rows. Same family: the `Multiply`/`Divide` arms match any literal operand, including negative ones that reverse ordering. Since the missing assertion exposes a production bug, please file a GitHub issue on `isCastPreservingOrdering` (numeric-to-numeric should require `Cast.canUpCast`), and either fix it in this PR or pin current behavior here with a TODO referencing the issue so the hazard is documented rather than invisible. -- 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]
