Java Code Examples for org.apache.calcite.rel.RelNode

The following examples show how to use org.apache.calcite.rel.RelNode. 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: calcite   Source File: SortRemoveRuleTest.java    License: Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2554">[CALCITE-2554]
 * Enrich enumerable join operators with order preserving information</a>.
 *
 * <p>Since join inputs are sorted, and this join preserves the order of the
 * left input, there shouldn't be any sort operator above the join.
 *
 * <p>Until CALCITE-2018 is fixed we can add back EnumerableRules.ENUMERABLE_SORT_RULE
 */
@Test void removeSortOverEnumerableCorrelate() throws Exception {
  RuleSet prepareRules =
      RuleSets.ofList(
          SortProjectTransposeRule.INSTANCE,
          JoinToCorrelateRule.INSTANCE,
          EnumerableRules.ENUMERABLE_PROJECT_RULE,
          EnumerableRules.ENUMERABLE_CORRELATE_RULE,
          EnumerableRules.ENUMERABLE_FILTER_RULE,
          EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE);
  for (String joinType : Arrays.asList("left", "inner")) {
    String sql =
        "select e.\"deptno\" from \"hr\".\"emps\" e "
            + joinType + " join \"hr\".\"depts\" d "
            + " on e.\"deptno\" = d.\"deptno\" "
            + "order by e.\"empid\" ";
    RelNode actualPlan = transform(sql, prepareRules);
    assertThat(
        toString(actualPlan),
        allOf(
            containsString("EnumerableCorrelate"),
            not(containsString("EnumerableSort"))));
  }
}
 
Example 2
Source Project: calcite   Source File: RelBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
/** Tests that a projection retains field names after a join. */
@Test void testProjectJoin() {
  final RelBuilder builder = RelBuilder.create(config().build());
  RelNode root =
      builder.scan("EMP")
          .as("e")
          .scan("DEPT")
          .join(JoinRelType.INNER)
          .project(builder.field("DEPT", "DEPTNO"),
              builder.field(0),
              builder.field("e", "MGR"))
          // essentially a no-op, was previously throwing exception due to
          // project() using join-renamed fields
          .project(builder.field("DEPT", "DEPTNO"),
              builder.field(1),
              builder.field("e", "MGR"))
          .build();
  final String expected = ""
      + "LogicalProject(DEPTNO=[$8], EMPNO=[$0], MGR=[$3])\n"
      + "  LogicalJoin(condition=[true], joinType=[inner])\n"
      + "    LogicalTableScan(table=[[scott, EMP]])\n"
      + "    LogicalTableScan(table=[[scott, DEPT]])\n";
  assertThat(root, hasTree(expected));
}
 
Example 3
Source Project: Bats   Source File: VolcanoPlanner.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Checks internal consistency.
 */
protected boolean isValid(Litmus litmus) {
  for (RelSet set : allSets) {
    if (set.equivalentSet != null) {
      return litmus.fail("set [{}] has been merged: it should not be in the list", set);
    }
    for (RelSubset subset : set.subsets) {
      if (subset.set != set) {
        return litmus.fail("subset [{}] is in wrong set [{}]",
            subset.getDescription(), set);
      }
      for (RelNode rel : subset.getRels()) {
        RelOptCost relCost = getCost(rel, rel.getCluster().getMetadataQuery());
        if (relCost.isLt(subset.bestCost)) {
          return litmus.fail("rel [{}] has lower cost {} than best cost {} of subset [{}]",
              rel.getDescription(), relCost, subset.bestCost, subset.getDescription());
        }
      }
    }
  }
  return litmus.succeed();
}
 
Example 4
Source Project: calcite   Source File: RelOptUtil.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns whether relational expression {@code target} occurs within a
 * relational expression {@code ancestor}. */
public static boolean contains(RelNode ancestor, final RelNode target) {
  if (ancestor == target) {
    // Short-cut common case.
    return true;
  }
  try {
    new RelVisitor() {
      public void visit(RelNode node, int ordinal, RelNode parent) {
        if (node == target) {
          throw Util.FoundOne.NULL;
        }
        super.visit(node, ordinal, parent);
      }
    // CHECKSTYLE: IGNORE 1
    }.go(ancestor);
    return false;
  } catch (Util.FoundOne e) {
    return true;
  }
}
 
