use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class EnumerableWindow method getBlockBuilderWinAggFrameResultContextFunction.
private Function<BlockBuilder, WinAggFrameResultContext> getBlockBuilderWinAggFrameResultContextFunction(final JavaTypeFactory typeFactory, final Result result, final List<Expression> translatedConstants, final Expression comparator_, final Expression rows_, final ParameterExpression i_, final Expression startX, final Expression endX, final Expression minX, final Expression maxX, final Expression hasRows, final Expression frameRowCount, final Expression partitionRowCount, final DeclarationStatement jDecl, final PhysType inputPhysType) {
return new Function<BlockBuilder, WinAggFrameResultContext>() {
public WinAggFrameResultContext apply(final BlockBuilder block) {
return new WinAggFrameResultContext() {
public RexToLixTranslator rowTranslator(Expression rowIndex) {
Expression row = getRow(rowIndex);
final RexToLixTranslator.InputGetter inputGetter = new WindowRelInputGetter(row, inputPhysType, result.physType.getRowType().getFieldCount(), translatedConstants);
return RexToLixTranslator.forAggregation(typeFactory, block, inputGetter);
}
public Expression computeIndex(Expression offset, WinAggImplementor.SeekType seekType) {
Expression index;
if (seekType == WinAggImplementor.SeekType.AGG_INDEX) {
index = jDecl.parameter;
} else if (seekType == WinAggImplementor.SeekType.SET) {
index = i_;
} else if (seekType == WinAggImplementor.SeekType.START) {
index = startX;
} else if (seekType == WinAggImplementor.SeekType.END) {
index = endX;
} else {
throw new IllegalArgumentException("SeekSet " + seekType + " is not supported");
}
if (!Expressions.constant(0).equals(offset)) {
index = block.append("idx", Expressions.add(index, offset));
}
return index;
}
private Expression checkBounds(Expression rowIndex, Expression minIndex, Expression maxIndex) {
if (rowIndex == i_ || rowIndex == startX || rowIndex == endX) {
// No additional bounds check required
return hasRows;
}
// noinspection UnnecessaryLocalVariable
Expression res = block.append("rowInFrame", Expressions.foldAnd(ImmutableList.of(hasRows, Expressions.greaterThanOrEqual(rowIndex, minIndex), Expressions.lessThanOrEqual(rowIndex, maxIndex))));
return res;
}
public Expression rowInFrame(Expression rowIndex) {
return checkBounds(rowIndex, startX, endX);
}
public Expression rowInPartition(Expression rowIndex) {
return checkBounds(rowIndex, minX, maxX);
}
public Expression compareRows(Expression a, Expression b) {
return Expressions.call(comparator_, BuiltInMethod.COMPARATOR_COMPARE.method, getRow(a), getRow(b));
}
public Expression getRow(Expression rowIndex) {
return block.append("jRow", RexToLixTranslator.convert(Expressions.arrayIndex(rows_, rowIndex), inputPhysType.getJavaRowType()));
}
public Expression index() {
return i_;
}
public Expression startIndex() {
return startX;
}
public Expression endIndex() {
return endX;
}
public Expression hasRows() {
return hasRows;
}
public Expression getFrameRowCount() {
return frameRowCount;
}
public Expression getPartitionRowCount() {
return partitionRowCount;
}
};
}
};
}
use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class JdbcToEnumerableConverter method implement.
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
// Generate:
// ResultSetEnumerable.of(schema.getDataSource(), "select ...")
final BlockBuilder builder0 = new BlockBuilder(false);
final JdbcRel child = (JdbcRel) getInput();
final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.prefer(JavaRowFormat.CUSTOM));
final JdbcConvention jdbcConvention = (JdbcConvention) child.getConvention();
String sql = generateSql(jdbcConvention.dialect);
if (CalcitePrepareImpl.DEBUG) {
System.out.println("[" + sql + "]");
}
Hook.QUERY_PLAN.run(sql);
final Expression sql_ = builder0.append("sql", Expressions.constant(sql));
final int fieldCount = getRowType().getFieldCount();
BlockBuilder builder = new BlockBuilder();
final ParameterExpression resultSet_ = Expressions.parameter(Modifier.FINAL, ResultSet.class, builder.newName("resultSet"));
final SqlDialect.CalendarPolicy calendarPolicy = jdbcConvention.dialect.getCalendarPolicy();
final Expression calendar_;
switch(calendarPolicy) {
case LOCAL:
calendar_ = builder0.append("calendar", Expressions.call(Calendar.class, "getInstance", getTimeZoneExpression(implementor)));
break;
default:
calendar_ = null;
}
if (fieldCount == 1) {
final ParameterExpression value_ = Expressions.parameter(Object.class, builder.newName("value"));
builder.add(Expressions.declare(Modifier.FINAL, value_, null));
generateGet(implementor, physType, builder, resultSet_, 0, value_, calendar_, calendarPolicy);
builder.add(Expressions.return_(null, value_));
} else {
final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(fieldCount)));
for (int i = 0; i < fieldCount; i++) {
generateGet(implementor, physType, builder, resultSet_, i, Expressions.arrayIndex(values_, Expressions.constant(i)), calendar_, calendarPolicy);
}
builder.add(Expressions.return_(null, values_));
}
final ParameterExpression e_ = Expressions.parameter(SQLException.class, builder.newName("e"));
final Expression rowBuilderFactory_ = builder0.append("rowBuilderFactory", Expressions.lambda(Expressions.block(Expressions.return_(null, Expressions.lambda(Expressions.block(Expressions.tryCatch(builder.toBlock(), Expressions.catch_(e_, Expressions.throw_(Expressions.new_(RuntimeException.class, e_)))))))), resultSet_));
final Expression enumerable = builder0.append("enumerable", Expressions.call(BuiltInMethod.RESULT_SET_ENUMERABLE_OF.method, Expressions.call(Schemas.unwrap(jdbcConvention.expression, JdbcSchema.class), BuiltInMethod.JDBC_SCHEMA_DATA_SOURCE.method), sql_, rowBuilderFactory_));
builder0.add(Expressions.return_(null, enumerable));
return implementor.result(physType, builder0.toBlock());
}
use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class RelOptTableImpl method create.
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, final CalciteSchema.TableEntry tableEntry, Double rowCount) {
final Table table = tableEntry.getTable();
Function<Class, Expression> expressionFunction = getClassExpressionFunction(tableEntry, table);
return new RelOptTableImpl(schema, rowType, tableEntry.path(), table, expressionFunction, rowCount);
}
use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class RelOptTableImpl method create.
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, Table table, Path path) {
final SchemaPlus schemaPlus = MySchemaPlus.create(path);
Function<Class, Expression> expressionFunction = getClassExpressionFunction(schemaPlus, Util.last(path).left, table);
return new RelOptTableImpl(schema, rowType, Pair.left(path), table, expressionFunction, table.getStatistic().getRowCount());
}
use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class EnumerableToSparkConverter method implementSpark.
public Result implementSpark(Implementor implementor) {
// Generate:
// Enumerable source = ...;
// return SparkRuntime.createRdd(sparkContext, source);
final BlockBuilder list = new BlockBuilder();
final EnumerableRel child = (EnumerableRel) getInput();
final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM);
// TODO:
final Expression source = null;
final Expression sparkContext = Expressions.call(SparkMethod.GET_SPARK_CONTEXT.method, implementor.getRootExpression());
final Expression rdd = list.append("rdd", Expressions.call(SparkMethod.CREATE_RDD.method, sparkContext, source));
list.add(Expressions.return_(null, rdd));
return implementor.result(physType, list.toBlock());
}
Aggregations