You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@auron.apache.org by "weiqingy (via GitHub)" <gi...@apache.org> on 2026/04/05 21:17:09 UTC

[PR] Auron 1859 impl v2 [auron]

weiqingy opened a new pull request, #2167:
URL: https://github.com/apache/auron/pull/2167

   # Which issue does this PR close?
   
   Closes #1859
   
   # Rationale for this change
   
   Next step in the Flink integration track (#1264). After #1856 established the converter framework (`FlinkRexNodeConverter`, `FlinkNodeConverterFactory`, `ConverterContext`), this PR adds the first concrete converter implementations that translate Flink/Calcite `RexNode` expressions into Auron native `PhysicalExprNode` protobuf representations. Required before #1857 (`FlinkAuronCalcOperator`) can wire them into the execution pipeline.
   
   # What changes are included in this PR?
   
   Three new `FlinkRexNodeConverter` implementations (3 commits, 6 new files, 1 modified):
   
   **Commit 1 — `RexInputRefConverter`**
   - Converts `RexInputRef` column references to `PhysicalColumn` (name + index)
   - Resolves column names from the input schema via `ConverterContext`
   
   **Commit 2 — `RexLiteralConverter`**
   - Converts `RexLiteral` scalar values to `ScalarValue` with Arrow IPC bytes
   - Supports: TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL, BOOLEAN, CHAR, VARCHAR, NULL
   - Serializes via single-element Arrow vector → `ArrowStreamWriter` → `ipc_bytes` field
   
   **Commit 3 — `RexCallConverter`**
   - Converts `RexCall` operator expressions dispatched by `SqlKind`
   - Arithmetic: `+`, `-`, `*`, `/`, `%` → `PhysicalBinaryExprNode` with explicit type promotion
   - Unary: `-` → `PhysicalNegativeNode`, `+` → identity passthrough
   - CAST → `PhysicalTryCastNode`
   - Type promotion via `getCommonTypeForComparison()` + `castIfNecessary()` (per reviewer PoC)
   - Recursive operand conversion via `FlinkNodeConverterFactory`
   
   **pom.xml** — Added `**/*Test.java` to surefire includes for the planner module
   
   **Key design decisions** (per @Tartarus0zm PoC in https://github.com/apache/auron/issues/1859#issuecomment-4181565860):
   - `PhysicalTryCastNode` (not `PhysicalCastNode`) for all type casts
   - Explicit type promotion — does not rely solely on Calcite upstream CASTs
   - Output type cast when compatible type differs from declared return type
   - Division by zero handling deferred to #1857 (requires Rust-side `Flink_NullIfZero`)
   
   # Are there any user-facing changes?
   
   No.
   
   # How was this patch tested?
   
   33 unit tests across 3 test classes:
   - `RexInputRefConverterTest` — 4 tests (node class, isSupported, first/second column)
   - `RexLiteralConverterTest` — 12 tests (all supported types, null literal, unsupported type rejection)
   - `RexCallConverterTest` — 17 tests (all 5 arithmetic ops, unary minus/plus, cast, mixed-type promotion, output-type cast, nested expressions, isSupported checks, `getCommonTypeForComparison` direct tests)


-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #2167:
URL: https://github.com/apache/auron/pull/2167#issuecomment-4189557013

   Hi @Tartarus0zm, the implementation PR for https://github.com/apache/auron/issues/1859 is ready for review.
   
   To help with the review, I also included:
   
   docs/PR-AURON-1859/AURON-1859-DESIGN.md
   docs/reviewhelper/AURON-1859/
   These markdown files are only for review support and will be removed after the review is complete.
   
   Could you please take a look when you have a chance? Thanks!


-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038138707


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if ((t1 == SqlTypeName.BIGINT || t2 == SqlTypeName.BIGINT) && notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }

Review Comment:
   Resolved. 
   
   The observation is technically correct - mixed exact integer types (e.g., TINYINT + SMALLINT) would fall through to DOUBLE. In practice, this path is unreachable because Calcite/Flink's type coercion inserts explicit CAST nodes to align operands before the arithmetic `RexCall` reaches our converter.
   
   That said, since the fix is trivial and makes the safety net semantically correct, I've tightened the condition in b1749a81: any two exact integer types now promote to BIGINT instead of falling through to DOUBLE. The change only affects the previously-unreachable mixed-exact-integer path — all other promotion behavior is unchanged.
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038136384


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }

Review Comment:
   The default-precision DECIMAL returned by `createSqlType(SqlTypeName.DECIMAL)` is used as the promotion target, but crucially:
   
   1. When both operands are DECIMAL (different precision), `getCommonTypeForComparison` returns `type1` directly (line 199 — `SqlTypeName` match short-circuits), so neither operand gets a default-precision cast.
   2. For cross-family promotion (e.g., INTEGER + DECIMAL), only the INTEGER operand gets cast to DECIMAL, which is lossless regardless of precision.
   3. The final output is always cast to `call.getType()`, which carries Calcite's correctly computed precision/scale.
   
   The intermediate `compatibleType` controls which operands get promoted, not the final result precision. This pattern follows the reviewer's PoC code.
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038128213


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexInputRefConverter.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.auron.flink.table.planner.converter;
+
+import org.apache.auron.protobuf.PhysicalColumn;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts a Calcite {@link RexInputRef} (column reference) to an Auron native {@link
+ * PhysicalExprNode} containing a {@link PhysicalColumn}.
+ *
+ * <p>Column references are always supported — every {@code RexInputRef} maps directly to a named,
+ * indexed column in the input schema provided by the {@link ConverterContext}.
+ */
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    /**
+     * Always returns {@code true} — column references are unconditionally supported.
+     *
+     * @param node the RexNode to check (must be a {@link RexInputRef})
+     * @param context shared conversion state
+     * @return {@code true}
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        return true;
+    }

Review Comment:
   Resolved in b1749a81: `isSupported()` now validates `inputRef.getIndex() < context.getInputType().getFieldCount()`. Added a test for the out-of-range case.
   
   Note: the factory's catch-all in `convertRexNode()` (line 116-119) would have prevented a crash, but having the check in `isSupported()` makes the intent explicit and avoids relying on exception-based flow control.



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexInputRefConverter.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.auron.flink.table.planner.converter;
+
+import org.apache.auron.protobuf.PhysicalColumn;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts a Calcite {@link RexInputRef} (column reference) to an Auron native {@link
+ * PhysicalExprNode} containing a {@link PhysicalColumn}.
+ *
+ * <p>Column references are always supported — every {@code RexInputRef} maps directly to a named,
+ * indexed column in the input schema provided by the {@link ConverterContext}.
+ */
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    /**
+     * Always returns {@code true} — column references are unconditionally supported.
+     *
+     * @param node the RexNode to check (must be a {@link RexInputRef})
+     * @param context shared conversion state
+     * @return {@code true}
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexInputRef} to a {@link PhysicalExprNode} with a {@link
+     * PhysicalColumn}.
+     *
+     * <p>Resolves the column name from the input schema via {@link
+     * ConverterContext#getInputType()}.
+     *
+     * @param node the RexNode to convert (must be a {@link RexInputRef})
+     * @param context shared conversion state containing the input schema
+     * @return a {@link PhysicalExprNode} wrapping a {@link PhysicalColumn} with name and index
+     * @throws IllegalArgumentException if the node is not a {@link RexInputRef}
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {

Review Comment:
   The factory dispatches by `node.getClass()` at `FlinkNodeConverterFactory.java:109` — only `RexInputRef` instances reach this converter. The cast is guaranteed safe by the framework's dispatch contract.
   
   The `@throws IllegalArgumentException` in the Javadoc inherits from the `FlinkNodeConverter` interface (line 62), which documents the general contract for all converter implementations. In practice, this exception path is unreachable through normal factory dispatch.
   
   Adding an `instanceof` check would validate an internal framework invariant on every call — per our project conventions, we validate at system boundaries rather than internal API boundaries. The factory itself is the boundary.
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3094579898


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(

Review Comment:
   Done in 4e50bfd4. Extracted `getCommonTypeForComparison()`, `castIfNecessary()`, `wrapInTryCast()`, and the `TYPE_FACTORY` constant into a new `TypeCastUtils` utility class in the same package.
   
   `castIfNecessary()` signature was decoupled from the factory — it now takes `(PhysicalExprNode converted, RelDataType sourceType, RelDataType targetType)` instead of `(RexNode, RelDataType, ConverterContext)`, making it genuinely reusable by future converters without requiring a factory reference.
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3094602034


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexLiteralConverter.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import com.google.protobuf.ByteString;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BigIntVector;
+import org.apache.arrow.vector.BitVector;
+import org.apache.arrow.vector.DecimalVector;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.Float4Vector;
+import org.apache.arrow.vector.Float8Vector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.SmallIntVector;
+import org.apache.arrow.vector.TinyIntVector;
+import org.apache.arrow.vector.VarCharVector;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.ipc.ArrowStreamWriter;
+import org.apache.arrow.vector.types.FloatingPointPrecision;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.ScalarValue;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+/**
+ * Converts a Calcite {@link RexLiteral} to an Auron native {@link PhysicalExprNode}
+ * containing a {@link ScalarValue} with Arrow IPC bytes.
+ *
+ * <p>The literal value is serialized as a single-element Arrow vector in IPC stream format,
+ * following the same pattern as the Spark implementation in {@code NativeConverters}.
+ *
+ * <p>Supported types: {@code TINYINT}, {@code SMALLINT}, {@code INTEGER}, {@code BIGINT},
+ * {@code FLOAT}, {@code DOUBLE}, {@code DECIMAL}, {@code BOOLEAN}, {@code CHAR},
+ * {@code VARCHAR}, and {@code NULL} (of a supported type).
+ */
+public class RexLiteralConverter implements FlinkRexNodeConverter {
+
+    private static final Set<SqlTypeName> SUPPORTED_TYPES = EnumSet.of(
+            SqlTypeName.TINYINT,
+            SqlTypeName.SMALLINT,
+            SqlTypeName.INTEGER,
+            SqlTypeName.BIGINT,
+            SqlTypeName.FLOAT,
+            SqlTypeName.DOUBLE,
+            SqlTypeName.DECIMAL,
+            SqlTypeName.BOOLEAN,
+            SqlTypeName.CHAR,
+            SqlTypeName.VARCHAR);
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexLiteral.class;
+    }
+
+    /**
+     * Returns {@code true} if the literal's SQL type is supported for native conversion.
+     *
+     * <p>For null literals, the underlying type is still checked — a null of an unsupported
+     * type (e.g., TIMESTAMP) returns {@code false}.
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        SqlTypeName typeName = literal.getType().getSqlTypeName();
+        return isSupportedType(typeName);
+    }
+
+    /**
+     * Converts the given {@link RexLiteral} to a {@link PhysicalExprNode} with Arrow IPC bytes.
+     *
+     * @throws IllegalArgumentException if the literal type is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        byte[] ipcBytes = serializeToIpc(literal);
+        return PhysicalExprNode.newBuilder()
+                .setLiteral(ScalarValue.newBuilder().setIpcBytes(ByteString.copyFrom(ipcBytes)))
+                .build();
+    }
+
+    private static boolean isSupportedType(SqlTypeName typeName) {
+        return SUPPORTED_TYPES.contains(typeName);
+    }
+
+    /**
+     * Serializes the literal value as a single-element Arrow vector in IPC stream format.
+     */
+    private static byte[] serializeToIpc(RexLiteral literal) {
+        Field field = arrowFieldForType(literal);

Review Comment:
   Thanks for the PoC — adopted the Flink RowData approach in 4e50bfd4. Removed the manual `arrowFieldForType()` and `setVectorValue()` switch statements and replaced with `GenericRowData` + `FlinkArrowWriter`.
   
   A few adaptations from the PoC:
   - Used Auron's own `FlinkArrowUtils`/`FlinkArrowWriter` (from `auron-flink-runtime`) instead of Flink-internal `ArrowUtils`
   - Added explicit per-type dispatch in `toFlinkInternalValue()` since `getValueAs(Comparable.class)` returns `BigDecimal` for integer types in Calcite, which breaks the typed Arrow writers. DECIMAL uses `DecimalData.fromBigDecimal()` and CHAR/VARCHAR uses `StringData.fromString()`.
   - Kept try-with-resources for resource management (child allocator + VectorSchemaRoot)
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #2167:
URL: https://github.com/apache/auron/pull/2167#issuecomment-4261547009

   > thanks for your contributions! I left a few comments
   
   @Tartarus0zm Thanks for the review! I’ve addressed your comments - could you take another look? 


-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038141169


##########
docs/PR-AURON-1859/AURON-1859-DESIGN.md:
##########
@@ -0,0 +1,514 @@
+# Design — AURON-1859: Convert Math Operators to Auron Native Operators
+
+**Rev 1** — 2026-04-01
+**Issue**: https://github.com/apache/auron/issues/1859
+**Prerequisite**: #1856 (Flink Node Converter Tools) — PR #2146 merged
+
+---
+
+## 1. Problem Statement
+
+The Flink integration track needs concrete expression converters that translate Flink/Calcite `RexNode` expressions into Auron native `PhysicalExprNode` protobuf representations. Issue #1859 targets math operators (`+`, `-`, `*`, `/`, `%`) as the first converters, unlocking #1857 (FlinkAuronCalcOperator) and #1853 (StreamExecCalc rewrite).
+
+The converter framework from #1856 provides the dispatch infrastructure (`FlinkNodeConverterFactory`, `FlinkRexNodeConverter`, `ConverterContext`) but has zero concrete converter implementations. This PR delivers the first three.
+
+---
+
+## 2. Approach Candidates
+
+### Candidate A: Single RexCallConverter with SqlKind Switch
+
+One `RexCallConverter` registered for `RexCall.class`, dispatching by `SqlKind` internally.
+
+**Pros**: Matches the factory's class-based dispatch design. Simple, one registration call.
+**Cons**: Class will grow as more operators are added in future PRs (#1860, #1861, #1864).
+
+### Candidate B: Per-Operator Converter Classes (Gluten-style)
+
+Separate converter class for each operator (e.g., `PlusConverter`, `MinusConverter`).
+
+**Pros**: Small focused classes.
+**Cons**: Cannot work with our factory — it keys by `RexNode` subclass (`RexCall.class`), so only ONE converter can be registered for `RexCall`. Would require redesigning the factory.
+
+**Evidence**: The factory throws `IllegalArgumentException` on duplicate registration:
+```java
+// FlinkNodeConverterFactory.java:77-79
+if (rexConverterMap.containsKey(nodeClass)) {
+    throw new IllegalArgumentException("Duplicate RexNode converter for " + nodeClass.getName());
+}
+```
+
+### Candidate C: Sub-dispatch Registry Inside RexCallConverter (Gluten hybrid)
+
+One `RexCallConverter` for the factory, but internally uses a `Map<SqlKind, BiFunction>` to delegate to handler methods. Future PRs add entries to this map.
+
+**Pros**: Extensible without modifying the switch statement. Separates concerns.
+**Cons**: More infrastructure than needed for 7 operators. Over-engineered for Phase 1.
+
+### Decision: **Candidate A** — Single RexCallConverter with SqlKind Switch
+
+**Rationale**:
+- Matches Spark Auron's pattern (single `convertExprWithFallback()` with pattern match — `NativeConverters.scala:395-1183`)
+- The factory's class-based dispatch forces one converter per RexNode subclass anyway
+- Simple switch statement is readable and maintainable up to ~20 cases
+- Future PRs (#1860, #1861, #1864) just add new `case` clauses — additive, no structural changes
+- If it grows beyond ~30 cases, we can refactor to Candidate C later
+
+---
+
+## 3. Detailed Design
+
+### 3.1 Converter Classes
+
+Three new Java classes in `org.apache.auron.flink.table.planner.converter`:
+
+#### 3.1.1 RexInputRefConverter
+
+Converts column references to native `PhysicalColumn` protobuf.
+
+**Prior art**:
+- **Gluten-Flink**: `RexNodeConverter.toTypedExpr()` handles `RexInputRef` by resolving the column name from `context.getInputAttributeNames()` using the index, then creates `FieldAccessTypedExpr(type, name)` — Source: `gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java`
+- **Spark Auron**: Column references are converted elsewhere (in plan-level conversion, not expression-level) — Source: `NativeConverters.scala:1319-1325`
+
+**Design**:
+```java
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexInputRef ref = (RexInputRef) node;
+        return ref.getIndex() < context.getInputType().getFieldCount();
+    }
+
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexInputRef ref = (RexInputRef) node;
+        int index = ref.getIndex();
+        String name = context.getInputType().getFieldNames().get(index);
+        return PhysicalExprNode.newBuilder()
+                .setColumn(PhysicalColumn.newBuilder()
+                        .setName(name)
+                        .setIndex(index)
+                        .build())
+                .build();
+    }
+}
+```
+
+**Target protobuf**: `PhysicalColumn` (name + index) — Source: `auron.proto:512-515`
+
+#### 3.1.2 RexLiteralConverter
+
+Converts scalar literal values to native `ScalarValue` protobuf using Arrow IPC serialization.
+
+**Prior art**:
+- **Spark Auron**: Serializes literals as Arrow IPC bytes via `ArrowStreamWriter` → `ScalarValue.ipc_bytes` — Source: `NativeConverters.scala:409-430`
+- **Gluten-Flink**: Uses individually typed `Variant` values (`IntegerValue`, `BigIntValue`, etc.) via `toVariant()` — Source: `gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java`
+- **Native engine (Rust)**: Deserializes `ScalarValue.ipc_bytes` via Arrow IPC `StreamReader` → `ScalarValue::try_from_array` — Source: `auron-planner/src/lib.rs:446-455`
+
+**Decision: Arrow IPC bytes (Spark pattern)**
+
+Auron's `ScalarValue` protobuf has only `ipc_bytes` (no typed fields) — Source: `auron.proto:873-875`. We must use Arrow IPC serialization. Gluten's approach (typed Variant objects) targets Velox, not DataFusion.
+
+**Design**:
+```java
+public class RexLiteralConverter implements FlinkRexNodeConverter {
+
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexLiteral.class;
+    }
+
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        if (literal.isNull()) {
+            return true;  // null literals always supported
+        }
+        switch (literal.getType().getSqlTypeName()) {
+            case TINYINT: case SMALLINT: case INTEGER: case BIGINT:
+            case FLOAT: case DOUBLE: case DECIMAL:
+            case BOOLEAN: case CHAR: case VARCHAR:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        byte[] ipcBytes = serializeToArrowIpc(literal);
+        return PhysicalExprNode.newBuilder()
+                .setLiteral(ScalarValue.newBuilder()
+                        .setIpcBytes(ByteString.copyFrom(ipcBytes))
+                        .build())
+                .build();
+    }
+
+    // Creates a single-row Arrow IPC stream from the literal value
+    private byte[] serializeToArrowIpc(RexLiteral literal) {
+        // 1. Map SqlTypeName to Arrow Field
+        // 2. Create VectorSchemaRoot with one column
+        // 3. Set value into the appropriate vector (or leave null)
+        // 4. Serialize via ArrowStreamWriter to ByteArrayOutputStream
+        // 5. Return bytes
+    }
+}
+```
+
+**Type mapping for value extraction**:
+
+| SqlTypeName | Arrow Vector | RexLiteral extraction | Evidence |
+|---|---|---|---|
+| `TINYINT` | `TinyIntVector` | `getValueAs(Byte.class)` | Gluten: `Integer.valueOf(literal.getValue().toString())` |
+| `SMALLINT` | `SmallIntVector` | `getValueAs(Short.class)` | Gluten: same pattern |
+| `INTEGER` | `IntVector` | `getValueAs(Integer.class)` | Gluten: same pattern |
+| `BIGINT` | `BigIntVector` | `getValueAs(Long.class)` | Gluten: `Long.valueOf(literal.getValue().toString())` |
+| `FLOAT` | `Float4Vector` | `getValueAs(Float.class)` | (Gluten lacks FLOAT — gap in their impl) |
+| `DOUBLE` | `Float8Vector` | `getValueAs(Double.class)` | Gluten: `Double.valueOf(literal.getValue().toString())` |
+| `DECIMAL` | `DecimalVector` | `getValueAs(BigDecimal.class)` | Gluten: `literal.getValueAs(BigDecimal.class)` |
+| `BOOLEAN` | `BitVector` | `getValueAs(Boolean.class)` | Gluten: `(boolean) literal.getValue()` |
+| `CHAR`/`VARCHAR` | `VarCharVector` | `getValueAs(String.class)` | Gluten: `literal.getValueAs(String.class)` |
+| `NULL` | (any nullable) | N/A — leave vector null | (Gluten lacks null handling — gap) |
+
+**Arrow type construction**: Reuse `SchemaConverters.convertToAuronArrowType()` for proto ArrowType, but for Arrow Java `Field`, use the standard `ArrowType` mapping (e.g., `new ArrowType.Int(32, true)` for INT32). A helper method `sqlTypeNameToArrowField()` will map `SqlTypeName` → Arrow Java `Field`.
+
+**Null handling**: For null literals, create the vector but don't call `setSafe()`. The validity buffer defaults to null. This is the same pattern as Spark — Source: `NativeConverters.scala:412` (`e.eval(null)` returns null for null literals).
+
+#### 3.1.3 RexCallConverter
+
+Converts function/operator calls to native expressions. Dispatches by `SqlKind`.
+
+**Prior art**:
+- **Gluten-Flink**: `RexCallConverterFactory` dispatches by operator name string. `BasicArithmeticOperatorRexCallConverter` handles `+`, `-`, `*`. Each converter calls `getParams()` which recursively converts operands via `RexNodeConverter.toTypedExpr()` — Source: `gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/BaseRexCallConverters.java`
+- **Spark Auron**: Pattern match per expression class, calls `buildBinaryExprNode(lhs, rhs, opString)` which recursively calls `convertExprWithFallback()` — Source: `NativeConverters.scala:576-774`
+
+**Design**:
+```java
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES,
+            SqlKind.DIVIDE, SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,  // unary minus
+            SqlKind.PLUS_PREFIX,   // unary plus (identity)
+            SqlKind.CAST           // basic numeric cast
+    );
+
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        if (!SUPPORTED_KINDS.contains(call.getKind())) {
+            return false;
+        }
+        // For arithmetic ops, verify the result type is numeric.
+        // This prevents accepting TIMESTAMP + INTERVAL (also SqlKind.PLUS)
+        // or other non-numeric uses of arithmetic SqlKinds.
+        switch (call.getKind()) {
+            case PLUS: case MINUS: case TIMES: case DIVIDE: case MOD:
+            case MINUS_PREFIX:
+                return isNumericType(call.getType());
+            case CAST:
+                return isNumericType(call.getType());
+            case PLUS_PREFIX:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    private static boolean isNumericType(RelDataType type) {
+        switch (type.getSqlTypeName()) {
+            case TINYINT: case SMALLINT: case INTEGER: case BIGINT:
+            case FLOAT: case DOUBLE: case DECIMAL:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        switch (call.getKind()) {
+            case PLUS:         return buildBinaryExpr(call, "Plus", context);
+            case MINUS:        return buildBinaryExpr(call, "Minus", context);
+            case TIMES:        return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:       return buildDivide(call, context);
+            case MOD:          return buildModulo(call, context);
+            case MINUS_PREFIX: return buildNegative(call, context);
+            case PLUS_PREFIX:  return convertOperand(call.getOperands().get(0), context);
+            case CAST:         return buildCast(call, context);
+            default:
+                throw new UnsupportedOperationException(
+                        "Unsupported RexCall kind: " + call.getKind());
+        }
+    }
+}
+```
+
+**Recursive operand conversion**:
+
+Following Gluten-Flink's two-layer recursion pattern (converter calls back into factory for operands — Source: `RexNodeConverter.java` → `BaseRexCallConverters.getParams()` → `RexNodeConverter.toTypedExpr()`), the `RexCallConverter` converts operands via the factory.
+
+**Factory injection** (not singleton access): The `RexCallConverter` takes a `FlinkNodeConverterFactory` reference in its constructor rather than calling `getInstance()`. This aligns with #1856's testability design — the factory test (`FlinkNodeConverterFactoryTest.java:53`) uses a package-private constructor to create fresh instances for test isolation. Our tests do the same: create a fresh factory, register all 3 converters, test against that instance.
+
+```java
+public class RexCallConverter implements FlinkRexNodeConverter {
+    private final FlinkNodeConverterFactory factory;
+
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = Objects.requireNonNull(factory, "factory must not be null");
+    }
+
+    private PhysicalExprNode convertOperand(RexNode operand, ConverterContext context) {
+        return factory.convertRexNode(operand, context)
+                .orElseThrow(() -> new IllegalStateException(
+                        "Failed to convert operand: " + operand));
+    }
+```
+
+This handles nested expressions naturally: `(a + 1) * b` → the outer `TIMES` RexCall converts its left operand (a `PLUS` RexCall), which recursively converts `a` (RexInputRef) and `1` (RexLiteral).
+
+**Binary expression builder**:
+
+```java
+private PhysicalExprNode buildBinaryExpr(
+        RexCall call, String op, ConverterContext context) {
+    PhysicalExprNode left = convertOperand(call.getOperands().get(0), context);
+    PhysicalExprNode right = convertOperand(call.getOperands().get(1), context);
+    return PhysicalExprNode.newBuilder()
+            .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                    .setL(left)
+                    .setR(right)
+                    .setOp(op)
+                    .build())
+            .build();
+}
+```
+
+**Evidence**: Spark uses the same pattern — `buildBinaryExprNode(lhs, rhs, "Plus")` at `NativeConverters.scala:612`.
+
+### 3.2 Division by Zero Handling
+
+**Problem**: DataFusion errors on integer division by zero. Flink SQL expects NULL.
+
+**Prior art**:
+- **Spark Auron**: Wraps the divisor in `Spark_NullIfZero` extended scalar function — Source: `NativeConverters.scala:735,748,771`
+- **Gluten-Flink**: Does NOT add any div-by-zero protection. Relies on Velox returning NULL natively — Source: `gluten-flink/.../BaseRexCallConverters.java`
+- **Native engine naming convention** (`datafusion-ext-functions/src/lib.rs:43-44`):
+  ```rust
+  // auron ext functions, if used for spark should be start with 'Spark_',
+  // if used for flink should be start with 'Flink_',
+  // same to other engines.
+  ```
+
+**Critical finding**: The native engine's `create_auron_ext_function()` (`lib.rs:89`) has an exhaustive match that errors on unknown names:
+```rust
+_ => df_unimplemented_err!("spark ext function not implemented: {name}")?,
+```
+Calling `Flink_NullIfZero` would hit this default and **error at runtime**. Using `Spark_NullIfZero` works but violates the project's naming convention.
+
+**Decision**: **Defer div-by-zero wrapping to #1857.**
+
+**Rationale**:
+1. Adding `Flink_NullIfZero` requires a one-line Rust change (`lib.rs`), which expands this Java-only PR into a cross-language change
+2. This PR is about the **converter framework** (RexNode → protobuf translation). Division-by-zero is a **runtime behavior** concern
+3. The gap is documented and will be caught immediately in #1857 integration tests
+4. #1857 (FlinkAuronCalcOperator) is the natural place to add `Flink_NullIfZero` since it owns the runtime pipeline and can include the Rust registration alongside the Java wrapping
+
+DIVIDE and MOD will be converted as straightforward `PhysicalBinaryExprNode` in this PR — same as PLUS/MINUS/TIMES. The `wrapNullIfZero` logic will be added in #1857.
+
+**Design for DIVIDE (this PR)**:
+```java
+// Simple conversion — div-by-zero handling deferred to #1857
+private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+    PhysicalExprNode left = convertOperand(call.getOperands().get(0), context);
+    PhysicalExprNode right = convertOperand(call.getOperands().get(1), context);
+    return PhysicalExprNode.newBuilder()
+            .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                    .setL(left).setR(right).setOp(op).build())
+            .build();
+}
+```
+
+### 3.3 CAST Handling
+
+**Prior art**:
+- **Spark Auron**: CAST is in the same `convertExprWithFallback()` as arithmetic. Uses `PhysicalTryCastNode` (not `PhysicalCastNode`) as the default — Source: `NativeConverters.scala:473-503`
+- **Gluten-Flink**: CAST is registered in the same `RexCallConverterFactory` map as arithmetic via `Map.entry("CAST", Arrays.asList(() -> new DefaultRexCallConverter("cast")))` — Source: `RexCallConverterFactory.java`
+- **Flink/Calcite**: Optimizer inserts explicit CAST RexCall nodes for type promotion in arithmetic (via `StandardConvertletTable.convertOperands()` → `RexBuilder.ensureType()` → `makeCast()`) — Source: `apache/calcite StandardConvertletTable.java`
+
+**Decision**: Include basic numeric CAST in this PR. Use `PhysicalCastNode` (not `PhysicalTryCastNode`).
+
+**Rationale for `PhysicalCastNode` over `PhysicalTryCastNode`**:
+- Flink's CAST is strict by default (errors on invalid input)
+- Flink has `TRY_CAST` as a separate function for safe casting
+- Spark uses `TryCast` because Spark's CAST is lenient by default — different semantics
+- For numeric-to-numeric widening (INT→BIGINT), both behave identically anyway
+

Review Comment:
   Updated the design doc to reflect the current implementation in b1749a81.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "Tartarus0zm (via GitHub)" <gi...@apache.org>.
Tartarus0zm commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3098217118


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/TypeCastUtils.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Utility class for type promotion and casting operations used during
+ * {@link RexCallConverter} expression conversion.
+ *
+ * <p>Provides helpers to compute a common numeric type for binary arithmetic
+ * operands and to wrap native expressions in {@code TryCast} nodes.
+ */
+public final class TypeCastUtils {

Review Comment:
   Once the fix are complete, please delete the files used for review.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038039538


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexInputRefConverter.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.auron.flink.table.planner.converter;
+
+import org.apache.auron.protobuf.PhysicalColumn;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts a Calcite {@link RexInputRef} (column reference) to an Auron native {@link
+ * PhysicalExprNode} containing a {@link PhysicalColumn}.
+ *
+ * <p>Column references are always supported — every {@code RexInputRef} maps directly to a named,
+ * indexed column in the input schema provided by the {@link ConverterContext}.
+ */
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    /**
+     * Always returns {@code true} — column references are unconditionally supported.
+     *
+     * @param node the RexNode to check (must be a {@link RexInputRef})
+     * @param context shared conversion state
+     * @return {@code true}
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexInputRef} to a {@link PhysicalExprNode} with a {@link
+     * PhysicalColumn}.
+     *
+     * <p>Resolves the column name from the input schema via {@link
+     * ConverterContext#getInputType()}.
+     *
+     * @param node the RexNode to convert (must be a {@link RexInputRef})
+     * @param context shared conversion state containing the input schema
+     * @return a {@link PhysicalExprNode} wrapping a {@link PhysicalColumn} with name and index
+     * @throws IllegalArgumentException if the node is not a {@link RexInputRef}
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {

Review Comment:
   The Javadoc says `convert()` throws `IllegalArgumentException` when the node is not a `RexInputRef`, but the implementation performs an unchecked cast and would throw `ClassCastException` instead. Either align the Javadoc with the framework guarantee, or add an explicit type check and throw `IllegalArgumentException` to match the `FlinkNodeConverter` contract.
   ```suggestion
       public PhysicalExprNode convert(RexNode node, ConverterContext context) {
           if (!(node instanceof RexInputRef)) {
               throw new IllegalArgumentException(
                       "RexInputRefConverter can only convert RexInputRef nodes, but got: "
                               + (node == null ? "null" : node.getClass().getName()));
           }
   ```



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexInputRefConverter.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.auron.flink.table.planner.converter;
+
+import org.apache.auron.protobuf.PhysicalColumn;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts a Calcite {@link RexInputRef} (column reference) to an Auron native {@link
+ * PhysicalExprNode} containing a {@link PhysicalColumn}.
+ *
+ * <p>Column references are always supported — every {@code RexInputRef} maps directly to a named,
+ * indexed column in the input schema provided by the {@link ConverterContext}.
+ */
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    /**
+     * Always returns {@code true} — column references are unconditionally supported.
+     *
+     * @param node the RexNode to check (must be a {@link RexInputRef})
+     * @param context shared conversion state
+     * @return {@code true}
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        return true;
+    }

Review Comment:
   `isSupported()` always returns true, but `convert()` blindly indexes into `context.getInputType().getFieldNames()` using `inputRef.getIndex()`. If a `RexInputRef` index is out of range, this will throw `IndexOutOfBoundsException` and bypass the factory's "unsupported" path. Consider validating the index against `context.getInputType().getFieldCount()` in `isSupported()` (and/or in `convert()` with a clear exception).



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if ((t1 == SqlTypeName.BIGINT || t2 == SqlTypeName.BIGINT) && notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }
+        if (SqlTypeUtil.inCharFamily(type1) && SqlTypeUtil.inCharFamily(type2)) {
+            return typeFactory.createSqlType(SqlTypeName.VARCHAR);
+        }
+        return null;
+    }
+
+    private static boolean notApproxType(SqlTypeName typeName) {
+        return typeName != SqlTypeName.FLOAT && typeName != SqlTypeName.DOUBLE;
+    }
+
+    /**
+     * Wraps the converted operand in a TryCast if its type differs from the
+     * target type.
+     */
+    private PhysicalExprNode castIfNecessary(RexNode expr, RelDataType targetType, ConverterContext context) {
+        PhysicalExprNode converted = convertOperand(expr, context);
+        if (expr.getType().getSqlTypeName().equals(targetType.getSqlTypeName())) {
+            return converted;
+        }
+        return wrapInTryCast(converted, targetType);
+    }
+

Review Comment:
   `castIfNecessary()` decides whether to wrap a TryCast by comparing only `SqlTypeName`. This will treat all DECIMALs as the same type even when precision/scale differ, and can also miss other type attributes (nullability, charset/collation). For arithmetic, this can yield a binary expression whose operands have incompatible Arrow types at runtime. Consider comparing full `RelDataType` equality (or at least DECIMAL precision/scale) and casting when they differ.
   ```suggestion
           if (hasSameType(expr.getType(), targetType)) {
               return converted;
           }
           return wrapInTryCast(converted, targetType);
       }
   
       private static boolean hasSameType(RelDataType sourceType, RelDataType targetType) {
           if (sourceType == targetType) {
               return true;
           }
           if (sourceType == null || targetType == null) {
               return false;
           }
           return sourceType.getFullTypeString().equals(targetType.getFullTypeString());
       }
   ```



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if ((t1 == SqlTypeName.BIGINT || t2 == SqlTypeName.BIGINT) && notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }

Review Comment:
   For exact integer mixes like TINYINT/SMALLINT/INTEGER (where neither side is BIGINT/DECIMAL and neither is FLOAT/DOUBLE), `getCommonTypeForComparison()` currently falls back to DOUBLE. This changes arithmetic semantics (compute in floating point, then potentially TryCast back) and is unlikely to match Flink/Calcite's least-restrictive integer promotion rules. Consider promoting exact integer combinations to the widest exact integer type (e.g., INTEGER or BIGINT) rather than DOUBLE.



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }

Review Comment:
   `getCommonTypeForComparison()` returns `typeFactory.createSqlType(DECIMAL)` when either operand is DECIMAL, which drops the original precision/scale. That can lead to casts (and Arrow types) that don't match the operands' declared decimal types, potentially changing rounding/overflow behavior. Consider constructing a DECIMAL common type that preserves or derives precision/scale (e.g., max scale and sufficient precision) instead of using the default DECIMAL type.



##########
docs/PR-AURON-1859/AURON-1859-DESIGN.md:
##########
@@ -0,0 +1,514 @@
+# Design — AURON-1859: Convert Math Operators to Auron Native Operators
+
+**Rev 1** — 2026-04-01
+**Issue**: https://github.com/apache/auron/issues/1859
+**Prerequisite**: #1856 (Flink Node Converter Tools) — PR #2146 merged
+
+---
+
+## 1. Problem Statement
+
+The Flink integration track needs concrete expression converters that translate Flink/Calcite `RexNode` expressions into Auron native `PhysicalExprNode` protobuf representations. Issue #1859 targets math operators (`+`, `-`, `*`, `/`, `%`) as the first converters, unlocking #1857 (FlinkAuronCalcOperator) and #1853 (StreamExecCalc rewrite).
+
+The converter framework from #1856 provides the dispatch infrastructure (`FlinkNodeConverterFactory`, `FlinkRexNodeConverter`, `ConverterContext`) but has zero concrete converter implementations. This PR delivers the first three.
+
+---
+
+## 2. Approach Candidates
+
+### Candidate A: Single RexCallConverter with SqlKind Switch
+
+One `RexCallConverter` registered for `RexCall.class`, dispatching by `SqlKind` internally.
+
+**Pros**: Matches the factory's class-based dispatch design. Simple, one registration call.
+**Cons**: Class will grow as more operators are added in future PRs (#1860, #1861, #1864).
+
+### Candidate B: Per-Operator Converter Classes (Gluten-style)
+
+Separate converter class for each operator (e.g., `PlusConverter`, `MinusConverter`).
+
+**Pros**: Small focused classes.
+**Cons**: Cannot work with our factory — it keys by `RexNode` subclass (`RexCall.class`), so only ONE converter can be registered for `RexCall`. Would require redesigning the factory.
+
+**Evidence**: The factory throws `IllegalArgumentException` on duplicate registration:
+```java
+// FlinkNodeConverterFactory.java:77-79
+if (rexConverterMap.containsKey(nodeClass)) {
+    throw new IllegalArgumentException("Duplicate RexNode converter for " + nodeClass.getName());
+}
+```
+
+### Candidate C: Sub-dispatch Registry Inside RexCallConverter (Gluten hybrid)
+
+One `RexCallConverter` for the factory, but internally uses a `Map<SqlKind, BiFunction>` to delegate to handler methods. Future PRs add entries to this map.
+
+**Pros**: Extensible without modifying the switch statement. Separates concerns.
+**Cons**: More infrastructure than needed for 7 operators. Over-engineered for Phase 1.
+
+### Decision: **Candidate A** — Single RexCallConverter with SqlKind Switch
+
+**Rationale**:
+- Matches Spark Auron's pattern (single `convertExprWithFallback()` with pattern match — `NativeConverters.scala:395-1183`)
+- The factory's class-based dispatch forces one converter per RexNode subclass anyway
+- Simple switch statement is readable and maintainable up to ~20 cases
+- Future PRs (#1860, #1861, #1864) just add new `case` clauses — additive, no structural changes
+- If it grows beyond ~30 cases, we can refactor to Candidate C later
+
+---
+
+## 3. Detailed Design
+
+### 3.1 Converter Classes
+
+Three new Java classes in `org.apache.auron.flink.table.planner.converter`:
+
+#### 3.1.1 RexInputRefConverter
+
+Converts column references to native `PhysicalColumn` protobuf.
+
+**Prior art**:
+- **Gluten-Flink**: `RexNodeConverter.toTypedExpr()` handles `RexInputRef` by resolving the column name from `context.getInputAttributeNames()` using the index, then creates `FieldAccessTypedExpr(type, name)` — Source: `gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java`
+- **Spark Auron**: Column references are converted elsewhere (in plan-level conversion, not expression-level) — Source: `NativeConverters.scala:1319-1325`
+
+**Design**:
+```java
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexInputRef ref = (RexInputRef) node;
+        return ref.getIndex() < context.getInputType().getFieldCount();
+    }
+
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexInputRef ref = (RexInputRef) node;
+        int index = ref.getIndex();
+        String name = context.getInputType().getFieldNames().get(index);
+        return PhysicalExprNode.newBuilder()
+                .setColumn(PhysicalColumn.newBuilder()
+                        .setName(name)
+                        .setIndex(index)
+                        .build())
+                .build();
+    }
+}
+```
+
+**Target protobuf**: `PhysicalColumn` (name + index) — Source: `auron.proto:512-515`
+
+#### 3.1.2 RexLiteralConverter
+
+Converts scalar literal values to native `ScalarValue` protobuf using Arrow IPC serialization.
+
+**Prior art**:
+- **Spark Auron**: Serializes literals as Arrow IPC bytes via `ArrowStreamWriter` → `ScalarValue.ipc_bytes` — Source: `NativeConverters.scala:409-430`
+- **Gluten-Flink**: Uses individually typed `Variant` values (`IntegerValue`, `BigIntValue`, etc.) via `toVariant()` — Source: `gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java`
+- **Native engine (Rust)**: Deserializes `ScalarValue.ipc_bytes` via Arrow IPC `StreamReader` → `ScalarValue::try_from_array` — Source: `auron-planner/src/lib.rs:446-455`
+
+**Decision: Arrow IPC bytes (Spark pattern)**
+
+Auron's `ScalarValue` protobuf has only `ipc_bytes` (no typed fields) — Source: `auron.proto:873-875`. We must use Arrow IPC serialization. Gluten's approach (typed Variant objects) targets Velox, not DataFusion.
+
+**Design**:
+```java
+public class RexLiteralConverter implements FlinkRexNodeConverter {
+
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexLiteral.class;
+    }
+
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        if (literal.isNull()) {
+            return true;  // null literals always supported
+        }
+        switch (literal.getType().getSqlTypeName()) {
+            case TINYINT: case SMALLINT: case INTEGER: case BIGINT:
+            case FLOAT: case DOUBLE: case DECIMAL:
+            case BOOLEAN: case CHAR: case VARCHAR:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        byte[] ipcBytes = serializeToArrowIpc(literal);
+        return PhysicalExprNode.newBuilder()
+                .setLiteral(ScalarValue.newBuilder()
+                        .setIpcBytes(ByteString.copyFrom(ipcBytes))
+                        .build())
+                .build();
+    }
+
+    // Creates a single-row Arrow IPC stream from the literal value
+    private byte[] serializeToArrowIpc(RexLiteral literal) {
+        // 1. Map SqlTypeName to Arrow Field
+        // 2. Create VectorSchemaRoot with one column
+        // 3. Set value into the appropriate vector (or leave null)
+        // 4. Serialize via ArrowStreamWriter to ByteArrayOutputStream
+        // 5. Return bytes
+    }
+}
+```
+
+**Type mapping for value extraction**:
+
+| SqlTypeName | Arrow Vector | RexLiteral extraction | Evidence |
+|---|---|---|---|
+| `TINYINT` | `TinyIntVector` | `getValueAs(Byte.class)` | Gluten: `Integer.valueOf(literal.getValue().toString())` |
+| `SMALLINT` | `SmallIntVector` | `getValueAs(Short.class)` | Gluten: same pattern |
+| `INTEGER` | `IntVector` | `getValueAs(Integer.class)` | Gluten: same pattern |
+| `BIGINT` | `BigIntVector` | `getValueAs(Long.class)` | Gluten: `Long.valueOf(literal.getValue().toString())` |
+| `FLOAT` | `Float4Vector` | `getValueAs(Float.class)` | (Gluten lacks FLOAT — gap in their impl) |
+| `DOUBLE` | `Float8Vector` | `getValueAs(Double.class)` | Gluten: `Double.valueOf(literal.getValue().toString())` |
+| `DECIMAL` | `DecimalVector` | `getValueAs(BigDecimal.class)` | Gluten: `literal.getValueAs(BigDecimal.class)` |
+| `BOOLEAN` | `BitVector` | `getValueAs(Boolean.class)` | Gluten: `(boolean) literal.getValue()` |
+| `CHAR`/`VARCHAR` | `VarCharVector` | `getValueAs(String.class)` | Gluten: `literal.getValueAs(String.class)` |
+| `NULL` | (any nullable) | N/A — leave vector null | (Gluten lacks null handling — gap) |
+
+**Arrow type construction**: Reuse `SchemaConverters.convertToAuronArrowType()` for proto ArrowType, but for Arrow Java `Field`, use the standard `ArrowType` mapping (e.g., `new ArrowType.Int(32, true)` for INT32). A helper method `sqlTypeNameToArrowField()` will map `SqlTypeName` → Arrow Java `Field`.
+
+**Null handling**: For null literals, create the vector but don't call `setSafe()`. The validity buffer defaults to null. This is the same pattern as Spark — Source: `NativeConverters.scala:412` (`e.eval(null)` returns null for null literals).
+
+#### 3.1.3 RexCallConverter
+
+Converts function/operator calls to native expressions. Dispatches by `SqlKind`.
+
+**Prior art**:
+- **Gluten-Flink**: `RexCallConverterFactory` dispatches by operator name string. `BasicArithmeticOperatorRexCallConverter` handles `+`, `-`, `*`. Each converter calls `getParams()` which recursively converts operands via `RexNodeConverter.toTypedExpr()` — Source: `gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/BaseRexCallConverters.java`
+- **Spark Auron**: Pattern match per expression class, calls `buildBinaryExprNode(lhs, rhs, opString)` which recursively calls `convertExprWithFallback()` — Source: `NativeConverters.scala:576-774`
+
+**Design**:
+```java
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES,
+            SqlKind.DIVIDE, SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,  // unary minus
+            SqlKind.PLUS_PREFIX,   // unary plus (identity)
+            SqlKind.CAST           // basic numeric cast
+    );
+
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        if (!SUPPORTED_KINDS.contains(call.getKind())) {
+            return false;
+        }
+        // For arithmetic ops, verify the result type is numeric.
+        // This prevents accepting TIMESTAMP + INTERVAL (also SqlKind.PLUS)
+        // or other non-numeric uses of arithmetic SqlKinds.
+        switch (call.getKind()) {
+            case PLUS: case MINUS: case TIMES: case DIVIDE: case MOD:
+            case MINUS_PREFIX:
+                return isNumericType(call.getType());
+            case CAST:
+                return isNumericType(call.getType());
+            case PLUS_PREFIX:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    private static boolean isNumericType(RelDataType type) {
+        switch (type.getSqlTypeName()) {
+            case TINYINT: case SMALLINT: case INTEGER: case BIGINT:
+            case FLOAT: case DOUBLE: case DECIMAL:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        switch (call.getKind()) {
+            case PLUS:         return buildBinaryExpr(call, "Plus", context);
+            case MINUS:        return buildBinaryExpr(call, "Minus", context);
+            case TIMES:        return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:       return buildDivide(call, context);
+            case MOD:          return buildModulo(call, context);
+            case MINUS_PREFIX: return buildNegative(call, context);
+            case PLUS_PREFIX:  return convertOperand(call.getOperands().get(0), context);
+            case CAST:         return buildCast(call, context);
+            default:
+                throw new UnsupportedOperationException(
+                        "Unsupported RexCall kind: " + call.getKind());
+        }
+    }
+}
+```
+
+**Recursive operand conversion**:
+
+Following Gluten-Flink's two-layer recursion pattern (converter calls back into factory for operands — Source: `RexNodeConverter.java` → `BaseRexCallConverters.getParams()` → `RexNodeConverter.toTypedExpr()`), the `RexCallConverter` converts operands via the factory.
+
+**Factory injection** (not singleton access): The `RexCallConverter` takes a `FlinkNodeConverterFactory` reference in its constructor rather than calling `getInstance()`. This aligns with #1856's testability design — the factory test (`FlinkNodeConverterFactoryTest.java:53`) uses a package-private constructor to create fresh instances for test isolation. Our tests do the same: create a fresh factory, register all 3 converters, test against that instance.
+
+```java
+public class RexCallConverter implements FlinkRexNodeConverter {
+    private final FlinkNodeConverterFactory factory;
+
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = Objects.requireNonNull(factory, "factory must not be null");
+    }
+
+    private PhysicalExprNode convertOperand(RexNode operand, ConverterContext context) {
+        return factory.convertRexNode(operand, context)
+                .orElseThrow(() -> new IllegalStateException(
+                        "Failed to convert operand: " + operand));
+    }
+```
+
+This handles nested expressions naturally: `(a + 1) * b` → the outer `TIMES` RexCall converts its left operand (a `PLUS` RexCall), which recursively converts `a` (RexInputRef) and `1` (RexLiteral).
+
+**Binary expression builder**:
+
+```java
+private PhysicalExprNode buildBinaryExpr(
+        RexCall call, String op, ConverterContext context) {
+    PhysicalExprNode left = convertOperand(call.getOperands().get(0), context);
+    PhysicalExprNode right = convertOperand(call.getOperands().get(1), context);
+    return PhysicalExprNode.newBuilder()
+            .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                    .setL(left)
+                    .setR(right)
+                    .setOp(op)
+                    .build())
+            .build();
+}
+```
+
+**Evidence**: Spark uses the same pattern — `buildBinaryExprNode(lhs, rhs, "Plus")` at `NativeConverters.scala:612`.
+
+### 3.2 Division by Zero Handling
+
+**Problem**: DataFusion errors on integer division by zero. Flink SQL expects NULL.
+
+**Prior art**:
+- **Spark Auron**: Wraps the divisor in `Spark_NullIfZero` extended scalar function — Source: `NativeConverters.scala:735,748,771`
+- **Gluten-Flink**: Does NOT add any div-by-zero protection. Relies on Velox returning NULL natively — Source: `gluten-flink/.../BaseRexCallConverters.java`
+- **Native engine naming convention** (`datafusion-ext-functions/src/lib.rs:43-44`):
+  ```rust
+  // auron ext functions, if used for spark should be start with 'Spark_',
+  // if used for flink should be start with 'Flink_',
+  // same to other engines.
+  ```
+
+**Critical finding**: The native engine's `create_auron_ext_function()` (`lib.rs:89`) has an exhaustive match that errors on unknown names:
+```rust
+_ => df_unimplemented_err!("spark ext function not implemented: {name}")?,
+```
+Calling `Flink_NullIfZero` would hit this default and **error at runtime**. Using `Spark_NullIfZero` works but violates the project's naming convention.
+
+**Decision**: **Defer div-by-zero wrapping to #1857.**
+
+**Rationale**:
+1. Adding `Flink_NullIfZero` requires a one-line Rust change (`lib.rs`), which expands this Java-only PR into a cross-language change
+2. This PR is about the **converter framework** (RexNode → protobuf translation). Division-by-zero is a **runtime behavior** concern
+3. The gap is documented and will be caught immediately in #1857 integration tests
+4. #1857 (FlinkAuronCalcOperator) is the natural place to add `Flink_NullIfZero` since it owns the runtime pipeline and can include the Rust registration alongside the Java wrapping
+
+DIVIDE and MOD will be converted as straightforward `PhysicalBinaryExprNode` in this PR — same as PLUS/MINUS/TIMES. The `wrapNullIfZero` logic will be added in #1857.
+
+**Design for DIVIDE (this PR)**:
+```java
+// Simple conversion — div-by-zero handling deferred to #1857
+private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+    PhysicalExprNode left = convertOperand(call.getOperands().get(0), context);
+    PhysicalExprNode right = convertOperand(call.getOperands().get(1), context);
+    return PhysicalExprNode.newBuilder()
+            .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                    .setL(left).setR(right).setOp(op).build())
+            .build();
+}
+```
+
+### 3.3 CAST Handling
+
+**Prior art**:
+- **Spark Auron**: CAST is in the same `convertExprWithFallback()` as arithmetic. Uses `PhysicalTryCastNode` (not `PhysicalCastNode`) as the default — Source: `NativeConverters.scala:473-503`
+- **Gluten-Flink**: CAST is registered in the same `RexCallConverterFactory` map as arithmetic via `Map.entry("CAST", Arrays.asList(() -> new DefaultRexCallConverter("cast")))` — Source: `RexCallConverterFactory.java`
+- **Flink/Calcite**: Optimizer inserts explicit CAST RexCall nodes for type promotion in arithmetic (via `StandardConvertletTable.convertOperands()` → `RexBuilder.ensureType()` → `makeCast()`) — Source: `apache/calcite StandardConvertletTable.java`
+
+**Decision**: Include basic numeric CAST in this PR. Use `PhysicalCastNode` (not `PhysicalTryCastNode`).
+
+**Rationale for `PhysicalCastNode` over `PhysicalTryCastNode`**:
+- Flink's CAST is strict by default (errors on invalid input)
+- Flink has `TRY_CAST` as a separate function for safe casting
+- Spark uses `TryCast` because Spark's CAST is lenient by default — different semantics
+- For numeric-to-numeric widening (INT→BIGINT), both behave identically anyway
+

Review Comment:
   The design doc states CAST should be implemented with `PhysicalCastNode` (and that type coercion can rely on upstream Calcite CASTs), but the actual implementation uses `PhysicalTryCastNode` and performs additional type promotion in `RexCallConverter`. Please update the design section to reflect the current implementation choices so future work (#1857+) doesn’t rely on outdated semantics.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038135057


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if ((t1 == SqlTypeName.BIGINT || t2 == SqlTypeName.BIGINT) && notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }
+        if (SqlTypeUtil.inCharFamily(type1) && SqlTypeUtil.inCharFamily(type2)) {
+            return typeFactory.createSqlType(SqlTypeName.VARCHAR);
+        }
+        return null;
+    }
+
+    private static boolean notApproxType(SqlTypeName typeName) {
+        return typeName != SqlTypeName.FLOAT && typeName != SqlTypeName.DOUBLE;
+    }
+
+    /**
+     * Wraps the converted operand in a TryCast if its type differs from the
+     * target type.
+     */
+    private PhysicalExprNode castIfNecessary(RexNode expr, RelDataType targetType, ConverterContext context) {
+        PhysicalExprNode converted = convertOperand(expr, context);
+        if (expr.getType().getSqlTypeName().equals(targetType.getSqlTypeName())) {
+            return converted;
+        }
+        return wrapInTryCast(converted, targetType);
+    }
+

Review Comment:
   The `SqlTypeName`-only comparison is intentional — `castIfNecessary()` handles **cross-family type promotion** (e.g., INT→BIGINT, INT→DECIMAL), not intra-family precision alignment.
   
   For the DECIMAL case: when both operands are DECIMAL with different precision/scale, `castIfNecessary` correctly skips the cast (both are `SqlTypeName.DECIMAL`), and DataFusion's binary arithmetic kernels handle precision alignment natively at the Arrow level.
   
   Using full `RelDataType` equality (e.g., `getFullTypeString()`) would trigger unnecessary `TryCast` wrapping for cases like `DECIMAL(10,2)` vs `DECIMAL(10,3)`, and would also fire on nullability differences (`INTEGER NOT NULL` vs `INTEGER`), which is undesirable for arithmetic operand promotion.
   
   This pattern follows the reviewer's PoC code (see [issue comment](https://github.com/apache/auron/issues/1859#issuecomment-4181565860)), which uses the same `SqlTypeName`-level comparison.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "Tartarus0zm (via GitHub)" <gi...@apache.org>.
Tartarus0zm commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3098190892


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/TypeCastUtils.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Utility class for type promotion and casting operations used during
+ * {@link RexCallConverter} expression conversion.
+ *
+ * <p>Provides helpers to compute a common numeric type for binary arithmetic
+ * operands and to wrap native expressions in {@code TryCast} nodes.
+ */
+public final class TypeCastUtils {

Review Comment:
   How about rename to `FlinkNodeConverterUtils`? 
   APIs unrelated to `cast` can also be included.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038128213


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexInputRefConverter.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.auron.flink.table.planner.converter;
+
+import org.apache.auron.protobuf.PhysicalColumn;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts a Calcite {@link RexInputRef} (column reference) to an Auron native {@link
+ * PhysicalExprNode} containing a {@link PhysicalColumn}.
+ *
+ * <p>Column references are always supported — every {@code RexInputRef} maps directly to a named,
+ * indexed column in the input schema provided by the {@link ConverterContext}.
+ */
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    /**
+     * Always returns {@code true} — column references are unconditionally supported.
+     *
+     * @param node the RexNode to check (must be a {@link RexInputRef})
+     * @param context shared conversion state
+     * @return {@code true}
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        return true;
+    }

Review Comment:
   Fixed in b1749a81: `isSupported()` now validates `inputRef.getIndex() < context.getInputType().getFieldCount()`. Added a test for the out-of-range case.
   
   Note: the factory's catch-all in `convertRexNode()` (line 116-119) would have prevented a crash, but having the check in `isSupported()` makes the intent explicit and avoids relying on exception-based flow control.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038128213


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexInputRefConverter.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.auron.flink.table.planner.converter;
+
+import org.apache.auron.protobuf.PhysicalColumn;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts a Calcite {@link RexInputRef} (column reference) to an Auron native {@link
+ * PhysicalExprNode} containing a {@link PhysicalColumn}.
+ *
+ * <p>Column references are always supported — every {@code RexInputRef} maps directly to a named,
+ * indexed column in the input schema provided by the {@link ConverterContext}.
+ */
+public class RexInputRefConverter implements FlinkRexNodeConverter {
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexInputRef.class;
+    }
+
+    /**
+     * Always returns {@code true} — column references are unconditionally supported.
+     *
+     * @param node the RexNode to check (must be a {@link RexInputRef})
+     * @param context shared conversion state
+     * @return {@code true}
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        return true;
+    }

Review Comment:
   RESOLVED in b1749a81: `isSupported()` now validates `inputRef.getIndex() < context.getInputType().getFieldCount()`. Added a test for the out-of-range case.
   
   Note: the factory's catch-all in `convertRexNode()` (line 116-119) would have prevented a crash, but having the check in `isSupported()` makes the intent explicit and avoids relying on exception-based flow control.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "Tartarus0zm (via GitHub)" <gi...@apache.org>.
Tartarus0zm commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3062991736


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(

Review Comment:
   I suggest placing this API in a utility class, since it will also be used in logical expressions.



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexLiteralConverter.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import com.google.protobuf.ByteString;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BigIntVector;
+import org.apache.arrow.vector.BitVector;
+import org.apache.arrow.vector.DecimalVector;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.Float4Vector;
+import org.apache.arrow.vector.Float8Vector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.SmallIntVector;
+import org.apache.arrow.vector.TinyIntVector;
+import org.apache.arrow.vector.VarCharVector;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.ipc.ArrowStreamWriter;
+import org.apache.arrow.vector.types.FloatingPointPrecision;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.ScalarValue;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+/**
+ * Converts a Calcite {@link RexLiteral} to an Auron native {@link PhysicalExprNode}
+ * containing a {@link ScalarValue} with Arrow IPC bytes.
+ *
+ * <p>The literal value is serialized as a single-element Arrow vector in IPC stream format,
+ * following the same pattern as the Spark implementation in {@code NativeConverters}.
+ *
+ * <p>Supported types: {@code TINYINT}, {@code SMALLINT}, {@code INTEGER}, {@code BIGINT},
+ * {@code FLOAT}, {@code DOUBLE}, {@code DECIMAL}, {@code BOOLEAN}, {@code CHAR},
+ * {@code VARCHAR}, and {@code NULL} (of a supported type).
+ */
+public class RexLiteralConverter implements FlinkRexNodeConverter {
+
+    private static final Set<SqlTypeName> SUPPORTED_TYPES = EnumSet.of(
+            SqlTypeName.TINYINT,
+            SqlTypeName.SMALLINT,
+            SqlTypeName.INTEGER,
+            SqlTypeName.BIGINT,
+            SqlTypeName.FLOAT,
+            SqlTypeName.DOUBLE,
+            SqlTypeName.DECIMAL,
+            SqlTypeName.BOOLEAN,
+            SqlTypeName.CHAR,
+            SqlTypeName.VARCHAR);
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexLiteral.class;
+    }
+
+    /**
+     * Returns {@code true} if the literal's SQL type is supported for native conversion.
+     *
+     * <p>For null literals, the underlying type is still checked — a null of an unsupported
+     * type (e.g., TIMESTAMP) returns {@code false}.
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        SqlTypeName typeName = literal.getType().getSqlTypeName();
+        return isSupportedType(typeName);
+    }
+
+    /**
+     * Converts the given {@link RexLiteral} to a {@link PhysicalExprNode} with Arrow IPC bytes.
+     *
+     * @throws IllegalArgumentException if the literal type is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexLiteral literal = (RexLiteral) node;
+        byte[] ipcBytes = serializeToIpc(literal);
+        return PhysicalExprNode.newBuilder()
+                .setLiteral(ScalarValue.newBuilder().setIpcBytes(ByteString.copyFrom(ipcBytes)))
+                .build();
+    }
+
+    private static boolean isSupportedType(SqlTypeName typeName) {
+        return SUPPORTED_TYPES.contains(typeName);
+    }
+
+    /**
+     * Serializes the literal value as a single-element Arrow vector in IPC stream format.
+     */
+    private static byte[] serializeToIpc(RexLiteral literal) {
+        Field field = arrowFieldForType(literal);

Review Comment:
   In my previous POC, I used Flink RowData to store the data.
   ```java
               RexLiteral literal = (RexLiteral) rexNode;
               LogicalType logicalType = FlinkTypeFactory.toLogicalType(literal.getType());
               GenericRowData rowData = new GenericRowData(1);
               rowData.setField(0, RexLiteralUtil.toFlinkInternalValue(literal.getValueAs(Comparable.class), logicalType));
               BufferAllocator allocator =
                       ArrowUtils.getRootAllocator().newChildAllocator("SerializationRexLiteral", 0, Long.MAX_VALUE);
               VectorSchemaRoot root =
                       VectorSchemaRoot.create(ArrowUtils.toArrowSchema(RowType.of(logicalType)), allocator);
               ArrowWriter<RowData> arrowWriter = ArrowUtils.createRowDataArrowWriter(root, RowType.of(logicalType));
               arrowWriter.write(rowData);
               arrowWriter.finish();
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               CDataDictionaryProvider dictionaryProvider = new CDataDictionaryProvider();
               ArrowStreamWriter arrowStreamWriter = new ArrowStreamWriter(root, dictionaryProvider, baos);
               arrowStreamWriter.start();
               arrowStreamWriter.writeBatch();
               arrowStreamWriter.end();
               root.close();
               arrowStreamWriter.close();
               return PhysicalExprNode.newBuilder()
                       .setLiteral(ScalarValue.newBuilder().setIpcBytes(ByteString.copyFrom(baos.toByteArray())))
                       .build();
   ```



##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if (notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }
+        if (SqlTypeUtil.inCharFamily(type1) && SqlTypeUtil.inCharFamily(type2)) {
+            return typeFactory.createSqlType(SqlTypeName.VARCHAR);
+        }
+        return null;
+    }
+
+    private static boolean notApproxType(SqlTypeName typeName) {
+        return typeName != SqlTypeName.FLOAT && typeName != SqlTypeName.DOUBLE;
+    }
+
+    /**
+     * Wraps the converted operand in a TryCast if its type differs from the
+     * target type.
+     */
+    private PhysicalExprNode castIfNecessary(RexNode expr, RelDataType targetType, ConverterContext context) {

Review Comment:
   ditto



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3038138707


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if ((t1 == SqlTypeName.BIGINT || t2 == SqlTypeName.BIGINT) && notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }

Review Comment:
   Resolved. The observation is technically correct - mixed exact integer types (e.g., TINYINT + SMALLINT) would fall through to DOUBLE. In practice, this path is unreachable because Calcite/Flink's type coercion inserts explicit CAST nodes to align operands before the arithmetic `RexCall` reaches our converter.
   
   That said, since the fix is trivial and makes the safety net semantically correct, I've tightened the condition in b1749a81: any two exact integer types now promote to BIGINT instead of falling through to DOUBLE. The change only affects the previously-unreachable mixed-exact-integer path — all other promotion behavior is unchanged.
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "Tartarus0zm (via GitHub)" <gi...@apache.org>.
Tartarus0zm merged PR #2167:
URL: https://github.com/apache/auron/pull/2167


-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3094589848


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/RexCallConverter.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.EnumSet;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalBinaryExprNode;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalNegativeNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Converts a Calcite {@link RexCall} (operator expression) to an Auron native
+ * {@link PhysicalExprNode}.
+ *
+ * <p>Handles arithmetic operators ({@code +}, {@code -}, {@code *}, {@code /},
+ * {@code %}), unary minus/plus, and {@code CAST}. Binary arithmetic operands
+ * are promoted to a common type before conversion, and the result is cast to
+ * the output type if it differs from the common type.
+ */
+public class RexCallConverter implements FlinkRexNodeConverter {
+
+    private static final RelDataTypeFactory TYPE_FACTORY = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+    /** Binary arithmetic kinds that require numeric result type. */
+    private static final Set<SqlKind> BINARY_ARITHMETIC_KINDS =
+            EnumSet.of(SqlKind.PLUS, SqlKind.MINUS, SqlKind.TIMES, SqlKind.DIVIDE, SqlKind.MOD);
+
+    /** All supported SqlKinds including unary and cast. */
+    private static final Set<SqlKind> SUPPORTED_KINDS = EnumSet.of(
+            SqlKind.PLUS,
+            SqlKind.MINUS,
+            SqlKind.TIMES,
+            SqlKind.DIVIDE,
+            SqlKind.MOD,
+            SqlKind.MINUS_PREFIX,
+            SqlKind.PLUS_PREFIX,
+            SqlKind.CAST);
+
+    private final FlinkNodeConverterFactory factory;
+
+    /**
+     * Creates a new converter that delegates operand conversion to the given
+     * factory.
+     *
+     * @param factory the factory used for recursive operand conversion
+     */
+    public RexCallConverter(FlinkNodeConverterFactory factory) {
+        this.factory = factory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Class<? extends RexNode> getNodeClass() {
+        return RexCall.class;
+    }
+
+    /**
+     * Returns {@code true} if the call's {@link SqlKind} is supported.
+     *
+     * <p>For binary arithmetic kinds, the call's result type must also be
+     * numeric to reject non-arithmetic uses (e.g., TIMESTAMP + INTERVAL).
+     */
+    @Override
+    public boolean isSupported(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        if (!SUPPORTED_KINDS.contains(kind)) {
+            return false;
+        }
+        if (BINARY_ARITHMETIC_KINDS.contains(kind)) {
+            return SqlTypeUtil.isNumeric(call.getType());
+        }
+        return true;
+    }
+
+    /**
+     * Converts the given {@link RexCall} to a native {@link PhysicalExprNode}.
+     *
+     * <p>Dispatches by {@link SqlKind}:
+     * <ul>
+     *   <li>Binary arithmetic → {@link PhysicalBinaryExprNode} with type
+     *       promotion
+     *   <li>{@code MINUS_PREFIX} → {@link PhysicalNegativeNode}
+     *   <li>{@code PLUS_PREFIX} → identity (passthrough to operand)
+     *   <li>{@code CAST} → {@link PhysicalTryCastNode}
+     * </ul>
+     *
+     * @throws IllegalArgumentException if the SqlKind is not supported
+     */
+    @Override
+    public PhysicalExprNode convert(RexNode node, ConverterContext context) {
+        RexCall call = (RexCall) node;
+        SqlKind kind = call.getKind();
+        switch (kind) {
+            case PLUS:
+                return buildBinaryExpr(call, "Plus", context);
+            case MINUS:
+                return buildBinaryExpr(call, "Minus", context);
+            case TIMES:
+                return buildBinaryExpr(call, "Multiply", context);
+            case DIVIDE:
+                return buildBinaryExpr(call, "Divide", context);
+            case MOD:
+                return buildBinaryExpr(call, "Modulo", context);
+            case MINUS_PREFIX:
+                return buildNegative(call, context);
+            case PLUS_PREFIX:
+                return convertOperand(call.getOperands().get(0), context);
+            case CAST:
+                return buildTryCast(call, context);
+            default:
+                throw new IllegalArgumentException("Unsupported SqlKind: " + kind);
+        }
+    }
+
+    /**
+     * Builds a binary expression with type promotion between operands.
+     *
+     * <p>Operands are promoted to a common type. If the call's output type
+     * differs from the common type, the result is wrapped in a TryCast.
+     */
+    private PhysicalExprNode buildBinaryExpr(RexCall call, String op, ConverterContext context) {
+        RexNode left = call.getOperands().get(0);
+        RexNode right = call.getOperands().get(1);
+        RelDataType outputType = call.getType();
+
+        RelDataType compatibleType = getCommonTypeForComparison(left.getType(), right.getType(), TYPE_FACTORY);
+        if (compatibleType == null) {
+            throw new IllegalStateException("Incompatible types: "
+                    + left.getType().getSqlTypeName()
+                    + " and "
+                    + right.getType().getSqlTypeName());
+        }
+
+        PhysicalExprNode leftExpr = castIfNecessary(left, compatibleType, context);
+        PhysicalExprNode rightExpr = castIfNecessary(right, compatibleType, context);
+
+        PhysicalExprNode binaryExpr = PhysicalExprNode.newBuilder()
+                .setBinaryExpr(PhysicalBinaryExprNode.newBuilder()
+                        .setL(leftExpr)
+                        .setR(rightExpr)
+                        .setOp(op))
+                .build();
+
+        if (!outputType.getSqlTypeName().equals(compatibleType.getSqlTypeName())) {
+            return wrapInTryCast(binaryExpr, outputType);
+        }
+        return binaryExpr;
+    }
+
+    /**
+     * Computes the common type for two operand types during arithmetic
+     * promotion.
+     *
+     * <p>Rules:
+     * <ul>
+     *   <li>Same type → return as-is
+     *   <li>Both numeric: DECIMAL wins; BIGINT wins over smaller integers
+     *       (when neither is approximate); otherwise DOUBLE
+     *   <li>Both character → VARCHAR
+     *   <li>Otherwise → {@code null} (incompatible)
+     * </ul>
+     *
+     * @param type1 left operand type
+     * @param type2 right operand type
+     * @param typeFactory factory for creating result types
+     * @return the common type, or {@code null} if incompatible
+     */
+    static RelDataType getCommonTypeForComparison(
+            RelDataType type1, RelDataType type2, RelDataTypeFactory typeFactory) {
+        if (type1.getSqlTypeName().equals(type2.getSqlTypeName())) {
+            return type1;
+        }
+        if (SqlTypeUtil.isNumeric(type1) && SqlTypeUtil.isNumeric(type2)) {
+            SqlTypeName t1 = type1.getSqlTypeName();
+            SqlTypeName t2 = type2.getSqlTypeName();
+            if (t1 == SqlTypeName.DECIMAL || t2 == SqlTypeName.DECIMAL) {
+                return typeFactory.createSqlType(SqlTypeName.DECIMAL);
+            }
+            if (notApproxType(t1) && notApproxType(t2)) {
+                return typeFactory.createSqlType(SqlTypeName.BIGINT);
+            }
+            return typeFactory.createSqlType(SqlTypeName.DOUBLE);
+        }
+        if (SqlTypeUtil.inCharFamily(type1) && SqlTypeUtil.inCharFamily(type2)) {
+            return typeFactory.createSqlType(SqlTypeName.VARCHAR);
+        }
+        return null;
+    }
+
+    private static boolean notApproxType(SqlTypeName typeName) {
+        return typeName != SqlTypeName.FLOAT && typeName != SqlTypeName.DOUBLE;
+    }
+
+    /**
+     * Wraps the converted operand in a TryCast if its type differs from the
+     * target type.
+     */
+    private PhysicalExprNode castIfNecessary(RexNode expr, RelDataType targetType, ConverterContext context) {

Review Comment:
   Addressed together with R1 — `castIfNecessary()` and `wrapInTryCast()` are both in `TypeCastUtils` now. See 4e50bfd4.



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1859] Convert math operators to Auron native operators [auron]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #2167:
URL: https://github.com/apache/auron/pull/2167#discussion_r3098640814


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/TypeCastUtils.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import org.apache.auron.flink.utils.SchemaConverters;
+import org.apache.auron.protobuf.PhysicalExprNode;
+import org.apache.auron.protobuf.PhysicalTryCastNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.types.logical.LogicalType;
+
+/**
+ * Utility class for type promotion and casting operations used during
+ * {@link RexCallConverter} expression conversion.
+ *
+ * <p>Provides helpers to compute a common numeric type for binary arithmetic
+ * operands and to wrap native expressions in {@code TryCast} nodes.
+ */
+public final class TypeCastUtils {

Review Comment:
   Both addressed - thanks for the review!
   
   - **Rename** (`a0e4a32e`): `TypeCastUtils` → `FlinkNodeConverterUtils`. Javadoc broadened so future helpers shared across Flink converters can live there. Updated all call sites in `RexCallConverter` and the test class.
   - **Cleanup** (`87f22aab`): removed `AURON-1859-DESIGN.md` and the three `docs/reviewhelper/AURON-1859/*.md` files now that the review is done.
   
   Tests still green: `FlinkNodeConverterUtilsTest` 9/9, `RexCallConverterTest` 14/14.
   



-- 
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: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org