org.codehaus.commons.compiler.CompilerFactoryFactory Java Examples

The following examples show how to use org.codehaus.commons.compiler.CompilerFactoryFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HiveEnumerableInterpretable.java    From marble with Apache License 2.0 6 votes vote down vote up
static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
    throws CompileException, IOException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setExtendedClass(Utilities.class);
  cbe.setImplementedInterfaces(
      fieldCount == 1
          ? new Class[]{Bindable.class, Typed.class}
          : new Class[]{ArrayBindable.class});
  cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
  if (CalcitePrepareImpl.DEBUG) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }
  return (Bindable) cbe.createInstance(new StringReader(s));
}
 
Example #2
Source File: RexToJavaCompiler.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the instance of the class defined in {@link ClassDeclaration}
 * @param expr Interface whose instance needs to be created.
 * @param s The java code that implements the interface which should be used to create the instance.
 * @return The object of the class which implements the interface {@link Expression} with the code that is passed as input.
 * @throws CompileException
 * @throws IOException
 */
static Expression getExpression(ClassDeclaration expr, String s) throws CompileException, IOException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException("Unable to instantiate java compiler", e);
  }
  IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setImplementedInterfaces(expr.implemented.toArray(new Class[expr.implemented.size()]));
  cbe.setParentClassLoader(RexToJavaCompiler.class.getClassLoader());
  cbe.setDebuggingInformation(true, true, true);

  return (org.apache.samza.sql.data.Expression) cbe.createInstance(new StringReader(s));
}
 
Example #3
Source File: CalciteDataSource.java    From mat-calcite-plugin with Apache License 2.0 6 votes vote down vote up
private static void initJanino() throws SQLException {
    // For unknown reason, threadContextClassLoader.getResource("org.codehaus.commons.compiler.properties")
    // returns null when accessed via BundleClassLoader
    // We make a shortcut
    // Some OSGi WA might probably exist
    if (initCompilerDone) {
        return;
    }
    initCompilerDone = true;
    Thread currentThread = Thread.currentThread();
    ClassLoader cl = currentThread.getContextClassLoader();
    try{
        currentThread.setContextClassLoader(CompilerFactoryFactory.class.getClassLoader());
        if (CompilerFactoryFactory.getDefaultCompilerFactory() == null) {
            throw new SQLException("Janino compiler is not initialized: CompilerFactoryFactory.getDefaultCompilerFactory() == null");
        };
    } catch (Exception e) {
        throw new SQLException("Unable to load Janino compiler", e);
    } finally {
        currentThread.setContextClassLoader(cl);
    }
}
 
Example #4
Source File: JaninoRexCompiler.java    From calcite with Apache License 2.0 6 votes vote down vote up
static Scalar getScalar(ClassDeclaration expr, String s)
    throws CompileException, IOException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setImplementedInterfaces(new Class[]{Scalar.class});
  cbe.setParentClassLoader(JaninoRexCompiler.class.getClassLoader());
  if (CalciteSystemProperty.DEBUG.value()) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }
  return (Scalar) cbe.createInstance(new StringReader(s));
}
 
Example #5
Source File: EnumerableInterpretable.java    From Quicksql with MIT License 5 votes vote down vote up
static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
    throws CompileException, IOException, ExecutionException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  final IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setExtendedClass(Utilities.class);
  cbe.setImplementedInterfaces(
      fieldCount == 1
          ? new Class[] {Bindable.class, Typed.class}
          : new Class[] {ArrayBindable.class});
  cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
  if (CalciteSystemProperty.DEBUG.value()) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }

  if (CalciteSystemProperty.BINDABLE_CACHE_MAX_SIZE.value() != 0) {
    StaticFieldDetector detector = new StaticFieldDetector();
    expr.accept(detector);
    if (!detector.containsStaticField) {
      return BINDABLE_CACHE.get(s, () -> (Bindable) cbe.createInstance(new StringReader(s)));
    }
  }
  return (Bindable) cbe.createInstance(new StringReader(s));
}
 
