org.apache.calcite.linq4j.tree.ClassDeclaration Java Examples

The following examples show how to use org.apache.calcite.linq4j.tree.ClassDeclaration. 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: 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 #4
Source File: HiveEnumerableRelImplementor.java    From marble with Apache License 2.0 5 votes vote down vote up
@Override protected void addMemberDeclaration(
    List<MemberDeclaration> memberDeclarations) {
  ClassDeclaration classDeclaration =
      Expressions.classDecl(
          Modifier.PUBLIC | Modifier.STATIC,
          "HiveUDFInstanceHolder",
          null,
          ImmutableList.of(Serializable.class),
          new ArrayList<>());
  classDeclaration.memberDeclarations.addAll(
      HiveUDFInstanceCollecterPerSqlQuery.get()
          .getStashedFieldsForHiveUDFInstanceHolder());
  memberDeclarations.add(classDeclaration);
}
 
Example #5
Source File: HiveEnumerableInterpretable.java    From marble with Apache License 2.0 5 votes vote down vote up
public static Bindable toBindable(Map<String, Object> parameters,
    CalcitePrepare.SparkHandler spark, EnumerableRel rel,
    EnumerableRel.Prefer prefer) {
  HiveEnumerableRelImplementor relImplementor =
      new HiveEnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

  final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
  String s = Expressions.toString(expr.memberDeclarations, "\n", false);

  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, s);
  }

  Hook.JAVA_PLAN.run(s);

  try {
    if (spark != null && spark.enabled()) {
      return spark.compile(expr, s);
    } else {
      return getBindable(expr, s,
          rel.getRowType().getFieldCount());
    }
  } catch (Exception e) {
    throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n"
        + s, e);
  }
}
 
Example #6
Source File: RexNodeToJavaCodeCompiler.java    From streamline with Apache License 2.0 5 votes vote down vote up
/** Given a method that implements {@link ExecutableExpression#execute(Context, Object[])},
 * adds a bridge method that implements {@link ExecutableExpression#execute(Context)}, and
 * compiles. */
static String baz(ParameterExpression context_,
                  ParameterExpression outputValues_, BlockStatement block, String className) {
  final List<MemberDeclaration> declarations = Lists.newArrayList();

  // public void execute(Context, Object[] outputValues)
  declarations.add(
          Expressions.methodDecl(Modifier.PUBLIC, void.class,
                  StreamlineBuiltInMethod.EXPR_EXECUTE2.method.getName(),
                  ImmutableList.of(context_, outputValues_), block));

  // public Object execute(Context)
  final BlockBuilder builder = new BlockBuilder();
  final Expression values_ = builder.append("values",
          Expressions.newArrayBounds(Object.class, 1,
                  Expressions.constant(1)));
  builder.add(
          Expressions.statement(
                  Expressions.call(
                          Expressions.parameter(ExecutableExpression.class, "this"),
                          StreamlineBuiltInMethod.EXPR_EXECUTE2.method, context_, values_)));
  builder.add(
          Expressions.return_(null,
                  Expressions.arrayIndex(values_, Expressions.constant(0))));
  declarations.add(
          Expressions.methodDecl(Modifier.PUBLIC, Object.class,
                  StreamlineBuiltInMethod.EXPR_EXECUTE1.method.getName(),
                  ImmutableList.of(context_), builder.toBlock()));

  final ClassDeclaration classDeclaration =
          Expressions.classDecl(Modifier.PUBLIC, className, null,
                  ImmutableList.<Type>of(ExecutableExpression.class), declarations);

  return Expressions.toString(Lists.newArrayList(classDeclaration), "\n", false);
}
 
Example #7
Source File: SparkHandlerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public ArrayBindable compile(ClassDeclaration expr, String s) {
  final String className = "CalciteProgram" + classId.getAndIncrement();
  final String classFileName = className + ".java";
  String source = "public class " + className + "\n"
      + "    implements " + ArrayBindable.class.getName()
      + ", " + Serializable.class.getName()
      + " {\n"
      + s + "\n"
      + "}\n";

  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, source);
  }

  JaninoCompiler compiler = new JaninoCompiler();
  compiler.getArgs().setDestdir(CLASS_DIR.getAbsolutePath());
  compiler.getArgs().setSource(source, classFileName);
  compiler.getArgs().setFullClassName(className);
  compiler.compile();
  try {
    @SuppressWarnings("unchecked")
    final Class<ArrayBindable> clazz =
        (Class<ArrayBindable>) compiler.getClassLoader().loadClass(className);
    final Constructor<ArrayBindable> constructor = clazz.getConstructor();
    return constructor.newInstance();
  } catch (ClassNotFoundException | InstantiationException
      | IllegalAccessException | NoSuchMethodException
      | InvocationTargetException e) {
    throw new RuntimeException(e);
  }
}
 
Example #8
Source File: ExpressionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testClassDecl() {
  final NewExpression newExpression =
      Expressions.new_(
          Object.class,
          ImmutableList.of(),
          Arrays.asList(
              Expressions.fieldDecl(
                  Modifier.PUBLIC | Modifier.FINAL,
                  Expressions.parameter(String.class, "foo"),
                  Expressions.constant("bar")),
              new ClassDeclaration(
                  Modifier.PUBLIC | Modifier.STATIC,
                  "MyClass",
                  null,
                  ImmutableList.of(),
                  Arrays.asList(
                      new FieldDeclaration(
                          0,
                          Expressions.parameter(int.class, "x"),
                          Expressions.constant(0)))),
              Expressions.fieldDecl(
                  0,
                  Expressions.parameter(int.class, "i"))));
  assertEquals(
      "new Object(){\n"
          + "  public final String foo = \"bar\";\n"
          + "  public static class MyClass {\n"
          + "    int x = 0;\n"
          + "  }\n"
          + "  int i;\n"
          + "}",
      Expressions.toString(newExpression));
  newExpression.accept(new Shuttle());
}
 
