Search in sources :

Example 6 with Expression

use of org.apache.calcite.linq4j.tree.Expression 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());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) TreeSet(java.util.TreeSet) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Shuttle(org.apache.calcite.linq4j.tree.Shuttle) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 7 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class ExpressionTest method testWrite.

@Test
public void testWrite() {
    assertEquals("1 + 2.0F + 3L + Long.valueOf(4L)", Expressions.toString(Expressions.add(Expressions.add(Expressions.add(Expressions.constant(1), Expressions.constant(2F, Float.TYPE)), Expressions.constant(3L, Long.TYPE)), Expressions.constant(4L, Long.class))));
    assertEquals("new java.math.BigDecimal(31415926L, 7)", Expressions.toString(Expressions.constant(BigDecimal.valueOf(314159260, 8))));
    // Parentheses needed, to override the left-associativity of +.
    assertEquals("1 + (2 + 3)", Expressions.toString(Expressions.add(Expressions.constant(1), Expressions.add(Expressions.constant(2), Expressions.constant(3)))));
    // No parentheses needed; higher precedence of * achieves the desired
    // effect.
    assertEquals("1 + 2 * 3", Expressions.toString(Expressions.add(Expressions.constant(1), Expressions.multiply(Expressions.constant(2), Expressions.constant(3)))));
    assertEquals("1 * (2 + 3)", Expressions.toString(Expressions.multiply(Expressions.constant(1), Expressions.add(Expressions.constant(2), Expressions.constant(3)))));
    // Parentheses needed, to overcome right-associativity of =.
    assertEquals("(1 = 2) = 3", Expressions.toString(Expressions.assign(Expressions.assign(Expressions.constant(1), Expressions.constant(2)), Expressions.constant(3))));
    // Ternary operator.
    assertEquals("1 < 2 ? (3 < 4 ? 5 : 6) : 7 < 8 ? 9 : 10", Expressions.toString(Expressions.condition(Expressions.lessThan(Expressions.constant(1), Expressions.constant(2)), Expressions.condition(Expressions.lessThan(Expressions.constant(3), Expressions.constant(4)), Expressions.constant(5), Expressions.constant(6)), Expressions.condition(Expressions.lessThan(Expressions.constant(7), Expressions.constant(8)), Expressions.constant(9), Expressions.constant(10)))));
    assertEquals("0 + (double) (2 + 3)", Expressions.toString(Expressions.add(Expressions.constant(0), Expressions.convert_(Expressions.add(Expressions.constant(2), Expressions.constant(3)), Double.TYPE))));
    // "--5" would be a syntax error
    assertEquals("- - 5", Expressions.toString(Expressions.negate(Expressions.negate(Expressions.constant(5)))));
    assertEquals("a.empno", Expressions.toString(Expressions.field(Expressions.parameter(Linq4jTest.Employee.class, "a"), "empno")));
    assertEquals("a.length", Expressions.toString(Expressions.field(Expressions.parameter(Object[].class, "a"), "length")));
    assertEquals("java.util.Collections.EMPTY_LIST", Expressions.toString(Expressions.field(null, Collections.class, "EMPTY_LIST")));
    final ParameterExpression paramX = Expressions.parameter(String.class, "x");
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public int apply(String x) {\n" + "    return x.length();\n" + "  }\n" + "  public Object apply(Object x) {\n" + "    return apply(\n" + "      (String) x);\n" + "  }\n" + "}\n", Expressions.toString(Expressions.lambda(Function1.class, Expressions.call(paramX, "length", Collections.<Expression>emptyList()), Arrays.asList(paramX))));
    // 1-dimensional array with initializer
    assertEquals("new String[] {\n" + "  \"foo\",\n" + "  null,\n" + "  \"bar\\\"baz\"}", Expressions.toString(Expressions.newArrayInit(String.class, Expressions.constant("foo"), Expressions.constant(null), Expressions.constant("bar\"baz"))));
    // 2-dimensional array with initializer
    assertEquals("new String[][] {\n" + "  new String[] {\n" + "    \"foo\",\n" + "    \"bar\"},\n" + "  null,\n" + "  new String[] {\n" + "    null}}", Expressions.toString(Expressions.newArrayInit(String.class, 2, Expressions.constant(new String[] { "foo", "bar" }), Expressions.constant(null), Expressions.constant(new String[] { null }))));
    // 1-dimensional array
    assertEquals("new String[x + 1]", Expressions.toString(Expressions.newArrayBounds(String.class, 1, Expressions.add(Expressions.parameter(0, int.class, "x"), Expressions.constant(1)))));
    // 3-dimensional array
    assertEquals("new String[x + 1][][]", Expressions.toString(Expressions.newArrayBounds(String.class, 3, Expressions.add(Expressions.parameter(0, int.class, "x"), Expressions.constant(1)))));
    assertEquals("(int) ((String) (Object) \"foo\").length()", Expressions.toString(Expressions.convert_(Expressions.call(Expressions.convert_(Expressions.convert_(Expressions.constant("foo"), Object.class), String.class), "length", Collections.<Expression>emptyList()), Integer.TYPE)));
    // resolving a static method
    assertEquals("Integer.valueOf(\"0123\")", Expressions.toString(Expressions.call(Integer.class, "valueOf", Collections.<Expression>singletonList(Expressions.constant("0123")))));
    // precedence of not and instanceof
    assertEquals("!(o instanceof String)", Expressions.toString(Expressions.not(Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class))));
    // not not
    assertEquals("!!(o instanceof String)", Expressions.toString(Expressions.not(Expressions.not(Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class)))));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 8 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class InlinerTest method testMultiPassOptimization.

