use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.
the class ExpressionTest method testBlockBuilder2.
@Test
public void testBlockBuilder2() {
BlockBuilder statements = new BlockBuilder();
Expression element = statements.append("element", Expressions.constant(null));
Expression comparator = statements.append("comparator", Expressions.constant(null, Comparator.class));
Expression treeSet = statements.append("treeSet", Expressions.new_(TreeSet.class, Arrays.asList(comparator)));
statements.add(Expressions.return_(null, Expressions.call(treeSet, "add", element)));
BlockStatement expression = statements.toBlock();
final String expected = "{\n" + " final java.util.TreeSet treeSet = new java.util.TreeSet(\n" + " (java.util.Comparator) null);\n" + " return treeSet.add(null);\n" + "}\n";
assertThat(Expressions.toString(expression), is(expected));
expression.accept(new Shuttle());
}
use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.
the class ExpressionTest method testWriteAnonymousClass.
@Test
public void testWriteAnonymousClass() {
// final List<String> baz = Arrays.asList("foo", "bar");
// new AbstractList<String>() {
// public int size() {
// return baz.size();
// }
// public String get(int index) {
// return ((String) baz.get(index)).toUpperCase();
// }
// }
final ParameterExpression bazParameter = Expressions.parameter(Types.of(List.class, String.class), "baz");
final ParameterExpression indexParameter = Expressions.parameter(Integer.TYPE, "index");
BlockStatement e = Expressions.block(Expressions.declare(Modifier.FINAL, bazParameter, Expressions.call(Arrays.class, "asList", Arrays.<Expression>asList(Expressions.constant("foo"), Expressions.constant("bar")))), Expressions.statement(Expressions.new_(Types.of(AbstractList.class, String.class), Collections.<Expression>emptyList(), Arrays.<MemberDeclaration>asList(Expressions.fieldDecl(Modifier.PUBLIC | Modifier.FINAL, Expressions.parameter(String.class, "qux"), Expressions.constant("xyzzy")), Expressions.methodDecl(Modifier.PUBLIC, Integer.TYPE, "size", Collections.<ParameterExpression>emptyList(), Blocks.toFunctionBlock(Expressions.call(bazParameter, "size", Collections.<Expression>emptyList()))), Expressions.methodDecl(Modifier.PUBLIC, String.class, "get", Arrays.asList(indexParameter), Blocks.toFunctionBlock(Expressions.call(Expressions.convert_(Expressions.call(bazParameter, "get", Arrays.<Expression>asList(indexParameter)), String.class), "toUpperCase", Collections.<Expression>emptyList())))))));
assertEquals("{\n" + " final java.util.List<String> baz = java.util.Arrays.asList(\"foo\", \"bar\");\n" + " new java.util.AbstractList<String>(){\n" + " public final String qux = \"xyzzy\";\n" + " public int size() {\n" + " return baz.size();\n" + " }\n" + "\n" + " public String get(int index) {\n" + " return ((String) baz.get(index)).toUpperCase();\n" + " }\n" + "\n" + " };\n" + "}\n", Expressions.toString(e));
}
use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.
the class BlockBuilderBase method optimizeStatement.
public static BlockStatement optimizeStatement(Statement statement) {
BlockBuilder b = new BlockBuilder(true);
if (!(statement instanceof BlockStatement)) {
b.add(statement);
} else {
BlockStatement bs = (BlockStatement) statement;
for (Statement stmt : bs.statements) {
b.add(stmt);
}
}
BlockStatement bs = b.toBlock();
return bs;
}
use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.
the class StrictAggImplementor method implementResult.
public final Expression implementResult(AggContext info, final AggResultContext result) {
if (!needTrackEmptySet) {
return RexToLixTranslator.convert(implementNotNullResult(info, result), info.returnType());
}
String tmpName = result.accumulator().isEmpty() ? "ar" : (result.accumulator().get(0) + "$Res");
ParameterExpression res = Expressions.parameter(0, info.returnType(), result.currentBlock().newName(tmpName));
List<Expression> acc = result.accumulator();
final BlockBuilder thenBlock = result.nestBlock();
Expression nonNull = RexToLixTranslator.convert(implementNotNullResult(info, result), info.returnType());
result.exitBlock();
thenBlock.add(Expressions.statement(Expressions.assign(res, nonNull)));
BlockStatement thenBranch = thenBlock.toBlock();
Expression seenNotNullRows = trackNullsPerRow ? acc.get(acc.size() - 1) : ((WinAggResultContext) result).hasRows();
if (thenBranch.statements.size() == 1) {
return Expressions.condition(seenNotNullRows, nonNull, RexImpTable.getDefaultValue(res.getType()));
}
result.currentBlock().add(Expressions.declare(0, res, null));
result.currentBlock().add(Expressions.ifThenElse(seenNotNullRows, thenBranch, Expressions.statement(Expressions.assign(res, RexImpTable.getDefaultValue(res.getType())))));
return res;
}
use of org.apache.calcite.linq4j.tree.BlockStatement in project calcite by apache.
the class EnumerableCalc method implement.
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
final JavaTypeFactory typeFactory = implementor.getTypeFactory();
final BlockBuilder builder = new BlockBuilder();
final EnumerableRel child = (EnumerableRel) getInput();
final Result result = implementor.visitChild(this, 0, child, pref);
final PhysType physType = PhysTypeImpl.of(typeFactory, getRowType(), pref.prefer(result.format));
// final Enumerable<Employee> inputEnumerable = <<child adapter>>;
// return new Enumerable<IntString>() {
// Enumerator<IntString> enumerator() {
// return new Enumerator<IntString>() {
// public void reset() {
// ...
Type outputJavaType = physType.getJavaRowType();
final Type enumeratorType = Types.of(Enumerator.class, outputJavaType);
Type inputJavaType = result.physType.getJavaRowType();
ParameterExpression inputEnumerator = Expressions.parameter(Types.of(Enumerator.class, inputJavaType), "inputEnumerator");
Expression input = RexToLixTranslator.convert(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_CURRENT.method), inputJavaType);
final RexBuilder rexBuilder = getCluster().getRexBuilder();
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RelOptPredicateList predicates = mq.getPulledUpPredicates(child);
final RexSimplify simplify = new RexSimplify(rexBuilder, predicates, false, RexUtil.EXECUTOR);
final RexProgram program = this.program.normalize(rexBuilder, simplify);
BlockStatement moveNextBody;
if (program.getCondition() == null) {
moveNextBody = Blocks.toFunctionBlock(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_MOVE_NEXT.method));
} else {
final BlockBuilder builder2 = new BlockBuilder();
Expression condition = RexToLixTranslator.translateCondition(program, typeFactory, builder2, new RexToLixTranslator.InputGetterImpl(Collections.singletonList(Pair.of(input, result.physType))), implementor.allCorrelateVariables);
builder2.add(Expressions.ifThen(condition, Expressions.return_(null, Expressions.constant(true))));
moveNextBody = Expressions.block(Expressions.while_(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_MOVE_NEXT.method), builder2.toBlock()), Expressions.return_(null, Expressions.constant(false)));
}
final BlockBuilder builder3 = new BlockBuilder();
List<Expression> expressions = RexToLixTranslator.translateProjects(program, typeFactory, builder3, physType, DataContext.ROOT, new RexToLixTranslator.InputGetterImpl(Collections.singletonList(Pair.of(input, result.physType))), implementor.allCorrelateVariables);
builder3.add(Expressions.return_(null, physType.record(expressions)));
BlockStatement currentBody = builder3.toBlock();
final Expression inputEnumerable = builder.append("inputEnumerable", result.block, false);
final Expression body = Expressions.new_(enumeratorType, NO_EXPRS, Expressions.list(Expressions.fieldDecl(Modifier.PUBLIC | Modifier.FINAL, inputEnumerator, Expressions.call(inputEnumerable, BuiltInMethod.ENUMERABLE_ENUMERATOR.method)), EnumUtils.overridingMethodDecl(BuiltInMethod.ENUMERATOR_RESET.method, NO_PARAMS, Blocks.toFunctionBlock(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_RESET.method))), EnumUtils.overridingMethodDecl(BuiltInMethod.ENUMERATOR_MOVE_NEXT.method, NO_PARAMS, moveNextBody), EnumUtils.overridingMethodDecl(BuiltInMethod.ENUMERATOR_CLOSE.method, NO_PARAMS, Blocks.toFunctionBlock(Expressions.call(inputEnumerator, BuiltInMethod.ENUMERATOR_CLOSE.method))), Expressions.methodDecl(Modifier.PUBLIC, BRIDGE_METHODS ? Object.class : outputJavaType, "current", NO_PARAMS, currentBody)));
builder.add(Expressions.return_(null, Expressions.new_(BuiltInMethod.ABSTRACT_ENUMERABLE_CTOR.constructor, // Collections.singletonList(inputRowType),
NO_EXPRS, ImmutableList.<MemberDeclaration>of(Expressions.methodDecl(Modifier.PUBLIC, enumeratorType, BuiltInMethod.ENUMERABLE_ENUMERATOR.method.getName(), NO_PARAMS, Blocks.toFunctionBlock(body))))));
return implementor.result(physType, builder.toBlock());
}
Aggregations