org.apache.calcite.adapter.enumerable.EnumerableRel Java Examples

The following examples show how to use org.apache.calcite.adapter.enumerable.EnumerableRel. 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: CalcitePrepareImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
CalcitePreparingStmt(CalcitePrepareImpl prepare,
    Context context,
    CatalogReader catalogReader,
    RelDataTypeFactory typeFactory,
    CalciteSchema schema,
    EnumerableRel.Prefer prefer,
    RelOptCluster cluster,
    Convention resultConvention,
    SqlRexConvertletTable convertletTable) {
  super(context, catalogReader, resultConvention);
  this.prepare = prepare;
  this.schema = schema;
  this.prefer = prefer;
  this.cluster = cluster;
  this.planner = cluster.getPlanner();
  this.rexBuilder = cluster.getRexBuilder();
  this.typeFactory = typeFactory;
  this.convertletTable = convertletTable;
}
 
Example #2
Source File: EnumerableToSparkConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Result implementSpark(Implementor implementor) {
  // Generate:
  //   Enumerable source = ...;
  //   return SparkRuntime.createRdd(sparkContext, source);
  final BlockBuilder list = new BlockBuilder();
  final EnumerableRel child = (EnumerableRel) getInput();
  final PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(), getRowType(),
          JavaRowFormat.CUSTOM);
  final Expression source = null; // TODO:
  final Expression sparkContext =
      Expressions.call(
          SparkMethod.GET_SPARK_CONTEXT.method,
          implementor.getRootExpression());
  final Expression rdd =
      list.append(
          "rdd",
          Expressions.call(
              SparkMethod.CREATE_RDD.method,
              sparkContext,
              source));
  list.add(
      Expressions.return_(null, rdd));
  return implementor.result(physType, list.toBlock());
}
 
Example #3
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 #4
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    try {
        return new EnumerableAggregate(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
                sole(inputs), indicator, this.groupSet, this.groupSets, rewriteAggCalls);
    } catch (InvalidRelException e) {
        throw new IllegalStateException("Can't create EnumerableAggregate!", e);
    }
}
 
Example #5
Source File: OLAPJoinRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    if (this.hasSubQuery) {
        try {
            return constr.newInstance(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
                    inputs.get(0), inputs.get(1), condition, leftKeys, rightKeys, variablesSet, joinType);
        } catch (Exception e) {
            throw new IllegalStateException("Can't create EnumerableJoin!", e);
        }
    } else {
        return this;
    }
}
 
Example #6
Source File: OLAPLimitRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    EnumerableRel input = sole(inputs);
    if (input instanceof OLAPRel) {
        ((OLAPRel) input).replaceTraitSet(EnumerableConvention.INSTANCE);
    }
    return EnumerableLimit.create(input, localOffset, localFetch);
}
 
Example #7
Source File: OLAPWindowRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    for (EnumerableRel input : inputs) {
        if (input instanceof OLAPRel) {
            ((OLAPRel) input).replaceTraitSet(EnumerableConvention.INSTANCE);
        }
    }
    return EnumerableWindowBridge.createEnumerableWindow(getCluster(), traitSet, inputs.get(0), constants, rowType,
            groups);
}
 
Example #8
Source File: KylinEnumerableUnion.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder builder = new BlockBuilder();
    Expression unionExp = null;
    for (Ord<RelNode> ord : Ord.zip(inputs)) {
        EnumerableRel input = (EnumerableRel) ord.e;
        final Result result = implementor.visitChild(this, ord.i, input, pref);
        Expression childExp =
                builder.append(
                        "child" + ord.i,
                        result.block);

        if (unionExp == null) {
            unionExp = childExp;
        } else {
            unionExp = createUnionExpression(unionExp, childExp, result.format == JavaRowFormat.ARRAY);
        }
    }

    builder.add(unionExp);
    final PhysType physType =
            PhysTypeImpl.of(
                    implementor.getTypeFactory(),
                    getRowType(),
                    pref.prefer(JavaRowFormat.CUSTOM));
    return implementor.result(physType, builder.toBlock());
}
 
Example #9
Source File: OLAPUnionRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    ArrayList<RelNode> relInputs = new ArrayList<>(inputs.size());
    for (EnumerableRel input : inputs) {
        if (input instanceof OLAPRel) {
            ((OLAPRel) input).replaceTraitSet(EnumerableConvention.INSTANCE);
        }
        relInputs.add(input);
    }
    return new KylinEnumerableUnion(getCluster(), traitSet.replace(EnumerableConvention.INSTANCE), relInputs, all);
}
 
Example #10
Source File: OLAPFilterRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    // keep it for having clause
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    RelDataType inputRowType = getInput().getRowType();
    RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(this.condition);
    RexProgram program = programBuilder.getProgram();

    return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
            sole(inputs), program);
}
 
Example #11
Source File: OLAPRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
public EnumerableRel createEnumerable(OLAPRel parent) {
    ArrayList<EnumerableRel> enumInputs = null;
    List<RelNode> children = parent.getInputs();
    if (children != null) {
        enumInputs = Lists.newArrayListWithCapacity(children.size());
        for (RelNode child : children) {
            enumInputs.add(createEnumerable((OLAPRel) child));
        }
    }

    EnumerableRel result = parent.implementEnumerable(enumInputs);
    relContexts.put(result, parent.getContext());
    return result;
}
 
Example #12
Source File: OLAPRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel.Result visitChild(EnumerableRel parent, int ordinal, EnumerableRel child,
        EnumerableRel.Prefer prefer) {

    if (calciteDebug) {
        OLAPContext context;
        if (child instanceof OLAPRel)
            context = ((OLAPRel) child).getContext();
        else
            context = relContexts.get(child);
        System.out.println(context + " - " + child);
    }

    return super.visitChild(parent, ordinal, child, prefer);
}
 
