Search in sources :

Example 1 with NlsString

use of 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()));
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) NlsString(org.apache.calcite.util.NlsString) Period(org.joda.time.Period) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal)

Example 2 with NlsString

use of 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;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString)

Example 3 with NlsString

use of 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;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) NlsString(org.apache.calcite.util.NlsString) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlLiteral(org.apache.calcite.sql.SqlLiteral) BigDecimal(java.math.BigDecimal) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with NlsString

use of 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;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) NlsString(org.apache.calcite.util.NlsString)

Example 5 with NlsString

use of 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();
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) Expression(org.apache.calcite.linq4j.tree.Expression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelDataTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeFactoryImpl) NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

NlsString (org.apache.calcite.util.NlsString)32 BigDecimal (java.math.BigDecimal)13 TimestampString (org.apache.calcite.util.TimestampString)9 DateString (org.apache.calcite.util.DateString)8 RelDataType (org.apache.calcite.rel.type.RelDataType)7 TimeString (org.apache.calcite.util.TimeString)7 ByteString (org.apache.calcite.avatica.util.ByteString)6 RexLiteral (org.apache.calcite.rex.RexLiteral)6 RexNode (org.apache.calcite.rex.RexNode)6 SqlNode (org.apache.calcite.sql.SqlNode)6 RexCall (org.apache.calcite.rex.RexCall)5 Charset (java.nio.charset.Charset)4 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)4 SqlCollation (org.apache.calcite.sql.SqlCollation)4 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)4 ImmutableList (com.google.common.collect.ImmutableList)3 SimpleDateFormat (java.text.SimpleDateFormat)3 List (java.util.List)3