@Test
public void testMultiPassOptimization() {
    // int t = u + v;
    // boolean b = t > 1 ? true : true; -- optimized out, thus t can be inlined
    // return b ? t : 2
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression u = Expressions.parameter(int.class, "u");
    final ParameterExpression v = Expressions.parameter(int.class, "v");
    Expression t = builder.append("t", Expressions.add(u, v));
    Expression b = builder.append("b", Expressions.condition(Expressions.greaterThan(t, ONE), TRUE, TRUE));
    builder.add(Expressions.return_(null, Expressions.condition(b, t, TWO)));
    assertEquals("{\n" + "  return u + v;\n" + "}\n", Expressions.toString(builder.toBlock()));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.Test)

Example 9 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class InlinerTest method checkAssignInConditionOptimizedOut.

void checkAssignInConditionOptimizedOut(int modifiers, String s) {
    // int t;
    // return (t = 1) != a ? b : c
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression t = Expressions.parameter(int.class, "t");
    builder.add(Expressions.declare(modifiers, t, null));
    Expression v = builder.append("v", Expressions.makeTernary(ExpressionType.Conditional, Expressions.makeBinary(ExpressionType.NotEqual, Expressions.assign(t, Expressions.constant(1)), Expressions.parameter(int.class, "a")), Expressions.parameter(int.class, "b"), Expressions.parameter(int.class, "c")));
    builder.add(Expressions.return_(null, v));
    assertThat(Expressions.toString(builder.toBlock()), CoreMatchers.equalTo(s));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 10 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class OptimizerTest method testConditionalIfBoolTrue.

@Test
public void testConditionalIfBoolTrue() {
    // if (bool) {return 1} else if (true) {return 2}
    Expression bool = Expressions.parameter(boolean.class, "bool");
    assertEquals("{\n" + "  if (bool) {\n" + "    return 1;\n" + "  } else {\n" + "    return 2;\n" + "  }\n" + "}\n", optimize(Expressions.ifThenElse(bool, Expressions.return_(null, ONE), TRUE, Expressions.return_(null, TWO))));
}
Also used : ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Aggregations

Expression (org.apache.calcite.linq4j.tree.Expression)91 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)64 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)56 ArrayList (java.util.ArrayList)22 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)19 RelDataType (org.apache.calcite.rel.type.RelDataType)19 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)16 Test (org.junit.Test)16 Type (java.lang.reflect.Type)11 PhysType (org.apache.calcite.adapter.enumerable.PhysType)11 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)11 RexNode (org.apache.calcite.rex.RexNode)10 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)9 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)8 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)8 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)8 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)7 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5