use of org.apache.calcite.adapter.enumerable.PhysType 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.adapter.enumerable.PhysType 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());
}
use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.
the class SparkToEnumerableConverter method implement.
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
// Generate:
// RDD rdd = ...;
// return SparkRuntime.asEnumerable(rdd);
final BlockBuilder list = new BlockBuilder();
final SparkRel child = (SparkRel) getInput();
final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM);
SparkRel.Implementor sparkImplementor = new SparkImplementorImpl(implementor);
final SparkRel.Result result = child.implementSpark(sparkImplementor);
final Expression rdd = list.append("rdd", result.block);
final Expression enumerable = list.append("enumerable", Expressions.call(SparkMethod.AS_ENUMERABLE.method, rdd));
list.add(Expressions.return_(null, enumerable));
return implementor.result(physType, list.toBlock());
}
use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.
the class MongoToEnumerableConverter method implement.
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
// Generates a call to "find" or "aggregate", depending upon whether
// an aggregate is present.
//
// ((MongoTable) schema.getTable("zips")).find(
// "{state: 'CA'}",
// "{city: 1, zipcode: 1}")
//
// ((MongoTable) schema.getTable("zips")).aggregate(
// "{$filter: {state: 'CA'}}",
// "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: "$pop"}}")
final BlockBuilder list = new BlockBuilder();
final MongoRel.Implementor mongoImplementor = new MongoRel.Implementor();
mongoImplementor.visitChild(0, getInput());
int aggCount = 0;
int findCount = 0;
String project = null;
String filter = null;
for (Pair<String, String> op : mongoImplementor.list) {
if (op.left == null) {
++aggCount;
}
if (op.right.startsWith("{$match:")) {
filter = op.left;
++findCount;
}
if (op.right.startsWith("{$project:")) {
project = op.left;
++findCount;
}
}
final RelDataType rowType = getRowType();
final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
final Expression fields = list.append("fields", constantArrayList(Pair.zip(MongoRules.mongoFieldNames(rowType), new AbstractList<Class>() {
@Override
public Class get(int index) {
return physType.fieldClass(index);
}
@Override
public int size() {
return rowType.getFieldCount();
}
}), Pair.class));
final Expression table = list.append("table", mongoImplementor.table.getExpression(MongoTable.MongoQueryable.class));
List<String> opList = Pair.right(mongoImplementor.list);
final Expression ops = list.append("ops", constantArrayList(opList, String.class));
Expression enumerable = list.append("enumerable", Expressions.call(table, MongoMethod.MONGO_QUERYABLE_AGGREGATE.method, fields, ops));
if (CalcitePrepareImpl.DEBUG) {
System.out.println("Mongo: " + opList);
}
Hook.QUERY_PLAN.run(opList);
list.add(Expressions.return_(null, enumerable));
return implementor.result(physType, list.toBlock());
}
use of org.apache.calcite.adapter.enumerable.PhysType in project streamline by hortonworks.
the class RexNodeToJavaCodeCompiler method compileToBlock.
private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context_, ParameterExpression outputValues_) {
RelDataType inputRowType = program.getInputRowType();
final BlockBuilder builder = new BlockBuilder();
final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
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 builder;
}
Aggregations