Example #13
Source File: OLAPRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public EnumerableRel createEnumerable(OLAPRel parent) {
    ArrayList<EnumerableRel> enumInputs = null;
    List<RelNode> children = parent.getInputs();
    if (children != null) {
        enumInputs = Lists.newArrayListWithCapacity(children.size());
        for (RelNode child : children) {
            enumInputs.add(createEnumerable((OLAPRel) child));
        }
    }

    EnumerableRel result = parent.implementEnumerable(enumInputs);
    relContexts.put(result, parent.getContext());
    return result;
}
 
Example #14
Source File: OLAPRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel.Result visitChild(EnumerableRel parent, int ordinal, EnumerableRel child,
        EnumerableRel.Prefer prefer) {

    if (calciteDebug) {
        OLAPContext context;
        if (child instanceof OLAPRel)
            context = ((OLAPRel) child).getContext();
        else
            context = relContexts.get(child);
        System.out.println(context + " - " + child);
    }

    return super.visitChild(parent, ordinal, child, prefer);
}
 
Example #15
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    try {
        return new EnumerableAggregate(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
                sole(inputs), indicator, this.groupSet, this.groupSets, rewriteAggCalls);
    } catch (InvalidRelException e) {
        throw new IllegalStateException("Can't create EnumerableAggregate!", e);
    }
}
 
Example #16
Source File: OLAPJoinRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    if (this.hasSubQuery) {
        try {
            return constr.newInstance(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
                    inputs.get(0), inputs.get(1), condition, leftKeys, rightKeys, variablesSet, joinType);
        } catch (Exception e) {
            throw new IllegalStateException("Can't create EnumerableJoin!", e);
        }
    } else {
        return this;
    }
}
 
Example #17
Source File: OLAPLimitRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    EnumerableRel input = sole(inputs);
    if (input instanceof OLAPRel) {
        ((OLAPRel) input).replaceTraitSet(EnumerableConvention.INSTANCE);
    }
    return EnumerableLimit.create(input, localOffset, localFetch);
}
 
Example #18
Source File: CalciteMaterializer.java    From calcite with Apache License 2.0 5 votes vote down vote up
CalciteMaterializer(CalcitePrepareImpl prepare,
    CalcitePrepare.Context context,
    CatalogReader catalogReader, CalciteSchema schema,
    RelOptCluster cluster, SqlRexConvertletTable convertletTable) {
  super(prepare, context, catalogReader, catalogReader.getTypeFactory(),
      schema, EnumerableRel.Prefer.ANY, cluster, BindableConvention.INSTANCE,
      convertletTable);
}
 
Example #19
Source File: OLAPWindowRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    for (EnumerableRel input : inputs) {
        if (input instanceof OLAPRel) {
            ((OLAPRel) input).replaceTraitSet(EnumerableConvention.INSTANCE);
        }
    }
    return EnumerableWindowBridge.createEnumerableWindow(getCluster(), traitSet, inputs.get(0), constants, rowType,
            groups);
}
 
Example #20
Source File: KylinEnumerableUnion.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder builder = new BlockBuilder();
    Expression unionExp = null;
    for (Ord<RelNode> ord : Ord.zip(inputs)) {
        EnumerableRel input = (EnumerableRel) ord.e;
        final Result result = implementor.visitChild(this, ord.i, input, pref);
        Expression childExp =
                builder.append(
                        "child" + ord.i,
                        result.block);

        if (unionExp == null) {
            unionExp = childExp;
        } else {
            unionExp = createUnionExpression(unionExp, childExp, result.format == JavaRowFormat.ARRAY);
        }
    }

    builder.add(unionExp);
    final PhysType physType =
            PhysTypeImpl.of(
                    implementor.getTypeFactory(),
                    getRowType(),
                    pref.prefer(JavaRowFormat.CUSTOM));
    return implementor.result(physType, builder.toBlock());
}
 
Example #21
Source File: OLAPUnionRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    ArrayList<RelNode> relInputs = new ArrayList<>(inputs.size());
    for (EnumerableRel input : inputs) {
        if (input instanceof OLAPRel) {
            ((OLAPRel) input).replaceTraitSet(EnumerableConvention.INSTANCE);
        }
        relInputs.add(input);
    }
    return new KylinEnumerableUnion(getCluster(), traitSet.replace(EnumerableConvention.INSTANCE), relInputs, all);
}
 
Example #22
Source File: OLAPFilterRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    // keep it for having clause
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    RelDataType inputRowType = getInput().getRowType();
    RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(this.condition);
    RexProgram program = programBuilder.getProgram();

    return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
            sole(inputs), program);
}
 
Example #23
Source File: OLAPSortRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    return new EnumerableSort(getCluster(),
            getCluster().traitSetOf(EnumerableConvention.INSTANCE).replace(collation), //
            sole(inputs), collation, offset, fetch);
}
 
Example #24
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 #25
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {

    return this;
}
 
Example #26
Source File: OLAPNonEquiJoinRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    return super.copy(traitSet, condition, inputs.get(0), inputs.get(1), joinType, isSemiJoinDone());
}
 
Example #27
Source File: OLAPValuesRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    return EnumerableValues.create(getCluster(), getRowType(), getTuples());
}
 
Example #28
Source File: OLAPSortRel.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    return new EnumerableSort(getCluster(),
            getCluster().traitSetOf(EnumerableConvention.INSTANCE).replace(collation), //
            sole(inputs), collation, offset, fetch);
}
 
Example #29
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {

    return this;
}
 
Example #30
Source File: OLAPValuesRel.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    return EnumerableValues.create(getCluster(), getRowType(), getTuples());
}