Copilot commented on code in PR #13282:
URL: https://github.com/apache/gluten/pull/13282#discussion_r4584028193


##########
cpp/velox/tests/SparkFunctionTest.cc:
##########
@@ -125,6 +126,114 @@ TEST_F(SparkFunctionTest, roundWithDecimal) {
   runRoundWithDecimalTest<int8_t>(testRoundWithDecIntegralData<int8_t>());
 }
 
+TEST_F(SparkFunctionTest, integralRoundUsesSessionAnsi) {
+  const auto input = makeRowVector({makeNullableFlatVector<int8_t>({127, -128, 
25, -25, std::nullopt})});
+  for (const auto ansi : {false, true}) {
+    queryCtx_->testingOverrideConfigUnsafe({{sparkAnsiEnabledConfigKey(), ansi 
? "true" : "false"}});
+    const auto expression = "round(c0, cast(-1 as integer))";
+    if (ansi) {
+      VELOX_ASSERT_USER_THROW(evaluate(expression, input), "Arithmetic 
overflow: round(");
+      facebook::velox::test::assertEqualVectors(
+          makeNullableFlatVector<int8_t>({std::nullopt, std::nullopt, 30, -30, 
std::nullopt}),
+          evaluate("try(round(c0, cast(-1 as integer)))", input));
+    } else {
+      facebook::velox::test::assertEqualVectors(
+          makeNullableFlatVector<int8_t>({-126, 126, 30, -30, std::nullopt}), 
evaluate(expression, input));
+    }
+  }
+}
+
+TEST_F(SparkFunctionTest, integralRoundNullScaleSkipsChild) {
+  queryCtx_->testingOverrideConfigUnsafe({{sparkAnsiEnabledConfigKey(), 
"true"}});
+  const auto input = makeRowVector({makeNullableFlatVector<int8_t>({127, -128, 
25, std::nullopt})});
+  facebook::velox::test::assertEqualVectors(
+      makeNullableFlatVector<int8_t>({std::nullopt, std::nullopt, 
std::nullopt, std::nullopt}),
+      evaluate("round(round(c0, cast(-1 as integer)), cast(null as integer))", 
input));
+}
+
+TEST_F(SparkFunctionTest, integralRoundScaleValidation) {
+  const auto validate = [&](std::optional<int32_t> scale, bool castInput) {
+    ::substrait::Expression expression;
+    auto* function = expression.mutable_scalar_function();
+    function->set_function_reference(1);
+    auto* value = function->add_arguments()->mutable_value();
+    if (castInput) {
+      auto* cast = value->mutable_cast();
+      
cast->mutable_type()->mutable_i8()->set_nullability(::substrait::Type_Nullability_NULLABILITY_NULLABLE);
+      value = cast->mutable_input();
+    }
+    
value->mutable_selection()->mutable_direct_reference()->mutable_struct_field()->set_field(0);
+    auto* literal = 
function->add_arguments()->mutable_value()->mutable_literal();
+    if (scale.has_value()) {
+      literal->set_i32(scale.value());
+    } else {
+      
literal->mutable_null()->mutable_i32()->set_nullability(::substrait::Type_Nullability_NULLABILITY_NULLABLE);
+    }
+    
function->mutable_output_type()->mutable_i8()->set_nullability(::substrait::Type_Nullability_NULLABILITY_NULLABLE);
+    gluten::SubstraitToVeloxPlanValidator validator(pool());
+    TypePtr inputType = TINYINT();
+    if (castInput) {
+      inputType = VARCHAR();
+    }
+    return validator.validate(expression, ROW({"value"}, {inputType}), {{1, 
"round:opt_i8_i32"}});
+  };
+
+  for (const auto scale : {0, 1, 400, 401, 
std::numeric_limits<int32_t>::max()}) {
+    SCOPED_TRACE(scale);
+    EXPECT_TRUE(validate(scale, false));
+  }
+  EXPECT_TRUE(validate(std::nullopt, false));
+  EXPECT_TRUE(validate(std::nullopt, true));
+  for (const auto scale : {std::numeric_limits<int32_t>::min(), -401, -1}) {
+    SCOPED_TRACE(scale);
+    EXPECT_FALSE(validate(scale, false));
+  }

Review Comment:
   The test (and current native validator behavior) treats large positive 
literal scales (e.g., 401 and INT_MAX) as valid for native execution, but the 
Spark/JVM validator and docs in this PR constrain integral native eligibility 
to literal scales in [0, 400] or NULL. This cross-layer mismatch makes the 
effective contract harder to reason about and can lead to inconsistent 
validation logs/behavior if the JVM gate is bypassed. Recommendation: align 
native `validateRound` with the same upper bound (reject > 400 with a clear 
validation message) and update this test accordingly, or relax the JVM/docs 
bound if the intent is to support all nonnegative literal scales.



##########
backends-velox/src/test/scala/org/apache/gluten/backendsapi/velox/VeloxIntegralRoundSuite.scala:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.gluten.backendsapi.velox
+
+import org.apache.gluten.config.GlutenConfig
+
+import org.apache.spark.sql.catalyst.expressions.{BoundReference, Literal, 
Round}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+import org.scalatest.funsuite.AnyFunSuite
+
+class VeloxIntegralRoundSuite extends AnyFunSuite {
+  test("integral ROUND rejects unsafe scales even when general native 
validation is disabled") {
+    val conf = new SQLConf
+    conf.setConfString(GlutenConfig.NATIVE_VALIDATION_ENABLED.key, "false")
+    conf.setConfString(SQLConf.ANSI_ENABLED.key, "false")
+    SQLConf.withExistingConf(conf) {
+      val validator = new VeloxValidatorApi
+      for (dataType <- Seq(ByteType, ShortType, IntegerType, LongType)) {
+        val value = BoundReference(0, dataType, nullable = true)
+        Seq(0, 1, 400).foreach {
+          scale => assert(validator.doExprValidate("round", Round(value, 
Literal(scale))))
+        }
+        Seq(Int.MinValue, -401, -400, -1, 401, Int.MaxValue).foreach {
+          scale => assert(!validator.doExprValidate("round", Round(value, 
Literal(scale))))
+        }
+        assert(validator.doExprValidate(
+          "round",
+          Round(value, Literal.create(null, IntegerType))))
+        assert(!validator.doExprValidate(
+          "round",
+          Round(value, BoundReference(1, IntegerType, nullable = false))))
+      }
+    }
+  }
+
+  test("negative integral ROUND preserves Spark's captured overflow mode") {
+    val conf = new SQLConf
+    SQLConf.withExistingConf(conf) {
+      val validator = new VeloxValidatorApi
+      val value = BoundReference(0, ByteType, nullable = true)
+      conf.setConfString(SQLConf.ANSI_ENABLED.key, "false")
+      val legacy = Round(value, Literal(-1))
+      assert(!validator.doExprValidate("round", legacy))
+      conf.setConfString(SQLConf.ANSI_ENABLED.key, "true")
+      assert(!validator.doExprValidate("round", legacy))
+      val current = Round(value, Literal(-1))
+      assert(!validator.doExprValidate("round", current))
+    }

Review Comment:
   This test name says it verifies Spark’s *captured overflow mode*, but the 
assertions only verify that negative-scale integral `round` is always rejected 
by the validator (regardless of when the expression was constructed). Consider 
renaming the test to match what it actually asserts (e.g., that negative 
integral `ROUND` is always ineligible for native execution), or extend the 
assertions to explicitly validate any captured-mode behavior you intend to 
cover here.



-- 
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]

Reply via email to