Search in sources :

Example 6 with ArraySqlType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.ArraySqlType in project calcite by apache.

the class RexBuilder method makeLiteral.

/**
 * Creates a literal of a given type. The value is assumed to be
 * compatible with the type.
 *
 * @param value     Value
 * @param type      Type
 * @param allowCast Whether to allow a cast. If false, value is always a
 *                  {@link RexLiteral} but may not be the exact type
 * @return Simple literal, or cast simple literal
 */
public RexNode makeLiteral(Object value, RelDataType type, boolean allowCast) {
    if (value == null) {
        return makeCast(type, constantNull);
    }
    if (type.isNullable()) {
        final RelDataType typeNotNull = typeFactory.createTypeWithNullability(type, false);
        RexNode literalNotNull = makeLiteral(value, typeNotNull, allowCast);
        return makeAbstractCast(type, literalNotNull);
    }
    value = clean(value, type);
    RexLiteral literal;
    final List<RexNode> operands;
    switch(type.getSqlTypeName()) {
        case CHAR:
            return makeCharLiteral(padRight((NlsString) value, type.getPrecision()));
        case VARCHAR:
            literal = makeCharLiteral((NlsString) value);
            if (allowCast) {
                return makeCast(type, literal);
            } else {
                return literal;
            }
        case BINARY:
            return makeBinaryLiteral(padRight((ByteString) value, type.getPrecision()));
        case VARBINARY:
            literal = makeBinaryLiteral((ByteString) value);
            if (allowCast) {
                return makeCast(type, literal);
            } else {
                return literal;
            }
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case DECIMAL:
            return makeExactLiteral((BigDecimal) value, type);
        case FLOAT:
        case REAL:
        case DOUBLE:
            return makeApproxLiteral((BigDecimal) value, type);
        case BOOLEAN:
            return (Boolean) value ? booleanTrue : booleanFalse;
        case TIME:
            return makeTimeLiteral((TimeString) value, type.getPrecision());
        case TIME_WITH_LOCAL_TIME_ZONE:
            return makeTimeWithLocalTimeZoneLiteral((TimeString) value, type.getPrecision());
        case DATE:
            return makeDateLiteral((DateString) value);
        case TIMESTAMP:
            return makeTimestampLiteral((TimestampString) value, type.getPrecision());
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            return makeTimestampWithLocalTimeZoneLiteral((TimestampString) value, type.getPrecision());
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            return makeIntervalLiteral((BigDecimal) value, type.getIntervalQualifier());
        case MAP:
            final MapSqlType mapType = (MapSqlType) type;
            @SuppressWarnings("unchecked") final Map<Object, Object> map = (Map) value;
            operands = new ArrayList<>();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                operands.add(makeLiteral(entry.getKey(), mapType.getKeyType(), allowCast));
                operands.add(makeLiteral(entry.getValue(), mapType.getValueType(), allowCast));
            }
            return makeCall(SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR, operands);
        case ARRAY:
            final ArraySqlType arrayType = (ArraySqlType) type;
            @SuppressWarnings("unchecked") final List<Object> listValue = (List) value;
            operands = new ArrayList<>();
            for (Object entry : listValue) {
                operands.add(makeLiteral(entry, arrayType.getComponentType(), allowCast));
            }
            return makeCall(SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR, operands);
        case MULTISET:
            final MultisetSqlType multisetType = (MultisetSqlType) type;
            operands = new ArrayList<>();
            for (Object entry : (List) value) {
                final RexNode e = entry instanceof RexLiteral ? (RexNode) entry : makeLiteral(entry, multisetType.getComponentType(), allowCast);
                operands.add(e);
            }
            if (allowCast) {
                return makeCall(SqlStdOperatorTable.MULTISET_VALUE, operands);
            } else {
                return new RexLiteral((Comparable) FlatLists.of(operands), type, type.getSqlTypeName());
            }
        case ROW:
            operands = new ArrayList<>();
            // noinspection unchecked
            for (Pair<RelDataTypeField, Object> pair : Pair.zip(type.getFieldList(), (List<Object>) value)) {
                final RexNode e = pair.right instanceof RexLiteral ? (RexNode) pair.right : makeLiteral(pair.right, pair.left.getType(), allowCast);
                operands.add(e);
            }
            return new RexLiteral((Comparable) FlatLists.of(operands), type, type.getSqlTypeName());
        case ANY:
            return makeLiteral(value, guessType(value), allowCast);
        default:
            throw Util.unexpected(type.getSqlTypeName());
    }
}
Also used : ByteString(org.apache.calcite.avatica.util.ByteString) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) MapSqlType(org.apache.calcite.sql.type.MapSqlType) MultisetSqlType(org.apache.calcite.sql.type.MultisetSqlType) NlsString(org.apache.calcite.util.NlsString) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map)

Example 7 with ArraySqlType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.ArraySqlType in project calcite by apache.

the class SqlValidatorTest method testArrayAssignment.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1804">[CALCITE-1804]
 * Cannot assign NOT NULL array to nullable array</a>.
 */
@Test
public void testArrayAssignment() {
    final SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataType bigint = typeFactory.createSqlType(SqlTypeName.BIGINT);
    final RelDataType bigintNullable = typeFactory.createTypeWithNullability(bigint, true);
    final RelDataType bigintNotNull = typeFactory.createTypeWithNullability(bigint, false);
    final RelDataType date = typeFactory.createSqlType(SqlTypeName.DATE);
    final RelDataType dateNotNull = typeFactory.createTypeWithNullability(date, false);
    assertThat(SqlTypeUtil.canAssignFrom(bigintNullable, bigintNotNull), is(true));
    assertThat(SqlTypeUtil.canAssignFrom(bigintNullable, dateNotNull), is(false));
    final RelDataType bigintNullableArray = typeFactory.createArrayType(bigintNullable, -1);
    final RelDataType bigintArrayNullable = typeFactory.createTypeWithNullability(bigintNullableArray, true);
    final RelDataType bigintNotNullArray = new ArraySqlType(bigintNotNull, false);
    assertThat(SqlTypeUtil.canAssignFrom(bigintArrayNullable, bigintNotNullArray), is(true));
    final RelDataType dateNotNullArray = new ArraySqlType(dateNotNull, false);
    assertThat(SqlTypeUtil.canAssignFrom(bigintArrayNullable, dateNotNullArray), is(false));
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) Test(org.junit.Test)

Aggregations

ArraySqlType (org.apache.calcite.sql.type.ArraySqlType)6 RelDataType (org.apache.calcite.rel.type.RelDataType)4 ArrayList (java.util.ArrayList)3 MapSqlType (org.apache.calcite.sql.type.MapSqlType)3 Test (org.junit.Test)3 BigDecimal (java.math.BigDecimal)2 RexNode (org.apache.calcite.rex.RexNode)2 BasicSqlType (org.apache.calcite.sql.type.BasicSqlType)2 MultisetSqlType (org.apache.calcite.sql.type.MultisetSqlType)2 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Map (java.util.Map)1 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeField (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField)1 ArraySqlType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.ArraySqlType)1 MapSqlType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.MapSqlType)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1