This is an automated email from the ASF dual-hosted git repository.

joemcdonnell pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 3d24f45f9c530b7512c62a692aabf148d8236457
Author: Steve Carlin <[email protected]>
AuthorDate: Tue Feb 25 07:24:25 2025 -0800

    IMPALA-13796: Calcite planner: Improper casting for char on join condition
    
    For the following query:
    
    SELECT COUNT(*) from orders t1 LEFT OUTER JOIN orders t2
    ON cast(t1.o_comment as char(120)) = cast(t2.o_comment as char(120));
    
    The join condition uses the Function "=(CHAR,CHAR)".  The function
    defined within Impala uses a wildcard for the length of the char (-1).
    
    Previous to the fix, the code detected that the char(120) needed casting,
    would cast it to a char(1), and this produced erroneous results.
    
    The fix is to make sure we don't cast from a char(x) to a char(-1).
    
    Change-Id: Ib9f44e3d5a7623a20d9841541bb496c1dee32d1e
    Reviewed-on: http://gerrit.cloudera.org:8080/22541
    Tested-by: Impala Public Jenkins <[email protected]>
    Reviewed-by: Steve Carlin <[email protected]>
---
 .../apache/impala/calcite/coercenodes/CoerceOperandShuttle.java    | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git 
a/java/calcite-planner/src/main/java/org/apache/impala/calcite/coercenodes/CoerceOperandShuttle.java
 
b/java/calcite-planner/src/main/java/org/apache/impala/calcite/coercenodes/CoerceOperandShuttle.java
index 654afad1b..135fd350f 100644
--- 
a/java/calcite-planner/src/main/java/org/apache/impala/calcite/coercenodes/CoerceOperandShuttle.java
+++ 
b/java/calcite-planner/src/main/java/org/apache/impala/calcite/coercenodes/CoerceOperandShuttle.java
@@ -338,6 +338,13 @@ public class CoerceOperandShuttle extends RexShuttle {
   private static RelDataType getCastedToType(RelDataType fromType,
       Type toImpalaType, RelDataTypeFactory factory) {
 
+    // Special case: If the "to" type is a generic CHAR (where len = -1),
+    // there is no casting needed if the "from" type is also a CHAR.
+    if (toImpalaType.equals(Type.CHAR) &&
+        fromType.getSqlTypeName().equals(SqlTypeName.CHAR)) {
+      return fromType;
+    }
+
     if (!toImpalaType.isDecimal() || SqlTypeUtil.isNull(fromType)) {
       return ImpalaTypeConverter.getRelDataType(toImpalaType);
     }

Reply via email to