Search in sources :

Example 46 with SqlTypeName

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;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 47 with SqlTypeName

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");
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.jupiter.api.Test)

Example 48 with SqlTypeName

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");
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.jupiter.api.Test)

Example 49 with SqlTypeName

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");
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.jupiter.api.Test)

Example 50 with SqlTypeName

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");
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.jupiter.api.Test)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)184 RelDataType (org.apache.calcite.rel.type.RelDataType)62 Test (org.junit.jupiter.api.Test)39 List (java.util.List)31 BigDecimal (java.math.BigDecimal)30 ArrayList (java.util.ArrayList)30 ImmutableList (com.google.common.collect.ImmutableList)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 Map (java.util.Map)25 RexNode (org.apache.calcite.rex.RexNode)25 NlsString (org.apache.calcite.util.NlsString)21 DateString (org.apache.calcite.util.DateString)18 TimeString (org.apache.calcite.util.TimeString)18 TimestampString (org.apache.calcite.util.TimestampString)18 ByteString (org.apache.calcite.avatica.util.ByteString)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)16 SqlKind (org.apache.calcite.sql.SqlKind)15 Calendar (java.util.Calendar)14 Objects (java.util.Objects)13 Util (org.apache.calcite.util.Util)13