Example 5
Source Project: calcite   Source File: RuleQueue.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns whether to skip a match. This happens if any of the
 * {@link RelNode}s have importance zero. */
private boolean skipMatch(VolcanoRuleMatch match) {
  for (RelNode rel : match.rels) {
    if (planner.prunedNodes.contains(rel)) {
      return true;
    }
  }

  // If the same subset appears more than once along any path from root
  // operand to a leaf operand, we have matched a cycle. A relational
  // expression that consumes its own output can never be implemented, and
  // furthermore, if we fire rules on it we may generate lots of garbage.
  // For example, if
  //   Project(A, X = X + 0)
  // is in the same subset as A, then we would generate
  //   Project(A, X = X + 0 + 0)
  //   Project(A, X = X + 0 + 0 + 0)
  // also in the same subset. They are valid but useless.
  final Deque<RelSubset> subsets = new ArrayDeque<>();
  try {
    checkDuplicateSubsets(subsets, match.rule.getOperand(), match.rels);
  } catch (Util.FoundOne e) {
    return true;
  }
  return false;
}
 
Example 6
Source Project: calcite   Source File: RelMetadataTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testColumnUniquenessForCorrelateWithConstantColumns() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  RelNode rel0 = builder.scan("EMP")
      .project(builder.field("DEPTNO"), builder.field("SAL"))
      .distinct()
      .filter(builder.equals(builder.field("SAL"), builder.literal(1)))
      .build();
  final Holder<RexCorrelVariable> v = Holder.of(null);
  final RelNode rel1 = builder.scan("EMP")
      .variable(v)
      .project(builder.field("DEPTNO"), builder.field("SAL"))
      .filter(
          builder.equals(builder.field(0), builder.field(v.get(), "DEPTNO")))
      .build();
  final RelNode correl = builder.push(rel0)
      .variable(v)
      .push(rel1)
      .correlate(JoinRelType.SEMI, v.get().id, builder.field(2, 0, "DEPTNO"))
      .build();
  final RelMetadataQuery mq = correl.getCluster().getMetadataQuery();
  assertThat(mq.areColumnsUnique(correl, ImmutableBitSet.of(0)), is(true));
}
 
Example 7
Source Project: calcite   Source File: FilterRemoveIsNotDistinctFromRule.java    License: Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  Filter oldFilter = call.rel(0);
  RexNode oldFilterCond = oldFilter.getCondition();

  if (RexUtil.findOperatorCall(
      SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
      oldFilterCond)
      == null) {
    // no longer contains isNotDistinctFromOperator
    return;
  }

  // Now replace all the "a isNotDistinctFrom b"
  // with the RexNode given by RelOptUtil.isDistinctFrom() method

  RemoveIsNotDistinctFromRexShuttle rewriteShuttle =
      new RemoveIsNotDistinctFromRexShuttle(
          oldFilter.getCluster().getRexBuilder());

  final RelBuilder relBuilder = call.builder();
  final RelNode newFilterRel = relBuilder
      .push(oldFilter.getInput())
      .filter(oldFilterCond.accept(rewriteShuttle))
      .build();

  call.transformTo(newFilterRel);
}
 
Example 8
Source Project: calcite   Source File: ToLogicalConverterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testValues() {
  // Equivalent SQL:
  //   VALUES (true, 1), (false, -50) AS t(a, b)
  final RelBuilder builder = builder();
  final RelNode rel =
      builder
          .values(new String[]{"a", "b"}, true, 1, false, -50)
          .build();
  verify(rel,
      "EnumerableValues(tuples=[[{ true, 1 }, { false, -50 }]])\n",
      "LogicalValues(tuples=[[{ true, 1 }, { false, -50 }]])\n");
}
 
Example 9
Source Project: Mycat2   Source File: HBTBaseTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void testRel(RelNode relNode, String expect) {
    Assert.assertEquals(
            expect.replace("\n", "").replace("\r", "").trim()
            ,
            RelOptUtil.toString(relNode, SqlExplainLevel.EXPPLAN_ATTRIBUTES)
                    .replace("\n", "").replace("\r", "").trim()
    );
}
 
Example 10
Source Project: kylin   Source File: OLAPUnionRel.java    License: 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 11
Source Project: Bats   Source File: RelOptListener.java    License: Apache License 2.0 5 votes vote down vote up
public RelEquivalenceEvent(
    Object eventSource,
    RelNode rel,
    Object equivalenceClass,
    boolean isPhysical) {
  super(eventSource, rel);
  this.equivalenceClass = equivalenceClass;
  this.isPhysical = isPhysical;
}
 
