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);
}
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());
}
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]);
}
Aggregations