Java Code Examples for org.apache.calcite.rel.RelNode#getCluster()

The following examples show how to use org.apache.calcite.rel.RelNode#getCluster() . 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: LimitUnionExchangeTransposeRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LimitPrel limit = (LimitPrel) call.rel(0);
  final UnionExchangePrel unionExchangePrel = (UnionExchangePrel) call.rel(1);

  RelNode child = unionExchangePrel.getInput();

  final int offset = limit.getOffset() != null ? Math.max(0, RexLiteral.intValue(limit.getOffset())) : 0;
  final int fetch = Math.max(0, RexLiteral.intValue(limit.getFetch()));

  // child Limit uses conservative approach:  use offset 0 and fetch = parent limit offset + parent limit fetch.
  final RexNode childFetch = limit.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(offset + fetch));

  final RelNode limitUnderExchange = new LimitPrel(child.getCluster(), child.getTraitSet(), child, null, childFetch);
  final RelNode newUnionExch = new UnionExchangePrel(unionExchangePrel.getCluster(), unionExchangePrel.getTraitSet(), limitUnderExchange);
  final RelNode limitAboveExchange = new LimitPrel(limit.getCluster(), limit.getTraitSet(), newUnionExch, limit.getOffset(), limit.getFetch(), true);

  call.transformTo(limitAboveExchange);
}
 
Example 2
Source File: JdbcRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final Minus minus = (Minus) rel;
  if (minus.all) {
    return null; // EXCEPT ALL not implemented
  }
  final RelTraitSet traitSet =
      rel.getTraitSet().replace(out);
  return new JdbcMinus(rel.getCluster(), traitSet,
      convertList(minus.getInputs(), out), false);
}
 
Example 3
Source File: ElasticsearchSortRule.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode relNode) {
    final LogicalSort sort = (LogicalSort) relNode;
    final RelTraitSet traitSet = sort.getTraitSet().replace(getOutTrait()).replace(sort.getCollation());
    return new ElasticsearchSort(relNode.getCluster(), traitSet,
            convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY)), sort.getCollation(),
            sort.offset, sort.fetch);
}
 
Example 4
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 5
Source File: SemiJoin.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a SemiJoin. */
public static SemiJoin create(RelNode left, RelNode right, RexNode condition,
    ImmutableIntList leftKeys, ImmutableIntList rightKeys) {
  final RelOptCluster cluster = left.getCluster();
  return new SemiJoin(cluster, cluster.traitSetOf(Convention.NONE), left,
      right, condition, leftKeys, rightKeys);
}
 
Example 6
Source File: OLAPWindowRule.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    final Window window = (Window) rel;
    final RelTraitSet traitSet = window.getTraitSet().replace(OLAPRel.CONVENTION);
    final RelNode input = window.getInput();
    return new OLAPWindowRel(rel.getCluster(), traitSet,
            convert(input, input.getTraitSet().replace(OLAPRel.CONVENTION)), window.constants, window.getRowType(),
            window.groups);
}
 
Example 7
Source File: OLAPUnionRule.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    final Union union = (Union) rel;
    final RelTraitSet traitSet = union.getTraitSet().replace(OLAPRel.CONVENTION);
    final List<RelNode> inputs = union.getInputs();
    return new OLAPUnionRel(rel.getCluster(), traitSet, convertList(inputs, OLAPRel.CONVENTION), union.all);
}
 
Example 8
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet = agg.getTraitSet().replace(out);
  try {
    return new ElasticsearchAggregate(
        rel.getCluster(),
        traitSet,
        convert(agg.getInput(), traitSet.simplify()),
        agg.getGroupSet(),
        agg.getGroupSets(),
        agg.getAggCallList());
  } catch (InvalidRelException e) {
    return null;
  }
}
 
Example 9
Source File: EnumerableMinusRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalMinus minus = (LogicalMinus) rel;
  final EnumerableConvention out = EnumerableConvention.INSTANCE;
  final RelTraitSet traitSet =
      rel.getTraitSet().replace(
          EnumerableConvention.INSTANCE);
  return new EnumerableMinus(rel.getCluster(), traitSet,
      convertList(minus.getInputs(), out), minus.all);
}
 
Example 10
Source File: LogicalJoin.java    From Bats 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,
    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, left, right, condition,
      variablesSet, joinType, semiJoinDone, systemFieldList);
}
 
