org.apache.calcite.rel.SingleRel Java Examples

The following examples show how to use org.apache.calcite.rel.SingleRel. 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: PigRelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces the relational algebra operator at the top of the stack with
 * a new one.
 *
 * @param newRel the new relational algebra operator to replace
 */
void replaceTop(RelNode newRel) {
  final RelNode topRel = peek();
  if (topRel instanceof SingleRel) {
    String alias = reverseAliasMap.get(topRel);
    if (alias != null) {
      reverseAliasMap.remove(topRel);
      reverseAliasMap.put(newRel, alias);
      aliasMap.put(alias, newRel);
    }
    Operator pig = getPig(topRel);
    if (pig != null) {
      relPigMap.remove(topRel);
      relPigMap.put(newRel, pig);
      pigRelMap.put(pig, newRel);
    }
    build();
    push(newRel);
  }
}
 
Example #2
Source File: FlinkAggregateJoinTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean containsSnapshot(RelNode relNode) {
	RelNode original = null;
	if (relNode instanceof RelSubset) {
		original = ((RelSubset) relNode).getOriginal();
	} else if (relNode instanceof HepRelVertex) {
		original = ((HepRelVertex) relNode).getCurrentRel();
	} else {
		original = relNode;
	}
	if (original instanceof LogicalSnapshot) {
		return true;
	} else if (original instanceof SingleRel) {
		return containsSnapshot(((SingleRel) original).getInput());
	} else {
		return false;
	}
}
 
Example #3
Source File: FlinkAggregateJoinTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean containsSnapshot(RelNode relNode) {
	RelNode original = null;
	if (relNode instanceof RelSubset) {
		original = ((RelSubset) relNode).getOriginal();
	} else if (relNode instanceof HepRelVertex) {
		original = ((HepRelVertex) relNode).getCurrentRel();
	} else {
		original = relNode;
	}
	if (original instanceof LogicalSnapshot) {
		return true;
	} else if (original instanceof SingleRel) {
		return containsSnapshot(((SingleRel) original).getInput());
	} else {
		return false;
	}
}
 
Example #4
Source File: Collect.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Derives the output type of a collect relational expression.
 *
 * @param rel       relational expression
 * @param fieldName name of sole output field
 * @return output type of a collect relational expression
 */
public static RelDataType deriveCollectRowType(
    SingleRel rel,
    String fieldName) {
  RelDataType childType = rel.getInput().getRowType();
  assert childType.isStruct();
  final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
  RelDataType ret =
      SqlTypeUtil.createMultisetType(
          typeFactory,
          childType,
          false);
  ret = typeFactory.builder().add(fieldName, ret).build();
  return typeFactory.createTypeWithNullability(ret, false);
}
 
Example #5
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	final SingleRel relNode = call.rel(0);
	return relNode instanceof LogicalWindowAggregate ||
		relNode instanceof LogicalAggregate ||
		relNode instanceof TableAggregate;
}
 
Example #6
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	final SingleRel relNode = call.rel(0);
	return relNode instanceof LogicalWindowAggregate ||
		relNode instanceof LogicalAggregate ||
		relNode instanceof TableAggregate;
}
 
Example #7
Source File: DrillRelMdDistinctRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Double getDistinctRowCount(RelNode rel, RelMetadataQuery mq, ImmutableBitSet groupKey, RexNode predicate) {
  if (rel instanceof DrillScanRelBase) {                  // Applies to both Drill Logical and Physical Rels
    if (!DrillRelOptUtil.guessRows(rel)) {
      DrillTable table = Utilities.getDrillTable(rel.getTable());
      return getDistinctRowCountInternal(((DrillScanRelBase) rel), mq, table, groupKey, rel.getRowType(), predicate);
    } else {
      /* If we are not using statistics OR there is no table or metadata (stats) table associated with scan,
       * estimate the distinct row count. Consistent with the estimation of Aggregate row count in
       * RelMdRowCount: distinctRowCount = rowCount * 10%.
       */
      if (rel instanceof DrillScanRel) {
        // The existing Drill behavior is to only use this estimation for DrillScanRel and not ScanPrel.
        // TODO: We may potentially do it for ScanPrel (outside the scope of statistics)
        return rel.estimateRowCount(mq) * 0.1;
      }
    }
  } else if (rel instanceof SingleRel && !DrillRelOptUtil.guessRows(rel)) {
    if (rel instanceof Window) {
      int childFieldCount = ((Window) rel).getInput().getRowType().getFieldCount();
      // For window aggregates delegate ndv to parent
      for (int bit : groupKey) {
        if (bit >= childFieldCount) {
          return super.getDistinctRowCount(rel, mq, groupKey, predicate);
        }
      }
    }
    return mq.getDistinctRowCount(((SingleRel) rel).getInput(), groupKey, predicate);
  } else if (rel instanceof DrillJoinRelBase && !DrillRelOptUtil.guessRows(rel)) {
    //Assume ndv is unaffected by the join
    return getDistinctRowCountInternal(((DrillJoinRelBase) rel), mq, groupKey, predicate);
  } else if (rel instanceof RelSubset && !DrillRelOptUtil.guessRows(rel)) {
    if (((RelSubset) rel).getBest() != null) {
      return mq.getDistinctRowCount(((RelSubset) rel).getBest(), groupKey, predicate);
    } else if (((RelSubset) rel).getOriginal() != null) {
      return mq.getDistinctRowCount(((RelSubset) rel).getOriginal(), groupKey, predicate);
    }
  }
  return super.getDistinctRowCount(rel, mq, groupKey, predicate);
}
 
