Skip to content

Commit

Permalink
[CALCITE-5192] CodeGenerationBenchmark throws IllegalStateException
Browse files Browse the repository at this point in the history
This closes #2834
  • Loading branch information
libenchao committed Aug 5, 2022
1 parent 35eb99f commit 0e57722
Showing 1 changed file with 30 additions and 21 deletions.
Expand Up @@ -39,6 +39,7 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.CompilerFactoryFactory;
import org.codehaus.commons.compiler.IClassBodyEvaluator;
import org.codehaus.commons.compiler.ICompilerFactory;
Expand All @@ -61,6 +62,7 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -111,6 +113,8 @@ public static class QueryState {
*/
PlanInfo[] planInfos;

ICompilerFactory compilerFactory;

private int currentPlan = 0;

@Setup(Level.Trial)
Expand Down Expand Up @@ -182,27 +186,32 @@ public void setup() {
info.classExpr = relImplementor.implementRoot(plan, EnumerableRel.Prefer.ARRAY);
info.javaCode =
Expressions.toString(info.classExpr.memberDeclarations, "\n", false);

ICompilerFactory compilerFactory;
try {
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(
CodeGenerationBenchmark.class.getClassLoader());
} catch (Exception e) {
throw new IllegalStateException(
"Unable to instantiate java compiler", e);
}
IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
cbe.setClassName(info.classExpr.name);
cbe.setExtendedClass(Utilities.class);
cbe.setImplementedInterfaces(
plan.getRowType().getFieldCount() == 1
? new Class[]{Bindable.class, Typed.class}
: new Class[]{ArrayBindable.class});
cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
info.cbe = cbe;
info.plan = plan;
planInfos[i] = info;
}

try {
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(
CodeGenerationBenchmark.class.getClassLoader());
} catch (Exception e) {
throw new IllegalStateException(
"Unable to instantiate java compiler", e);
}

}

Bindable compile(EnumerableRel plan, String className, String code)
throws CompileException, IOException {
final StringReader stringReader = new StringReader(code);
IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
cbe.setClassName(className);
cbe.setExtendedClass(Utilities.class);
cbe.setImplementedInterfaces(
plan.getRowType().getFieldCount() == 1
? new Class[]{Bindable.class, Typed.class}
: new Class[]{ArrayBindable.class});
cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
return (Bindable) cbe.createInstance(stringReader);
}

int nextPlan() {
Expand All @@ -215,7 +224,7 @@ int nextPlan() {
/** Plan information. */
private static class PlanInfo {
ClassDeclaration classExpr;
IClassBodyEvaluator cbe;
EnumerableRel plan;
String javaCode;
}

Expand Down Expand Up @@ -246,7 +255,7 @@ public void setup() {
@Benchmark
public Bindable<?> getBindableNoCache(QueryState state) throws Exception {
PlanInfo info = state.planInfos[state.nextPlan()];
return (Bindable) info.cbe.createInstance(new StringReader(info.javaCode));
return state.compile(info.plan, info.classExpr.name, info.javaCode);
}

/**
Expand All @@ -267,7 +276,7 @@ public Bindable<?> getBindableWithCache(
if (!detector.containsStaticField) {
return cache.get(
info.javaCode,
() -> (Bindable) info.cbe.createInstance(new StringReader(info.javaCode)));
() -> jState.compile(info.plan, info.classExpr.name, info.javaCode));
}
throw new IllegalStateException("Benchmark queries should not arrive here");
}
Expand Down

0 comments on commit 0e57722

Please sign in to comment.