use of org.apache.calcite.linq4j.function.Function1 in project apex-malhar by apache.
the class ExpressionCompiler method getExpression.
/**
* Create quasi-Java expression from given {@link RexNode}
*
* @param node Expression in the form of {@link RexNode}
* @param inputRowType Input Data type to expression in the form of {@link RelDataType}
* @param outputRowType Output data type of expression in the form of {@link RelDataType}
*
* @return Returns quasi-Java expression
*/
public String getExpression(RexNode node, RelDataType inputRowType, RelDataType outputRowType) {
final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
programBuilder.addProject(node, null);
final RexProgram program = programBuilder.getProgram();
final BlockBuilder builder = new BlockBuilder();
final JavaTypeFactory javaTypeFactory = (JavaTypeFactory) rexBuilder.getTypeFactory();
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.variable(Object[].class, "inputValues"), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {
public RexToLixTranslator.InputGetter apply(String a0) {
throw new UnsupportedOperationException();
}
};
final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, PhysTypeImpl.of(javaTypeFactory, outputRowType, JavaRowFormat.ARRAY, false), null, inputGetter, correlates);
for (int i = 0; i < list.size(); i++) {
Statement statement = Expressions.statement(list.get(i));
builder.add(statement);
}
return finalizeExpression(builder.toBlock(), inputRowType);
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class TableScanNode method createQueryable.
private static TableScanNode createQueryable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects, QueryableTable queryableTable) {
final DataContext root = compiler.getDataContext();
final RelOptTable relOptTable = rel.getTable();
final Type elementType = queryableTable.getElementType();
SchemaPlus schema = root.getRootSchema();
for (String name : Util.skipLast(relOptTable.getQualifiedName())) {
schema = schema.getSubSchema(name);
}
final Enumerable<Row> rowEnumerable;
if (elementType instanceof Class) {
// noinspection unchecked
final Queryable<Object> queryable = Schemas.queryable(root, (Class) elementType, relOptTable.getQualifiedName());
ImmutableList.Builder<Field> fieldBuilder = ImmutableList.builder();
Class type = (Class) elementType;
for (Field field : type.getFields()) {
if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
fieldBuilder.add(field);
}
}
final List<Field> fields = fieldBuilder.build();
rowEnumerable = queryable.select(new Function1<Object, Row>() {
public Row apply(Object o) {
final Object[] values = new Object[fields.size()];
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
try {
values[i] = field.get(o);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return new Row(values);
}
});
} else {
rowEnumerable = Schemas.queryable(root, Row.class, relOptTable.getQualifiedName());
}
return createEnumerable(compiler, rel, rowEnumerable, null, filters, projects);
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class ReflectiveSchemaTest method testQueryProviderSingleColumn.
@Test
public void testQueryProviderSingleColumn() 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<Integer> 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).select(Expressions.<Function1<Employee, Integer>>lambda(Expressions.field(e, "empid"), e)).toList();
assertEquals(Arrays.asList(100, 200, 150, 110), list);
}
use of org.apache.calcite.linq4j.function.Function1 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]);
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class ChunkListTest method testPerformance.
@Test
public void testPerformance() {
if (!Benchmark.enabled()) {
return;
}
// noinspection unchecked
final Iterable<Pair<Function0<List<Integer>>, String>> factories0 = Pair.zip(Arrays.asList(new Function0<List<Integer>>() {
public List<Integer> apply() {
return new ArrayList<>();
}
}, new Function0<List<Integer>>() {
public List<Integer> apply() {
return new LinkedList<>();
}
}, new Function0<List<Integer>>() {
public List<Integer> apply() {
return new ChunkList<>();
}
}), Arrays.asList("ArrayList", "LinkedList", "ChunkList-64"));
final List<Pair<Function0<List<Integer>>, String>> factories1 = new ArrayList<>();
for (Pair<Function0<List<Integer>>, String> pair : factories0) {
factories1.add(pair);
}
List<Pair<Function0<List<Integer>>, String>> factories = factories1.subList(2, 3);
Iterable<Pair<Integer, String>> sizes = Pair.zip(Arrays.asList(100000, 1000000, 10000000), Arrays.asList("100k", "1m", "10m"));
for (final Pair<Function0<List<Integer>>, String> pair : factories) {
new Benchmark("add 10m values, " + pair.right, new Function1<Benchmark.Statistician, Void>() {
public Void apply(Benchmark.Statistician statistician) {
final List<Integer> list = pair.left.apply();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
list.add(1);
}
statistician.record(start);
return null;
}
}, 10).run();
}
for (final Pair<Function0<List<Integer>>, String> pair : factories) {
new Benchmark("iterate over 10m values, " + pair.right, new Function1<Benchmark.Statistician, Void>() {
public Void apply(Benchmark.Statistician statistician) {
final List<Integer> list = pair.left.apply();
list.addAll(Collections.nCopies(10000000, 1));
long start = System.currentTimeMillis();
int count = 0;
for (Integer integer : list) {
count += integer;
}
statistician.record(start);
assert count == 10000000;
return null;
}
}, 10).run();
}
for (final Pair<Function0<List<Integer>>, String> pair : factories) {
for (final Pair<Integer, String> size : sizes) {
if (size.left > 1000000) {
continue;
}
new Benchmark("delete 10% of " + size.right + " values, " + pair.right, new Function1<Benchmark.Statistician, Void>() {
public Void apply(Benchmark.Statistician statistician) {
final List<Integer> list = pair.left.apply();
list.addAll(Collections.nCopies(size.left, 1));
long start = System.currentTimeMillis();
int n = 0;
for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
Integer integer = it.next();
Util.discard(integer);
if (n++ % 10 == 0) {
it.remove();
}
}
statistician.record(start);
return null;
}
}, 10).run();
}
}
for (final Pair<Function0<List<Integer>>, String> pair : factories) {
for (final Pair<Integer, String> size : sizes) {
if (size.left > 1000000) {
continue;
}
new Benchmark("get from " + size.right + " values, " + (size.left / 1000) + " times, " + pair.right, new Function1<Benchmark.Statistician, Void>() {
public Void apply(Benchmark.Statistician statistician) {
final List<Integer> list = pair.left.apply();
list.addAll(Collections.nCopies(size.left, 1));
final int probeCount = size.left / 1000;
final Random random = new Random(1);
long start = System.currentTimeMillis();
int n = 0;
for (int i = 0; i < probeCount; i++) {
n += list.get(random.nextInt(list.size()));
}
assert n == probeCount;
statistician.record(start);
return null;
}
}, 10).run();
}
}
for (final Pair<Function0<List<Integer>>, String> pair : factories) {
for (final Pair<Integer, String> size : sizes) {
if (size.left > 1000000) {
continue;
}
new Benchmark("add " + size.right + " values, delete 10%, insert 20%, get 1%, using " + pair.right, new Function1<Benchmark.Statistician, Void>() {
public Void apply(Benchmark.Statistician statistician) {
final List<Integer> list = pair.left.apply();
final int probeCount = size.left / 100;
long start = System.currentTimeMillis();
list.addAll(Collections.nCopies(size.left, 1));
final Random random = new Random(1);
for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
Integer integer = it.next();
Util.discard(integer);
if (random.nextInt(10) == 0) {
it.remove();
}
}
for (ListIterator<Integer> it = list.listIterator(); it.hasNext(); ) {
Integer integer = it.next();
Util.discard(integer);
if (random.nextInt(5) == 0) {
it.add(2);
}
}
int n = 0;
for (int i = 0; i < probeCount; i++) {
n += list.get(random.nextInt(list.size()));
}
assert n > probeCount;
statistician.record(start);
return null;
}
}, 10).run();
}
}
}
Aggregations