Example #8
Source File: PruneEmptyRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a RemoveEmptySingleRule. */
public <R extends SingleRel> RemoveEmptySingleRule(Class<R> clazz,
    Predicate<R> predicate, RelBuilderFactory relBuilderFactory,
    String description) {
  super(
      operandJ(clazz, null, predicate,
          operandJ(Values.class, null, Values::isEmpty, none())),
      relBuilderFactory, description);
}
 
Example #9
Source File: Collect.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Derives the output type of a collect relational expression.
 *
 * @param rel       relational expression
 * @param fieldName name of sole output field
 * @return output type of a collect relational expression
 */
public static RelDataType deriveCollectRowType(
    SingleRel rel,
    String fieldName) {
  RelDataType childType = rel.getInput().getRowType();
  assert childType.isStruct();
  final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
  RelDataType ret =
      SqlTypeUtil.createMultisetType(
          typeFactory,
          childType,
          false);
  ret = typeFactory.builder().add(fieldName, ret).build();
  return typeFactory.createTypeWithNullability(ret, false);
}
 
Example #10
Source File: PruneEmptyRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("Guava")
@Deprecated // to be removed before 2.0
public <R extends SingleRel> RemoveEmptySingleRule(Class<R> clazz,
    com.google.common.base.Predicate<R> predicate,
    RelBuilderFactory relBuilderFactory, String description) {
  this(clazz, (Predicate<R>) predicate::apply, relBuilderFactory,
      description);
}
 
Example #11
Source File: PruneEmptyRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("Guava")
@Deprecated // to be removed before 2.0
public <R extends SingleRel> RemoveEmptySingleRule(Class<R> clazz,
    com.google.common.base.Predicate<R> predicate,
    RelBuilderFactory relBuilderFactory, String description) {
  this(clazz, (Predicate<R>) predicate::apply, relBuilderFactory,
      description);
}
 
Example #12
Source File: PruneEmptyRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a RemoveEmptySingleRule. */
public <R extends SingleRel> RemoveEmptySingleRule(Class<R> clazz,
    Predicate<R> predicate, RelBuilderFactory relBuilderFactory,
    String description) {
  super(
      operandJ(clazz, null, predicate,
          operandJ(Values.class, null, Values::isEmpty, none())),
      relBuilderFactory, description);
}
 
Example #13
Source File: PruneEmptyRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  SingleRel singleRel = call.rel(0);
  RelNode emptyValues = call.builder().push(singleRel).empty().build();
  RelTraitSet traits = singleRel.getTraitSet();
  // propagate all traits (except convention) from the original singleRel into the empty values
  if (emptyValues.getConvention() != null) {
    traits = traits.replace(emptyValues.getConvention());
  }
  emptyValues = emptyValues.copy(traits, Collections.emptyList());
  call.transformTo(emptyValues);
}
 
Example #14
Source File: PruneEmptyRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates a simple RemoveEmptySingleRule. */
public <R extends SingleRel> RemoveEmptySingleRule(Class<R> clazz,
    String description) {
  this(clazz, (Predicate<R>) singleRel -> true, RelFactories.LOGICAL_BUILDER,
      description);
}
 
Example #15
Source File: RelMdRowCount.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Double getRowCount(SingleRel rel, RelMetadataQuery mq) {
  return mq.getRowCount(rel.getInput());
}
 
