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