use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class AbstractTypeCoercion method commonTypeForBinaryComparison.
/**
* Determines common type for a comparison operator when one operand is String type and the
* other is not. For date + timestamp operands, use timestamp as common type,
* i.e. Timestamp(2017-01-01 00:00 ...) > Date(2018) evaluates to be false.
*/
@Override
@Nullable
public RelDataType commonTypeForBinaryComparison(@Nullable RelDataType type1, @Nullable RelDataType type2) {
if (type1 == null || type2 == null) {
return null;
}
SqlTypeName typeName1 = type1.getSqlTypeName();
SqlTypeName typeName2 = type2.getSqlTypeName();
if (typeName1 == null || typeName2 == null) {
return null;
}
// that coerce Datetime and CHARACTER comparison.
if (SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isDatetime(type2)) {
return type2;
}
if (SqlTypeUtil.isDatetime(type1) && SqlTypeUtil.isCharacter(type2)) {
return type1;
}
// DATE + TIMESTAMP -> TIMESTAMP
if (SqlTypeUtil.isDate(type1) && SqlTypeUtil.isTimestamp(type2)) {
return type2;
}
if (SqlTypeUtil.isDate(type2) && SqlTypeUtil.isTimestamp(type1)) {
return type1;
}
if (SqlTypeUtil.isString(type1) && typeName2 == SqlTypeName.NULL) {
return type1;
}
if (typeName1 == SqlTypeName.NULL && SqlTypeUtil.isString(type2)) {
return type2;
}
if (SqlTypeUtil.isDecimal(type1) && SqlTypeUtil.isCharacter(type2) || SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isDecimal(type2)) {
// as the best we can do.
return SqlTypeUtil.getMaxPrecisionScaleDecimal(factory);
}
// 2. CHARACTER can only be coerced to DOUBLE/DECIMAL.
if (SqlTypeUtil.isBinary(type2) && SqlTypeUtil.isApproximateNumeric(type1) || SqlTypeUtil.isBinary(type1) && SqlTypeUtil.isApproximateNumeric(type2)) {
return null;
}
// 1 > '1' will be coerced to 1 > 1.
if (SqlTypeUtil.isAtomic(type1) && SqlTypeUtil.isCharacter(type2)) {
if (SqlTypeUtil.isTimestamp(type1)) {
return null;
}
return type1;
}
if (SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isAtomic(type2)) {
if (SqlTypeUtil.isTimestamp(type2)) {
return null;
}
return type2;
}
return null;
}
use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class SqlTypeNameTest method testArray.
@Test
void testArray() {
SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.ARRAY);
assertEquals(ARRAY, tn, "ARRAY did not map to ARRAY");
}
use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class SqlTypeNameTest method testNull.
@Test
void testNull() {
SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.NULL);
assertEquals(null, tn, "NULL did not map to null");
}
use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class SqlTypeNameTest method testLongvarchar.
@Test
void testLongvarchar() {
SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.LONGVARCHAR);
assertEquals(null, tn, "LONGVARCHAR did not map to null");
}
use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class SqlTypeNameTest method testInteger.
@Test
void testInteger() {
SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.INTEGER);
assertEquals(INTEGER, tn, "INTEGER did not map to INTEGER");
}
Aggregations