Example #16
Source File: RelMdColumnUniqueness.java    From calcite with Apache License 2.0 4 votes vote down vote up
private Boolean areProjectColumnsUnique(
    SingleRel rel, RelMetadataQuery mq,
    ImmutableBitSet columns, boolean ignoreNulls, List<RexNode> projExprs) {
  RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
  ImmutableBitSet.Builder childColumns = ImmutableBitSet.builder();
  for (int bit : columns) {
    RexNode projExpr = projExprs.get(bit);
    if (projExpr instanceof RexInputRef) {
      childColumns.set(((RexInputRef) projExpr).getIndex());
    } else if (projExpr instanceof RexCall && ignoreNulls) {
      // If the expression is a cast such that the types are the same
      // except for the nullability, then if we're ignoring nulls,
      // it doesn't matter whether the underlying column reference
      // is nullable.  Check that the types are the same by making a
      // nullable copy of both types and then comparing them.
      RexCall call = (RexCall) projExpr;
      if (call.getOperator() != SqlStdOperatorTable.CAST) {
        continue;
      }
      RexNode castOperand = call.getOperands().get(0);
      if (!(castOperand instanceof RexInputRef)) {
        continue;
      }
      RelDataType castType =
          typeFactory.createTypeWithNullability(
              projExpr.getType(), true);
      RelDataType origType = typeFactory.createTypeWithNullability(
          castOperand.getType(),
          true);
      if (castType.equals(origType)) {
        childColumns.set(((RexInputRef) castOperand).getIndex());
      }
    } else {
      // If the expression will not influence uniqueness of the
      // projection, then skip it.
      continue;
    }
  }

  // If no columns can affect uniqueness, then return unknown
  if (childColumns.cardinality() == 0) {
    return null;
  }

  return mq.areColumnsUnique(rel.getInput(), childColumns.build(),
      ignoreNulls);
}
 
Example #17
Source File: RelMdDistribution.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDistribution distribution(SingleRel rel, RelMetadataQuery mq) {
  return mq.distribution(rel.getInput());
}
 
Example #18
Source File: OLAPRel.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public void fixSharedOlapTableScan(SingleRel parent) {
    OLAPTableScan copy = copyTableScanIfNeeded(parent.getInput());
    if (copy != null)
        parent.replaceInput(0, copy);
}
 
Example #19
Source File: RelMdUniqueKeys.java    From calcite with Apache License 2.0 4 votes vote down vote up
private Set<ImmutableBitSet> getProjectUniqueKeys(SingleRel rel, RelMetadataQuery mq,
    boolean ignoreNulls, List<RexNode> projExprs) {
  // LogicalProject maps a set of rows to a different set;
  // Without knowledge of the mapping function(whether it
  // preserves uniqueness), it is only safe to derive uniqueness
  // info from the child of a project when the mapping is f(a) => a.
  //
  // Further more, the unique bitset coming from the child needs
  // to be mapped to match the output of the project.

  // Single input can be mapped to multiple outputs
  ImmutableMultimap.Builder<Integer, Integer> inToOutPosBuilder = ImmutableMultimap.builder();
  ImmutableBitSet.Builder mappedInColumnsBuilder = ImmutableBitSet.builder();

  // Build an input to output position map.
  for (int i = 0; i < projExprs.size(); i++) {
    RexNode projExpr = projExprs.get(i);
    if (projExpr instanceof RexInputRef) {
      int inputIndex = ((RexInputRef) projExpr).getIndex();
      inToOutPosBuilder.put(inputIndex, i);
      mappedInColumnsBuilder.set(inputIndex);
    }
  }
  ImmutableBitSet inColumnsUsed = mappedInColumnsBuilder.build();

  if (inColumnsUsed.isEmpty()) {
    // if there's no RexInputRef in the projected expressions
    // return empty set.
    return ImmutableSet.of();
  }

  Set<ImmutableBitSet> childUniqueKeySet =
      mq.getUniqueKeys(rel.getInput(), ignoreNulls);

  if (childUniqueKeySet == null) {
    return ImmutableSet.of();
  }

  Map<Integer, ImmutableBitSet> mapInToOutPos =
      Maps.transformValues(inToOutPosBuilder.build().asMap(), ImmutableBitSet::of);

  ImmutableSet.Builder<ImmutableBitSet> resultBuilder = ImmutableSet.builder();
  // Now add to the projUniqueKeySet the child keys that are fully
  // projected.
  for (ImmutableBitSet colMask : childUniqueKeySet) {
    ImmutableBitSet.Builder tmpMask = ImmutableBitSet.builder();
    if (!inColumnsUsed.contains(colMask)) {
      // colMask contains a column that is not projected as RexInput => the key is not unique
      continue;
    }
    // colMask is mapped to output project, however, the column can be mapped more than once:
    // select id, id, id, unique2, unique2
    // the resulting unique keys would be {{0},{3}}, {{0},{4}}, {{0},{1},{4}}, ...

    Iterable<List<ImmutableBitSet>> product = Linq4j.product(
        Iterables.transform(colMask,
            in -> Iterables.filter(mapInToOutPos.get(in).powerSet(), bs -> !bs.isEmpty())));

    resultBuilder.addAll(Iterables.transform(product, ImmutableBitSet::union));
  }
  return resultBuilder.build();
}
 
Example #20
Source File: OLAPRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
public void fixSharedOlapTableScan(SingleRel parent) {
    OLAPTableScan copy = copyTableScanIfNeeded(parent.getInput());
    if (copy != null)
        parent.replaceInput(0, copy);
}
 
