org.apache.calcite.plan.RelOptCluster Java Examples

The following examples show how to use org.apache.calcite.plan.RelOptCluster. 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: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected TesterImpl(DiffRepository diffRepos, boolean enableDecorrelate,
    boolean enableTrim, boolean enableExpand, boolean enableLateDecorrelate,
    boolean enableTypeCoercion,
    SqlTestFactory.MockCatalogReaderFactory catalogReaderFactory,
    Function<RelOptCluster, RelOptCluster> clusterFactory,
    SqlToRelConverter.Config config, SqlConformance conformance,
    Context context) {
  this.diffRepos = diffRepos;
  this.enableDecorrelate = enableDecorrelate;
  this.enableTrim = enableTrim;
  this.enableExpand = enableExpand;
  this.enableLateDecorrelate = enableLateDecorrelate;
  this.enableTypeCoercion = enableTypeCoercion;
  this.catalogReaderFactory = catalogReaderFactory;
  this.clusterFactory = clusterFactory;
  this.config = config;
  this.conformance = conformance;
  this.context = context;
}
 
Example #2
Source File: StreamRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  final TableScan scan = call.rel(1);
  final RelOptCluster cluster = delta.getCluster();
  final RelOptTable relOptTable = scan.getTable();
  final StreamableTable streamableTable =
      relOptTable.unwrap(StreamableTable.class);
  if (streamableTable != null) {
    final Table table1 = streamableTable.stream();
    final RelOptTable relOptTable2 =
        RelOptTableImpl.create(relOptTable.getRelOptSchema(),
            relOptTable.getRowType(), table1,
            ImmutableList.<String>builder()
                .addAll(relOptTable.getQualifiedName())
                .add("(STREAM)").build());
    final LogicalTableScan newScan =
        LogicalTableScan.create(cluster, relOptTable2, scan.getHints());
    call.transformTo(newScan);
  }
}
 
Example #3
Source File: StreamAggPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static void validateCollation(RelOptCluster cluster, RelNode child, ImmutableBitSet groupSet) {
  if (groupSet.isEmpty()) {
    // If no groups, no collation is required
    return;
  }

  final RelCollation requiredCollation = RelCollations.of(
      StreamSupport.stream(groupSet.spliterator(), false).map(RelFieldCollation::new).collect(Collectors.toList()));

  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final List<RelCollation> collations = mq.collations(child);

  for(RelCollation collation: collations) {
    if (collation.satisfies(requiredCollation)) {
      return;
    }
  }

  throw new AssertionError("child collations [" + collations + "] does not match expected collation [" + requiredCollation + "]");
}
 
Example #4
Source File: SystemScanPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public SystemScanPrel(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelOptTable table,
    TableMetadata dataset,
    List<SchemaPath> projectedColumns,
    double observedRowcountAdjustment,
    RelDataType rowType
    ) {
  super(cluster, traitSet, table, dataset.getStoragePluginId(), dataset, projectedColumns, observedRowcountAdjustment);

  final EntityPath datasetPath = new EntityPath(dataset.getName().getPathComponents());
  final Optional<SystemTable> systemTable = SystemStoragePlugin.getDataset(datasetPath);
  Preconditions.checkArgument(systemTable.isPresent(), "Unexpected system table path: %s", datasetPath);
  this.systemTable = systemTable.get();

  this.executorCount = PrelUtil.getPlannerSettings(cluster).getExecutorCount();
  this.rowType = rowType;
  this.pluginId = dataset.getStoragePluginId();
}
 
Example #5
Source File: StreamAggPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static StreamAggPrel create(RelOptCluster cluster,
                     RelTraitSet traits,
                     RelNode child,
                     boolean indicator,
                     ImmutableBitSet groupSet,
                     List<ImmutableBitSet> groupSets,
                     List<AggregateCall> aggCalls,
                     OperatorPhase phase) throws InvalidRelException {
  final RelTraitSet adjustedTraits = AggPrelBase.adjustTraits(traits, child, groupSet)
      .replaceIf(RelCollationTraitDef.INSTANCE, () -> {
        // Validate input collation which should match groups
        if (AssertionUtil.isAssertionsEnabled()) {
          validateCollation(cluster, child, groupSet);
        }

        return collation(groupSet);
      });

  return new StreamAggPrel(cluster, adjustedTraits, child, indicator, groupSet, groupSets, aggCalls, phase);
}
 
