Search in sources :

Example 6 with BitString

use of org.apache.calcite.util.BitString in project calcite by apache.

the class SqlNodeToRexConverterImpl method convertLiteral.

public RexNode convertLiteral(SqlRexContext cx, SqlLiteral literal) {
    RexBuilder rexBuilder = cx.getRexBuilder();
    RelDataTypeFactory typeFactory = cx.getTypeFactory();
    SqlValidator validator = cx.getValidator();
    if (literal.getValue() == null) {
        // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
        // treat it as a cast(null as boolean)
        RelDataType type;
        if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
            type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
            type = typeFactory.createTypeWithNullability(type, true);
        } else {
            type = validator.getValidatedNodeType(literal);
        }
        return rexBuilder.makeCast(type, rexBuilder.constantNull());
    }
    BitString bitString;
    SqlIntervalLiteral.IntervalValue intervalValue;
    long l;
    switch(literal.getTypeName()) {
        case DECIMAL:
            // exact number
            BigDecimal bd = literal.getValueAs(BigDecimal.class);
            return rexBuilder.makeExactLiteral(bd, literal.createSqlType(typeFactory));
        case DOUBLE:
            // TODO:  preserve fixed-point precision and large integers
            return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));
        case CHAR:
            return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
        case BOOLEAN:
            return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
        case BINARY:
            bitString = literal.getValueAs(BitString.class);
            Preconditions.checkArgument((bitString.getBitCount() % 8) == 0, "incomplete octet");
            // An even number of hexits (e.g. X'ABCD') makes whole number
            // of bytes.
            ByteString byteString = new ByteString(bitString.getAsByteArray());
            return rexBuilder.makeBinaryLiteral(byteString);
        case SYMBOL:
            return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
        case TIMESTAMP:
            return rexBuilder.makeTimestampLiteral(literal.getValueAs(TimestampString.class), ((SqlTimestampLiteral) literal).getPrec());
        case TIME:
            return rexBuilder.makeTimeLiteral(literal.getValueAs(TimeString.class), ((SqlTimeLiteral) literal).getPrec());
        case DATE:
            return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));
        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:
            SqlIntervalQualifier sqlIntervalQualifier = literal.getValueAs(SqlIntervalLiteral.IntervalValue.class).getIntervalQualifier();
            return rexBuilder.makeIntervalLiteral(literal.getValueAs(BigDecimal.class), sqlIntervalQualifier);
        default:
            throw Util.unexpected(literal.getTypeName());
    }
}
Also used : TimeString(org.apache.calcite.util.TimeString) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) ByteString(org.apache.calcite.avatica.util.ByteString) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) BitString(org.apache.calcite.util.BitString) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) DateString(org.apache.calcite.util.DateString) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) NlsString(org.apache.calcite.util.NlsString) TimestampString(org.apache.calcite.util.TimestampString)

Aggregations

BitString (org.apache.calcite.util.BitString)5 BigDecimal (java.math.BigDecimal)3 SqlIntervalLiteral (org.apache.calcite.sql.SqlIntervalLiteral)3 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)3 NlsString (org.apache.calcite.util.NlsString)3 BigInteger (java.math.BigInteger)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Charset (java.nio.charset.Charset)1 ByteString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString)1 RexCall (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall)1 RexDynamicParam (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)1 RexLiteral (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral)1 RexLocalRef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef)1 SqlTypeFamily (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily)1 SqlTypeName (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName)1 BitString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString)1 TimestampString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString)1 ByteString (org.apache.calcite.avatica.util.ByteString)1