Example #21
Source File: RelMdDistribution.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelDistribution distribution(SingleRel rel, RelMetadataQuery mq) {
  return mq.distribution(rel.getInput());
}
 
Example #22
Source File: AnalyzePrule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final DrillAnalyzeRel analyze = call.rel(0);
  final RelNode input = call.rel(1);
  final SingleRel newAnalyze;
  final RelTraitSet singleDistTrait = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL)
      .plus(DrillDistributionTrait.SINGLETON);

  // Generate parallel ANALYZE plan:
  // Writer<-Unpivot<-StatsAgg(Phase2)<-Exchange<-StatsAgg(Phase1)<-Scan
  final RelTraitSet traits = input.getTraitSet().plus(Prel.DRILL_PHYSICAL).
      plus(DrillDistributionTrait.DEFAULT);
  RelNode convertedInput = convert(input, traits);

  final List<String> mapFields1 = Lists.newArrayList(PHASE_1_FUNCTIONS);
  final Map<String, String> mapFields2 = Maps.newHashMap(PHASE_2_FUNCTIONS);
  final List<String> mapFields3 = Lists.newArrayList(UNPIVOT_FUNCTIONS);
  mapFields1.add(0, Statistic.COLNAME);
  mapFields1.add(1, Statistic.COLTYPE);
  mapFields2.put(Statistic.COLNAME, Statistic.COLNAME);
  mapFields2.put(Statistic.COLTYPE, Statistic.COLTYPE);
  mapFields3.add(0, Statistic.COLNAME);
  mapFields3.add(1, Statistic.COLTYPE);
  // Now generate the two phase plan physical operators bottom-up:
  // STATSAGG->EXCHANGE->STATSMERGE->UNPIVOT
  if (analyze.getSamplePercent() < 100.0) {
    // If a sample samplePercent is specified add a filter for Bernoulli sampling
    RexBuilder builder = convertedInput.getCluster().getRexBuilder();
    RexNode sampleCondition;
    if (PrelUtil.getSettings(convertedInput.getCluster()).getOptions().getOption(ExecConstants.DETERMINISTIC_SAMPLING_VALIDATOR)) {
      sampleCondition = builder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL,
          builder.makeCall(SqlStdOperatorTable.RAND, builder.makeExactLiteral(BigDecimal.valueOf(1))),
          builder.makeExactLiteral(BigDecimal.valueOf(analyze.getSamplePercent()/100.0)));
    } else {
      sampleCondition = builder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL,
          builder.makeCall(SqlStdOperatorTable.RAND),
          builder.makeExactLiteral(BigDecimal.valueOf(analyze.getSamplePercent()/100.0)));
    }
    convertedInput = new FilterPrel(convertedInput.getCluster(), convertedInput.getTraitSet(),
            convertedInput, sampleCondition);
  }
  final StatsAggPrel statsAggPrel = new StatsAggPrel(analyze.getCluster(), traits,
      convertedInput, PHASE_1_FUNCTIONS);
  UnionExchangePrel exch = new UnionExchangePrel(statsAggPrel.getCluster(), singleDistTrait,
      statsAggPrel);
  final StatsMergePrel statsMergePrel = new StatsMergePrel(exch.getCluster(), singleDistTrait,
      exch, mapFields2, analyze.getSamplePercent());
  newAnalyze = new UnpivotMapsPrel(statsMergePrel.getCluster(), singleDistTrait, statsMergePrel,
      mapFields3);
  call.transformTo(newAnalyze);
}
 
Example #23
Source File: DrillReduceExpressionsRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private static RelNode createEmptyEmptyRelHelper(SingleRel input) {
  return LogicalSort.create(input.getInput(), RelCollations.EMPTY,
      input.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(0)),
      input.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(0)));
}
 
Example #24
Source File: DrillRelMdRowCount.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public Double getRowCount(SingleRel rel, RelMetadataQuery mq) {
  return rel.estimateRowCount(mq);
}
 
Example #25
Source File: PruneEmptyRules.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  SingleRel single = call.rel(0);
  call.transformTo(call.builder().push(single).empty().build());
}
 
Example #26
Source File: PruneEmptyRules.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Creates a simple RemoveEmptySingleRule. */
public <R extends SingleRel> RemoveEmptySingleRule(Class<R> clazz,
    String description) {
  this(clazz, (Predicate<R>) project -> true, RelFactories.LOGICAL_BUILDER,
      description);
}
 
Example #27
Source File: RelMdRowCount.java    From Bats with Apache License 2.0 4 votes vote down vote up
public Double getRowCount(SingleRel rel, RelMetadataQuery mq) {
  return mq.getRowCount(rel.getInput());
}