org.apache.calcite.rel.RelNode Java Examples

The following examples show how to use org.apache.calcite.rel.RelNode. 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: RelOptUtil.java    From calcite with 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 #2
Source File: VolcanoPlanner.java    From Bats with 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 #3
Source File: SortRemoveRuleTest.java    From calcite with 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 #4
Source File: RuleQueue.java    From calcite with 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 #5
Source File: RelBuilderTest.java    From calcite with 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 #6
Source File: RelMetadataTest.java    From calcite with 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 File: LogicalJoin.java    From calcite with 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 #8
Source File: TopNPrel.java    From Bats with 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 #9
Source File: MaterializedViewRule.java    From calcite with 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 #10
Source File: StreamRules.java    From calcite with 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 #11
Source File: HashJoinPrel.java    From Bats with 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 #12
Source File: JoinInfo.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@code JoinInfo} by analyzing a condition. */
public static JoinInfo of(RelNode left, RelNode right, RexNode condition) {
  final List<Integer> leftKeys = new ArrayList<>();
  final List<Integer> rightKeys = new ArrayList<>();
  final List<Boolean> filterNulls = new ArrayList<>();
  final List<RexNode> nonEquiList = new ArrayList<>();
  RelOptUtil.splitJoinCondition(left, right, condition, leftKeys, rightKeys,
      filterNulls, nonEquiList);
  return new JoinInfo(ImmutableIntList.copyOf(leftKeys),
      ImmutableIntList.copyOf(rightKeys), ImmutableList.copyOf(nonEquiList));
}
 
Example #13
Source File: CorrelateRel.java    From dremio-oss with 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 #14
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  for (Program program : programs) {
    rel = program.run(
        planner, rel, requiredOutputTraits, materializations, lattices);
  }
  return rel;
}
 
Example #15
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 #16
Source File: RelOptListener.java    From Bats with 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 #17
Source File: OLAPUnionRel.java    From kylin with 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 #18
Source File: HBTBaseTest.java    From Mycat2 with 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 #19
Source File: Foreman.java    From dremio-oss with 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 #20
Source File: RelMdUtil.java    From Bats with 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 #21
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testAggregateWithoutAlias() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  // The rel node stands for sql: SELECT max(SAL) from EMP group by JOB;
  final RelNode rel = builder
      .scan("EMP")
      .project(
          builder.field("JOB"),
          builder.field("SAL"))
      .aggregate(
          builder.groupKey("JOB"),
          builder.max(builder.field("SAL")))
      .project(
          builder.field(1))
      .build();
  final RelJsonWriter jsonWriter = new RelJsonWriter();
  rel.explain(jsonWriter);
  final String relJson = jsonWriter.asString();
  String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
  final String expected = ""
      + "LogicalProject($f1=[$1])\n"
      + "  LogicalAggregate(group=[{0}], agg#0=[MAX($1)])\n"
      + "    LogicalProject(JOB=[$2], SAL=[$5])\n"
      + "      LogicalTableScan(table=[[scott, EMP]])\n";

  assertThat(s, isLinux(expected));
}
 
Example #22
Source File: RelBuilderTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testExchange() {
  final RelBuilder builder = RelBuilder.create(config().build());
  final RelNode root = builder.scan("EMP")
      .exchange(RelDistributions.hash(Lists.newArrayList(0)))
      .build();
  final String expected =
      "LogicalExchange(distribution=[hash[0]])\n"
          + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(root, hasTree(expected));
}
 
Example #23
Source File: RelMetadataTest.java    From calcite with 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));
}
 
Example #24
Source File: JdbcRules.java    From calcite with 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 #25
Source File: RelDecorrelator.java    From Bats with 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 #26
Source File: RelBuilder.java    From Bats with 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 #27
Source File: TableFunctionScan.java    From Bats with 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 #28
Source File: AggregateCall.java    From calcite with 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 #29
Source File: SparkRules.java    From calcite with 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 File: CollationConversionTest.java    From calcite with 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));
}