This is an automated email from the ASF dual-hosted git repository.
philo-he pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git
The following commit(s) were added to refs/heads/main by this push:
new a49e0f3e79 [GLUTEN-13411][FLINK] Add support for ABS, FLOOR, CEIL,
ROUND and unary minus (#13412)
a49e0f3e79 is described below
commit a49e0f3e7909fcebd79f53523ec91ea1c8de9692
Author: wud <[email protected]>
AuthorDate: Thu Nov 12 14:27:07 2026 +0800
[GLUTEN-13411][FLINK] Add support for ABS, FLOOR, CEIL, ROUND and unary
minus (#13412)
---
.../java/org/apache/gluten/rexnode/TypeUtils.java | 9 +
.../rexnode/functions/BaseRexCallConverters.java | 75 +++++++
.../functions/DecimalRexCallConverters.java | 7 +
.../functions/NumericOnlyRexCallConverter.java | 73 +++++++
.../rexnode/functions/RexCallConverterFactory.java | 20 +-
.../functions/RexCallConverterFactoryTest.java | 238 +++++++++++++++++++++
.../runtime/stream/custom/AbsFunctionsTest.java | 69 ++++++
.../stream/custom/ArithmeticDualRunSqlTest.java | 138 ++++++++++++
.../stream/custom/FloorCeilFunctionsTest.java | 83 +++++++
.../runtime/stream/custom/RoundFunctionsTest.java | 92 ++++++++
.../stream/custom/UnaryMinusFunctionsTest.java | 58 +++++
11 files changed, 861 insertions(+), 1 deletion(-)
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/TypeUtils.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/TypeUtils.java
index 388a8f4084..166163c0eb 100644
---
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/TypeUtils.java
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/TypeUtils.java
@@ -49,6 +49,15 @@ public class TypeUtils {
return NUMERIC_TYPE_PRIORITY_LIST.contains(type.getClass());
}
+ /**
+ * Operand-guard variant of the numeric check: unlike {@link
#isNumericType}, also accepts
+ * DECIMAL. The plain list must stay decimal-free — the basic arithmetic
converters rely on it to
+ * decline decimal operands so the decimal converters (with their promotion
casts) take them.
+ */
+ public static boolean isNumericOrDecimalType(Type type) {
+ return isNumericType(type) || type instanceof DecimalType;
+ }
+
public static boolean isStringType(Type type) {
return type instanceof VarCharType;
}
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/BaseRexCallConverters.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/BaseRexCallConverters.java
index 7522c805fa..aaa255dbf2 100644
---
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/BaseRexCallConverters.java
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/BaseRexCallConverters.java
@@ -22,8 +22,10 @@ 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.CastTypedExpr;
import io.github.zhztheplayer.velox4j.expression.TypedExpr;
import io.github.zhztheplayer.velox4j.type.BigIntType;
+import io.github.zhztheplayer.velox4j.type.DecimalType;
import io.github.zhztheplayer.velox4j.type.TimestampType;
import io.github.zhztheplayer.velox4j.type.Type;
@@ -89,6 +91,79 @@ class DefaultRexCallConverter extends BaseRexCallConverter {
}
}
+/**
+ * Converter for the one-operand "-" RexCall (Flink's minusPrefix / SQL -x).
The name maps to the
+ * flinksql negate function; the result type equals the operand type, so no
cast is needed.
+ */
+class UnaryMinusRexCallConverter extends BaseRexCallConverter {
+ public UnaryMinusRexCallConverter(String functionName) {
+ super(functionName);
+ }
+
+ @Override
+ public ValidationResult isSuitable(RexCall callNode, RexConversionContext
context) {
+ if (callNode.getOperands().size() != 1) {
+ return ValidationResult.failure(
+ "Unary minus requires exactly one operand, but found: "
+ + getFunctionProtoTypeName(callNode));
+ }
+ // Numeric operand only: the interval form of unary minus would
+ // otherwise be accepted by the arity guard and fail later at
+ // toType (no interval type mapping); reject it at planning time.
+ // Decimals are accepted: velox's flinksql negate covers them.
+ try {
+ if (!TypeUtils.isNumericOrDecimalType(
+ RexNodeConverter.toType(callNode.getOperands().get(0).getType()))) {
+ return ValidationResult.failure(
+ "Unary minus requires a numeric operand, but found: "
+ + getFunctionProtoTypeName(callNode));
+ }
+ } catch (RuntimeException e) {
+ // Operand types without a velox mapping (e.g. intervals) are
+ // rejected at planning time rather than blowing up in toType.
+ return ValidationResult.failure(
+ "Unary minus requires a numeric operand, but found: "
+ + getFunctionProtoTypeName(callNode));
+ }
+ return ValidationResult.success();
+ }
+
+ @Override
+ public TypedExpr toTypedExpr(RexCall callNode, RexConversionContext context)
{
+ List<TypedExpr> params = getParams(callNode, context);
+ Type resultType = getResultType(callNode);
+ return new CallTypedExpr(resultType, params, functionName);
+ }
+}
+
+/**
+ * Converter for Flink functions whose decimal result type differs from the
operand type by a
+ * value-dependent precision/scale change (e.g. ROUND(x, d) narrows the scale
to d). The velox-side
+ * function stays same-typed (its result type cannot depend on an argument
value), so the plan emits
+ * the same-typed call followed by a cast to Flink's result type. Rounding at
the original scale
+ * leaves only zero digits below the target scale, so the cast is an exact
rescale with no double
+ * rounding.
+ */
+class DecimalPostCastRexCallConverter extends BaseRexCallConverter {
+ public DecimalPostCastRexCallConverter(String functionName) {
+ super(functionName);
+ }
+
+ @Override
+ public TypedExpr toTypedExpr(RexCall callNode, RexConversionContext context)
{
+ List<TypedExpr> params = getParams(callNode, context);
+ Type resultType = getResultType(callNode);
+ if (resultType instanceof DecimalType &&
!callNode.getOperands().isEmpty()) {
+ Type inputType =
RexNodeConverter.toType(callNode.getOperands().get(0).getType());
+ if (!inputType.equals(resultType)) {
+ TypedExpr sameTypeCall = new CallTypedExpr(inputType, params,
functionName);
+ return CastTypedExpr.create(resultType, sameTypeCall, false);
+ }
+ }
+ return new CallTypedExpr(resultType, params, functionName);
+ }
+}
+
class BasicArithmeticOperatorRexCallConverter extends BaseRexCallConverter {
public BasicArithmeticOperatorRexCallConverter(String functionName) {
super(functionName);
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/DecimalRexCallConverters.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/DecimalRexCallConverters.java
index a00abeac6c..e92c6620c9 100644
---
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/DecimalRexCallConverters.java
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/DecimalRexCallConverters.java
@@ -55,6 +55,13 @@ class DecimalArithmeticOperatorRexCallConverters extends
BaseRexCallConverter {
@Override
public ValidationResult isSuitable(RexCall callNode, RexConversionContext
context) {
+ // Binary arithmetic: a one-operand "-" is the unary minus handled by
+ // UnaryMinusRexCallConverter.
+ if (callNode.getOperands().size() != 2) {
+ return ValidationResult.failure(
+ "Decimal arithmetic requires exactly two operands, but found: "
+ + getFunctionProtoTypeName(callNode));
+ }
Type resultType = getResultType(callNode);
List<Type> operandsType =
callNode.getOperands().stream()
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/NumericOnlyRexCallConverter.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/NumericOnlyRexCallConverter.java
new file mode 100644
index 0000000000..151afca474
--- /dev/null
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/NumericOnlyRexCallConverter.java
@@ -0,0 +1,73 @@
+/*
+ * 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()
+ .allMatch(
+ arg ->
TypeUtils.isNumericOrDecimalType(RexNodeConverter.toType(arg.getType())));
+ if (!allNumeric) {
+ return notNumericFailure(callNode);
+ }
+ } catch (RuntimeException e) {
+ // Operand types without a velox mapping are rejected at planning
+ // time rather than blowing up in toType.
+ return notNumericFailure(callNode);
+ }
+ return ValidationResult.success();
+ }
+
+ private ValidationResult notNumericFailure(RexCall callNode) {
+ return ValidationResult.failure(
+ String.format(
+ "Numeric-only operation requires all operands to be numeric or
decimal types, but found: %s",
+ getFunctionProtoTypeName(callNode)));
+ }
+
+ @Override
+ public TypedExpr toTypedExpr(RexCall callNode, RexConversionContext context)
{
+ List<TypedExpr> params = getParams(callNode, context);
+ return new CallTypedExpr(getResultType(callNode), params, functionName);
+ }
+}
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
index 8f98fbdbd3..877adadea0 100644
---
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
@@ -77,6 +77,7 @@ public class RexCallConverterFactory {
Map.entry(
"-",
Arrays.asList(
+ () -> new UnaryMinusRexCallConverter("negate"),
() -> new SubtractRexCallConverter(),
() -> new
DecimalArithmeticOperatorRexCallConverters("subtract"),
() -> new IntervalArithmeticRexCallConverter("minus",
false))),
@@ -145,6 +146,13 @@ 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("ROUND", Arrays.asList(() -> new
DecimalPostCastRexCallConverter("round"))),
// Offload targets implemented in velox's flinksql namespace;
// the names arrive upper-cased from the operator table.
Map.entry(
@@ -180,7 +188,17 @@ public class RexCallConverterFactory {
.map(RexCallConverterBuilder::build)
.filter(
c -> {
- ValidationResult validationResult = c.isSuitable(callNode,
context);
+ ValidationResult validationResult;
+ try {
+ validationResult = c.isSuitable(callNode, context);
+ } catch (RuntimeException e) {
+ // A converter blowing up while probing operand types
+ // (e.g. toType on an unmapped interval) counts as not
+ // suitable so the decision stays a planning-time
+ // rejection instead of a hard failure.
+ failureMessages.add(c.getClass().getName() + ": " +
e.getMessage());
+ return false;
+ }
if (!validationResult.isOk()) {
failureMessages.add(
c.getClass().getName() + ": " +
validationResult.getMessage());
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/functions/RexCallConverterFactoryTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/functions/RexCallConverterFactoryTest.java
new file mode 100644
index 0000000000..96947857b7
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/functions/RexCallConverterFactoryTest.java
@@ -0,0 +1,238 @@
+/*
+ * 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 io.github.zhztheplayer.velox4j.expression.CallTypedExpr;
+import io.github.zhztheplayer.velox4j.expression.CastTypedExpr;
+import io.github.zhztheplayer.velox4j.expression.TypedExpr;
+
+import org.apache.flink.table.planner.calcite.FlinkRexBuilder;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.planner.calcite.FlinkTypeSystem;
+import org.apache.flink.table.planner.functions.sql.FlinkSqlOperatorTable;
+
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.util.Collections;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class RexCallConverterFactoryTest {
+
+ private static FlinkTypeFactory typeFactory;
+ private static RexBuilder rexBuilder;
+ private static RexConversionContext context;
+
+ @BeforeAll
+ public static void setUp() {
+ typeFactory =
+ new FlinkTypeFactory(
+ Thread.currentThread().getContextClassLoader(),
FlinkTypeSystem.INSTANCE);
+ rexBuilder = new FlinkRexBuilder(typeFactory);
+ context = new RexConversionContext(Collections.emptyList());
+ }
+
+ /**
+ * A one-operand "-" is the Flink unary minus; it must resolve to the negate
converter and emit a
+ * same-typed negate call.
+ */
+ @Test
+ public void testUnaryMinusResolvesWithSingleOperand() {
+ SqlFunction minus =
+ new SqlFunction(
+ "-",
+ SqlKind.OTHER_FUNCTION,
+ ReturnTypes.ARG0,
+ null,
+ OperandTypes.NUMERIC,
+ SqlFunctionCategory.NUMERIC);
+ RexNode operand =
+ rexBuilder.makeLiteral(7,
typeFactory.createSqlType(SqlTypeName.INTEGER), false);
+ RexCall call = (RexCall) rexBuilder.makeCall(minus, operand);
+ RexCallConverter converter = RexCallConverterFactory.getConverter(call,
context);
+ assertThat(converter).isInstanceOf(UnaryMinusRexCallConverter.class);
+ TypedExpr expr = converter.toTypedExpr(call, context);
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr) expr).getFunctionName()).isEqualTo("negate");
+ assertThat(expr.getInputs()).hasSize(1);
+ }
+
+ /** A two-operand "-" over decimals still resolves to the decimal arithmetic
converter. */
+ @Test
+ public void testBinaryDecimalMinusResolvesToDecimalConverter() {
+ SqlFunction minus =
+ new SqlFunction(
+ "-",
+ SqlKind.OTHER_FUNCTION,
+ ReturnTypes.ARG0,
+ null,
+ OperandTypes.NUMERIC,
+ SqlFunctionCategory.NUMERIC);
+ RexNode left =
+ rexBuilder.makeExactLiteral(
+ new BigDecimal("12.34"),
typeFactory.createSqlType(SqlTypeName.DECIMAL, 10, 2));
+ RexNode right =
+ rexBuilder.makeExactLiteral(
+ new BigDecimal("1.00"),
typeFactory.createSqlType(SqlTypeName.DECIMAL, 10, 2));
+ RexCall call = (RexCall) rexBuilder.makeCall(minus, left, right);
+ RexCallConverter converter = RexCallConverterFactory.getConverter(call,
context);
+
assertThat(converter).isInstanceOf(DecimalArithmeticOperatorRexCallConverters.class);
+ }
+
+ /**
+ * FLOOR over a DECIMAL resolves through the numeric-only converter: the
operand guard accepts
+ * DECIMAL (velox's flinksql floor/ceil cover decimal(p, s) -> decimal(p,
0)), so the decimal form
+ * is no longer rejected at planning time.
+ */
+ @Test
+ public void testFloorDecimalResolvesToNumericOnlyConverter() {
+ RexNode operand =
+ rexBuilder.makeExactLiteral(
+ new BigDecimal("55.67"),
typeFactory.createSqlType(SqlTypeName.DECIMAL, 10, 2));
+ RexCall call = (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.FLOOR,
operand);
+ RexCallConverter converter = RexCallConverterFactory.getConverter(call,
context);
+ assertThat(converter).isInstanceOf(NumericOnlyRexCallConverter.class);
+ TypedExpr expr = converter.toTypedExpr(call, context);
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr) expr).getFunctionName()).isEqualTo("floor");
+ }
+
+ /** Unary minus over a DECIMAL resolves through the negate converter (guard
accepts DECIMAL). */
+ @Test
+ public void testUnaryMinusDecimalResolvesToNegateConverter() {
+ SqlFunction minus =
+ new SqlFunction(
+ "-",
+ SqlKind.OTHER_FUNCTION,
+ ReturnTypes.ARG0,
+ null,
+ OperandTypes.NUMERIC,
+ SqlFunctionCategory.NUMERIC);
+ RexNode operand =
+ rexBuilder.makeExactLiteral(
+ new BigDecimal("-12.34"),
typeFactory.createSqlType(SqlTypeName.DECIMAL, 10, 2));
+ RexCall call = (RexCall) rexBuilder.makeCall(minus, operand);
+ RexCallConverter converter = RexCallConverterFactory.getConverter(call,
context);
+ assertThat(converter).isInstanceOf(UnaryMinusRexCallConverter.class);
+ }
+
+ /**
+ * ROUND over a decimal narrows the scale by the (value-dependent) second
argument, which a velox
+ * signature cannot express; the converter must emit a same-typed round call
wrapped in a cast to
+ * Flink's inferred result type.
+ */
+ @Test
+ public void testRoundDecimalEmitsPostCast() {
+ RexNode operand =
+ rexBuilder.makeExactLiteral(
+ new BigDecimal("55.67"),
typeFactory.createSqlType(SqlTypeName.DECIMAL, 10, 2));
+ RexCall call = (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.ROUND,
operand);
+ RexCallConverter converter = RexCallConverterFactory.getConverter(call,
context);
+ assertThat(converter).isInstanceOf(DecimalPostCastRexCallConverter.class);
+ TypedExpr expr = converter.toTypedExpr(call, context);
+ assertThat(expr).isInstanceOf(CastTypedExpr.class);
+ TypedExpr input = expr.getInputs().get(0);
+ assertThat(input).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr) input).getFunctionName()).isEqualTo("round");
+ // The inner call stays same-typed (decimal in, decimal out)...
+ assertThat(input.getReturnType())
+ .isInstanceOf(io.github.zhztheplayer.velox4j.type.DecimalType.class);
+ // ...and the cast lands on Flink's round type (decimal with narrowed
scale).
+ assertThat(expr.getReturnType())
+ .isInstanceOf(io.github.zhztheplayer.velox4j.type.DecimalType.class);
+ assertThat(expr.getReturnType()).isNotEqualTo(input.getReturnType());
+ }
+
+ /** Simple name-mapped entries resolve to the default converter. */
+ @Test
+ public void testFloorResolvesToNumericOnlyConverter() {
+ RexNode operand =
+ rexBuilder.makeLiteral(1.5,
typeFactory.createSqlType(SqlTypeName.DOUBLE), false);
+ RexLiteral lit = (RexLiteral) operand;
+ RexCall call = (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.FLOOR,
lit);
+ RexCallConverter converter = RexCallConverterFactory.getConverter(call,
context);
+ assertThat(converter).isInstanceOf(NumericOnlyRexCallConverter.class);
+ TypedExpr expr = converter.toTypedExpr(call, context);
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr) expr).getFunctionName()).isEqualTo("floor");
+ }
+
+ /** The datetime form FLOOR(ts TO DAY) is rejected at planning time, not at
velox runtime. */
+ @Test
+ public void testFloorDatetimeFormRejectedAtPlanning() {
+ SqlFunction floor =
+ new SqlFunction(
+ "FLOOR",
+ SqlKind.FLOOR,
+ ReturnTypes.ARG0,
+ null,
+ OperandTypes.ANY,
+ SqlFunctionCategory.NUMERIC);
+ RexNode ts =
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.TIMESTAMP), 0);
+ // The parser renders FLOOR(ts TO DAY) with a SYMBOL operand; SYMBOL
+ // maps to VARCHAR at the converter layer, so a VARCHAR literal is
+ // type-equivalent for the guard under test.
+ RexNode unit =
+ rexBuilder.makeLiteral("DAY",
typeFactory.createSqlType(SqlTypeName.VARCHAR), false);
+ RexCall call = (RexCall) rexBuilder.makeCall(floor, ts, unit);
+ assertThatThrownBy(() -> RexCallConverterFactory.getConverter(call,
context))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("No suitable converter found for: FLOOR");
+ }
+
+ /** Unary minus over a year-month interval is rejected at planning time. */
+ @Test
+ public void testUnaryMinusIntervalRejectedAtPlanning() {
+ SqlFunction minus =
+ new SqlFunction(
+ "-",
+ SqlKind.OTHER_FUNCTION,
+ ReturnTypes.ARG0,
+ null,
+ OperandTypes.NUMERIC,
+ SqlFunctionCategory.NUMERIC);
+ RexNode operand =
+ rexBuilder.makeNullLiteral(
+ typeFactory.createSqlIntervalType(
+ new org.apache.calcite.sql.SqlIntervalQualifier(
+ org.apache.calcite.avatica.util.TimeUnit.YEAR,
+
org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED,
+ org.apache.calcite.avatica.util.TimeUnit.YEAR,
+
org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED,
+ org.apache.calcite.sql.parser.SqlParserPos.ZERO)));
+ RexCall call = (RexCall) rexBuilder.makeCall(minus, operand);
+ assertThatThrownBy(() -> RexCallConverterFactory.getConverter(call,
context))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("No suitable converter found for: -");
+ }
+}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/AbsFunctionsTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/AbsFunctionsTest.java
new file mode 100644
index 0000000000..c81e9554e3
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/AbsFunctionsTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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 AbsFunctionsTest extends GlutenStreamingTestBase {
+
+ @Override
+ @BeforeEach
+ public void before() throws Exception {
+ super.before();
+ }
+
+ @Test
+ void testAbsWithNegativeValues() {
+ List<Row> rows =
+ Arrays.asList(
+ Row.of(-25L, new BigDecimal("-12.34"), -2.5d),
+ Row.of(25L, new BigDecimal("12.34"), 2.5d),
+ Row.of(null, null, null));
+ createSimpleBoundedValuesTable("tblAbs", "i bigint, p decimal(10, 2), x
double", rows);
+ String query = "select ABS(i), ABS(p), ABS(x) from tblAbs";
+ runAndCheck(
+ query, Arrays.asList("+I[25, 12.34, 2.5]", "+I[25, 12.34, 2.5]",
"+I[null, null, null]"));
+ }
+
+ @Test
+ void testAbsWithArithmetic() {
+ List<Row> rows = Arrays.asList(Row.of(3L), Row.of(19L), Row.of((Object)
null));
+ createSimpleBoundedValuesTable("tblAbsA", "i bigint", rows);
+ String query = "select ABS(i - 30) from tblAbsA";
+ runAndCheck(query, Arrays.asList("+I[27]", "+I[11]", "+I[null]"));
+ }
+
+ /** INT_MIN and the int64 most-negative value wrap to themselves under abs
(Math.abs). */
+ @Test
+ void testAbsIntegerBoundaries() {
+ List<Row> rows =
+ Arrays.asList(Row.of(Integer.MIN_VALUE, Long.MIN_VALUE), Row.of(-1,
-9007199254740993L));
+ createSimpleBoundedValuesTable("tblAbsEdge", "i int, l bigint", rows);
+ String query = "select ABS(i), ABS(l) from tblAbsEdge";
+ runAndCheck(
+ query, Arrays.asList("+I[-2147483648, -9223372036854775808]", "+I[1,
9007199254740993]"));
+ }
+}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ArithmeticDualRunSqlTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ArithmeticDualRunSqlTest.java
new file mode 100644
index 0000000000..6b9f3db31b
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ArithmeticDualRunSqlTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * The dual-run SQL cases for the arithmetic functions, embedded verbatim:
each query is the exact
+ * SELECT (including the VALUES source) that ran on Flink 1.19.2 in the
dual-run verification
+ * (native vs gluten-offloaded, identical inputs, exact changelog-multiset
comparison), and the
+ * expected rows are the native output the dual-run matched against. Kept here
so the queries and
+ * their verified results live with the code.
+ */
+class ArithmeticDualRunSqlTest extends GlutenStreamingTestBase {
+
+ @Test
+ void testRoundDoubleDualRunSql() {
+ String query =
+ "SELECT\n"
+ + " ROUND(x),\n"
+ + " ROUND(x, 1),\n"
+ + " ROUND(x, -1),\n"
+ + " ROUND(x, 2),\n"
+ + " ROUND(y, 1)\n"
+ + "FROM (\n"
+ + " VALUES\n"
+ + " (28.05, 0.15),\n"
+ + " (99.99, -2.5),\n"
+ + " (2.5, 12.5),\n"
+ + " (-251.0, 1234.789),\n"
+ + " (CAST(NULL AS DOUBLE), 28.05)\n"
+ + ") AS t (x, y)";
+ List<String> expected =
+ Arrays.asList(
+ "+I[28.0, 28.1, 30.0, 28.05, 0.2]",
+ "+I[100.0, 100.0, 100.0, 99.99, -2.5]",
+ "+I[3.0, 2.5, 0.0, 2.5, 12.5]",
+ "+I[-251.0, -251.0, -250.0, -251.0, 1234.8]",
+ "+I[null, null, null, null, 28.1]");
+ runAndCheck(query, expected);
+ }
+
+ @Test
+ void testRoundDecimalDualRunSql() {
+ String query =
+ "SELECT\n"
+ + " ROUND(p),\n"
+ + " ROUND(p, 1),\n"
+ + " ROUND(p, -1),\n"
+ + " ROUND(p, 2)\n"
+ + "FROM (\n"
+ + " VALUES\n"
+ + " (55.67),\n"
+ + " (28.55),\n"
+ + " (28.05),\n"
+ + " (45.00),\n"
+ + " (CAST(NULL AS DECIMAL(10, 2)))\n"
+ + ") AS t (p)";
+ List<String> expected =
+ Arrays.asList(
+ "+I[56, 55.7, 60, 55.67]",
+ "+I[29, 28.6, 30, 28.55]",
+ "+I[28, 28.1, 30, 28.05]",
+ "+I[45, 45.0, 50, 45.00]",
+ "+I[null, null, null, null]");
+ runAndCheck(query, expected);
+ }
+
+ @Test
+ void testAbsUminusDualRunSql() {
+ String query =
+ "SELECT\n"
+ + " ABS(i),\n"
+ + " ABS(p),\n"
+ + " -x,\n"
+ + " -p,\n"
+ + " -ABS(-i)\n"
+ + "FROM (\n"
+ + " VALUES\n"
+ + " (-25, -12.34, 2.5),\n"
+ + " (25, 12.34, -2.5),\n"
+ + " (CAST(NULL AS BIGINT), CAST(NULL AS DECIMAL(10, 2)),
CAST(NULL AS DOUBLE))\n"
+ + ") AS t (i, p, x)";
+ List<String> expected =
+ Arrays.asList(
+ "+I[25, 12.34, -2.5, 12.34, -25]",
+ "+I[25, 12.34, 2.5, -12.34, -25]",
+ "+I[null, null, null, null, null]");
+ runAndCheck(query, expected);
+ }
+
+ @Test
+ void testFloorCeilDualRunSql() {
+ String query =
+ "SELECT\n"
+ + " FLOOR(x),\n"
+ + " CEIL(x),\n"
+ + " FLOOR(p),\n"
+ + " CEIL(p),\n"
+ + " FLOOR(i),\n"
+ + " CEIL(i)\n"
+ + "FROM (\n"
+ + " VALUES\n"
+ + " (55.67, 28.55, 7),\n"
+ + " (-55.01, -28.55, -7),\n"
+ + " (45.00, 45.00, 0),\n"
+ + " (-0.5, 0.5, CAST(NULL AS BIGINT)),\n"
+ + " (CAST(NULL AS DOUBLE), CAST(NULL AS DECIMAL(10, 2)), 9)\n"
+ + ") AS t (x, p, i)";
+ List<String> expected =
+ Arrays.asList(
+ "+I[55.0, 56.0, 28, 29, 7, 7]",
+ "+I[-56.0, -55.0, -29, -28, -7, -7]",
+ "+I[45.0, 45.0, 45, 45, 0, 0]",
+ "+I[-1.0, -0.0, 0, 1, null, null]",
+ "+I[null, null, null, null, 9, 9]");
+ runAndCheck(query, expected);
+ }
+}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/FloorCeilFunctionsTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/FloorCeilFunctionsTest.java
new file mode 100644
index 0000000000..0c9a87a48a
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/FloorCeilFunctionsTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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 FloorCeilFunctionsTest extends GlutenStreamingTestBase {
+
+ @Override
+ @BeforeEach
+ public void before() throws Exception {
+ super.before();
+ }
+
+ @Test
+ void testFloorCeilDouble() {
+ List<Row> rows =
+ Arrays.asList(
+ Row.of(55.67d), Row.of(-55.01d), Row.of(45.00d), Row.of(-0.5d),
Row.of((Object) null));
+ createSimpleBoundedValuesTable("tblFC", "x double", rows);
+ String query = "select FLOOR(x), CEIL(x) from tblFC";
+ runAndCheck(
+ query,
+ Arrays.asList(
+ "+I[55.0, 56.0]",
+ "+I[-56.0, -55.0]",
+ "+I[45.0, 45.0]",
+ "+I[-1.0, -0.0]",
+ "+I[null, null]"));
+ }
+
+ @Test
+ void testFloorCeilDecimal() {
+ List<Row> rows =
+ Arrays.asList(
+ Row.of(new BigDecimal("55.67")),
+ Row.of(new BigDecimal("-55.01")),
+ Row.of(new BigDecimal("45.00")),
+ Row.of((Object) null));
+ createSimpleBoundedValuesTable("tblFCP", "p decimal(10, 2)", rows);
+ String query = "select FLOOR(p), CEIL(p) from tblFCP";
+ runAndCheck(query, Arrays.asList("+I[55, 56]", "+I[-56, -55]", "+I[45,
45]", "+I[null, null]"));
+ }
+
+ @Test
+ void testFloorCeilIntegerIdentity() {
+ List<Row> rows = Arrays.asList(Row.of(7L), Row.of(-7L), Row.of((Object)
null));
+ createSimpleBoundedValuesTable("tblFCI", "i bigint", rows);
+ String query = "select FLOOR(i), CEIL(i) from tblFCI";
+ runAndCheck(query, Arrays.asList("+I[7, 7]", "+I[-7, -7]", "+I[null,
null]"));
+ }
+
+ @Test
+ void testCeilingAlias() {
+ List<Row> rows = Arrays.asList(Row.of(55.67d));
+ createSimpleBoundedValuesTable("tblCA", "x double", rows);
+ String query = "select CEILING(x) from tblCA";
+ runAndCheck(query, Arrays.asList("+I[56.0]"));
+ }
+}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/RoundFunctionsTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/RoundFunctionsTest.java
new file mode 100644
index 0000000000..2866b74936
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/RoundFunctionsTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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 the 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";
+ // ROUND(-2.5, 2) keeps the value: no tie at scale 2 (Flink returns -2.5).
+ runAndCheck(query, Arrays.asList("+I[2.68]", "+I[647568.82]", "+I[-2.5]"));
+ }
+}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/UnaryMinusFunctionsTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/UnaryMinusFunctionsTest.java
new file mode 100644
index 0000000000..4452ed4a1d
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/UnaryMinusFunctionsTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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 UnaryMinusFunctionsTest extends GlutenStreamingTestBase {
+
+ @Override
+ @BeforeEach
+ public void before() throws Exception {
+ super.before();
+ }
+
+ @Test
+ void testUnaryMinus() {
+ List<Row> rows =
+ Arrays.asList(
+ Row.of(3L, new BigDecimal("12.34"), 2.5d),
+ Row.of(-3L, new BigDecimal("-12.34"), -2.5d),
+ Row.of(null, null, null));
+ createSimpleBoundedValuesTable("tblNeg", "i bigint, p decimal(10, 2), x
double", rows);
+ String query = "select -i, -p, -x from tblNeg";
+ runAndCheck(
+ query, Arrays.asList("+I[-3, -12.34, -2.5]", "+I[3, 12.34, 2.5]",
"+I[null, null, null]"));
+ }
+
+ @Test
+ void testUnaryMinusNestedWithAbs() {
+ List<Row> rows = Arrays.asList(Row.of(7L), Row.of(-7L), Row.of((Object)
null));
+ createSimpleBoundedValuesTable("tblNegA", "i bigint", rows);
+ String query = "select -ABS(i) from tblNegA";
+ runAndCheck(query, Arrays.asList("+I[-7]", "+I[-7]", "+I[null]"));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]