Search in sources :

Example 16 with Function1

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

the class UtilTest method testIsDistinctBenchmark.

/**
 * Benchmark for {@link Util#isDistinct}. Has determined that map-based
 * implementation is better than nested loops implementation if list is larger
 * than about 15.
 */
@Test
public void testIsDistinctBenchmark() {
    // Run a much quicker form of the test during regular testing.
    final int limit = Benchmark.enabled() ? 1000000 : 10;
    final int zMax = 100;
    for (int i = 0; i < 30; i++) {
        final int size = i;
        new Benchmark("isDistinct " + i + " (set)", new Function1<Benchmark.Statistician, Void>() {

            public Void apply(Benchmark.Statistician statistician) {
                final Random random = new Random(0);
                final List<List<Integer>> lists = new ArrayList<List<Integer>>();
                for (int z = 0; z < zMax; z++) {
                    final List<Integer> list = new ArrayList<Integer>();
                    for (int k = 0; k < size; k++) {
                        list.add(random.nextInt(size * size));
                    }
                    lists.add(list);
                }
                long nanos = System.nanoTime();
                int n = 0;
                for (int j = 0; j < limit; j++) {
                    n += Util.firstDuplicate(lists.get(j % zMax));
                }
                statistician.record(nanos);
                Util.discard(n);
                return null;
            }
        }, 5).run();
    }
}
Also used : Random(java.util.Random) Function1(org.apache.calcite.linq4j.function.Function1) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ConsList(org.apache.calcite.runtime.ConsList) Test(org.junit.Test)

Example 17 with Function1

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

the class RexExecutable method compile.

private static Function1<DataContext, Object[]> compile(String code, Object reason) {
    try {
        final ClassBodyEvaluator cbe = new ClassBodyEvaluator();
        cbe.setClassName(GENERATED_CLASS_NAME);
        cbe.setExtendedClass(Utilities.class);
        cbe.setImplementedInterfaces(new Class[] { Function1.class, Serializable.class });
        cbe.setParentClassLoader(RexExecutable.class.getClassLoader());
        cbe.cook(new Scanner(null, new StringReader(code)));
        Class c = cbe.getClazz();
        // noinspection unchecked
        final Constructor<Function1<DataContext, Object[]>> constructor = c.getConstructor();
        return constructor.newInstance();
    } catch (CompileException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new RuntimeException("While compiling " + reason, e);
    }
}
Also used : Scanner(org.codehaus.janino.Scanner) Function1(org.apache.calcite.linq4j.function.Function1) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringReader(java.io.StringReader) ClassBodyEvaluator(org.codehaus.janino.ClassBodyEvaluator) CompileException(org.codehaus.commons.compiler.CompileException)

Example 18 with Function1

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

the class ExpressionTest method testCompile.

@Test
public void testCompile() throws NoSuchMethodException {
    // Creating a parameter for the expression tree.
    ParameterExpression param = Expressions.parameter(String.class);
    // Creating an expression for the method call and specifying its
    // parameter.
    MethodCallExpression methodCall = Expressions.call(Integer.class, "valueOf", Collections.<Expression>singletonList(param));
    // The following statement first creates an expression tree,
    // then compiles it, and then runs it.
    int x = Expressions.<Function1<String, Integer>>lambda(methodCall, new ParameterExpression[] { param }).getFunction().apply("1234");
    assertEquals(1234, x);
}
Also used : MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Function1(org.apache.calcite.linq4j.function.Function1) 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