ggjh-159 commented on code in PR #13412:
URL: https://github.com/apache/gluten/pull/13412#discussion_r4430380587


##########
gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/NumericOnlyRexCallConverter.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.rexnode.functions;
+
+import org.apache.gluten.rexnode.RexConversionContext;
+import org.apache.gluten.rexnode.RexNodeConverter;
+import org.apache.gluten.rexnode.TypeUtils;
+import org.apache.gluten.rexnode.ValidationResult;
+
+import io.github.zhztheplayer.velox4j.expression.CallTypedExpr;
+import io.github.zhztheplayer.velox4j.expression.TypedExpr;
+
+import org.apache.calcite.rex.RexCall;
+
+import java.util.List;
+
+/**
+ * Converter for functions whose velox registrations cover numeric operands 
only. Non-numeric
+ * operands are rejected here so conversion fails loudly at planning time 
(e.g. the datetime form
+ * FLOOR(ts TO DAY) carries a SYMBOL literal that maps to VARCHAR and would 
otherwise produce an
+ * unresolvable velox signature at runtime).
+ */
+class NumericOnlyRexCallConverter extends BaseRexCallConverter {
+
+  public NumericOnlyRexCallConverter(String functionName) {
+    super(functionName);
+  }
+
+  @Override
+  public ValidationResult isSuitable(RexCall callNode, RexConversionContext 
context) {
+    try {
+      boolean allNumeric =
+          callNode.getOperands().stream()

Review Comment:
   Decimal operands are rejected at planning time: `TypeUtils.isNumericType` 
returns false for DECIMAL (the numeric type list in TypeUtils has no 
`DecimalType`), so FLOOR/CEIL and unary minus fail the `isSuitable` guard and 
the PR's own decimal tests error out. The chain supports them — the decimal 
ROUND and ABS paths have no such guard and pass the same test run — so please 
make the numeric check accept DECIMAL.



##########
gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java:
##########
@@ -119,6 +120,14 @@ public class RexCallConverterFactory {
           // substring returns an empty string. Start semantics (0 counts as 1,
           // negative counts from the end) already match Flink.
           Map.entry("SUBSTRING", Arrays.asList(() -> new 
SubstringRexCallConverter())),
+          // Offload targets implemented in velox's flinksql namespace. ABS/
+          // FLOOR/CEIL/ROUND arrive upper-cased from the operator table;
+          // unary minus arrives as a one-operand "-" (see above).
+          Map.entry("ABS", Arrays.asList(() -> new 
DefaultRexCallConverter("abs"))),
+          Map.entry("FLOOR", Arrays.asList(() -> new 
NumericOnlyRexCallConverter("floor"))),
+          Map.entry("CEIL", Arrays.asList(() -> new 
NumericOnlyRexCallConverter("ceil"))),
+          Map.entry("CEILING", Arrays.asList(() -> new 
DefaultRexCallConverter("ceil"))),
+          Map.entry("ROUND", Arrays.asList(() -> new 
DecimalPostCastRexCallConverter("round"))),

Review Comment:
   `ROUND(647568.815, 2)` over DOUBLE returns 647569 in the end-to-end test 
(`testRoundHalfUpTieInDecimalDomain`), losing the rounded scale — Flink returns 
647568.82, the pinned velox `round` implementation asserts 647568.82 for the 
same input in its own unit test, and the 2.675 row of the same test comes back 
correct (2.68). Please fix the offload path so the rounded scale survives.



##########
gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/RoundFunctionsTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.table.runtime.stream.custom;
+
+import org.apache.gluten.table.runtime.stream.common.GlutenStreamingTestBase;
+
+import org.apache.flink.types.Row;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.List;
+
+class RoundFunctionsTest extends GlutenStreamingTestBase {
+
+  @Override
+  @BeforeEach
+  public void before() throws Exception {
+    super.before();
+  }
+
+  @Test
+  void testRoundDoubleHalfUp() {
+    List<Row> rows =
+        Arrays.asList(
+            Row.of(28.05d, 0.15d),
+            Row.of(99.99d, -2.5d),
+            Row.of(2.5d, 12.5d),
+            Row.of(-251.0d, 1234.789d),
+            Row.of(null, 28.05d));
+    createSimpleBoundedValuesTable("tblRoundD", "x double, y double", rows);
+    String query = "select ROUND(x), ROUND(x, 1), ROUND(x, -1), ROUND(y, 1) 
from tblRoundD";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[28.0, 28.1, 30.0, 0.2]",
+            "+I[100.0, 100.0, 100.0, -2.5]",
+            "+I[3.0, 2.5, 0.0, 12.5]",
+            "+I[-251.0, -251.0, -250.0, 1234.8]",
+            "+I[null, null, null, 28.1]"));
+  }
+
+  @Test
+  void testRoundDoubleNegativeScale() {
+    List<Row> rows = Arrays.asList(Row.of(25.0d), Row.of(-25.0d), 
Row.of(249.0d), Row.of(-251.0d));
+    createSimpleBoundedValuesTable("tblRoundN", "x double", rows);
+    String query = "select ROUND(x, -1) from tblRoundN";
+    runAndCheck(query, Arrays.asList("+I[30.0]", "+I[-30.0]", "+I[250.0]", 
"+I[-250.0]"));
+  }
+
+  @Test
+  void testRoundDecimal() {
+    List<Row> rows =
+        Arrays.asList(
+            Row.of(new BigDecimal("55.67")),
+            Row.of(new BigDecimal("28.55")),
+            Row.of(new BigDecimal("45.00")),
+            Row.of((Object) null));
+    createSimpleBoundedValuesTable("tblRoundP", "p decimal(10, 2)", rows);
+    String query = "select ROUND(p), ROUND(p, 1), ROUND(p, -1) from tblRoundP";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[56, 55.7, 60]", "+I[29, 28.6, 30]", "+I[45, 45.0, 50]", 
"+I[null, null, null]"));
+  }
+
+  /** The tie cases that separate HALF_UP-in-decimal-domain from binary-domain 
rounding. */
+  @Test
+  void testRoundHalfUpTieInDecimalDomain() {
+    List<Row> rows = Arrays.asList(Row.of(2.675d), Row.of(647568.815d), 
Row.of(-2.5d));
+    createSimpleBoundedValuesTable("tblRoundTie", "x double", rows);
+    String query = "select ROUND(x, 2) from tblRoundTie";
+    runAndCheck(query, Arrays.asList("+I[2.68]", "+I[647568.82]", "+I[-3.0]"));

Review Comment:
   The third expectation of `testRoundHalfUpTieInDecimalDomain` is wrong: Flink 
returns -2.5 for `ROUND(-2.5, 2)` (d keeps two decimal places, so no rounding 
happens for this value), and the run's actual -2.5 is correct — please change 
the expectation from `-3.0` to `-2.5`.



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