Example #6
Source File: ComboRuleTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testCombo() {
  VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.addRule(new ComboRule());
  planner.addRule(new AddIntermediateNodeRule());
  planner.addRule(new GoodSingleRule());

  RelOptCluster cluster = newCluster(planner);
  NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
  NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
  NoneSingleRel singleRel2 = new NoneSingleRel(cluster, singleRel);
  RelNode convertedRel =
      planner.changeTraits(singleRel2,
          cluster.traitSetOf(PHYS_CALLING_CONVENTION));
  planner.setRoot(convertedRel);
  RelNode result = planner.chooseDelegate().findBestExp();
  assertTrue(result instanceof IntermediateNode);
}
 
Example #7
Source File: LogicalCorrelate.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalCorrelate(RelOptCluster cluster, RelNode left, RelNode right,
    CorrelationId correlationId,
    ImmutableBitSet requiredColumns, SemiJoinType joinType) {
  this(cluster, cluster.traitSetOf(Convention.NONE), left, right,
      correlationId, requiredColumns, joinType);
}
 
Example #8
Source File: MutableMultiRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected MutableMultiRel(RelOptCluster cluster,
    RelDataType rowType, MutableRelType type, List<MutableRel> inputs) {
  super(cluster, rowType, type);
  this.inputs = ImmutableList.copyOf(inputs);
  for (Ord<MutableRel> input : Ord.zip(inputs)) {
    input.e.parent = this;
    input.e.ordinalInParent = input.i;
  }
}
 
Example #9
Source File: HashToMergeExchangePrel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public HashToMergeExchangePrel(RelOptCluster cluster, RelTraitSet traitSet, RelNode input,
                               List<DistributionField> fields,
                               RelCollation collation,
                               int numEndPoints) {
  super(cluster, traitSet, input);
  this.distFields = fields;
  this.collation = collation;
  this.numEndPoints = numEndPoints;
  assert input.getConvention() == Prel.PHYSICAL;
}
 
Example #10
Source File: ConverterImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ConverterImpl.
 *
 * @param cluster  planner's cluster
 * @param traitDef the RelTraitDef this converter converts
 * @param traits   the output traits of this converter
 * @param child    child rel (provides input traits)
 */
protected ConverterImpl(
    RelOptCluster cluster,
    RelTraitDef traitDef,
    RelTraitSet traits,
    RelNode child) {
  super(cluster, traits, child);
  this.inTraits = child.getTraitSet();
  this.traitDef = traitDef;
}
 
Example #11
Source File: VolcanoPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests a rule that is fired once per subset (whereas most rules are fired
 * once per rel in a set or rel in a subset)
 */
@Test void testSubsetRule() {
  VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);

  planner.addRule(new PhysLeafRule());
  planner.addRule(new GoodSingleRule());
  final List<String> buf = new ArrayList<>();
  planner.addRule(new SubsetRule(buf));

  RelOptCluster cluster = newCluster(planner);
  NoneLeafRel leafRel =
      new NoneLeafRel(
          cluster,
          "a");
  NoneSingleRel singleRel =
      new NoneSingleRel(
          cluster,
          leafRel);
  RelNode convertedRel =
      planner.changeTraits(
          singleRel,
          cluster.traitSetOf(PHYS_CALLING_CONVENTION));
  planner.changeTraits(leafRel,
      cluster.traitSetOf(PHYS_CALLING_CONVENTION)
      .plus(RelCollations.of(0)));
  planner.setRoot(convertedRel);
  RelNode result = planner.chooseDelegate().findBestExp();
  assertTrue(result instanceof PhysSingleRel);
  assertThat(sort(buf),
      equalTo(
          sort(
              "NoneSingleRel:RelSubset#0.NONE.[]",
              "PhysSingleRel:RelSubset#0.PHYS.[0]",
              "PhysSingleRel:RelSubset#0.PHYS.[]")));
}
 
Example #12
Source File: VolcanoPlannerTraitTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private AltTraitConverter(
    RelOptCluster cluster,
    RelNode child,
    RelTrait toTrait) {
  super(
      cluster,
      toTrait.getTraitDef(),
      child.getTraitSet().replace(toTrait),
      child);

  this.toTrait = toTrait;
}
 
Example #13
Source File: LogicalCorrelate.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalCorrelate. */
public static LogicalCorrelate create(RelNode left, RelNode right,
    CorrelationId correlationId, ImmutableBitSet requiredColumns,
    JoinRelType joinType) {
  final RelOptCluster cluster = left.getCluster();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE);
  return new LogicalCorrelate(cluster, traitSet, left, right, correlationId,
      requiredColumns, joinType);
}
 
Example #14
Source File: PigAggregate.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public PigAggregate(RelOptCluster cluster, RelTraitSet traitSet,
    RelNode input, boolean indicator, ImmutableBitSet groupSet,
    List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
  this(cluster, traitSet, input, groupSet, groupSets, aggCalls);
  checkIndicator(indicator);
}
 
