Search in sources :

Example 1 with UnaryExpression

use of org.apache.calcite.linq4j.tree.UnaryExpression in project calcite by apache.

the class RexToLixTranslator method convert.

public static Expression convert(Expression operand, Type fromType, Type toType) {
    if (fromType.equals(toType)) {
        return operand;
    }
    // E.g. from "Short" to "int".
    // Generate "x.intValue()".
    final Primitive toPrimitive = Primitive.of(toType);
    final Primitive toBox = Primitive.ofBox(toType);
    final Primitive fromBox = Primitive.ofBox(fromType);
    final Primitive fromPrimitive = Primitive.of(fromType);
    final boolean fromNumber = fromType instanceof Class && Number.class.isAssignableFrom((Class) fromType);
    if (fromType == String.class) {
        if (toPrimitive != null) {
            switch(toPrimitive) {
                case CHAR:
                case SHORT:
                case INT:
                case LONG:
                case FLOAT:
                case DOUBLE:
                    // Generate "SqlFunctions.toShort(x)".
                    return Expressions.call(SqlFunctions.class, "to" + SqlFunctions.initcap(toPrimitive.primitiveName), operand);
                default:
                    // Generate "Short.parseShort(x)".
                    return Expressions.call(toPrimitive.boxClass, "parse" + SqlFunctions.initcap(toPrimitive.primitiveName), operand);
            }
        }
        if (toBox != null) {
            switch(toBox) {
                case CHAR:
                    // Generate "SqlFunctions.toCharBoxed(x)".
                    return Expressions.call(SqlFunctions.class, "to" + SqlFunctions.initcap(toBox.primitiveName) + "Boxed", operand);
                default:
                    // Generate "Short.valueOf(x)".
                    return Expressions.call(toBox.boxClass, "valueOf", operand);
            }
        }
    }
    if (toPrimitive != null) {
        if (fromPrimitive != null) {
            // E.g. from "float" to "double"
            return Expressions.convert_(operand, toPrimitive.primitiveClass);
        }
        if (fromNumber || fromBox == Primitive.CHAR) {
            // Generate "x.shortValue()".
            return Expressions.unbox(operand, toPrimitive);
        } else {
            // Generate "SqlFunctions.toShort(x)"
            return Expressions.call(SqlFunctions.class, "to" + SqlFunctions.initcap(toPrimitive.primitiveName), operand);
        }
    } else if (fromNumber && toBox != null) {
        // Generate "x == null ? null : Integer.valueOf(x.intValue())"
        return Expressions.condition(Expressions.equal(operand, RexImpTable.NULL_EXPR), RexImpTable.NULL_EXPR, Expressions.box(Expressions.unbox(operand, toBox), toBox));
    } else if (fromPrimitive != null && toBox != null) {
        // Eliminate primitive casts like Long.valueOf((long) x)
        if (operand instanceof UnaryExpression) {
            UnaryExpression una = (UnaryExpression) operand;
            if (una.nodeType == ExpressionType.Convert || Primitive.of(una.getType()) == toBox) {
                return Expressions.box(una.expression, toBox);
            }
        }
        return Expressions.box(operand, toBox);
    } else if (fromType == java.sql.Date.class) {
        if (toBox == Primitive.INT) {
            return Expressions.call(BuiltInMethod.DATE_TO_INT.method, operand);
        } else {
            return Expressions.convert_(operand, toType);
        }
    } else if (toType == java.sql.Date.class) {
        // generate "SqlFunctions.internalToDate".
        if (isA(fromType, Primitive.INT)) {
            return Expressions.call(BuiltInMethod.INTERNAL_TO_DATE.method, operand);
        } else {
            return Expressions.convert_(operand, java.sql.Date.class);
        }
    } else if (toType == java.sql.Time.class) {
        // generate "SqlFunctions.internalToTime".
        if (isA(fromType, Primitive.INT)) {
            return Expressions.call(BuiltInMethod.INTERNAL_TO_TIME.method, operand);
        } else {
            return Expressions.convert_(operand, java.sql.Time.class);
        }
    } else if (toType == java.sql.Timestamp.class) {
        // generate "SqlFunctions.internalToTimestamp".
        if (isA(fromType, Primitive.LONG)) {
            return Expressions.call(BuiltInMethod.INTERNAL_TO_TIMESTAMP.method, operand);
        } else {
            return Expressions.convert_(operand, java.sql.Timestamp.class);
        }
    } else if (toType == BigDecimal.class) {
        if (fromBox != null) {
            // Generate "x == null ? null : new BigDecimal(x.intValue())"
            return Expressions.condition(Expressions.equal(operand, RexImpTable.NULL_EXPR), RexImpTable.NULL_EXPR, Expressions.new_(BigDecimal.class, Expressions.unbox(operand, fromBox)));
        }
        if (fromPrimitive != null) {
            // Generate "new BigDecimal(x)"
            return Expressions.new_(BigDecimal.class, operand);
        }
        // Generate "x == null ? null : SqlFunctions.toBigDecimal(x)"
        return Expressions.condition(Expressions.equal(operand, RexImpTable.NULL_EXPR), RexImpTable.NULL_EXPR, Expressions.call(SqlFunctions.class, "toBigDecimal", operand));
    } else if (toType == String.class) {
        if (fromPrimitive != null) {
            switch(fromPrimitive) {
                case DOUBLE:
                case FLOAT:
                    // Generate "SqlFunctions.toString(x)"
                    return Expressions.call(SqlFunctions.class, "toString", operand);
                default:
                    // Generate "Integer.toString(x)"
                    return Expressions.call(fromPrimitive.boxClass, "toString", operand);
            }
        } else if (fromType == BigDecimal.class) {
            // Generate "x.toString()"
            return Expressions.condition(Expressions.equal(operand, RexImpTable.NULL_EXPR), RexImpTable.NULL_EXPR, Expressions.call(SqlFunctions.class, "toString", operand));
        } else {
            // Generate "x == null ? null : x.toString()"
            return Expressions.condition(Expressions.equal(operand, RexImpTable.NULL_EXPR), RexImpTable.NULL_EXPR, Expressions.call(operand, "toString"));
        }
    }
    return Expressions.convert_(operand, toType);
}
Also used : Primitive(org.apache.calcite.linq4j.tree.Primitive) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)1 Primitive (org.apache.calcite.linq4j.tree.Primitive)1 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)1