Search in sources :

Example 16 with ByteString

use of org.apache.calcite.avatica.util.ByteString 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)

Example 17 with ByteString

use of org.apache.calcite.avatica.util.ByteString in project beam by apache.

the class BeamSqlUnparseContext method toSql.

@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
    if (rex.getKind().equals(SqlKind.LITERAL)) {
        final RexLiteral literal = (RexLiteral) rex;
        SqlTypeName name = literal.getTypeName();
        SqlTypeFamily family = name.getFamily();
        if (SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE.equals(name)) {
            TimestampString timestampString = literal.getValueAs(TimestampString.class);
            return new SqlDateTimeLiteral(timestampString, POS);
        } else if (SqlTypeFamily.BINARY.equals(family)) {
            ByteString byteString = literal.getValueAs(ByteString.class);
            BitString bitString = BitString.createFromHexString(byteString.toString(16));
            return new SqlByteStringLiteral(bitString, POS);
        } else if (SqlTypeFamily.CHARACTER.equals(family)) {
            String escaped = ESCAPE_FOR_ZETA_SQL.translate(literal.getValueAs(String.class));
            return SqlLiteral.createCharString(escaped, POS);
        } else if (SqlTypeName.SYMBOL.equals(literal.getTypeName())) {
            Enum symbol = literal.getValueAs(Enum.class);
            if (TimeUnitRange.DOW.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "DAYOFWEEK");
            } else if (TimeUnitRange.DOY.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "DAYOFYEAR");
            } else if (TimeUnitRange.WEEK.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "ISOWEEK");
            }
        }
    } else if (rex.getKind().equals(SqlKind.DYNAMIC_PARAM)) {
        final RexDynamicParam param = (RexDynamicParam) rex;
        final int index = param.getIndex();
        final String name = "null_param_" + index;
        nullParams.put(name, param.getType());
        return new NamedDynamicParam(index, POS, name);
    } else if (SqlKind.SEARCH.equals(rex.getKind())) {
        // Workaround CALCITE-4716
        RexCall search = (RexCall) rex;
        RexLocalRef ref = (RexLocalRef) search.operands.get(1);
        RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
        rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
    }
    return super.toSql(program, rex);
}
Also used : RexLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral) SqlTypeName(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName) SqlTypeFamily(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) BitString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) BitString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString) RexLocalRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) RexDynamicParam(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)

Aggregations

ByteString (org.apache.calcite.avatica.util.ByteString)15 BigDecimal (java.math.BigDecimal)9 DateString (org.apache.calcite.util.DateString)7 NlsString (org.apache.calcite.util.NlsString)7 TimeString (org.apache.calcite.util.TimeString)7 RelDataType (org.apache.calcite.rel.type.RelDataType)6 TimestampString (org.apache.calcite.util.TimestampString)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 Test (org.junit.Test)4 ImmutableList (com.google.common.collect.ImmutableList)3 Charset (java.nio.charset.Charset)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Calendar (java.util.Calendar)3 Map (java.util.Map)3 TimeZone (java.util.TimeZone)3 DateTimeUtils (org.apache.calcite.avatica.util.DateTimeUtils)3 SqlCollation (org.apache.calcite.sql.SqlCollation)3 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)3 BigInteger (java.math.BigInteger)2