Example #15
Source File: LogicalJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalJoin, flagged with whether it has been translated to a
 * semi-join. */
public static LogicalJoin create(RelNode left, RelNode right, List<RelHint> hints,
    RexNode condition, Set<CorrelationId> variablesSet, JoinRelType joinType,
    boolean semiJoinDone, ImmutableList<RelDataTypeField> systemFieldList) {
  final RelOptCluster cluster = left.getCluster();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE);
  return new LogicalJoin(cluster, traitSet, hints, left, right, condition,
      variablesSet, joinType, semiJoinDone, systemFieldList);
}
 
Example #16
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Decorrelates a query.
 *
 * <p>This is the main entry point to {@code RelDecorrelator}.
 *
 * @param rootRel           Root node of the query
 * @param relBuilder        Builder for relational expressions
 *
 * @return Equivalent query with all
 * {@link org.apache.calcite.rel.logical.LogicalCorrelate} instances removed
 */
public static RelNode decorrelateQuery(RelNode rootRel,
    RelBuilder relBuilder) {
  final CorelMap corelMap = new CorelMapBuilder().build(rootRel);
  if (!corelMap.hasCorrelation()) {
    return rootRel;
  }

  final RelOptCluster cluster = rootRel.getCluster();
  final RelDecorrelator decorrelator =
      new RelDecorrelator(corelMap,
          cluster.getPlanner().getContext(), relBuilder);

  RelNode newRootRel = decorrelator.removeCorrelationViaRule(rootRel);

  if (SQL2REL_LOGGER.isDebugEnabled()) {
    SQL2REL_LOGGER.debug(
        RelOptUtil.dumpPlan("Plan after removing Correlator", newRootRel,
            SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
  }

  if (!decorrelator.cm.mapCorToCorRel.isEmpty()) {
    newRootRel = decorrelator.decorrelate(newRootRel);
  }

  return newRootRel;
}
 
Example #17
Source File: Exchange.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an Exchange.
 *
 * @param cluster   Cluster this relational expression belongs to
 * @param traitSet  Trait set
 * @param input     Input relational expression
 * @param distribution Distribution specification
 */
protected Exchange(RelOptCluster cluster, RelTraitSet traitSet, RelNode input,
    RelDistribution distribution) {
  super(cluster, traitSet, input);
  this.distribution = Objects.requireNonNull(distribution);

  assert traitSet.containsIfApplicable(distribution)
      : "traits=" + traitSet + ", distribution" + distribution;
  assert distribution != RelDistributions.ANY;
}
 
Example #18
Source File: LogicalCorrelate.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalCorrelate. */
public static LogicalCorrelate create(RelNode left, RelNode right,
    CorrelationId correlationId, ImmutableBitSet requiredColumns,
    SemiJoinType joinType) {
  final RelOptCluster cluster = left.getCluster();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE);
  return new LogicalCorrelate(cluster, traitSet, left, right, correlationId,
      requiredColumns, joinType);
}
 
Example #19
Source File: SingleRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a <code>SingleRel</code>.
 *
 * @param cluster Cluster this relational expression belongs to
 * @param input   Input relational expression
 */
protected SingleRel(
    RelOptCluster cluster,
    RelTraitSet traits,
    RelNode input) {
  super(cluster, traits);
  this.input = input;
}
 
Example #20
Source File: ListTransientTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public TableModify toModificationRel(
    RelOptCluster cluster,
    RelOptTable table,
    Prepare.CatalogReader catalogReader,
    RelNode child,
    TableModify.Operation operation,
    List<String> updateColumnList,
    List<RexNode> sourceExpressionList,
    boolean flattened) {
  return LogicalTableModify.create(table, catalogReader, child, operation,
      updateColumnList, sourceExpressionList, flattened);
}
 
Example #21
Source File: JdbcRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public JdbcFilter(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode input,
    RexNode condition) {
  super(cluster, traitSet, input, condition);
  assert getConvention() instanceof JdbcConvention;
}
 
Example #22
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
public OLAPAggregateRel(RelOptCluster cluster, RelTraitSet traits, RelNode child, boolean indicator,
        ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls)
        throws InvalidRelException {
    super(cluster, traits, child, indicator, groupSet, groupSets, aggCalls);
    Preconditions.checkArgument(getConvention() == OLAPRel.CONVENTION);
    this.afterAggregate = false;
    this.rewriteAggCalls = aggCalls;
    this.rowType = getRowType();
}
 
Example #23
Source File: CollationConversionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCollationConversion() {
  final VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRelTraitDef(COLLATION_TRAIT_DEF);

  planner.addRule(new SingleNodeRule());
  planner.addRule(new LeafTraitRule());
  planner.addRule(ExpandConversionRule.INSTANCE);
  planner.setTopDownOpt(false);

  final RelOptCluster cluster = newCluster(planner);
  final NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
  final NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
  final RelNode convertedRel =
      planner.changeTraits(singleRel,
          cluster.traitSetOf(PHYS_CALLING_CONVENTION).plus(ROOT_COLLATION));
  planner.setRoot(convertedRel);
  RelNode result = planner.chooseDelegate().findBestExp();
  assertTrue(result instanceof RootSingleRel);
  assertTrue(result.getTraitSet().contains(ROOT_COLLATION));
  assertTrue(result.getTraitSet().contains(PHYS_CALLING_CONVENTION));

  final RelNode input = result.getInput(0);
  assertTrue(input instanceof PhysicalSort);
  assertTrue(result.getTraitSet().contains(ROOT_COLLATION));
  assertTrue(input.getTraitSet().contains(PHYS_CALLING_CONVENTION));

  final RelNode input2 = input.getInput(0);
  assertTrue(input2 instanceof LeafRel);
  assertTrue(input2.getTraitSet().contains(LEAF_COLLATION));
  assertTrue(input.getTraitSet().contains(PHYS_CALLING_CONVENTION));
}
 
Example #24
Source File: EnumerableLimit.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates an EnumerableLimit.
 *
 * <p>Use {@link #create} unless you know what you're doing. */
public EnumerableLimit(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode input,
    RexNode offset,
    RexNode fetch) {
  super(cluster, traitSet, input);
  this.offset = offset;
  this.fetch = fetch;
  assert getConvention() instanceof EnumerableConvention;
  assert getConvention() == input.getConvention();
}
 
Example #25
Source File: PigAggregate.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a PigAggregate. */
public PigAggregate(RelOptCluster cluster, RelTraitSet traitSet,
    RelNode input, ImmutableBitSet groupSet,
    List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
  super(cluster, traitSet, ImmutableList.of(), input, groupSet, groupSets, aggCalls);
  assert getConvention() == PigRel.CONVENTION;
}
 
Example #26
Source File: CassandraTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a CassandraTableScan.
 *
 * @param cluster        Cluster
 * @param traitSet       Traits
 * @param table          Table
 * @param cassandraTable Cassandra table
 * @param projectRowType Fields and types to project; null to project raw row
 */
protected CassandraTableScan(RelOptCluster cluster, RelTraitSet traitSet,
    RelOptTable table, CassandraTable cassandraTable, RelDataType projectRowType) {
  super(cluster, traitSet, ImmutableList.of(), table);
  this.cassandraTable = cassandraTable;
  this.projectRowType = projectRowType;

  assert cassandraTable != null;
  assert getConvention() == CassandraRel.CONVENTION;
}
 
Example #27
Source File: LogicalSnapshot.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalSnapshot. */
public static LogicalSnapshot create(RelNode input, RexNode period) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet = cluster.traitSet()
      .replace(Convention.NONE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> RelMdCollation.snapshot(mq, input))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> RelMdDistribution.snapshot(mq, input));
  return new LogicalSnapshot(cluster, traitSet, input, period);
}
 
