Java Code Examples for org.apache.calcite.plan.RelOptCluster

The following examples show how to use org.apache.calcite.plan.RelOptCluster. These examples are extracted from open source projects. 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 Project: dremio-oss   Source File: StreamAggPrel.java    License: 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 2
Source Project: calcite   Source File: StreamRules.java    License: 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 Project: dremio-oss   Source File: SystemScanPrel.java    License: 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 4
Source Project: dremio-oss   Source File: StreamAggPrel.java    License: 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 5
Source Project: calcite   Source File: SqlToRelTestBase.java    License: 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 6
Source Project: calcite   Source File: ComboRuleTest.java    License: 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 Project: calcite   Source File: GeodeAggregate.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public GeodeAggregate(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 8
Source Project: Bats   Source File: MutableMultiRel.java    License: 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 Project: dremio-oss   Source File: HashToMergeExchangePrel.java    License: 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 Project: Bats   Source File: ConverterImpl.java    License: 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 Project: calcite   Source File: LogicalCalc.java    License: Apache License 2.0 5 votes vote down vote up
public static LogicalCalc create(final RelNode input,
    final RexProgram program) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet = cluster.traitSet()
      .replace(Convention.NONE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> RelMdCollation.calc(mq, input, program))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> RelMdDistribution.calc(mq, input, program));
  return new LogicalCalc(cluster, traitSet, ImmutableList.of(), input, program);
}
 
Example 12
Source Project: calcite   Source File: RelSet.java    License: Apache License 2.0 5 votes vote down vote up
RelSubset getOrCreateSubset(
    RelOptCluster cluster, RelTraitSet traits, boolean required) {
  boolean needsConverter = false;
  final VolcanoPlanner planner = (VolcanoPlanner) cluster.getPlanner();
  RelSubset subset = getSubset(traits);

  if (subset == null) {
    needsConverter = true;
    subset = new RelSubset(cluster, this, traits);

    // Need to first add to subset before adding the abstract
    // converters (for others->subset), since otherwise during
    // register() the planner will try to add this subset again.
    subsets.add(subset);

    if (planner.getListener() != null) {
      postEquivalenceEvent(planner, subset);
    }
  } else if ((required && !subset.isRequired())
      || (!required && !subset.isDelivered())) {
    needsConverter = true;
  }

  if (subset.getConvention() == Convention.NONE) {
    needsConverter = false;
  } else if (required) {
    subset.setRequired();
  } else {
    subset.setDelivered();
  }

  if (needsConverter && !planner.topDownOpt) {
    addConverters(subset, required, true);
  }

  return subset;
}
 
Example 13
Source Project: calcite   Source File: Uncollect.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates an Uncollect.
 *
 * <p>Use {@link #create} unless you know what you're doing. */
public Uncollect(RelOptCluster cluster, RelTraitSet traitSet, RelNode input,
    boolean withOrdinality, List<String> itemAliases) {
  super(cluster, traitSet, input);
  this.withOrdinality = withOrdinality;
  this.itemAliases = ImmutableList.copyOf(itemAliases);
  assert deriveRowType() != null : "invalid child rowtype";
}
 
Example 14
Source Project: calcite   Source File: JdbcRules.java    License: Apache License 2.0 5 votes vote down vote up
public JdbcIntersect(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    List<RelNode> inputs,
    boolean all) {
  super(cluster, traitSet, inputs, all);
  assert !all;
}
 
Example 15
Source Project: Bats   Source File: LogicalAggregate.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalAggregate(
    RelOptCluster cluster,
    RelNode child,
    boolean indicator,
    ImmutableBitSet groupSet,
    List<ImmutableBitSet> groupSets,
    List<AggregateCall> aggCalls) {
  this(cluster, cluster.traitSetOf(Convention.NONE), child, indicator,
      groupSet, groupSets, aggCalls);
}
 
Example 16
Source Project: calcite   Source File: VolcanoPlannerTest.java    License: 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 17
Source Project: calcite   Source File: VolcanoPlannerTraitTest.java    License: 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 18
Source Project: calcite   Source File: LogicalCorrelate.java    License: 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 19
Source Project: calcite   Source File: PigAggregate.java    License: 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 20
Source Project: calcite   Source File: LogicalJoin.java    License: 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 21
Source Project: calcite   Source File: Exchange.java    License: 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 22
Source Project: flink   Source File: RelDecorrelator.java    License: 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 23
Source Project: Bats   Source File: LogicalCorrelate.java    License: 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 24
Source Project: Bats   Source File: LogicalCorrelate.java    License: 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 25
Source Project: Bats   Source File: SingleRel.java    License: 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 26
Source Project: calcite   Source File: ListTransientTable.java    License: 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 27
Source Project: calcite   Source File: JdbcRules.java    License: 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 28
Source Project: kylin   Source File: OLAPAggregateRel.java    License: 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 29
Source Project: calcite   Source File: CollationConversionTest.java    License: 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 30
Source Project: calcite   Source File: EnumerableLimit.java    License: 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();
}