use of org.apache.beam.vendor.calcite.v1_28_0.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());
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.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);
}
Aggregations