cast array element to same type (#10980)

Fix problem when there are element of different types in an array.
This commit is contained in:
Pxl
2022-07-19 21:47:10 +08:00
committed by GitHub
parent 371c7be235
commit 95366de7f6
4 changed files with 39 additions and 8 deletions

View File

@ -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) {

View File

@ -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;
}

View File

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

View File

@ -22,3 +22,4 @@ SELECT
0
];
SELECT [[[2]], [['aa'],[2,1.0]]];