Search in sources :

Example 26 with ParameterExpression

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

the class OptimizerTest method testNotEqualPrimitiveNull.

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

Example 27 with ParameterExpression

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

the class OptimizerTest method testNotEqualBoolTrue.

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

Example 28 with ParameterExpression

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

the class OptimizerTest method testNotEqualSameExpr.

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

Example 29 with ParameterExpression

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

the class OptimizerTest method testNotEqualObjectNull.

@Test
public void testNotEqualObjectNull() {
    // (Integer) x == null
    ParameterExpression x = Expressions.parameter(Integer.class, "x");
    assertEquals("{\n  return x != null;\n}\n", optimize(Expressions.notEqual(x, NULL)));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 30 with ParameterExpression

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

the class PhysTypeImpl method generateComparator.

public Expression generateComparator(RelCollation collation) {
    // int c;
    // c = Utilities.compare(v0, v1);
    // if (c != 0) return c; // or -c if descending
    // ...
    // return 0;
    BlockBuilder body = new BlockBuilder();
    final Type javaRowClass = Primitive.box(this.javaRowClass);
    final ParameterExpression parameterV0 = Expressions.parameter(javaRowClass, "v0");
    final ParameterExpression parameterV1 = Expressions.parameter(javaRowClass, "v1");
    final ParameterExpression parameterC = Expressions.parameter(int.class, "c");
    final int mod = collation.getFieldCollations().size() == 1 ? Modifier.FINAL : 0;
    body.add(Expressions.declare(mod, parameterC, null));
    for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
        final int index = fieldCollation.getFieldIndex();
        Expression arg0 = fieldReference(parameterV0, index);
        Expression arg1 = fieldReference(parameterV1, index);
        switch(Primitive.flavor(fieldClass(index))) {
            case OBJECT:
                arg0 = Types.castIfNecessary(Comparable.class, arg0);
                arg1 = Types.castIfNecessary(Comparable.class, arg1);
        }
        final boolean nullsFirst = fieldCollation.nullDirection == RelFieldCollation.NullDirection.FIRST;
        final boolean descending = fieldCollation.getDirection() == RelFieldCollation.Direction.DESCENDING;
        body.add(Expressions.statement(Expressions.assign(parameterC, Expressions.call(Utilities.class, fieldNullable(index) ? (nullsFirst != descending ? "compareNullsFirst" : "compareNullsLast") : "compare", arg0, arg1))));
        body.add(Expressions.ifThen(Expressions.notEqual(parameterC, Expressions.constant(0)), Expressions.return_(null, descending ? Expressions.negate(parameterC) : parameterC)));
    }
    body.add(Expressions.return_(null, Expressions.constant(0)));
    final List<MemberDeclaration> memberDeclarations = Expressions.<MemberDeclaration>list(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock()));
    if (EnumerableRules.BRIDGE_METHODS) {
        final ParameterExpression parameterO0 = Expressions.parameter(Object.class, "o0");
        final ParameterExpression parameterO1 = Expressions.parameter(Object.class, "o1");
        BlockBuilder bridgeBody = new BlockBuilder();
        bridgeBody.add(Expressions.return_(null, Expressions.call(Expressions.parameter(Comparable.class, "this"), BuiltInMethod.COMPARATOR_COMPARE.method, Expressions.convert_(parameterO0, javaRowClass), Expressions.convert_(parameterO1, javaRowClass))));
        memberDeclarations.add(overridingMethodDecl(BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock()));
    }
    return Expressions.new_(Comparator.class, Collections.<Expression>emptyList(), memberDeclarations);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) Utilities(org.apache.calcite.runtime.Utilities) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

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