Example 12
Source Project: calcite   Source File: SemiJoinRule.java    License: Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Project project = call.rel(0);
  final Join join = call.rel(1);
  final RelNode left = call.rel(2);
  final Aggregate aggregate = call.rel(3);
  perform(call, project, join, left, aggregate);
}
 
Example 13
Source Project: flink   Source File: RelDecorrelator.java    License: Apache License 2.0 5 votes vote down vote up
/** Adds a value generator to satisfy the correlating variables used by
 * a relational expression, if those variables are not already provided by
 * its input. */
private Frame maybeAddValueGenerator(RelNode rel, Frame frame) {
  final CorelMap cm1 = new CorelMapBuilder().build(frame.r, rel);
  if (!cm1.mapRefRelToCorRef.containsKey(rel)) {
    return frame;
  }
  final Collection<CorRef> needs = cm1.mapRefRelToCorRef.get(rel);
  final ImmutableSortedSet<CorDef> haves = frame.corDefOutputs.keySet();
  if (hasAll(needs, haves)) {
    return frame;
  }
  return decorrelateInputWithValueGenerator(rel, frame);
}
 
Example 14
Source Project: Bats   Source File: SortUnionTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Union union = call.rel(1);
  List<RelNode> inputs = new ArrayList<>();
  // Thus we use 'ret' as a flag to identify if we have finished pushing the
  // sort past a union.
  boolean ret = true;
  final RelMetadataQuery mq = call.getMetadataQuery();
  for (RelNode input : union.getInputs()) {
    if (!RelMdUtil.checkInputForCollationAndLimit(mq, input,
        sort.getCollation(), sort.offset, sort.fetch)) {
      ret = false;
      Sort branchSort = sort.copy(sort.getTraitSet(), input,
          sort.getCollation(), sort.offset, sort.fetch);
      inputs.add(branchSort);
    } else {
      inputs.add(input);
    }
  }
  // there is nothing to change
  if (ret) {
    return;
  }
  // create new union and sort
  Union unionCopy = (Union) union
      .copy(union.getTraitSet(), inputs, union.all);
  Sort result = sort.copy(sort.getTraitSet(), unionCopy, sort.getCollation(),
      sort.offset, sort.fetch);
  call.transformTo(result);
}
 
Example 15
Source Project: dremio-oss   Source File: CorrelateRel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Correlate copy(RelTraitSet traitSet,
  RelNode left, RelNode right, CorrelationId correlationId,
  ImmutableBitSet requiredColumns, SemiJoinType joinType) {
  return new CorrelateRel(getCluster(), traitSet, left, right,
    correlationId, requiredColumns, joinType);
}
 
Example 16
Source Project: calcite   Source File: LogicalJoin.java    License: Apache License 2.0 5 votes vote down vote up
@Override public LogicalJoin copy(RelTraitSet traitSet, RexNode conditionExpr,
    RelNode left, RelNode right, JoinRelType joinType, boolean semiJoinDone) {
  assert traitSet.containsIfApplicable(Convention.NONE);
  return new LogicalJoin(getCluster(),
      getCluster().traitSetOf(Convention.NONE), hints, left, right, conditionExpr,
      variablesSet, joinType, semiJoinDone, systemFieldList);
}
 
Example 17
Source Project: Bats   Source File: RelMdUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Boolean areColumnsUnique(RelMetadataQuery mq, RelNode rel,
    List<RexInputRef> columnRefs) {
  ImmutableBitSet.Builder colMask = ImmutableBitSet.builder();
  for (RexInputRef columnRef : columnRefs) {
    colMask.set(columnRef.getIndex());
  }
  return mq.areColumnsUnique(rel, colMask.build());
}
 
