You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by da...@apache.org on 2022/07/19 13:47:17 UTC

[doris] branch master updated: cast array element to same type (#10980)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 95366de7f6 cast array element to same type (#10980)
95366de7f6 is described below

commit 95366de7f6adcee95bf8633cbbba0f5b3ddb44e3
Author: Pxl <95...@qq.com>
AuthorDate: Tue Jul 19 21:47:10 2022 +0800

    cast array element to same type (#10980)
    
    Fix problem when there are element of different types in an array.
---
 .../org/apache/doris/analysis/ArrayLiteral.java    | 28 ++++++++++++++++------
 .../main/java/org/apache/doris/catalog/Type.java   | 14 +++++++++++
 .../sql_functions/array_functions/sql/q02.out      |  4 +++-
 .../sql_functions/array_functions/sql/q02.sql      |  1 +
 4 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java
index 60be1f6001..5a6bc2bd09 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java
@@ -29,7 +29,6 @@ import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 public class ArrayLiteral extends LiteralExpr {
@@ -39,20 +38,35 @@ public class ArrayLiteral extends LiteralExpr {
         children = new ArrayList<>();
     }
 
-    public ArrayLiteral(LiteralExpr... v) {
+    public ArrayLiteral(LiteralExpr... exprs) throws AnalysisException {
         Type itemType = Type.NULL;
         boolean containsNull = false;
-        for (LiteralExpr expr : v) {
-            if (itemType == Type.NULL || expr.type.getSlotSize() > itemType.getSlotSize()) {
-                itemType = expr.type;
+        for (LiteralExpr expr : exprs) {
+            if (itemType == Type.NULL) {
+                itemType = expr.getType();
+            } else {
+                itemType = Type.getAssignmentCompatibleType(itemType, expr.getType(), false);
             }
+
             if (expr.isNullable()) {
                 containsNull = true;
             }
         }
+
+        if (itemType == Type.NULL || itemType == Type.INVALID) {
+            throw new AnalysisException("Invalid element type in ARRAY");
+        }
+
         type = new ArrayType(itemType, containsNull);
-        children = new ArrayList<>(v.length);
-        children.addAll(Arrays.asList(v));
+
+        children = new ArrayList<>();
+        for (LiteralExpr expr : exprs) {
+            if (expr.getType() == itemType) {
+                children.add(expr);
+            } else {
+                children.add(expr.castTo(itemType));
+            }
+        }
     }
 
     protected ArrayLiteral(ArrayLiteral other) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
index 2709ca09f6..8be3d110d0 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
@@ -507,6 +507,20 @@ public abstract class Type {
         if (t1.isScalarType() && t2.isScalarType()) {
             return ScalarType.getAssignmentCompatibleType((ScalarType) t1, (ScalarType) t2, strict);
         }
+
+        if (t1.isArrayType() && t2.isArrayType()) {
+            ArrayType arrayType1 = (ArrayType) t1;
+            ArrayType arrayType2 = (ArrayType) t2;
+            Type itemCompatibleType = Type.getAssignmentCompatibleType(arrayType1.getItemType(),
+                    arrayType2.getItemType(), strict);
+
+            if (itemCompatibleType.isInvalid()) {
+                return itemCompatibleType;
+            }
+
+            return new ArrayType(itemCompatibleType, arrayType1.getContainsNull() || arrayType2.getContainsNull());
+        }
+
         return ScalarType.INVALID;
     }
 
diff --git a/regression-test/data/query/sql_functions/array_functions/sql/q02.out b/regression-test/data/query/sql_functions/array_functions/sql/q02.out
index b4df8f5b4c..71ec9a217e 100644
--- a/regression-test/data/query/sql_functions/array_functions/sql/q02.out
+++ b/regression-test/data/query/sql_functions/array_functions/sql/q02.out
@@ -9,7 +9,7 @@
 [1, 2]
 
 -- !q02_4 --
-[1, -8761903601633862942.548033534]
+[1, 2]
 
 -- !q02_5 --
 [-1, 2]
@@ -50,3 +50,5 @@
 -- !q02_17 --
 [0]
 
+-- !q02_18 --
+[[[2]], [[aa], [2, 1.0]]]
diff --git a/regression-test/suites/query/sql_functions/array_functions/sql/q02.sql b/regression-test/suites/query/sql_functions/array_functions/sql/q02.sql
index 41677f4b5c..0f84c40e9a 100644
--- a/regression-test/suites/query/sql_functions/array_functions/sql/q02.sql
+++ b/regression-test/suites/query/sql_functions/array_functions/sql/q02.sql
@@ -22,3 +22,4 @@ SELECT
     0
 ];
 
+SELECT [[[2]], [['aa'],[2,1.0]]];


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org