Search in sources :

Example 1 with Primitive

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

the class JdbcToEnumerableConverter method generateGet.

private void generateGet(EnumerableRelImplementor implementor, PhysType physType, BlockBuilder builder, ParameterExpression resultSet_, int i, Expression target, Expression calendar_, SqlDialect.CalendarPolicy calendarPolicy) {
    final Primitive primitive = Primitive.ofBoxOr(physType.fieldClass(i));
    final RelDataType fieldType = physType.getRowType().getFieldList().get(i).getType();
    final List<Expression> dateTimeArgs = new ArrayList<Expression>();
    dateTimeArgs.add(Expressions.constant(i + 1));
    SqlTypeName sqlTypeName = fieldType.getSqlTypeName();
    boolean offset = false;
    switch(calendarPolicy) {
        case LOCAL:
            dateTimeArgs.add(calendar_);
            break;
        case NULL:
            // instead use the version of the getXXX that doesn't take a Calendar
            break;
        case DIRECT:
            sqlTypeName = SqlTypeName.ANY;
            break;
        case SHIFT:
            switch(sqlTypeName) {
                case TIMESTAMP:
                case DATE:
                    offset = true;
            }
            break;
    }
    final Expression source;
    switch(sqlTypeName) {
        case DATE:
        case TIME:
        case TIMESTAMP:
            source = Expressions.call(getMethod(sqlTypeName, fieldType.isNullable(), offset), Expressions.<Expression>list().append(Expressions.call(resultSet_, getMethod2(sqlTypeName), dateTimeArgs)).appendIf(offset, getTimeZoneExpression(implementor)));
            break;
        case ARRAY:
            final Expression x = Expressions.convert_(Expressions.call(resultSet_, jdbcGetMethod(primitive), Expressions.constant(i + 1)), java.sql.Array.class);
            source = Expressions.call(BuiltInMethod.JDBC_ARRAY_TO_LIST.method, x);
            break;
        default:
            source = Expressions.call(resultSet_, jdbcGetMethod(primitive), Expressions.constant(i + 1));
    }
    builder.add(Expressions.statement(Expressions.assign(target, source)));
    // object
    if (primitive != null) {
        builder.add(Expressions.ifThen(Expressions.call(resultSet_, "wasNull"), Expressions.statement(Expressions.assign(target, Expressions.constant(null)))));
    }
}
Also used : Primitive(org.apache.calcite.linq4j.tree.Primitive) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 2 with Primitive

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

the class JdbcToSparkConverter method implementSpark.

public SparkRel.Result implementSpark(SparkRel.Implementor implementor) {
    // Generate:
    // ResultSetEnumerable.of(schema.getDataSource(), "select ...")
    final BlockBuilder list = new BlockBuilder();
    final JdbcRel child = (JdbcRel) getInput();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM);
    final JdbcConvention jdbcConvention = (JdbcConvention) child.getConvention();
    String sql = generateSql(jdbcConvention.dialect);
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("[" + sql + "]");
    }
    final Expression sqlLiteral = list.append("sql", Expressions.constant(sql));
    final List<Primitive> primitives = new ArrayList<Primitive>();
    for (int i = 0; i < getRowType().getFieldCount(); i++) {
        final Primitive primitive = Primitive.ofBoxOr(physType.fieldClass(i));
        primitives.add(primitive != null ? primitive : Primitive.OTHER);
    }
    final Expression primitivesLiteral = list.append("primitives", Expressions.constant(primitives.toArray(new Primitive[primitives.size()])));
    final Expression enumerable = list.append("enumerable", Expressions.call(BuiltInMethod.RESULT_SET_ENUMERABLE_OF.method, Expressions.call(Expressions.convert_(jdbcConvention.expression, JdbcSchema.class), BuiltInMethod.JDBC_SCHEMA_DATA_SOURCE.method), sqlLiteral, primitivesLiteral));
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) Primitive(org.apache.calcite.linq4j.tree.Primitive) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Expression(org.apache.calcite.linq4j.tree.Expression) ArrayList(java.util.ArrayList) JdbcRel(org.apache.calcite.adapter.jdbc.JdbcRel) JdbcConvention(org.apache.calcite.adapter.jdbc.JdbcConvention) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 3 with Primitive

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

the class PhysTypeImpl method generateSelector.

