use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class JdbcSchema method create.
public static JdbcSchema create(SchemaPlus parentSchema, String name, DataSource dataSource, SqlDialectFactory dialectFactory, String catalog, String schema) {
final Expression expression = Schemas.subSchemaExpression(parentSchema, name, JdbcSchema.class);
final SqlDialect dialect = createDialect(dialectFactory, dataSource);
final JdbcConvention convention = JdbcConvention.of(dialect, expression, name);
return new JdbcSchema(dataSource, dialect, convention, catalog, schema);
}
use of org.apache.calcite.linq4j.tree.Expression 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.Expression 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.Expression in project calcite by apache.
the class BlockBuilderTest method testReuseExpressionsFromUpperLevel.
@Test
public void testReuseExpressionsFromUpperLevel() {
Expression x = b.append("x", Expressions.add(ONE, TWO));
BlockBuilder nested = new BlockBuilder(true, b);
Expression y = nested.append("y", Expressions.add(ONE, TWO));
nested.add(Expressions.return_(null, Expressions.add(y, y)));
b.add(nested.toBlock());
assertEquals("{\n" + " final int x = 1 + 2;\n" + " {\n" + " return x + x;\n" + " }\n" + "}\n", b.toBlock().toString());
}
use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class BlockBuilderTest method testTestCustomOptimizer.
@Test
public void testTestCustomOptimizer() {
BlockBuilder b = new BlockBuilder() {
@Override
protected Shuttle createOptimizeShuttle() {
return new OptimizeShuttle() {
@Override
public Expression visit(BinaryExpression binary, Expression expression0, Expression expression1) {
if (binary.getNodeType() == ExpressionType.Add && ONE.equals(expression0) && TWO.equals(expression1)) {
return FOUR;
}
return super.visit(binary, expression0, expression1);
}
};
}
};
b.add(Expressions.return_(null, Expressions.add(ONE, TWO)));
assertEquals("{\n return 4;\n}\n", b.toBlock().toString());
}
Aggregations