Search in sources :

Example 11 with Function1

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);
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Statement(org.apache.calcite.linq4j.tree.Statement) Function1(org.apache.calcite.linq4j.function.Function1) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 12 with Function1

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);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Function1(org.apache.calcite.linq4j.function.Function1) Field(java.lang.reflect.Field) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) DataContext(org.apache.calcite.DataContext) RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 13 with Function1

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);
}
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) Test(org.junit.Test)

Example 14 with Function1

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

Example 15 with Function1

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();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Function1(org.apache.calcite.linq4j.function.Function1) Function0(org.apache.calcite.linq4j.function.Function0) LinkedList(java.util.LinkedList) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Function1 (org.apache.calcite.linq4j.function.Function1)18 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)8 Expression (org.apache.calcite.linq4j.tree.Expression)6 RelDataType (org.apache.calcite.rel.type.RelDataType)6 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 PhysType (org.apache.calcite.adapter.enumerable.PhysType)4 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 DataContext (org.apache.calcite.DataContext)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 Connection (java.sql.Connection)2 Iterator (java.util.Iterator)2 Random (java.util.Random)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)2 Enumerable (org.apache.calcite.linq4j.Enumerable)2 QueryProvider (org.apache.calcite.linq4j.QueryProvider)2