Example #6
Source File: JaninoRexCompiler.java    From Quicksql with MIT License 5 votes vote down vote up
static Scalar getScalar(ClassDeclaration expr, String s)
    throws CompileException, IOException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setImplementedInterfaces(new Class[]{Scalar.class});
  cbe.setParentClassLoader(JaninoRexCompiler.class.getClassLoader());
  if (CalciteSystemProperty.DEBUG.value()) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }
  return (Scalar) cbe.createInstance(new StringReader(s));
}
 
Example #7
Source File: EnumerableInterpretable.java    From calcite with Apache License 2.0 5 votes vote down vote up
static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
    throws CompileException, IOException, ExecutionException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  final IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setExtendedClass(Utilities.class);
  cbe.setImplementedInterfaces(
      fieldCount == 1
          ? new Class[] {Bindable.class, Typed.class}
          : new Class[] {ArrayBindable.class});
  cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
  if (CalciteSystemProperty.DEBUG.value()) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }

  if (CalciteSystemProperty.BINDABLE_CACHE_MAX_SIZE.value() != 0) {
    StaticFieldDetector detector = new StaticFieldDetector();
    expr.accept(detector);
    if (!detector.containsStaticField) {
      return BINDABLE_CACHE.get(s, () -> (Bindable) cbe.createInstance(new StringReader(s)));
    }
  }
  return (Bindable) cbe.createInstance(new StringReader(s));
}
 
Example #8
Source File: SqlPredicate.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
SqlPredicate(
    final Expression filterExpression,
    final Schema schema,
    boolean isWindowedKey,
    final FunctionRegistry functionRegistry
) {
  this.filterExpression = filterExpression;
  this.schema = schema;
  this.genericRowValueTypeEnforcer = new GenericRowValueTypeEnforcer(schema);
  this.isWindowedKey = isWindowedKey;
  this.functionRegistry = functionRegistry;

  CodeGenRunner codeGenRunner = new CodeGenRunner(schema, functionRegistry);
  Map<String, Class> parameterMap = codeGenRunner.getParameterInfo(filterExpression);

  String[] parameterNames = new String[parameterMap.size()];
  Class[] parameterTypes = new Class[parameterMap.size()];
  columnIndexes = new int[parameterMap.size()];

  int index = 0;
  for (Map.Entry<String, Class> entry : parameterMap.entrySet()) {
    parameterNames[index] = entry.getKey();
    parameterTypes[index] = entry.getValue();
    columnIndexes[index] = SchemaUtil.getFieldIndexByName(schema, entry.getKey());
    index++;
  }

  try {
    ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();

    // The expression will have two "int" parameters: "a" and "b".
    ee.setParameters(parameterNames, parameterTypes);

    // And the expression (i.e. "result") type is also "int".
    ee.setExpressionType(boolean.class);

    String expressionStr = new SqlToJavaVisitor(
        schema,
        functionRegistry
    ).process(filterExpression);

    // And now we "cook" (scan, parse, compile and load) the fabulous expression.
    ee.cook(expressionStr);
  } catch (Exception e) {
    throw new KsqlException(
        "Failed to generate code for SqlPredicate."
        + "filterExpression: "
        + filterExpression
        + "schema:"
        + schema
        + "isWindowedKey:"
        + isWindowedKey,
        e
    );
  }
}
 
Example #9
Source File: CodeGenRunner.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
public ExpressionMetadata buildCodeGenFromParseTree(
    final Expression expression
) throws Exception {
  Map<String, Class> parameterMap = getParameterInfo(expression);

  String[] parameterNames = new String[parameterMap.size()];
  Class[] parameterTypes = new Class[parameterMap.size()];
  int[] columnIndexes = new int[parameterMap.size()];
  Kudf[] kudfObjects = new Kudf[parameterMap.size()];

  int index = 0;
  for (Map.Entry<String, Class> entry : parameterMap.entrySet()) {
    parameterNames[index] = entry.getKey();
    parameterTypes[index] = entry.getValue();
    columnIndexes[index] = SchemaUtil.getFieldIndexByName(schema, entry.getKey());
    if (columnIndexes[index] < 0) {
      kudfObjects[index] = (Kudf) entry.getValue().newInstance();
    } else {
      kudfObjects[index] = null;
    }
    index++;
  }

  String javaCode = new SqlToJavaVisitor(schema, functionRegistry).process(expression);

  IExpressionEvaluator ee =
      CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();

  // The expression will have two "int" parameters: "a" and "b".
  ee.setParameters(parameterNames, parameterTypes);

  // And the expression (i.e. "result") type is also "int".
  ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(
      schema,
      functionRegistry
  );
  Schema expressionType = expressionTypeManager.getExpressionType(expression);

  ee.setExpressionType(SchemaUtil.getJavaType(expressionType));

  // And now we "cook" (scan, parse, compile and load) the fabulous expression.
  ee.cook(javaCode);

  return new ExpressionMetadata(ee, columnIndexes, kudfObjects, expressionType);
}
 