Example 18
Source Project: calcite   Source File: StreamRules.java    License: Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Join join = call.rel(1);
  final RelNode left = join.getLeft();
  final RelNode right = join.getRight();

  final LogicalDelta rightWithDelta = LogicalDelta.create(right);
  final LogicalJoin joinL = LogicalJoin.create(left,
      rightWithDelta,
      join.getHints(),
      join.getCondition(),
      join.getVariablesSet(),
      join.getJoinType(),
      join.isSemiJoinDone(),
      ImmutableList.copyOf(join.getSystemFieldList()));

  final LogicalDelta leftWithDelta = LogicalDelta.create(left);
  final LogicalJoin joinR = LogicalJoin.create(leftWithDelta,
      right,
      join.getHints(),
      join.getCondition(),
      join.getVariablesSet(),
      join.getJoinType(),
      join.isSemiJoinDone(),
      ImmutableList.copyOf(join.getSystemFieldList()));

  List<RelNode> inputsToUnion = new ArrayList<>();
  inputsToUnion.add(joinL);
  inputsToUnion.add(joinR);

  final LogicalUnion newNode = LogicalUnion.create(inputsToUnion, true);
  call.transformTo(newNode);
}
 
Example 19
Source Project: calcite   Source File: MaterializedViewRule.java    License: Apache License 2.0 5 votes vote down vote up
/** Currently we only support TableScan - Project - Filter - Inner Join */
protected boolean isValidRelNodePlan(RelNode node, RelMetadataQuery mq) {
  final Multimap<Class<? extends RelNode>, RelNode> m =
          mq.getNodeTypes(node);
  if (m == null) {
    return false;
  }

  for (Entry<Class<? extends RelNode>, Collection<RelNode>> e : m.asMap().entrySet()) {
    Class<? extends RelNode> c = e.getKey();
    if (!TableScan.class.isAssignableFrom(c)
            && !Project.class.isAssignableFrom(c)
            && !Filter.class.isAssignableFrom(c)
            && (!Join.class.isAssignableFrom(c))) {
      // Skip it
      return false;
    }
    if (Join.class.isAssignableFrom(c)) {
      for (RelNode n : e.getValue()) {
        final Join join = (Join) n;
        if (join.getJoinType() != JoinRelType.INNER && !join.isSemiJoin()) {
          // Skip it
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 20
Source Project: Bats   Source File: TopNPrel.java    License: Apache License 2.0 5 votes vote down vote up
private Prel transformTopNToSortAndLimit(List<RelNode> children, RelTraitSet traits, RelCollation collationTrait) {
  SortPrel sortprel = new SortPrel(this.getCluster(), traits, children.get(0), collationTrait);
  RexNode offset = this.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(0),
          this.getCluster().getTypeFactory().createSqlType(SqlTypeName.INTEGER));
  RexNode limit = this.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(this.limit),
          this.getCluster().getTypeFactory().createSqlType(SqlTypeName.INTEGER));
  //SMEX is not needed here because Lateral/Unnest pipeline doesn't support exchanges.
  LimitPrel limitPrel = new LimitPrel(this.getCluster(), traits, sortprel, offset, limit, false, true);
  return limitPrel;
}
 
Example 21
Source Project: dremio-oss   Source File: Foreman.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void planRelTransform(PlannerPhase phase, RelOptPlanner planner, RelNode before, RelNode after, long millisTaken) {
  if (phase == PlannerPhase.PHYSICAL) {
    containsHashAgg = containsHashAggregate(after);
  }
  super.planRelTransform(phase, planner, before, after, millisTaken);
}
 
Example 22
Source Project: Bats   Source File: HashJoinPrel.java    License: Apache License 2.0 5 votes vote down vote up
public HashJoinPrel(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, RexNode condition,
    JoinRelType joinType, boolean swapped, RuntimeFilterDef runtimeFilterDef,
    boolean isRowKeyJoin, int joinControl, boolean semiJoin) throws InvalidRelException {
  super(cluster, traits, left, right, condition, joinType, semiJoin);
  Preconditions.checkArgument(isSemiJoin && !swapped || swapped && !isSemiJoin || (!swapped && !isSemiJoin));
  if (isSemiJoin) {
    Preconditions.checkArgument(!swapped, "swapping of inputs is not allowed for semi-joins");
    Preconditions.checkArgument(validateTraits(traitSet, left, right));
  }
  this.swapped = swapped;
  this.isRowKeyJoin = isRowKeyJoin;
  joincategory = JoinUtils.getJoinCategory(left, right, condition, leftKeys, rightKeys, filterNulls);
  this.runtimeFilterDef = runtimeFilterDef;
  this.joinControl = joinControl;
}
 
Example 23
Source Project: calcite   Source File: JdbcRules.java    License: Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final Intersect intersect = (Intersect) rel;
  if (intersect.all) {
    return null; // INTERSECT ALL not implemented
  }
  final RelTraitSet traitSet =
      intersect.getTraitSet().replace(out);
  return new JdbcIntersect(rel.getCluster(), traitSet,
      convertList(intersect.getInputs(), out), false);
}
 
Example 24
Source Project: Bats   Source File: RelDecorrelator.java    License: Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
    return new RexVisitorImpl<Void>(true) {
        @Override
        public Void visitFieldAccess(RexFieldAccess fieldAccess) {
            final RexNode ref = fieldAccess.getReferenceExpr();
            if (ref instanceof RexCorrelVariable) {
                final RexCorrelVariable var = (RexCorrelVariable) ref;
                if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
                    // for cases where different Rel nodes are referring to
                    // same correlation var (e.g. in case of NOT IN)
                    // avoid generating another correlation var
                    // and record the 'rel' is using the same correlation
                    mapRefRelToCorRef.put(rel, mapFieldAccessToCorVar.get(fieldAccess));
                } else {
                    final CorRef correlation = new CorRef(var.getCorrelationId(),
                            fieldAccess.getField().getIndex(), corrIdGenerator++);
                    mapFieldAccessToCorVar.put(fieldAccess, correlation);
                    mapRefRelToCorRef.put(rel, correlation);
                }
            }
            return super.visitFieldAccess(fieldAccess);
        }

        @Override
        public Void visitSubQuery(RexSubQuery subQuery) {
            subQuery.getRel().accept(CorelMapBuilder.this);
            return super.visitSubQuery(subQuery);
        }
    };
}
 
