in fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java [2065:2173]
public static Type getCmpType(Type t1, Type t2, boolean enableDecimal256) {
if (t1.getPrimitiveType() == PrimitiveType.NULL_TYPE) {
return t2;
}
if (t2.getPrimitiveType() == PrimitiveType.NULL_TYPE) {
return t1;
}
PrimitiveType t1ResultType = t1.getResultType().getPrimitiveType();
PrimitiveType t2ResultType = t2.getResultType().getPrimitiveType();
if (canCompareDate(t1.getPrimitiveType(), t2.getPrimitiveType())) {
return getDateComparisonResultType((ScalarType) t1, (ScalarType) t2);
}
// Following logical is compatible with MySQL.
if (t1ResultType == PrimitiveType.VARCHAR && t2ResultType == PrimitiveType.VARCHAR) {
return Type.VARCHAR;
}
if ((t1ResultType == PrimitiveType.STRING && t2ResultType == PrimitiveType.STRING)
|| (t1ResultType == PrimitiveType.STRING && t2ResultType == PrimitiveType.VARCHAR)
|| (t1ResultType == PrimitiveType.VARCHAR && t2ResultType == PrimitiveType.STRING)) {
return Type.STRING;
}
// TODO(wzy): support NUMERIC/CHAR cast to JSONB
if (t1ResultType == PrimitiveType.JSONB && t2ResultType == PrimitiveType.JSONB) {
return Type.JSONB;
}
if ((t1ResultType == PrimitiveType.JSONB && t2ResultType == PrimitiveType.VARCHAR)
|| (t1ResultType == PrimitiveType.VARCHAR && t2ResultType == PrimitiveType.JSONB)) {
return Type.VARCHAR;
}
if ((t1ResultType == PrimitiveType.JSONB && t2ResultType == PrimitiveType.STRING)
|| (t1ResultType == PrimitiveType.STRING && t2ResultType == PrimitiveType.JSONB)) {
return Type.STRING;
}
// VARIANT
if (t1ResultType == PrimitiveType.VARIANT && t2ResultType == PrimitiveType.VARIANT) {
return Type.VARIANT;
}
if ((t1ResultType == PrimitiveType.VARIANT && t2ResultType == PrimitiveType.VARCHAR)
|| (t1ResultType == PrimitiveType.VARCHAR && t2ResultType == PrimitiveType.VARIANT)) {
return Type.VARCHAR;
}
if ((t1ResultType == PrimitiveType.VARIANT && t2ResultType == PrimitiveType.STRING)
|| (t1ResultType == PrimitiveType.STRING && t2ResultType == PrimitiveType.VARIANT)) {
return Type.STRING;
}
// int family type and char family type should cast to char family type
if ((t1.getPrimitiveType().isFixedPointType() && t2.getPrimitiveType().isCharFamily())
|| (t2.getPrimitiveType().isFixedPointType() && t1.getPrimitiveType().isCharFamily())) {
return Type.VARCHAR;
}
if (t1ResultType == PrimitiveType.BIGINT && t2ResultType == PrimitiveType.BIGINT) {
return getAssignmentCompatibleType(t1, t2, false, enableDecimal256);
}
if (t1.getPrimitiveType().isDecimalV3Type() && t2.getPrimitiveType().isDecimalV3Type()) {
int resultPrecision = Math.max(t1.getPrecision(), t2.getPrecision());
PrimitiveType resultDecimalType;
if (resultPrecision <= ScalarType.MAX_DECIMAL32_PRECISION) {
resultDecimalType = PrimitiveType.DECIMAL32;
} else if (resultPrecision <= ScalarType.MAX_DECIMAL64_PRECISION) {
resultDecimalType = PrimitiveType.DECIMAL64;
} else {
resultDecimalType = PrimitiveType.DECIMAL128;
}
if (resultPrecision <= ScalarType.MAX_DECIMAL128_PRECISION) {
return ScalarType.createDecimalType(resultDecimalType, resultPrecision, Math.max(
((ScalarType) t1).getScalarScale(), ((ScalarType) t2).getScalarScale()));
} else {
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, enableDecimal256);
}
if ((t1ResultType == PrimitiveType.BIGINT
|| t1ResultType == PrimitiveType.DECIMALV2)
&& (t2ResultType == PrimitiveType.BIGINT
|| t2ResultType == PrimitiveType.DECIMALV2)) {
return Type.DECIMALV2;
}
if ((t1ResultType == PrimitiveType.BIGINT
|| t1ResultType == PrimitiveType.LARGEINT)
&& (t2ResultType == PrimitiveType.BIGINT
|| t2ResultType == PrimitiveType.LARGEINT)) {
return Type.LARGEINT;
}
if ((t1ResultType == PrimitiveType.IPV4
|| t1ResultType == PrimitiveType.VARCHAR)
&& (t2ResultType == PrimitiveType.IPV4
|| t2ResultType == PrimitiveType.VARCHAR)) {
return Type.IPV4;
}
if ((t1ResultType == PrimitiveType.IPV6
|| t1ResultType == PrimitiveType.VARCHAR)
&& (t2ResultType == PrimitiveType.IPV6
|| t2ResultType == PrimitiveType.VARCHAR)) {
return Type.IPV6;
}
return Type.DOUBLE;
}