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)))));
}
}
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());
}
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);
}
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));
}
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()));
}
Aggregations