Example 25
Source Project: Bats   Source File: RelBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns references to the fields of a given input. */
public ImmutableList<RexNode> fields(int inputCount, int inputOrdinal) {
    final RelNode input = peek(inputCount, inputOrdinal);
    final RelDataType rowType = input.getRowType();
    final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder();
    for (int fieldOrdinal : Util.range(rowType.getFieldCount())) {
        nodes.add(field(inputCount, inputOrdinal, fieldOrdinal));
    }
    return nodes.build();
}
 
Example 26
Source Project: Bats   Source File: TableFunctionScan.java    License: Apache License 2.0 5 votes vote down vote up
public RelNode accept(RexShuttle shuttle) {
  RexNode rexCall = shuttle.apply(this.rexCall);
  if (rexCall == this.rexCall) {
    return this;
  }
  return copy(traitSet, inputs, rexCall, elementType, rowType,
      columnMappings);
}
 
Example 27
Source Project: calcite   Source File: AggregateCall.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static AggregateCall create(SqlAggFunction aggFunction,
    boolean distinct, boolean approximate, List<Integer> argList,
    int filterArg, RelCollation collation, int groupCount,
    RelNode input, RelDataType type, String name) {
  return create(aggFunction, distinct, approximate, false, argList, filterArg,
      collation, groupCount, input, type, name);
}
 
Example 28
Source Project: calcite   Source File: CollationConversionTest.java    License: Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  NoneSingleRel single = call.rel(0);
  RelNode input = single.getInput();
  RelNode physInput =
      convert(input,
          single.getTraitSet()
              .replace(PHYS_CALLING_CONVENTION)
              .plus(ROOT_COLLATION));
  call.transformTo(
      new RootSingleRel(
          single.getCluster(),
          physInput));
}
 
Example 29
Source Project: calcite   Source File: SparkRules.java    License: Apache License 2.0 5 votes vote down vote up
public SparkCalc(RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode input,
    RexProgram program) {
  super(cluster, traitSet, input);
  assert getConvention() == SparkRel.CONVENTION;
  assert !program.containsAggs();
  this.program = program;
  this.rowType = program.getOutputRowType();
}
 
Example 30
Source Project: calcite   Source File: RelMetadataTest.java    License: Apache License 2.0 5 votes vote down vote up
private void checkExchangeRowCount(RelNode rel, double expected, double expectedMin,
    double expectedMax) {
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final Double result = mq.getRowCount(rel);
  assertThat(result, notNullValue());
  assertThat(result, is(expected));
  final Double max = mq.getMaxRowCount(rel);
  assertThat(max, notNullValue());
  assertThat(max, is(expectedMax));
  final Double min = mq.getMinRowCount(rel);
  assertThat(min, notNullValue());
  assertThat(min, is(expectedMin));
}