[feature](decimal) support decimal256 when creating table (#26308)
This commit is contained in:
@ -104,7 +104,7 @@ public class ArrayType extends Type {
|
||||
|
||||
@Override
|
||||
public Type specializeTemplateType(Type specificType, Map<String, Type> specializedTypeMap,
|
||||
boolean useSpecializedType) throws TypeException {
|
||||
boolean useSpecializedType, boolean enableDecimal256) throws TypeException {
|
||||
ArrayType specificArrayType = null;
|
||||
if (specificType instanceof ArrayType) {
|
||||
specificArrayType = (ArrayType) specificType;
|
||||
@ -116,7 +116,7 @@ public class ArrayType extends Type {
|
||||
if (itemType.hasTemplateType()) {
|
||||
newItemType = itemType.specializeTemplateType(
|
||||
specificArrayType != null ? specificArrayType.itemType : specificType,
|
||||
specializedTypeMap, useSpecializedType);
|
||||
specializedTypeMap, useSpecializedType, enableDecimal256);
|
||||
}
|
||||
|
||||
return new ArrayType(newItemType);
|
||||
@ -163,8 +163,10 @@ public class ArrayType extends Type {
|
||||
return Type.canCastTo(type.getItemType(), targetType.getItemType());
|
||||
}
|
||||
|
||||
public static Type getAssignmentCompatibleType(ArrayType t1, ArrayType t2, boolean strict) {
|
||||
Type itemCompatibleType = Type.getAssignmentCompatibleType(t1.getItemType(), t2.getItemType(), strict);
|
||||
public static Type getAssignmentCompatibleType(
|
||||
ArrayType t1, ArrayType t2, boolean strict, boolean enableDecimal256) {
|
||||
Type itemCompatibleType = Type.getAssignmentCompatibleType(t1.getItemType(), t2.getItemType(), strict,
|
||||
enableDecimal256);
|
||||
|
||||
if (itemCompatibleType.isInvalid()) {
|
||||
return ScalarType.INVALID;
|
||||
|
||||
@ -142,7 +142,7 @@ public class MapType extends Type {
|
||||
|
||||
@Override
|
||||
public Type specializeTemplateType(Type specificType, Map<String, Type> specializedTypeMap,
|
||||
boolean useSpecializedType) throws TypeException {
|
||||
boolean useSpecializedType, boolean enableDecimal256) throws TypeException {
|
||||
MapType specificMapType = null;
|
||||
if (specificType instanceof MapType) {
|
||||
specificMapType = (MapType) specificType;
|
||||
@ -154,13 +154,13 @@ public class MapType extends Type {
|
||||
if (keyType.hasTemplateType()) {
|
||||
newKeyType = keyType.specializeTemplateType(
|
||||
specificMapType != null ? specificMapType.keyType : specificType,
|
||||
specializedTypeMap, useSpecializedType);
|
||||
specializedTypeMap, useSpecializedType, enableDecimal256);
|
||||
}
|
||||
Type newValueType = valueType;
|
||||
if (valueType.hasTemplateType()) {
|
||||
newValueType = valueType.specializeTemplateType(
|
||||
specificMapType != null ? specificMapType.valueType : specificType,
|
||||
specializedTypeMap, useSpecializedType);
|
||||
specializedTypeMap, useSpecializedType, enableDecimal256);
|
||||
}
|
||||
|
||||
Type newMapType = new MapType(newKeyType, newValueType);
|
||||
@ -198,12 +198,14 @@ public class MapType extends Type {
|
||||
|| targetType.getValueType().isStringType() && type.getValueType().isStringType());
|
||||
}
|
||||
|
||||
public static Type getAssignmentCompatibleType(MapType t1, MapType t2, boolean strict) {
|
||||
Type keyCompatibleType = Type.getAssignmentCompatibleType(t1.getKeyType(), t2.getKeyType(), strict);
|
||||
public static Type getAssignmentCompatibleType(MapType t1, MapType t2, boolean strict, boolean enableDecimal256) {
|
||||
Type keyCompatibleType = Type.getAssignmentCompatibleType(t1.getKeyType(), t2.getKeyType(), strict,
|
||||
enableDecimal256);
|
||||
if (keyCompatibleType.isInvalid()) {
|
||||
return ScalarType.INVALID;
|
||||
}
|
||||
Type valCompatibleType = Type.getAssignmentCompatibleType(t1.getValueType(), t2.getValueType(), strict);
|
||||
Type valCompatibleType = Type.getAssignmentCompatibleType(t1.getValueType(), t2.getValueType(), strict,
|
||||
enableDecimal256);
|
||||
if (valCompatibleType.isInvalid()) {
|
||||
return ScalarType.INVALID;
|
||||
}
|
||||
|
||||
@ -1019,7 +1019,7 @@ public class ScalarType extends Type {
|
||||
* is INVALID_TYPE.
|
||||
*/
|
||||
public static ScalarType getAssignmentCompatibleType(
|
||||
ScalarType t1, ScalarType t2, boolean strict) {
|
||||
ScalarType t1, ScalarType t2, boolean strict, boolean enableDecimal256) {
|
||||
if (!t1.isValid() || !t2.isValid()) {
|
||||
return INVALID;
|
||||
}
|
||||
@ -1119,7 +1119,11 @@ public class ScalarType extends Type {
|
||||
if (scale + integerPart <= ScalarType.MAX_DECIMAL128_PRECISION) {
|
||||
return ScalarType.createDecimalV3Type(scale + integerPart, scale);
|
||||
} else {
|
||||
return Type.DOUBLE;
|
||||
if (enableDecimal256) {
|
||||
return ScalarType.createDecimalV3Type(scale + integerPart, scale);
|
||||
} else {
|
||||
return Type.DOUBLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1177,8 +1181,8 @@ public class ScalarType extends Type {
|
||||
* If strict is true, only consider casts that result in no loss of precision.
|
||||
*/
|
||||
public static boolean isImplicitlyCastable(
|
||||
ScalarType t1, ScalarType t2, boolean strict) {
|
||||
return getAssignmentCompatibleType(t1, t2, strict).matchesType(t2);
|
||||
ScalarType t1, ScalarType t2, boolean strict, boolean enableDecimal256) {
|
||||
return getAssignmentCompatibleType(t1, t2, strict, enableDecimal256).matchesType(t2);
|
||||
}
|
||||
|
||||
public static boolean canCastTo(ScalarType type, ScalarType targetType) {
|
||||
|
||||
@ -111,7 +111,8 @@ public class StructType extends Type {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Type getAssignmentCompatibleType(StructType t1, StructType t2, boolean strict) {
|
||||
public static Type getAssignmentCompatibleType(
|
||||
StructType t1, StructType t2, boolean strict, boolean enableDecimal256) {
|
||||
ArrayList<StructField> fieldsLeft = t1.getFields();
|
||||
ArrayList<StructField> fieldsRight = t2.getFields();
|
||||
ArrayList<StructField> fieldsRes = new ArrayList<>();
|
||||
@ -120,7 +121,7 @@ public class StructType extends Type {
|
||||
StructField leftField = fieldsLeft.get(i);
|
||||
StructField rightField = fieldsRight.get(i);
|
||||
Type itemCompatibleType = Type.getAssignmentCompatibleType(leftField.getType(), rightField.getType(),
|
||||
strict);
|
||||
strict, enableDecimal256);
|
||||
if (itemCompatibleType.isInvalid()) {
|
||||
return ScalarType.INVALID;
|
||||
}
|
||||
@ -221,7 +222,7 @@ public class StructType extends Type {
|
||||
|
||||
@Override
|
||||
public Type specializeTemplateType(Type specificType, Map<String, Type> specializedTypeMap,
|
||||
boolean useSpecializedType) throws TypeException {
|
||||
boolean useSpecializedType, boolean enableDecimal256) throws TypeException {
|
||||
StructType specificStructType = null;
|
||||
if (specificType instanceof StructType) {
|
||||
specificStructType = (StructType) specificType;
|
||||
@ -234,7 +235,7 @@ public class StructType extends Type {
|
||||
if (fields.get(i).type.hasTemplateType()) {
|
||||
newTypes.add(fields.get(i).type.specializeTemplateType(
|
||||
specificStructType != null ? specificStructType.fields.get(i).type : specificType,
|
||||
specializedTypeMap, useSpecializedType));
|
||||
specializedTypeMap, useSpecializedType, enableDecimal256));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -81,7 +81,7 @@ public class TemplateType extends Type {
|
||||
|
||||
@Override
|
||||
public Type specializeTemplateType(Type specificType, Map<String, Type> specializedTypeMap,
|
||||
boolean useSpecializedType) throws TypeException {
|
||||
boolean useSpecializedType, boolean enableDecimal256) throws TypeException {
|
||||
if (specificType.hasTemplateType() && !specificType.isNull()) {
|
||||
throw new TypeException(specificType + " should not hasTemplateType");
|
||||
}
|
||||
@ -97,7 +97,7 @@ public class TemplateType extends Type {
|
||||
if (specializedType != null
|
||||
&& !specificType.equals(specializedType)
|
||||
&& !specificType.matchesType(specializedType)
|
||||
&& !Type.isImplicitlyCastable(specificType, specializedType, true)
|
||||
&& !Type.isImplicitlyCastable(specificType, specializedType, true, enableDecimal256)
|
||||
&& !Type.canCastTo(specificType, specializedType)) {
|
||||
throw new TypeException(
|
||||
String.format("can not specialize template type %s to %s since it's already specialized as %s",
|
||||
|
||||
@ -627,7 +627,7 @@ public abstract class Type {
|
||||
|
||||
// return a new type without template type, by specialize template type in this type
|
||||
public Type specializeTemplateType(Type specificType, Map<String, Type> specializedTypeMap,
|
||||
boolean useSpecializedType) throws TypeException {
|
||||
boolean useSpecializedType, boolean enableDecimal256) throws TypeException {
|
||||
if (hasTemplateType()) {
|
||||
// throw exception by default, sub class should specialize tempalte type properly
|
||||
throw new TypeException("specializeTemplateType not implemented");
|
||||
@ -728,9 +728,9 @@ public abstract class Type {
|
||||
* If strict is true, only consider casts that result in no loss of precision.
|
||||
* TODO: Support casting of non-scalar types.
|
||||
*/
|
||||
public static boolean isImplicitlyCastable(Type t1, Type t2, boolean strict) {
|
||||
public static boolean isImplicitlyCastable(Type t1, Type t2, boolean strict, boolean enableDecimal256) {
|
||||
if (t1.isScalarType() && t2.isScalarType()) {
|
||||
return ScalarType.isImplicitlyCastable((ScalarType) t1, (ScalarType) t2, strict);
|
||||
return ScalarType.isImplicitlyCastable((ScalarType) t1, (ScalarType) t2, strict, enableDecimal256);
|
||||
}
|
||||
if (t1.isComplexType() || t2.isComplexType()) {
|
||||
if ((t1.isArrayType() && t2.isArrayType()) || (t1.isMapType() && t2.isMapType())
|
||||
@ -771,17 +771,17 @@ public abstract class Type {
|
||||
* no such type or if any of t1 and t2 is INVALID_TYPE.
|
||||
* TODO: Support non-scalar types.
|
||||
*/
|
||||
public static Type getAssignmentCompatibleType(Type t1, Type t2, boolean strict) {
|
||||
public static Type getAssignmentCompatibleType(Type t1, Type t2, boolean strict, boolean enableDecimal256) {
|
||||
if (t1.isScalarType() && t2.isScalarType()) {
|
||||
return ScalarType.getAssignmentCompatibleType((ScalarType) t1, (ScalarType) t2, strict);
|
||||
return ScalarType.getAssignmentCompatibleType((ScalarType) t1, (ScalarType) t2, strict, enableDecimal256);
|
||||
}
|
||||
|
||||
if (t1.isArrayType() && t2.isArrayType()) {
|
||||
return ArrayType.getAssignmentCompatibleType((ArrayType) t1, (ArrayType) t2, strict);
|
||||
return ArrayType.getAssignmentCompatibleType((ArrayType) t1, (ArrayType) t2, strict, enableDecimal256);
|
||||
} else if (t1.isMapType() && t2.isMapType()) {
|
||||
return MapType.getAssignmentCompatibleType((MapType) t1, (MapType) t2, strict);
|
||||
return MapType.getAssignmentCompatibleType((MapType) t1, (MapType) t2, strict, enableDecimal256);
|
||||
} else if (t1.isStructType() && t2.isStructType()) {
|
||||
return StructType.getAssignmentCompatibleType((StructType) t1, (StructType) t2, strict);
|
||||
return StructType.getAssignmentCompatibleType((StructType) t1, (StructType) t2, strict, enableDecimal256);
|
||||
} else if (t1.isComplexType() && t2.isNull()) {
|
||||
return t1;
|
||||
} else if (t1.isNull() && t2.isComplexType()) {
|
||||
@ -1977,7 +1977,7 @@ public abstract class Type {
|
||||
}
|
||||
}
|
||||
|
||||
public static Type getCmpType(Type t1, Type t2) {
|
||||
public static Type getCmpType(Type t1, Type t2, boolean enableDecimal256) {
|
||||
if (t1.getPrimitiveType() == PrimitiveType.NULL_TYPE) {
|
||||
return t2;
|
||||
}
|
||||
@ -2033,7 +2033,7 @@ public abstract class Type {
|
||||
}
|
||||
|
||||
if (t1ResultType == PrimitiveType.BIGINT && t2ResultType == PrimitiveType.BIGINT) {
|
||||
return getAssignmentCompatibleType(t1, t2, false);
|
||||
return getAssignmentCompatibleType(t1, t2, false, enableDecimal256);
|
||||
}
|
||||
if (t1.getPrimitiveType().isDecimalV3Type() && t2.getPrimitiveType().isDecimalV3Type()) {
|
||||
int resultPrecision = Math.max(t1.getPrecision(), t2.getPrecision());
|
||||
@ -2049,11 +2049,16 @@ public abstract class Type {
|
||||
return ScalarType.createDecimalType(resultDecimalType, resultPrecision, Math.max(
|
||||
((ScalarType) t1).getScalarScale(), ((ScalarType) t2).getScalarScale()));
|
||||
} else {
|
||||
return Type.DOUBLE;
|
||||
if (enableDecimal256) {
|
||||
return ScalarType.createDecimalType(resultDecimalType, resultPrecision, Math.max(
|
||||
((ScalarType) t1).getScalarScale(), ((ScalarType) t2).getScalarScale()));
|
||||
} else {
|
||||
return Type.DOUBLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (t1ResultType.isDecimalV3Type() || t2ResultType.isDecimalV3Type()) {
|
||||
return getAssignmentCompatibleType(t1, t2, false);
|
||||
return getAssignmentCompatibleType(t1, t2, false, enableDecimal256);
|
||||
}
|
||||
if ((t1ResultType == PrimitiveType.BIGINT
|
||||
|| t1ResultType == PrimitiveType.DECIMALV2)
|
||||
|
||||
Reference in New Issue
Block a user