use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project drill by apache.
the class DrillValuesRel method writeLiteral.
private static void writeLiteral(RexLiteral literal, JsonOutput out) throws IOException {
switch(literal.getType().getSqlTypeName()) {
case BIGINT:
if (isLiteralNull(literal)) {
out.writeBigIntNull();
} else {
out.writeBigInt((((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP)).longValue());
}
return;
case BOOLEAN:
if (isLiteralNull(literal)) {
out.writeBooleanNull();
} else {
out.writeBoolean((Boolean) literal.getValue());
}
return;
case CHAR:
if (isLiteralNull(literal)) {
out.writeVarcharNull();
} else {
// Since Calcite treats string literals as fixed char and adds trailing spaces to the strings to make them the
// same length, here we do an rtrim() to get the string without the trailing spaces. If we don't rtrim, the comparison
// with Drill's varchar column values would not return a match.
// TODO: However, note that if the user had explicitly added spaces in the string literals then even those would get
// trimmed, so this exposes another issue that needs to be resolved.
out.writeVarChar(((NlsString) literal.getValue()).rtrim().getValue());
}
return;
case DOUBLE:
if (isLiteralNull(literal)) {
out.writeDoubleNull();
} else {
out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
}
return;
case FLOAT:
if (isLiteralNull(literal)) {
out.writeFloatNull();
} else {
out.writeFloat(((BigDecimal) literal.getValue()).floatValue());
}
return;
case INTEGER:
if (isLiteralNull(literal)) {
out.writeIntNull();
} else {
out.writeInt((((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP)).intValue());
}
return;
case DECIMAL:
if (isLiteralNull(literal)) {
out.writeDoubleNull();
} else {
out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
}
logger.warn("Converting exact decimal into approximate decimal. Should be fixed once decimal is implemented.");
return;
case VARCHAR:
if (isLiteralNull(literal)) {
out.writeVarcharNull();
} else {
out.writeVarChar(((NlsString) literal.getValue()).getValue());
}
return;
case SYMBOL:
if (isLiteralNull(literal)) {
out.writeVarcharNull();
} else {
out.writeVarChar(literal.getValue().toString());
}
return;
case DATE:
if (isLiteralNull(literal)) {
out.writeDateNull();
} else {
out.writeDate(new DateTime((GregorianCalendar) literal.getValue()));
}
return;
case TIME:
if (isLiteralNull(literal)) {
out.writeTimeNull();
} else {
out.writeTime(new DateTime((GregorianCalendar) literal.getValue()));
}
return;
case TIMESTAMP:
if (isLiteralNull(literal)) {
out.writeTimestampNull();
} else {
out.writeTimestamp(new DateTime((GregorianCalendar) literal.getValue()));
}
return;
case INTERVAL_YEAR_MONTH:
if (isLiteralNull(literal)) {
out.writeIntervalNull();
} else {
int months = ((BigDecimal) (literal.getValue())).intValue();
out.writeInterval(new Period().plusMonths(months));
}
return;
case INTERVAL_DAY_TIME:
if (isLiteralNull(literal)) {
out.writeIntervalNull();
} else {
long millis = ((BigDecimal) (literal.getValue())).longValue();
int days = (int) (millis / MILLIS_IN_DAY);
millis = millis - (days * MILLIS_IN_DAY);
out.writeInterval(new Period().plusDays(days).plusMillis((int) millis));
}
return;
case NULL:
out.writeUntypedNull();
return;
case ANY:
default:
throw new UnsupportedOperationException(String.format("Unable to convert the value of %s and type %s to a Drill constant expression.", literal, literal.getType().getSqlTypeName()));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project calcite by apache.
the class SplunkPushDownRule method toString.
private String toString(boolean like, RexLiteral literal) {
String value = null;
SqlTypeName litSqlType = literal.getTypeName();
if (SqlTypeName.NUMERIC_TYPES.contains(litSqlType)) {
value = literal.getValue().toString();
} else if (litSqlType == SqlTypeName.CHAR) {
value = ((NlsString) literal.getValue()).getValue();
if (like) {
value = value.replaceAll("%", "*");
}
value = searchEscape(value);
}
return value;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project calcite by apache.
the class SqlToRelConverter method convertLiteralInValuesList.
private RexLiteral convertLiteralInValuesList(SqlNode sqlNode, Blackboard bb, RelDataType rowType, int iField) {
if (!(sqlNode instanceof SqlLiteral)) {
return null;
}
RelDataTypeField field = rowType.getFieldList().get(iField);
RelDataType type = field.getType();
if (type.isStruct()) {
// don't use LogicalValues for those
return null;
}
RexNode literalExpr = exprConverter.convertLiteral(bb, (SqlLiteral) sqlNode);
if (!(literalExpr instanceof RexLiteral)) {
assert literalExpr.isA(SqlKind.CAST);
RexNode child = ((RexCall) literalExpr).getOperands().get(0);
assert RexLiteral.isNullLiteral(child);
// in LogicalValues digest, so it's OK to lose it here
return (RexLiteral) child;
}
RexLiteral literal = (RexLiteral) literalExpr;
Comparable value = literal.getValue();
if (SqlTypeUtil.isExactNumeric(type) && SqlTypeUtil.hasScale(type)) {
BigDecimal roundedValue = NumberUtil.rescaleBigDecimal((BigDecimal) value, type.getScale());
return rexBuilder.makeExactLiteral(roundedValue, type);
}
if ((value instanceof NlsString) && (type.getSqlTypeName() == SqlTypeName.CHAR)) {
// pad fixed character type
NlsString unpadded = (NlsString) value;
return rexBuilder.makeCharLiteral(new NlsString(Spaces.padRight(unpadded.getValue(), type.getPrecision()), unpadded.getCharsetName(), unpadded.getCollation()));
}
return literal;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project calcite by apache.
the class RelBuilder method inferAlias.
/**
* Infers the alias of an expression.
*
* <p>If the expression was created by {@link #alias}, replaces the expression
* in the project list.
*/
private String inferAlias(List<RexNode> exprList, RexNode expr) {
switch(expr.getKind()) {
case INPUT_REF:
final RexInputRef ref = (RexInputRef) expr;
return stack.peek().fields.get(ref.getIndex()).getValue().getName();
case CAST:
return inferAlias(exprList, ((RexCall) expr).getOperands().get(0));
case AS:
final RexCall call = (RexCall) expr;
for (; ; ) {
final int i = exprList.indexOf(expr);
if (i < 0) {
break;
}
exprList.set(i, call.getOperands().get(0));
}
return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue()).getValue();
default:
return null;
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project calcite by apache.
the class SqlUserDefinedTableMacro method coerce.
private static Object coerce(Object o, RelDataType type) {
if (o == null) {
return null;
}
if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) {
return null;
}
final RelDataTypeFactoryImpl.JavaType javaType = (RelDataTypeFactoryImpl.JavaType) type;
final Class clazz = javaType.getJavaClass();
// noinspection unchecked
if (clazz.isAssignableFrom(o.getClass())) {
return o;
}
if (clazz == String.class && o instanceof NlsString) {
return ((NlsString) o).getValue();
}
// We need optimization here for constant folding.
// Not all the expressions can be interpreted (e.g. ternary), so
// we rely on optimization capabilities to fold non-interpretable
// expressions.
BlockBuilder bb = new BlockBuilder();
final Expression expr = RexToLixTranslator.convert(Expressions.constant(o), clazz);
bb.add(Expressions.return_(null, expr));
final FunctionExpression convert = Expressions.lambda(bb.toBlock(), Collections.<ParameterExpression>emptyList());
return convert.compile().dynamicInvoke();
}
Aggregations