org.apache.calcite.runtime.Bindable Java Examples

The following examples show how to use org.apache.calcite.runtime.Bindable. 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: CalcitePrepare.java    From calcite with Apache License 2.0 6 votes vote down vote up
public CalciteSignature(String sql,
    List<AvaticaParameter> parameterList,
    Map<String, Object> internalParameters,
    RelDataType rowType,
    List<ColumnMetaData> columns,
    Meta.CursorFactory cursorFactory,
    CalciteSchema rootSchema,
    List<RelCollation> collationList,
    long maxRowCount,
    Bindable<T> bindable,
    Meta.StatementType statementType) {
  super(columns, sql, parameterList, internalParameters, cursorFactory,
      statementType);
  this.rowType = rowType;
  this.rootSchema = rootSchema;
  this.collationList = collationList;
  this.maxRowCount = maxRowCount;
  this.bindable = bindable;
}
 
Example #3
Source File: CodeGenerationBenchmark.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Benchmarks the part of creating Bindable instances from
 * {@link EnumerableInterpretable#getBindable(ClassDeclaration, String, int)}
 * method with an additional cache layer.
 */
@Benchmark
public Bindable<?> getBindableWithCache(
    QueryState jState,
    CacheState chState) throws Exception {
  PlanInfo info = jState.planInfos[jState.nextPlan()];
  Cache<String, Bindable> cache = chState.cache;

  EnumerableInterpretable.StaticFieldDetector detector =
      new EnumerableInterpretable.StaticFieldDetector();
  info.classExpr.accept(detector);
  if (!detector.containsStaticField) {
    return cache.get(
        info.javaCode,
        () -> (Bindable) info.cbe.createInstance(new StringReader(info.javaCode)));
  }
  throw new IllegalStateException("Benchmark queries should not arrive here");
}
 
Example #4
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public CalciteSignature(String sql, List<AvaticaParameter> parameterList,
    Map<String, Object> internalParameters, RelDataType rowType,
    List<ColumnMetaData> columns, Meta.CursorFactory cursorFactory,
    CalciteSchema rootSchema, List<RelCollation> collationList,
    long maxRowCount, Bindable<T> bindable) {
  this(sql, parameterList, internalParameters, rowType, columns,
      cursorFactory, rootSchema, collationList, maxRowCount, bindable,
      null);
}
 
Example #5
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 #6
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 #7
Source File: EnumerableInterpretable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Node implement(final InterpreterImplementor implementor) {
  final Bindable bindable = toBindable(implementor.internalParameters,
          implementor.spark, (EnumerableRel) getInput(),
      EnumerableRel.Prefer.ARRAY);
  final ArrayBindable arrayBindable = box(bindable);
  final Enumerable<Object[]> enumerable =
      arrayBindable.bind(implementor.dataContext);
  return new EnumerableNode(enumerable, implementor.compiler, this);
}
 
Example #8
Source File: EnumerableBindable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> bind(DataContext dataContext) {
  final ImmutableMap<String, Object> map = ImmutableMap.of();
  final Bindable bindable = EnumerableInterpretable.toBindable(map, null,
      (EnumerableRel) getInput(), EnumerableRel.Prefer.ARRAY);
  final ArrayBindable arrayBindable = EnumerableInterpretable.box(bindable);
  return arrayBindable.bind(dataContext);
}
 
Example #9
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Bindable getBindable(final Meta.CursorFactory cursorFactory) {
  final String explanation = getCode();
  return dataContext -> {
    switch (cursorFactory.style) {
    case ARRAY:
      return Linq4j.singletonEnumerable(new String[] {explanation});
    case OBJECT:
    default:
      return Linq4j.singletonEnumerable(explanation);
    }
  };
}
 
Example #10
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 #11
Source File: TableEnv.java    From marble with Apache License 2.0 5 votes vote down vote up
private DataTable implement(Bindable bindable, RelDataType rowType,
    RelDataTypeFactory typeFactory) {
  final DataContext dataContext = new TableDataContexImpl(null, rootSchema,
      (JavaTypeFactory) typeFactory, tableConfig);
  Enumerable<Object[]> enumerableResult = bindable.bind(dataContext);
  List rows = EnumerableDefaults.toList(enumerableResult);
  return new DataTable(rowType, rows);
}
 
Example #12
Source File: HiveTableEnv.java    From marble with Apache License 2.0 4 votes vote down vote up
@Override protected Bindable toBindable(EnumerableRel rel) {
  return HiveEnumerableInterpretable.toBindable(Maps.newHashMap(),
      null, rel, EnumerableRel.Prefer.ARRAY);
}
 
Example #13
Source File: TableEnv.java    From marble with Apache License 2.0 4 votes vote down vote up
protected Bindable toBindable(EnumerableRel rel) {
  return EnumerableInterpretable.toBindable(Maps.newHashMap(),
      null, rel, EnumerableRel.Prefer.ARRAY);
}
 
Example #14
Source File: CodeGenerationBenchmark.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Benchmarks the part creating Bindable instances from
 * {@link EnumerableInterpretable#getBindable(ClassDeclaration, String, int)}
 * method without any additional caching layer.
 */
@Benchmark
public Bindable<?> getBindableNoCache(QueryState state) throws Exception {
  PlanInfo info = state.planInfos[state.nextPlan()];
  return (Bindable) info.cbe.createInstance(new StringReader(info.javaCode));
}
 
Example #15
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override protected PreparedResult implement(RelRoot root) {
  Hook.PLAN_BEFORE_IMPLEMENTATION.run(root);
  RelDataType resultType = root.rel.getRowType();
  boolean isDml = root.kind.belongsTo(SqlKind.DML);
  final Bindable bindable;
  if (resultConvention == BindableConvention.INSTANCE) {
    bindable = Interpreters.bindable(root.rel);
  } else {
    EnumerableRel enumerable = (EnumerableRel) root.rel;
    if (!root.isRefTrivial()) {
      final List<RexNode> projects = new ArrayList<>();
      final RexBuilder rexBuilder = enumerable.getCluster().getRexBuilder();
      for (int field : Pair.left(root.fields)) {
        projects.add(rexBuilder.makeInputRef(enumerable, field));
      }
      RexProgram program = RexProgram.create(enumerable.getRowType(),
          projects, null, root.validatedRowType, rexBuilder);
      enumerable = EnumerableCalc.create(enumerable, program);
    }

    try {
      CatalogReader.THREAD_LOCAL.set(catalogReader);
      final SqlConformance conformance = context.config().conformance();
      internalParameters.put("_conformance", conformance);
      bindable = EnumerableInterpretable.toBindable(internalParameters,
          context.spark(), enumerable, prefer);
    } finally {
      CatalogReader.THREAD_LOCAL.remove();
    }
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end codegen");
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end compilation");
  }

  return new PreparedResultImpl(
      resultType,
      parameterRowType,
      fieldOrigins,
      root.collation.getFieldCollations().isEmpty()
          ? ImmutableList.of()
          : ImmutableList.of(root.collation),
      root.rel,
      mapTableModOp(isDml, root.kind),
      isDml) {
    public String getCode() {
      throw new UnsupportedOperationException();
    }

    public Bindable getBindable(Meta.CursorFactory cursorFactory) {
      return bindable;
    }

    public Type getElementType() {
      return ((Typed) bindable).getElementType();
    }
  };
}
 
Example #16
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;
  }

}
 
Example #17
Source File: TableEnv.java    From marble with Apache License 2.0 4 votes vote down vote up
public void setBindable(Bindable bindable) {
  this.bindable = bindable;
}
 
Example #18
Source File: TableEnv.java    From marble with Apache License 2.0 4 votes vote down vote up
public Bindable getBindable() {
  return bindable;
}
 
Example #19
Source File: TableEnv.java    From marble with Apache License 2.0 4 votes vote down vote up
public TableExecutionPlan(String code, Bindable bindable,
    RelDataType rowType) {
  this.code = code;
  this.bindable = bindable;
  this.rowType = rowType;
}
 
Example #20
Source File: Prepare.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Executes the prepared result.
 *
 * @param cursorFactory How to map values into a cursor
 * @return producer of rows resulting from execution
 */
Bindable getBindable(Meta.CursorFactory cursorFactory);