Search in sources :

Example 1 with ParameterExpression

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

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

the class JaninoRexCompiler method compile.

public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) {
    final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    for (RexNode node : nodes) {
        programBuilder.addProject(node, null);
    }
    final RexProgram program = programBuilder.getProgram();
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
    final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
    final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
    // public void execute(Context, Object[] outputValues)
    final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.field(context_, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
    final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {

        public RexToLixTranslator.InputGetter apply(String a0) {
            throw new UnsupportedOperationException();
        }
    };
    final Expression root = Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
    final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates);
    for (int i = 0; i < list.size(); i++) {
        builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues_, Expressions.constant(i)), list.get(i))));
    }
    return baz(context_, outputValues_, builder.toBlock());
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) Function1(org.apache.calcite.linq4j.function.Function1) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 3 with ParameterExpression

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

the class LinqFrontJdbcBackTest method testTableWhere.

@Test
public void testTableWhere() throws SQLException, ClassNotFoundException {
    final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
    final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c");
    String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer").where(Expressions.<Predicate1<JdbcTest.Customer>>lambda(Expressions.lessThan(Expressions.field(c, "customer_id"), Expressions.constant(5)), c)).toList().toString();
    Util.discard(s);
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 4 with ParameterExpression

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

the class ExpressionTest method testWriteTryCatchFinally.

@Test
public void testWriteTryCatchFinally() {
    final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
    final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
    Node node = Expressions.tryCatchFinally(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.statement(Expressions.call(Expressions.constant("foo"), "toUpperCase")), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.throw_(Expressions.new_(IndexOutOfBoundsException.class))));
    assertEquals("try {\n" + "  return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + "  return null;\n" + "} catch (RuntimeException re) {\n" + "  throw new IndexOutOfBoundsException();\n" + "} finally {\n" + "  \"foo\".toUpperCase();\n" + "}\n", Expressions.toString(node));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Node(org.apache.calcite.linq4j.tree.Node) Test(org.junit.Test)

Example 5 with ParameterExpression

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

the class ExpressionTest method testFor2.

@Test
public void testFor2() throws NoSuchFieldException {
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression i_ = Expressions.parameter(int.class, "i");
    final ParameterExpression j_ = Expressions.parameter(int.class, "j");
    builder.add(Expressions.for_(Arrays.asList(Expressions.declare(0, i_, Expressions.constant(0)), Expressions.declare(0, j_, Expressions.constant(10))), null, null, Expressions.block(Expressions.ifThen(Expressions.lessThan(Expressions.preIncrementAssign(i_), Expressions.preDecrementAssign(j_)), Expressions.break_(null)))));
    assertEquals("{\n" + "  for (int i = 0, j = 10; ; ) {\n" + "    if (++i < --j) {\n" + "      break;\n" + "    }\n" + "  }\n" + "}\n", Expressions.toString(builder.toBlock()));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.Test)

Aggregations

ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)83 Test (org.junit.Test)41 Expression (org.apache.calcite.linq4j.tree.Expression)38 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)32 ArrayList (java.util.ArrayList)12 RelDataType (org.apache.calcite.rel.type.RelDataType)12 Type (java.lang.reflect.Type)9 Function1 (org.apache.calcite.linq4j.function.Function1)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)8 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)6 Method (java.lang.reflect.Method)5 List (java.util.List)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)5 ClassDeclaration (org.apache.calcite.linq4j.tree.ClassDeclaration)5 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)5 RexNode (org.apache.calcite.rex.RexNode)5