Example 11
Source File: TraitConversionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelNode convert(RelOptPlanner planner, RelNode rel,
    SimpleDistribution toTrait, boolean allowInfiniteCostConverters) {
  if (toTrait == SIMPLE_DISTRIBUTION_ANY) {
    return rel;
  }

  return new BridgeRel(rel.getCluster(), rel);
}
 
Example 12
Source File: EnumerableFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates an EnumerableFilter. */
public static EnumerableFilter create(final RelNode input,
    RexNode condition) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet =
      cluster.traitSetOf(EnumerableConvention.INSTANCE)
          .replaceIfs(
              RelCollationTraitDef.INSTANCE,
              () -> RelMdCollation.filter(mq, input))
          .replaceIf(RelDistributionTraitDef.INSTANCE,
              () -> RelMdDistribution.filter(mq, input));
  return new EnumerableFilter(cluster, traitSet, input, condition);
}
 
Example 13
Source File: LogicalTableModify.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalTableModify. */
public static LogicalTableModify create(RelOptTable table,
    Prepare.CatalogReader schema, RelNode input,
    Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  final RelOptCluster cluster = input.getCluster();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE);
  return new LogicalTableModify(cluster, traitSet, table, schema, input,
      operation, updateColumnList, sourceExpressionList, flattened);
}
 
Example 14
Source File: LogicalSortExchange.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a LogicalSortExchange.
 *
 * @param input     Input relational expression
 * @param distribution Distribution specification
 * @param collation array of sort specifications
 */
public static LogicalSortExchange create(
    RelNode input,
    RelDistribution distribution,
    RelCollation collation) {
  RelOptCluster cluster = input.getCluster();
  collation = RelCollationTraitDef.INSTANCE.canonize(collation);
  distribution = RelDistributionTraitDef.INSTANCE.canonize(distribution);
  RelTraitSet traitSet =
      input.getTraitSet().replace(Convention.NONE).replace(distribution).replace(collation);
  return new LogicalSortExchange(cluster, traitSet, input, distribution,
      collation);
}
 
Example 15
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Reduces a list of expressions.
 *
 * <p>The {@code matchNullability} flag comes into play when reducing a
 * expression whose type is nullable. Suppose we are reducing an expression
 * {@code CASE WHEN 'a' = 'a' THEN 1 ELSE NULL END}. Before reduction the
 * type is {@code INTEGER} (nullable), but after reduction the literal 1 has
 * type {@code INTEGER NOT NULL}.
 *
 * <p>In some situations it is more important to preserve types; in this
 * case you should use {@code matchNullability = true} (which used to be
 * the default behavior of this method), and it will cast the literal to
 * {@code INTEGER} (nullable).
 *
 * <p>In other situations, you would rather propagate the new stronger type,
 * because it may allow further optimizations later; pass
 * {@code matchNullability = false} and no cast will be added, but you may
 * need to adjust types elsewhere in the expression tree.
 *
 * @param rel     Relational expression
 * @param expList List of expressions, modified in place
 * @param predicates Constraints known to hold on input expressions
 * @param unknownAsFalse Whether UNKNOWN will be treated as FALSE
 * @param matchNullability Whether Calcite should add a CAST to a literal
 *                         resulting from simplification and expression if the
 *                         expression had nullable type and the literal is
 *                         NOT NULL
 *
 * @return whether reduction found something to change, and succeeded
 */
protected static boolean reduceExpressions(RelNode rel, List<RexNode> expList,
    RelOptPredicateList predicates, boolean unknownAsFalse,
    boolean matchNullability) {
  final RelOptCluster cluster = rel.getCluster();
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  final List<RexNode> originExpList = Lists.newArrayList(expList);
  final RexExecutor executor =
      Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR);
  final RexSimplify simplify =
      new RexSimplify(rexBuilder, predicates, executor);

  // Simplify predicates in place
  final RexUnknownAs unknownAs = RexUnknownAs.falseIf(unknownAsFalse);
  final boolean reduced = reduceExpressionsInternal(rel, simplify, unknownAs,
      expList, predicates);

  boolean simplified = false;
  for (int i = 0; i < expList.size(); i++) {
    final RexNode expr2 =
        simplify.simplifyPreservingType(expList.get(i), unknownAs,
            matchNullability);
    if (!expr2.equals(expList.get(i))) {
      expList.set(i, expr2);
      simplified = true;
    }
  }

  if (reduced && simplified) {
    return !originExpList.equals(expList);
  }

  return reduced || simplified;
}
 