Example #28
Source File: WriterRelBase.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public WriterRelBase(Convention convention, RelOptCluster cluster, RelTraitSet traitSet, RelNode input,
    CreateTableEntry createTableEntry) {
  super(cluster, traitSet, input);
  assert input.getConvention() == convention;
  this.createTableEntry = createTableEntry;

  rowType = CalciteArrowHelper.wrap(RecordWriter.SCHEMA).toCalciteRecordType(getCluster().getTypeFactory());
}
 
Example #29
Source File: EnumerableTableModifyExtension.java    From kareldb with Apache License 2.0 5 votes vote down vote up
public EnumerableTableModifyExtension(RelOptCluster cluster, RelTraitSet traits,
                                      RelOptTable table, Prepare.CatalogReader catalogReader, RelNode child,
                                      Operation operation, List<String> updateColumnList,
                                      List<RexNode> sourceExpressionList, boolean flattened) {
    super(cluster, traits, table, catalogReader, child, operation,
        updateColumnList, sourceExpressionList, flattened);
}
 
Example #30
Source File: Bindables.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a BindableTableScan.
 *
 * <p>Use {@link #create} unless you know what you are doing. */
BindableTableScan(RelOptCluster cluster, RelTraitSet traitSet,
    RelOptTable table, ImmutableList<RexNode> filters,
    ImmutableIntList projects) {
  super(cluster, traitSet, ImmutableList.of(), table);
  this.filters = Objects.requireNonNull(filters);
  this.projects = Objects.requireNonNull(projects);
  Preconditions.checkArgument(canHandle(table));
}