public Expression generateSelector(final ParameterExpression parameter, final List<Integer> fields, List<Integer> usedFields, JavaRowFormat targetFormat) {
    final PhysType targetPhysType = project(fields, true, targetFormat);
    final List<Expression> expressions = Lists.newArrayList();
    for (Ord<Integer> ord : Ord.zip(fields)) {
        final Integer field = ord.e;
        if (usedFields.contains(field)) {
            expressions.add(fieldReference(parameter, field));
        } else {
            final Primitive primitive = Primitive.of(targetPhysType.fieldClass(ord.i));
            expressions.add(Expressions.constant(primitive != null ? primitive.defaultValue : null));
        }
    }
    for (Integer field : fields) {
        expressions.add(Expressions.constant(!usedFields.contains(field)));
    }
    return Expressions.lambda(Function1.class, targetPhysType.record(expressions), parameter);
}
Also used : Primitive(org.apache.calcite.linq4j.tree.Primitive) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 4 with Primitive

use of org.apache.calcite.linq4j.tree.Primitive 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)

Example 5 with Primitive

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

the class RexToLixTranslator method translateLiteral.

/**
 * Translates a literal.
 *
 * @throws AlwaysNull if literal is null but {@code nullAs} is
 * {@link org.apache.calcite.adapter.enumerable.RexImpTable.NullAs#NOT_POSSIBLE}.
 */
public static Expression translateLiteral(RexLiteral literal, RelDataType type, JavaTypeFactory typeFactory, RexImpTable.NullAs nullAs) {
    if (literal.isNull()) {
        switch(nullAs) {
            case TRUE:
            case IS_NULL:
                return RexImpTable.TRUE_EXPR;
            case FALSE:
            case IS_NOT_NULL:
                return RexImpTable.FALSE_EXPR;
            case NOT_POSSIBLE:
                throw AlwaysNull.INSTANCE;
            case NULL:
            default:
                return RexImpTable.NULL_EXPR;
        }
    } else {
        switch(nullAs) {
            case IS_NOT_NULL:
                return RexImpTable.TRUE_EXPR;
            case IS_NULL:
                return RexImpTable.FALSE_EXPR;
        }
    }
    Type javaClass = typeFactory.getJavaClass(type);
    final Object value2;
    switch(literal.getType().getSqlTypeName()) {
        case DECIMAL:
            final BigDecimal bd = literal.getValueAs(BigDecimal.class);
            if (javaClass == float.class) {
                return Expressions.constant(bd, javaClass);
            }
            assert javaClass == BigDecimal.class;
            return Expressions.new_(BigDecimal.class, Expressions.constant(bd.toString()));
        case DATE:
        case TIME:
        case TIME_WITH_LOCAL_TIME_ZONE:
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
            value2 = literal.getValueAs(Integer.class);
            javaClass = int.class;
            break;
        case TIMESTAMP:
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        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:
            value2 = literal.getValueAs(Long.class);
            javaClass = long.class;
            break;
        case CHAR:
        case VARCHAR:
            value2 = literal.getValueAs(String.class);
            break;
        case BINARY:
        case VARBINARY:
            return Expressions.new_(ByteString.class, Expressions.constant(literal.getValueAs(byte[].class), byte[].class));
        case SYMBOL:
            value2 = literal.getValueAs(Enum.class);
            javaClass = value2.getClass();
            break;
        default:
            final Primitive primitive = Primitive.ofBoxOr(javaClass);
            final Comparable value = literal.getValueAs(Comparable.class);
            if (primitive != null && value instanceof Number) {
                value2 = primitive.number((Number) value);
            } else {
                value2 = value;
            }
    }
    return Expressions.constant(value2, javaClass);
}
Also used : ExpressionType(org.apache.calcite.linq4j.tree.ExpressionType) Type(java.lang.reflect.Type) RelDataType(org.apache.calcite.rel.type.RelDataType) Primitive(org.apache.calcite.linq4j.tree.Primitive) ByteString(org.apache.calcite.avatica.util.ByteString) BigDecimal(java.math.BigDecimal)

Aggregations

Primitive (org.apache.calcite.linq4j.tree.Primitive)6 ArrayList (java.util.ArrayList)3 Expression (org.apache.calcite.linq4j.tree.Expression)3 BigDecimal (java.math.BigDecimal)2 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)2 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Type (java.lang.reflect.Type)1 PhysType (org.apache.calcite.adapter.enumerable.PhysType)1 JdbcConvention (org.apache.calcite.adapter.jdbc.JdbcConvention)1 JdbcRel (org.apache.calcite.adapter.jdbc.JdbcRel)1 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 ExpressionType (org.apache.calcite.linq4j.tree.ExpressionType)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 Test (org.junit.Test)1