Example #10
Source File: CommonsCompilerTest.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Test
public void testFactory() throws Exception {
  assertEquals(CompilerFactory.class, CompilerFactoryFactory.getDefaultCompilerFactory().getClass());
}
 
Example #11
Source File: CodeGenerationBenchmark.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
  planInfos = new PlanInfo[queries];
  VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRule(FilterToCalcRule.INSTANCE);
  planner.addRule(ProjectToCalcRule.INSTANCE);
  planner.addRule(EnumerableRules.ENUMERABLE_CALC_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_VALUES_RULE);

  RelDataTypeFactory typeFactory =
      new JavaTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
  RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(typeFactory));
  RelTraitSet desiredTraits =
      cluster.traitSet().replace(EnumerableConvention.INSTANCE);

  RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
  // Generates queries of the following form depending on the configuration parameters.
  // SELECT `t`.`name`
  // FROM (VALUES  (1, 'Value0')) AS `t` (`id`, `name`)
  // INNER JOIN (VALUES  (1, 'Value1')) AS `t` (`id`, `name`) AS `t0` ON `t`.`id` = `t0`.`id`
  // INNER JOIN (VALUES  (2, 'Value2')) AS `t` (`id`, `name`) AS `t1` ON `t`.`id` = `t1`.`id`
  // INNER JOIN (VALUES  (3, 'Value3')) AS `t` (`id`, `name`) AS `t2` ON `t`.`id` = `t2`.`id`
  // INNER JOIN ...
  // WHERE
  //  `t`.`name` = 'name0' OR
  //  `t`.`name` = 'name1' OR
  //  `t`.`name` = 'name2' OR
  //  ...
  //  OR `t`.`id` = 0
  // The last disjunction (i.e, t.id = $i) is what makes the queries different from one another
  // by assigning a different constant literal.
  for (int i = 0; i < queries; i++) {
    relBuilder.values(new String[]{"id", "name"}, 1, "Value" + 0);
    for (int j = 1; j <= joins; j++) {
      relBuilder
          .values(new String[]{"id", "name"}, j, "Value" + j)
          .join(JoinRelType.INNER, "id");
    }

    List<RexNode> disjunctions = new ArrayList<>();
    for (int j = 0; j < whereClauseDisjunctions; j++) {
      disjunctions.add(
          relBuilder.equals(
              relBuilder.field("name"),
              relBuilder.literal("name" + j)));
    }
    disjunctions.add(
        relBuilder.equals(
            relBuilder.field("id"),
            relBuilder.literal(i)));
    RelNode query =
        relBuilder
            .filter(relBuilder.or(disjunctions))
            .project(relBuilder.field("name"))
            .build();

    RelNode query0 = planner.changeTraits(query, desiredTraits);
    planner.setRoot(query0);

    PlanInfo info = new PlanInfo();
    EnumerableRel plan = (EnumerableRel) planner.findBestExp();

    EnumerableRelImplementor relImplementor =
        new EnumerableRelImplementor(plan.getCluster().getRexBuilder(), new HashMap<>());
    info.classExpr = relImplementor.implementRoot(plan, EnumerableRel.Prefer.ARRAY);
    info.javaCode =
        Expressions.toString(info.classExpr.memberDeclarations, "\n", false);

    ICompilerFactory compilerFactory;
    try {
      compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } 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;
    planInfos[i] = info;
  }

}