Search in sources :

Example 11 with ParameterExpression

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

the class InlinerTest method testInlineParameter.

@Test
public void testInlineParameter() {
    ParameterExpression pe = Expressions.parameter(int.class, "p");
    DeclarationStatement decl = Expressions.declare(16, "x", pe);
    b.add(decl);
    b.add(Expressions.return_(null, Expressions.add(decl.parameter, decl.parameter)));
    assertEquals("{\n  return p + p;\n}\n", b.toBlock().toString());
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.Test)

Example 12 with ParameterExpression

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

the class Linq4jTest method testAsQueryable.

@Test
public void testAsQueryable() {
    // "count" is an Enumerable method.
    final int n = Linq4j.asEnumerable(emps).asQueryable().count();
    assertEquals(4, n);
    // "where" is a Queryable method
    // first, use a lambda
    ParameterExpression parameter = Expressions.parameter(Employee.class);
    final Queryable<Employee> nh = Linq4j.asEnumerable(emps).asQueryable().where(Expressions.lambda(Predicate1.class, Expressions.equal(Expressions.field(parameter, Employee.class, "deptno"), Expressions.constant(10)), parameter));
    assertEquals(3, nh.count());
    // second, use an expression
    final Queryable<Employee> nh2 = Linq4j.asEnumerable(emps).asQueryable().where(Expressions.lambda(new Predicate1<Employee>() {

        public boolean apply(Employee v1) {
            return v1.deptno == 10;
        }
    }));
    assertEquals(3, nh2.count());
    // use lambda, this time call whereN
    ParameterExpression parameterE = Expressions.parameter(Employee.class);
    ParameterExpression parameterN = Expressions.parameter(Integer.TYPE);
    final Queryable<Employee> nh3 = Linq4j.asEnumerable(emps).asQueryable().whereN(Expressions.lambda(Predicate2.class, Expressions.andAlso(Expressions.equal(Expressions.field(parameterE, Employee.class, "deptno"), Expressions.constant(10)), Expressions.lessThan(parameterN, Expressions.constant(3))), parameterE, parameterN));
    assertEquals(2, nh3.count());
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Predicate1(org.apache.calcite.linq4j.function.Predicate1) Predicate2(org.apache.calcite.linq4j.function.Predicate2) Test(org.junit.Test)

Example 13 with ParameterExpression

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

the class OptimizerTest method testAssign.

@Test
public 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.Test)

Example 14 with ParameterExpression

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

the class OptimizerTest method testEqualPrimitiveNull.

@Test
public 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.Test)

Example 15 with ParameterExpression

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

the class OptimizerTest method testEqualSameExpr.

@Test
public void testEqualSameExpr() {
    // x == x
    ParameterExpression x = Expressions.parameter(int.class, "x");
    assertEquals("{\n  return true;\n}\n", optimize(Expressions.equal(x, x)));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Aggregations

ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)83 Test (org.junit.Test)41 Expression (org.apache.calcite.linq4j.tree.Expression)38 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)32 ArrayList (java.util.ArrayList)12 RelDataType (org.apache.calcite.rel.type.RelDataType)12 Type (java.lang.reflect.Type)9 Function1 (org.apache.calcite.linq4j.function.Function1)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)8 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)6 Method (java.lang.reflect.Method)5 List (java.util.List)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)5 ClassDeclaration (org.apache.calcite.linq4j.tree.ClassDeclaration)5 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)5 RexNode (org.apache.calcite.rex.RexNode)5