Example #9
Source File: EnumerableInterpretable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static Bindable toBindable(Map<String, Object> parameters,
    CalcitePrepare.SparkHandler spark, EnumerableRel rel,
    EnumerableRel.Prefer prefer) {
  EnumerableRelImplementor relImplementor =
      new EnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

  final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
  String s = Expressions.toString(expr.memberDeclarations, "\n", false);

  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, s);
  }

  Hook.JAVA_PLAN.run(s);

  try {
    if (spark != null && spark.enabled()) {
      return spark.compile(expr, s);
    } else {
      return getBindable(expr, s, rel.getRowType().getFieldCount());
    }
  } catch (Exception e) {
    throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n"
        + s, e);
  }
}
 
Example #10
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 #11
Source File: RexToJavaCompiler.java    From samza with Apache License 2.0 4 votes vote down vote up
/**
 * This method takes the java statement block, inputs, outputs needed by the statement block to create an object
 * of class that implements the interface {@link Expression}
 *
 * for e.g.
 *   Query : select id from profile
 *      where profile table has relational schema with id(NUMBER) and name(VARCHAR) columns.
 *    This query will result in the following relational plan
 *      LogicalProject(id=[$1])
 *        LogicalTableScan(table=[[profile]])
 *
 *
 *    And the corresponding expressions are
 *       inputs : EnumerableTableScan (Which is the output of LogicalTableScan)
 *       nodes : [$1] Which essentially means take pick the first column from the input
 *
 *    This expression corresponding to the logicalProject "[$1]" gets converted into a java statement block
 *    {
 *      outputValues[0] = (Integer) inputValues[1];
 *    }
 *
 *    This method converts this statement block into an equivalent {@link Expression} object whose execute methods
 *    execute the above java statement block
 *
 */
static org.apache.samza.sql.data.Expression createSamzaExpressionFromCalcite(ParameterExpression executionContext,
    ParameterExpression context, ParameterExpression dataContext, ParameterExpression inputValues,
    ParameterExpression outputValues, BlockStatement block) {
  final List<MemberDeclaration> declarations = Lists.newArrayList();

  // public void execute(Object[] inputValues, Object[] outputValues)
  declarations.add(
      Expressions.methodDecl(Modifier.PUBLIC, void.class, SamzaBuiltInMethod.EXPR_EXECUTE2.method.getName(),
          ImmutableList.of(executionContext, context, dataContext, inputValues, outputValues), block));

  final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, "SqlExpression", null,
      ImmutableList.<Type>of(org.apache.samza.sql.data.Expression.class), declarations);
  String s = Expressions.toString(declarations, "\n", false);

  log.info("Generated code for expression: {}", s);

  try {
    return getExpression(classDeclaration, s);
  } catch (Exception e) {
    throw new SamzaException("Expression compilation failure.", e);
  }
}
 
Example #12
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 4 votes vote down vote up
public ArrayBindable compile(ClassDeclaration expr, String s) {
  throw new UnsupportedOperationException();
}
 
Example #13
Source File: JaninoRexCompiler.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Given a method that implements {@link Scalar#execute(Context, Object[])},
 * adds a bridge method that implements {@link Scalar#execute(Context)}, and
 * compiles. */
static Scalar baz(ParameterExpression context_,
    ParameterExpression outputValues_, BlockStatement block) {
  final List<MemberDeclaration> declarations = new ArrayList<>();

  // public void execute(Context, Object[] outputValues)
  declarations.add(
      Expressions.methodDecl(Modifier.PUBLIC, void.class,
          BuiltInMethod.SCALAR_EXECUTE2.method.getName(),
          ImmutableList.of(context_, outputValues_), block));

  // public Object execute(Context)
  final BlockBuilder builder = new BlockBuilder();
  final Expression values_ = builder.append("values",
      Expressions.newArrayBounds(Object.class, 1,
          Expressions.constant(1)));
  builder.add(
      Expressions.statement(
          Expressions.call(
              Expressions.parameter(Scalar.class, "this"),
              BuiltInMethod.SCALAR_EXECUTE2.method, context_, values_)));
  builder.add(
      Expressions.return_(null,
          Expressions.arrayIndex(values_, Expressions.constant(0))));
  declarations.add(
      Expressions.methodDecl(Modifier.PUBLIC, Object.class,
          BuiltInMethod.SCALAR_EXECUTE1.method.getName(),
          ImmutableList.of(context_), builder.toBlock()));

  final ClassDeclaration classDeclaration =
      Expressions.classDecl(Modifier.PUBLIC, "Buzz", null,
          ImmutableList.of(Scalar.class), declarations);
  String s = Expressions.toString(declarations, "\n", false);
  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, s);
  }
  try {
    return getScalar(classDeclaration, s);
  } catch (CompileException | IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #14
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 votes vote down vote up
ArrayBindable compile(ClassDeclaration expr, String s);