Search in sources :

Example 1 with Predicate1

use of org.apache.calcite.linq4j.function.Predicate1 in project calcite by apache.

the class TableScanNode method createEnumerable.

private static TableScanNode createEnumerable(Compiler compiler, TableScan rel, Enumerable<Row> enumerable, final ImmutableIntList acceptedProjects, List<RexNode> rejectedFilters, final ImmutableIntList rejectedProjects) {
    if (!rejectedFilters.isEmpty()) {
        final RexNode filter = RexUtil.composeConjunction(rel.getCluster().getRexBuilder(), rejectedFilters, false);
        assert filter != null;
        // Re-map filter for the projects that have been applied already
        final RexNode filter2;
        final RelDataType inputRowType;
        if (acceptedProjects == null) {
            filter2 = filter;
            inputRowType = rel.getRowType();
        } else {
            final Mapping mapping = Mappings.target(acceptedProjects, rel.getTable().getRowType().getFieldCount());
            filter2 = RexUtil.apply(mapping, filter);
            final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
            final List<RelDataTypeField> fieldList = rel.getTable().getRowType().getFieldList();
            for (int acceptedProject : acceptedProjects) {
                builder.add(fieldList.get(acceptedProject));
            }
            inputRowType = builder.build();
        }
        final Scalar condition = compiler.compile(ImmutableList.of(filter2), inputRowType);
        final Context context = compiler.createContext();
        enumerable = enumerable.where(new Predicate1<Row>() {

            @Override
            public boolean apply(Row row) {
                context.values = row.getValues();
                Boolean b = (Boolean) condition.execute(context);
                return b != null && b;
            }
        });
    }
    if (rejectedProjects != null) {
        enumerable = enumerable.select(new Function1<Row, Row>() {

            final Object[] values = new Object[rejectedProjects.size()];

            @Override
            public Row apply(Row row) {
                final Object[] inValues = row.getValues();
                for (int i = 0; i < rejectedProjects.size(); i++) {
                    values[i] = inValues[rejectedProjects.get(i)];
                }
                return Row.asCopy(values);
            }
        });
    }
    return new TableScanNode(compiler, rel, enumerable);
}
Also used : DataContext(org.apache.calcite.DataContext) Function1(org.apache.calcite.linq4j.function.Function1) RelDataType(org.apache.calcite.rel.type.RelDataType) Mapping(org.apache.calcite.util.mapping.Mapping) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Predicate1(org.apache.calcite.linq4j.function.Predicate1) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with Predicate1

use of org.apache.calcite.linq4j.function.Predicate1 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 3 with Predicate1

use of org.apache.calcite.linq4j.function.Predicate1 in project calcite by apache.

the class ReflectiveSchemaTest method testQueryProvider.

/**
 * Test that uses a JDBC connection as a linq4j
 * {@link org.apache.calcite.linq4j.QueryProvider}.
 *
 * @throws Exception on error
 */
@Test
public void testQueryProvider() throws Exception {
    Connection connection = CalciteAssert.that(CalciteAssert.Config.REGULAR).connect();
    QueryProvider queryProvider = connection.unwrap(QueryProvider.class);
    ParameterExpression e = Expressions.parameter(Employee.class, "e");
    // "Enumerable<T> asEnumerable(final T[] ts)"
    List<Object[]> list = queryProvider.createQuery(Expressions.call(Expressions.call(Types.of(Enumerable.class, Employee.class), null, LINQ4J_AS_ENUMERABLE_METHOD, Expressions.constant(new JdbcTest.HrSchema().emps)), "asQueryable"), Employee.class).where(Expressions.<Predicate1<Employee>>lambda(Expressions.lessThan(Expressions.field(e, "empid"), Expressions.constant(160)), e)).where(Expressions.<Predicate1<Employee>>lambda(Expressions.greaterThan(Expressions.field(e, "empid"), Expressions.constant(140)), e)).select(Expressions.<Function1<Employee, Object[]>>lambda(Expressions.new_(Object[].class, Expressions.field(e, "empid"), Expressions.call(Expressions.field(e, "name"), "toUpperCase")), e)).toList();
    assertEquals(1, list.size());
    assertEquals(2, list.get(0).length);
    assertEquals(150, list.get(0)[0]);
    assertEquals("SEBASTIAN", list.get(0)[1]);
}
Also used : QueryProvider(org.apache.calcite.linq4j.QueryProvider) Employee(org.apache.calcite.test.JdbcTest.Employee) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Function1(org.apache.calcite.linq4j.function.Function1) Enumerable(org.apache.calcite.linq4j.Enumerable) Predicate1(org.apache.calcite.linq4j.function.Predicate1) Test(org.junit.Test)

Aggregations

Predicate1 (org.apache.calcite.linq4j.function.Predicate1)3 Function1 (org.apache.calcite.linq4j.function.Function1)2 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)2 Test (org.junit.Test)2 Connection (java.sql.Connection)1 DataContext (org.apache.calcite.DataContext)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 Enumerable (org.apache.calcite.linq4j.Enumerable)1 QueryProvider (org.apache.calcite.linq4j.QueryProvider)1 Predicate2 (org.apache.calcite.linq4j.function.Predicate2)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RexNode (org.apache.calcite.rex.RexNode)1 Employee (org.apache.calcite.test.JdbcTest.Employee)1 Mapping (org.apache.calcite.util.mapping.Mapping)1