Search in sources :

Example 96 with ParameterExpression

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

the class ExpressionTest method testSubExpressionElimination.

/**
 * Test for common sub-expression elimination.
 */
@Test
void testSubExpressionElimination() {
    final BlockBuilder builder = new BlockBuilder(true);
    ParameterExpression x = Expressions.parameter(Object.class, "p");
    Expression current4 = builder.append("current4", Expressions.convert_(x, Object[].class));
    Expression v = builder.append("v", Expressions.convert_(Expressions.arrayIndex(current4, Expressions.constant(4)), Short.class));
    Expression v0 = builder.append("v0", Expressions.convert_(v, Number.class));
    Expression v1 = builder.append("v1", Expressions.convert_(Expressions.arrayIndex(current4, Expressions.constant(4)), Short.class));
    Expression v2 = builder.append("v2", Expressions.convert_(v, Number.class));
    Expression v3 = builder.append("v3", Expressions.convert_(Expressions.arrayIndex(current4, Expressions.constant(4)), Short.class));
    Expression v4 = builder.append("v4", Expressions.convert_(v3, Number.class));
    Expression v5 = builder.append("v5", Expressions.call(v4, "intValue"));
    Expression v6 = builder.append("v6", Expressions.condition(Expressions.equal(v2, Expressions.constant(null)), Expressions.constant(null), Expressions.equal(v5, Expressions.constant(1997))));
    builder.add(Expressions.return_(null, v6));
    assertEquals("{\n" + "  final Short v = (Short) ((Object[]) p)[4];\n" + "  return (Number) v == null ? (Boolean) null : (" + "(Number) v).intValue() == 1997;\n" + "}\n", Expressions.toString(builder.toBlock()));
}
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) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.jupiter.api.Test)

Example 97 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpLong.

@Test
void testLambdaCallsBinaryOpLong() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Long.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 1L to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2L)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public long apply(long arg) {\n" + "    return arg + 2L;\n" + "  }\n" + "  public Object apply(Long arg) {\n" + "    return apply(\n" + "      arg.longValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Long) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 1L.
    long n = (Long) lambdaExpr.compile().dynamicInvoke(1L);
    // This code example produces the following output:
    // 
    // arg => (arg +2)
    // 3
    assertEquals(3L, n, 0d);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Example 98 with ParameterExpression

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

the class ExpressionTest method testWriteTryCatch.

@Test
void testWriteTryCatch() {
    final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
    final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
    Node node = Expressions.tryCatch(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.return_(null, Expressions.call(re_, "toString"))));
    assertEquals("try {\n" + "  return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + "  return null;\n" + "} catch (RuntimeException re) {\n" + "  return re.toString();\n" + "}\n", Expressions.toString(node));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Node(org.apache.calcite.linq4j.tree.Node) Test(org.junit.jupiter.api.Test)

Example 99 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpShort.

@Test
void testLambdaCallsBinaryOpShort() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Short.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    Short a = 2;
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(a)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public int apply(short arg) {\n" + "    return arg + (short)2;\n" + "  }\n" + "  public Object apply(Short arg) {\n" + "    return apply(\n" + "      arg.shortValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Short) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 1.
    Short b = 1;
    Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(b);
    // This code example produces the following output:
    // 
    // arg => (arg +2)
    // 3
    assertEquals(3, n, 0);
}
Also used : BigInteger(java.math.BigInteger) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Example 100 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpFloat.

@Test
void testLambdaCallsBinaryOpFloat() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Float.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 1f to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2.0f)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public float apply(float arg) {\n" + "    return arg + 2.0F;\n" + "  }\n" + "  public Object apply(Float arg) {\n" + "    return apply(\n" + "      arg.floatValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Float) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 1f
    float n = (Float) lambdaExpr.compile().dynamicInvoke(1f);
    // This code example produces the following output:
    // 
    // arg => (arg +2)
    // 3.0
    assertEquals(3.0f, n, 0f);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) 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