Search in sources :

Example 51 with ParameterExpression

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

the class OptimizerTest method testEqualPrimitiveNull.

@Test
void testEqualPrimitiveNull() {
    // (int) x == null
    ParameterExpression x = Expressions.parameter(int.class, "x");
    assertEquals("{\n  return false;\n}\n", optimize(Expressions.equal(x, NULL)));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Example 52 with ParameterExpression

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

the class OptimizerTest method testAssign.

@Test
void testAssign() {
    // long x = 0;
    // final long y = System.currentTimeMillis();
    // if (System.nanoTime() > 0) {
    // x = y;
    // }
    // System.out.println(x);
    // 
    // In bug https://github.com/julianhyde/linq4j/issues/27, this was
    // incorrectly optimized to
    // 
    // if (System.nanoTime() > 0L) {
    // System.currentTimeMillis();
    // }
    // System.out.println(0L);
    final ParameterExpression x_ = Expressions.parameter(long.class, "x");
    final ParameterExpression y_ = Expressions.parameter(long.class, "y");
    final Method mT = Linq4j.getMethod("java.lang.System", "currentTimeMillis");
    final Method mNano = Linq4j.getMethod("java.lang.System", "nanoTime");
    final ConstantExpression zero = Expressions.constant(0L);
    assertThat(optimize(Expressions.block(Expressions.declare(0, x_, zero), Expressions.declare(Modifier.FINAL, y_, Expressions.call(mT)), Expressions.ifThen(Expressions.greaterThan(Expressions.call(mNano), zero), Expressions.statement(Expressions.assign(x_, y_))), Expressions.statement(Expressions.call(Expressions.field(null, System.class, "out"), "println", x_)))), equalTo("{\n" + "  long x = 0L;\n" + "  if (System.nanoTime() > 0L) {\n" + "    x = System.currentTimeMillis();\n" + "  }\n" + "  System.out.println(x);\n" + "}\n"));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 53 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsTwoArgMethod.

@Test
void testLambdaCallsTwoArgMethod() throws NoSuchMethodException {
    // A parameter for the lambda expression.
    ParameterExpression paramS = Expressions.parameter(String.class, "s");
    ParameterExpression paramBegin = Expressions.parameter(Integer.TYPE, "begin");
    ParameterExpression paramEnd = Expressions.parameter(Integer.TYPE, "end");
    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.call(paramS, String.class.getMethod("substring", Integer.TYPE, Integer.TYPE), paramBegin, paramEnd), paramS, paramBegin, paramEnd);
    // Compile and run the lambda expression.
    String s = (String) lambdaExpr.compile().dynamicInvoke("hello world", 3, 7);
    assertEquals("lo w", s);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Example 54 with ParameterExpression

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

the class ExpressionTest method testWriteAnonymousClass.

@Test
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.emptyList(), Arrays.asList(Expressions.fieldDecl(Modifier.PUBLIC | Modifier.FINAL, Expressions.parameter(String.class, "qux"), Expressions.constant("xyzzy")), Expressions.methodDecl(Modifier.PUBLIC, Integer.TYPE, "size", Collections.emptyList(), Blocks.toFunctionBlock(Expressions.call(bazParameter, "size", Collections.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", ImmutableList.of())))))));
    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));
}
Also used : AbstractList(java.util.AbstractList) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) AbstractList(java.util.AbstractList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Test(org.junit.jupiter.api.Test)

Example 55 with ParameterExpression

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

the class ExpressionTest method testWrite.

@Test
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("java.math.BigDecimal.valueOf(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.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.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 : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)144 Expression (org.apache.calcite.linq4j.tree.Expression)80 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)58 Test (org.junit.jupiter.api.Test)51 ArrayList (java.util.ArrayList)30 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)30 RelDataType (org.apache.calcite.rel.type.RelDataType)30 Type (java.lang.reflect.Type)27 RexNode (org.apache.calcite.rex.RexNode)23 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)19 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)11 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)11 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 EnumUtils.generateCollatorExpression (org.apache.calcite.adapter.enumerable.EnumUtils.generateCollatorExpression)10 PhysType (org.apache.calcite.adapter.enumerable.PhysType)10 Function1 (org.apache.calcite.linq4j.function.Function1)10 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)10