use of org.apache.calcite.linq4j.tree.Shuttle 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.Shuttle in project calcite by apache.
the class ExpressionTest method testClassDecl.
@Test
public void testClassDecl() {
final NewExpression newExpression = Expressions.new_(Object.class, Collections.<Expression>emptyList(), Arrays.<MemberDeclaration>asList(Expressions.fieldDecl(Modifier.PUBLIC | Modifier.FINAL, Expressions.parameter(String.class, "foo"), Expressions.constant("bar")), new ClassDeclaration(Modifier.PUBLIC | Modifier.STATIC, "MyClass", null, Collections.<Type>emptyList(), Arrays.<MemberDeclaration>asList(new FieldDeclaration(0, Expressions.parameter(int.class, "x"), Expressions.constant(0)))), Expressions.fieldDecl(0, Expressions.parameter(int.class, "i"))));
assertEquals("new Object(){\n" + " public final String foo = \"bar\";\n" + " public static class MyClass {\n" + " int x = 0;\n" + " }\n" + " int i;\n" + "}", Expressions.toString(newExpression));
newExpression.accept(new Shuttle());
}
use of org.apache.calcite.linq4j.tree.Shuttle in project calcite by apache.
the class ExpressionTest method testConstantExpression.
@Test
public void testConstantExpression() {
final Expression constant = Expressions.constant(new Object[] { 1, new Object[] { (byte) 1, (short) 2, (int) 3, (long) 4, (float) 5, (double) 6, (char) 7, true, "string", null }, new AllType(true, (byte) 100, (char) 101, (short) 102, 103, (long) 104, (float) 105, (double) 106, new BigDecimal(107), new BigInteger("108"), "109", null) });
assertEquals("new Object[] {\n" + " 1,\n" + " new Object[] {\n" + " (byte)1,\n" + " (short)2,\n" + " 3,\n" + " 4L,\n" + " 5.0F,\n" + " 6.0D,\n" + " (char)7,\n" + " true,\n" + " \"string\",\n" + " null},\n" + " new org.apache.calcite.linq4j.test.ExpressionTest.AllType(\n" + " true,\n" + " (byte)100,\n" + " (char)101,\n" + " (short)102,\n" + " 103,\n" + " 104L,\n" + " 105.0F,\n" + " 106.0D,\n" + " new java.math.BigDecimal(107L),\n" + " new java.math.BigInteger(\"108\"),\n" + " \"109\",\n" + " null)}", constant.toString());
constant.accept(new Shuttle());
}
use of org.apache.calcite.linq4j.tree.Shuttle in project calcite by apache.
the class ExpressionTest method checkBlockBuilder.
public void checkBlockBuilder(boolean optimizing, String expected) {
BlockBuilder statements = new BlockBuilder(optimizing);
Expression one = statements.append("one", Expressions.constant(1));
Expression two = statements.append("two", Expressions.constant(2));
Expression three = statements.append("three", Expressions.add(one, two));
Expression six = statements.append("six", Expressions.multiply(three, two));
Expression nine = statements.append("nine", Expressions.multiply(three, three));
Expression eighteen = statements.append("eighteen", Expressions.add(Expressions.add(three, six), nine));
statements.add(Expressions.return_(null, eighteen));
BlockStatement expression = statements.toBlock();
assertEquals(expected, Expressions.toString(expression));
expression.accept(new Shuttle());
}
use of org.apache.calcite.linq4j.tree.Shuttle in project calcite by apache.
the class ExpressionTest method testBlockBuilder3.
@Test
public void testBlockBuilder3() {
/*
int a = 1;
int b = a + 2;
int c = a + 3;
int d = a + 4;
int e = {
int b = a + 3;
foo(b);
}
bar(a, b, c, d, e);
*/
BlockBuilder builder0 = new BlockBuilder();
final Expression a = builder0.append("_a", Expressions.constant(1));
final Expression b = builder0.append("_b", Expressions.add(a, Expressions.constant(2)));
final Expression c = builder0.append("_c", Expressions.add(a, Expressions.constant(3)));
final Expression d = builder0.append("_d", Expressions.add(a, Expressions.constant(4)));
BlockBuilder builder1 = new BlockBuilder();
final Expression b1 = builder1.append("_b", Expressions.add(a, Expressions.constant(3)));
builder1.add(Expressions.statement(Expressions.call(ExpressionTest.class, "foo", b1)));
final Expression e = builder0.append("e", builder1.toBlock());
builder0.add(Expressions.statement(Expressions.call(ExpressionTest.class, "bar", a, b, c, d, e)));
// With the bug in BlockBuilder.append(String, BlockExpression),
// bar(1, _b, _c, _d, foo(_d));
// Correct result is
// bar(1, _b, _c, _d, foo(_c));
// because _c has the same expression (a + 3) as inner b.
BlockStatement expression = builder0.toBlock();
assertEquals("{\n" + " final int _b = 1 + 2;\n" + " final int _c = 1 + 3;\n" + " final int _d = 1 + 4;\n" + " org.apache.calcite.linq4j.test.ExpressionTest.bar(1, _b, _c, _d, org.apache.calcite.linq4j.test.ExpressionTest.foo(_c));\n" + "}\n", Expressions.toString(expression));
expression.accept(new Shuttle());
}
Aggregations