Example 16
Source File: LogicalFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalFilter. */
public static LogicalFilter create(final RelNode input, RexNode condition,
    ImmutableSet<CorrelationId> variablesSet) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> RelMdCollation.filter(mq, input))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> RelMdDistribution.filter(mq, input));
  return new LogicalFilter(cluster, traitSet, input, condition, variablesSet);
}
 
Example 17
Source File: Bindables.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalWindow winAgg = (LogicalWindow) rel;
  final RelTraitSet traitSet =
      winAgg.getTraitSet().replace(BindableConvention.INSTANCE);
  final RelNode input = winAgg.getInput();
  final RelNode convertedInput =
      convert(input,
          input.getTraitSet().replace(BindableConvention.INSTANCE));
  return new BindableWindow(rel.getCluster(), traitSet, convertedInput,
      winAgg.getConstants(), winAgg.getRowType(), winAgg.groups);
}
 
Example 18
Source File: LogicalProject.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalProject, specifying row type rather than field names. */
public static LogicalProject create(final RelNode input,
    final List<? extends RexNode> projects, RelDataType rowType) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet =
      cluster.traitSet().replace(Convention.NONE)
          .replaceIfs(RelCollationTraitDef.INSTANCE,
              () -> RelMdCollation.project(mq, input, projects));
  return new LogicalProject(cluster, traitSet, input, projects, rowType);
}
 
Example 19
Source File: RelOptUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a plan suitable for use in <code>EXISTS</code> or <code>IN</code>
 * statements.
 *
 * @see org.apache.calcite.sql2rel.SqlToRelConverter#convertExists
 *
 * @param seekRel    A query rel, for example the resulting rel from 'select *
 *                   from emp' or 'values (1,2,3)' or '('Foo', 34)'.
 * @param subQueryType Sub-query type
 * @param logic  Whether to use 2- or 3-valued boolean logic
 * @param notIn Whether the operator is NOT IN
 * @param relBuilder Builder for relational expressions
 *
 * @return A pair of a relational expression which outer joins a boolean
 * condition column, and a numeric offset. The offset is 2 if column 0 is
 * the number of rows and column 1 is the number of rows with not-null keys;
 * 0 otherwise.
 */
public static Exists createExistsPlan(RelNode seekRel, SubQueryType subQueryType, Logic logic, boolean notIn,
        RelBuilder relBuilder) {
    switch (subQueryType) {
    case SCALAR:
        return new Exists(seekRel, false, true);
    }

    switch (logic) {
    case TRUE_FALSE_UNKNOWN:
    case UNKNOWN_AS_TRUE:
        if (notIn && !containsNullableFields(seekRel)) {
            logic = Logic.TRUE_FALSE;
        }
    }
    RelNode ret = seekRel;
    final RelOptCluster cluster = seekRel.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final int keyCount = ret.getRowType().getFieldCount();
    final boolean outerJoin = notIn || logic == RelOptUtil.Logic.TRUE_FALSE_UNKNOWN;
    if (!outerJoin) {
        final LogicalAggregate aggregate = LogicalAggregate.create(ret, ImmutableBitSet.range(keyCount), null,
                ImmutableList.of());
        return new Exists(aggregate, false, false);
    }

    // for IN/NOT IN, it needs to output the fields
    final List<RexNode> exprs = new ArrayList<>();
    if (subQueryType == SubQueryType.IN) {
        for (int i = 0; i < keyCount; i++) {
            exprs.add(rexBuilder.makeInputRef(ret, i));
        }
    }

    final int projectedKeyCount = exprs.size();
    exprs.add(rexBuilder.makeLiteral(true));

    ret = relBuilder.push(ret).project(exprs).build();

    final AggregateCall aggCall = AggregateCall.create(SqlStdOperatorTable.MIN, false, false,
            ImmutableList.of(projectedKeyCount), -1, RelCollations.EMPTY, projectedKeyCount, ret, null, null);

    ret = LogicalAggregate.create(ret, ImmutableBitSet.range(projectedKeyCount), null, ImmutableList.of(aggCall));

    switch (logic) {
    case TRUE_FALSE_UNKNOWN:
    case UNKNOWN_AS_TRUE:
        return new Exists(ret, true, true);
    default:
        return new Exists(ret, false, true);
    }
}
 
Example 20
Source File: HepRelVertex.java    From Bats with Apache License 2.0 4 votes vote down vote up
HepRelVertex(RelNode rel) {
  super(
      rel.getCluster(),
      rel.getTraitSet());
  currentRel = rel;
}