org.apache.calcite.rel.logical.LogicalCorrelate Java Examples

The following examples show how to use org.apache.calcite.rel.logical.LogicalCorrelate. 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: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void rewriteRel(LogicalCorrelate rel) {
  ImmutableBitSet.Builder newPos = ImmutableBitSet.builder();
  for (int pos : rel.getRequiredColumns()) {
    RelDataType corrFieldType =
        rel.getLeft().getRowType().getFieldList().get(pos)
            .getType();
    if (corrFieldType.isStruct()) {
      throw Util.needToImplement("correlation on structured type");
    }
    newPos.set(getNewForOldInput(pos));
  }
  LogicalCorrelate newRel =
      LogicalCorrelate.create(getNewForOldRel(rel.getLeft()),
          getNewForOldRel(rel.getRight()),
          rel.getCorrelationId(),
          newPos.build(),
          rel.getJoinType());
  setNewForOldRel(rel, newRel);
}
 
Example #2
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
private Function2<RelNode, RelNode, Void> createCopyHook() {
  return (oldNode, newNode) -> {
    if (cm.mapRefRelToCorRef.containsKey(oldNode)) {
      cm.mapRefRelToCorRef.putAll(newNode,
          cm.mapRefRelToCorRef.get(oldNode));
    }
    if (oldNode instanceof LogicalCorrelate
        && newNode instanceof LogicalCorrelate) {
      LogicalCorrelate oldCor = (LogicalCorrelate) oldNode;
      CorrelationId c = oldCor.getCorrelationId();
      if (cm.mapCorToCorRel.get(c) == oldNode) {
        cm.mapCorToCorRel.put(c, newNode);
      }

      if (generatedCorRels.contains(oldNode)) {
        generatedCorRels.add((LogicalCorrelate) newNode);
      }
    }
    return null;
  };
}
 
Example #3
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalCorrelate correlate) {
	// TODO does not allow correlation condition in its inputs now
	// If correlation conditions in correlate inputs reference to correlate outputs variable,
	// that should not be supported, e.g.
	// SELECT * FROM outer_table l WHERE l.c IN (
	//  SELECT f1 FROM (
	//   SELECT * FROM inner_table r WHERE r.d IN (SELECT x.i FROM x WHERE x.j = l.b)) t,
	//   LATERAL TABLE(table_func(t.f)) AS T(f1)
	//  ))
	// other cases should be supported, e.g.
	// SELECT * FROM outer_table l WHERE l.c IN (
	//  SELECT f1 FROM (
	//   SELECT * FROM inner_table r WHERE r.d IN (SELECT x.i FROM x WHERE x.j = r.e)) t,
	//   LATERAL TABLE(table_func(t.f)) AS T(f1)
	//  ))
	checkCorConditionOfInput(correlate.getLeft());
	checkCorConditionOfInput(correlate.getRight());

	visitChild(correlate, 0, correlate.getLeft());
	visitChild(correlate, 1, correlate.getRight());
	return correlate;
}
 
Example #4
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode visitInputRef(RexInputRef inputRef) {
    if (currentRel instanceof LogicalCorrelate) {
        // if this rel references corVar
        // and now it needs to be rewritten
        // it must have been pulled above the Correlate
        // replace the input ref to account for the LHS of the
        // Correlate
        final int leftInputFieldCount = ((LogicalCorrelate) currentRel).getLeft().getRowType().getFieldCount();
        RelDataType newType = inputRef.getType();

        if (projectPulledAboveLeftCorrelator) {
            newType = typeFactory.createTypeWithNullability(newType, true);
        }

        int pos = inputRef.getIndex();
        RexInputRef newInputRef = RexBuilder.getRexFactory().makeInputRef(leftInputFieldCount + pos, newType);

        if ((isCount != null) && isCount.contains(pos)) {
            return createCaseExpression(newInputRef, rexBuilder.makeExactLiteral(BigDecimal.ZERO), newInputRef);
        } else {
            return newInputRef;
        }
    }
    return inputRef;
}
 
Example #5
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalCorrelate correlate) {
	// TODO does not allow correlation condition in its inputs now
	// If correlation conditions in correlate inputs reference to correlate outputs variable,
	// that should not be supported, e.g.
	// SELECT * FROM outer_table l WHERE l.c IN (
	//  SELECT f1 FROM (
	//   SELECT * FROM inner_table r WHERE r.d IN (SELECT x.i FROM x WHERE x.j = l.b)) t,
	//   LATERAL TABLE(table_func(t.f)) AS T(f1)
	//  ))
	// other cases should be supported, e.g.
	// SELECT * FROM outer_table l WHERE l.c IN (
	//  SELECT f1 FROM (
	//   SELECT * FROM inner_table r WHERE r.d IN (SELECT x.i FROM x WHERE x.j = r.e)) t,
	//   LATERAL TABLE(table_func(t.f)) AS T(f1)
	//  ))
	checkCorConditionOfInput(correlate.getLeft());
	checkCorConditionOfInput(correlate.getRight());

	visitChild(correlate, 0, correlate.getLeft());
	visitChild(correlate, 1, correlate.getRight());
	return correlate;
}
 
Example #6
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
private Function2<RelNode, RelNode, Void> createCopyHook() {
  return (oldNode, newNode) -> {
    if (cm.mapRefRelToCorRef.containsKey(oldNode)) {
      cm.mapRefRelToCorRef.putAll(newNode,
          cm.mapRefRelToCorRef.get(oldNode));
    }
    if (oldNode instanceof LogicalCorrelate
        && newNode instanceof LogicalCorrelate) {
      LogicalCorrelate oldCor = (LogicalCorrelate) oldNode;
      CorrelationId c = oldCor.getCorrelationId();
      if (cm.mapCorToCorRel.get(c) == oldNode) {
        cm.mapCorToCorRel.put(c, newNode);
      }

      if (generatedCorRels.contains(oldNode)) {
        generatedCorRels.add((LogicalCorrelate) newNode);
      }
    }
    return null;
  };
}
 
Example #7
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 6 votes vote down vote up
private Function2<RelNode, RelNode, Void> createCopyHook() {
    return (oldNode, newNode) -> {
        if (cm.mapRefRelToCorRef.containsKey(oldNode)) {
            cm.mapRefRelToCorRef.putAll(newNode, cm.mapRefRelToCorRef.get(oldNode));
        }
        if (oldNode instanceof LogicalCorrelate && newNode instanceof LogicalCorrelate) {
            LogicalCorrelate oldCor = (LogicalCorrelate) oldNode;
            CorrelationId c = oldCor.getCorrelationId();
            if (cm.mapCorToCorRel.get(c) == oldNode) {
                cm.mapCorToCorRel.put(c, newNode);
            }

            if (generatedCorRels.contains(oldNode)) {
                generatedCorRels.add((LogicalCorrelate) newNode);
            }
        }
        return null;
    };
}
 
Example #8
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
AdjustProjectForCountAggregateRule(boolean flavor,
    RelBuilderFactory relBuilderFactory) {
  super(
      flavor
          ? operand(LogicalCorrelate.class,
              operand(RelNode.class, any()),
                  operand(LogicalProject.class,
                      operand(LogicalAggregate.class, any())))
          : operand(LogicalCorrelate.class,
              operand(RelNode.class, any()),
                  operand(LogicalAggregate.class, any())),
      relBuilderFactory, null);
  this.flavor = flavor;
}
 
Example #9
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
AdjustProjectForCountAggregateRule(boolean flavor,
    RelBuilderFactory relBuilderFactory) {
  super(
      flavor
          ? operand(LogicalCorrelate.class,
              operand(RelNode.class, any()),
                  operand(LogicalProject.class,
                      operand(LogicalAggregate.class, any())))
          : operand(LogicalCorrelate.class,
              operand(RelNode.class, any()),
                  operand(LogicalAggregate.class, any())),
      relBuilderFactory, null);
  this.flavor = flavor;
}
 
Example #10
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitInputRef(RexInputRef inputRef) {
  if (currentRel instanceof LogicalCorrelate) {
    // if this rel references corVar
    // and now it needs to be rewritten
    // it must have been pulled above the Correlate
    // replace the input ref to account for the LHS of the
    // Correlate
    final int leftInputFieldCount =
        ((LogicalCorrelate) currentRel).getLeft().getRowType()
            .getFieldCount();
    RelDataType newType = inputRef.getType();

    if (projectPulledAboveLeftCorrelator) {
      newType =
          typeFactory.createTypeWithNullability(newType, true);
    }

    int pos = inputRef.getIndex();
    RexInputRef newInputRef =
        new RexInputRef(leftInputFieldCount + pos, newType);

    if ((isCount != null) && isCount.contains(pos)) {
      return createCaseExpression(
          newInputRef,
          rexBuilder.makeExactLiteral(BigDecimal.ZERO),
          newInputRef);
    } else {
      return newInputRef;
    }
  }
  return inputRef;
}
 
Example #11
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarAggregateRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalCorrelate.class,
          operand(RelNode.class, any()),
          operand(LogicalProject.class,
              operandJ(LogicalAggregate.class, null, Aggregate::isSimple,
                  operand(LogicalProject.class,
                      operand(RelNode.class, any()))))),
      relBuilderFactory, null);
}
 
Example #12
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarProjectRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalCorrelate.class,
          operand(RelNode.class, any()),
          operand(LogicalAggregate.class,
              operand(LogicalProject.class,
                  operand(RelNode.class, any())))),
      relBuilderFactory, null);
}
 
Example #13
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitInputRef(RexInputRef inputRef) {
  if (currentRel instanceof LogicalCorrelate) {
    // if this rel references corVar
    // and now it needs to be rewritten
    // it must have been pulled above the Correlate
    // replace the input ref to account for the LHS of the
    // Correlate
    final int leftInputFieldCount =
        ((LogicalCorrelate) currentRel).getLeft().getRowType()
            .getFieldCount();
    RelDataType newType = inputRef.getType();

    if (projectPulledAboveLeftCorrelator) {
      newType =
          typeFactory.createTypeWithNullability(newType, true);
    }

    int pos = inputRef.getIndex();
    RexInputRef newInputRef =
        new RexInputRef(leftInputFieldCount + pos, newType);

    if ((isCount != null) && isCount.contains(pos)) {
      return createCaseExpression(
          newInputRef,
          rexBuilder.makeExactLiteral(BigDecimal.ZERO),
          newInputRef);
    } else {
      return newInputRef;
    }
  }
  return inputRef;
}
 
Example #14
Source File: CopyWithCluster.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalCorrelate correlate) {
  final RelNode left = correlate.getLeft().accept(this);
  final RelNode right = correlate.getRight().accept(this);

  return new LogicalCorrelate(
    cluster,
    copyOf(correlate.getTraitSet()),
    left,
    right,
    correlate.getCorrelationId(),
    correlate.getRequiredColumns(),
    correlate.getJoinType()
  );
}
 
Example #15
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarProjectRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalCorrelate.class,
          operand(RelNode.class, any()),
          operand(LogicalAggregate.class,
              operand(LogicalProject.class,
                  operand(RelNode.class, any())))),
      relBuilderFactory, null);
}
 
Example #16
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarAggregateRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalCorrelate.class,
          operand(RelNode.class, any()),
          operand(LogicalProject.class,
              operandJ(LogicalAggregate.class, null, Aggregate::isSimple,
                  operand(LogicalProject.class,
                      operand(RelNode.class, any()))))),
      relBuilderFactory, null);
}
 
Example #17
Source File: CorrelateRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalCorrelate correlate = call.rel(0);
  final RelTraitSet traits = correlate.getTraitSet().plus(Rel.LOGICAL);
  final RelNode left = correlate.getInput(0);
  final RelNode right = correlate.getInput(1);
  final RelNode convertedLeftInput = convert(left, left.getTraitSet().plus(Rel.LOGICAL).simplify());
  final RelNode convertedRightInput = convert(right, right.getTraitSet().plus(Rel.LOGICAL).simplify());
  CorrelateRel newRel = new CorrelateRel(correlate.getCluster(), traits, convertedLeftInput,
          convertedRightInput, correlate.getCorrelationId(), correlate.getRequiredColumns(), correlate.getJoinType());
  call.transformTo(newRel);
}
 
Example #18
Source File: DrillCorrelateRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalCorrelate correlate = call.rel(0);
  final RelNode left = correlate.getLeft();
  final RelNode right = correlate.getRight();
  final RelNode convertedLeft = convert(left, left.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());
  final RelNode convertedRight = convert(right, right.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());

  final RelTraitSet traits = correlate.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
  DrillLateralJoinRel lateralJoinRel = new DrillLateralJoinRel(correlate.getCluster(),
      traits, convertedLeft, convertedRight, false, correlate.getCorrelationId(),
      correlate.getRequiredColumns(), correlate.getJoinType());
  call.transformTo(lateralJoinRel);
}
 
Example #19
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalCorrelate rel) {
    ImmutableBitSet.Builder newPos = ImmutableBitSet.builder();
    for (int pos : rel.getRequiredColumns()) {
        RelDataType corrFieldType = rel.getLeft().getRowType().getFieldList().get(pos).getType();
        if (corrFieldType.isStruct()) {
            throw Util.needToImplement("correlation on structured type");
        }
        newPos.set(getNewForOldInput(pos));
    }
    LogicalCorrelate newRel = LogicalCorrelate.create(getNewForOldRel(rel.getLeft()),
            getNewForOldRel(rel.getRight()), rel.getCorrelationId(), newPos.build(), rel.getJoinType());
    setNewForOldRel(rel, newRel);
}
 
Example #20
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void updateRelInMap(
    SortedMap<CorrelationId, LogicalCorrelate> mapCorVarToCorRel) {
  for (CorrelationId corVar : mapCorVarToCorRel.keySet()) {
    LogicalCorrelate oldRel = mapCorVarToCorRel.get(corVar);
    if (oldToNewRelMap.containsKey(oldRel)) {
      RelNode newRel = oldToNewRelMap.get(oldRel);
      assert newRel instanceof LogicalCorrelate;
      mapCorVarToCorRel.put(corVar, (LogicalCorrelate) newRel);
    }
  }
}
 
Example #21
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void updateRelInMap(SortedMap<CorrelationId, LogicalCorrelate> mapCorVarToCorRel) {
    for (CorrelationId corVar : mapCorVarToCorRel.keySet()) {
        LogicalCorrelate oldRel = mapCorVarToCorRel.get(corVar);
        if (oldToNewRelMap.containsKey(oldRel)) {
            RelNode newRel = oldToNewRelMap.get(oldRel);
            assert newRel instanceof LogicalCorrelate;
            mapCorVarToCorRel.put(corVar, (LogicalCorrelate) newRel);
        }
    }
}
 
Example #22
Source File: EnumerableCorrelateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalCorrelate c = (LogicalCorrelate) rel;
  return EnumerableCorrelate.create(
      convert(c.getLeft(), c.getLeft().getTraitSet()
          .replace(EnumerableConvention.INSTANCE)),
      convert(c.getRight(), c.getRight().getTraitSet()
          .replace(EnumerableConvention.INSTANCE)),
      c.getCorrelationId(),
      c.getRequiredColumns(),
      c.getJoinType());
}
 
Example #23
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
AdjustProjectForCountAggregateRule(boolean flavor, RelBuilderFactory relBuilderFactory) {
    super(flavor
            ? operand(LogicalCorrelate.class, operand(RelNode.class, any()),
                    operand(LogicalProject.class, operand(LogicalAggregate.class, any())))
            : operand(LogicalCorrelate.class, operand(RelNode.class, any()),
                    operand(LogicalAggregate.class, any())),
            relBuilderFactory, null);
    this.flavor = flavor;
}
 
Example #24
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarAggregateRule(RelBuilderFactory relBuilderFactory) {
    super(operand(LogicalCorrelate.class, operand(RelNode.class, any()),
            operand(LogicalProject.class,
                    operandJ(LogicalAggregate.class, null, Aggregate::isSimple,
                            operand(LogicalProject.class, operand(RelNode.class, any()))))),
            relBuilderFactory, null);
}
 
Example #25
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public RelNode visit(LogicalCorrelate correlate) {
  mapCorToCorRel.put(correlate.getCorrelationId(), correlate);
  return visitJoin(correlate);
}
 
Example #26
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
public Frame decorrelateRel(LogicalCorrelate rel) {
	// does not allow correlation condition in its inputs now, so choose default behavior
	return decorrelateRel((RelNode) rel);
}
 
Example #27
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Frame decorrelateRel(LogicalCorrelate rel) {
  return decorrelateRel((Correlate) rel);
}
 
Example #28
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
private void onMatch2(
    RelOptRuleCall call,
    LogicalCorrelate correlate,
    RelNode leftInput,
    LogicalProject aggOutputProject,
    LogicalAggregate aggregate) {
  if (generatedCorRels.contains(correlate)) {
    // This Correlate was generated by a previous invocation of
    // this rule. No further work to do.
    return;
  }

  setCurrent(call.getPlanner().getRoot(), correlate);

  // check for this pattern
  // The pattern matching could be simplified if rules can be applied
  // during decorrelation,
  //
  // CorrelateRel(left correlation, condition = true)
  //   leftInput
  //   Project-A (a RexNode)
  //     Aggregate (groupby (0), agg0(), agg1()...)

  // check aggOutputProj projects only one expression
  List<RexNode> aggOutputProjExprs = aggOutputProject.getProjects();
  if (aggOutputProjExprs.size() != 1) {
    return;
  }

  JoinRelType joinType = correlate.getJoinType();
  // corRel.getCondition was here, however Correlate was updated so it
  // never includes a join condition. The code was not modified for brevity.
  RexNode joinCond = relBuilder.literal(true);
  if ((joinType != JoinRelType.LEFT)
      || (joinCond != relBuilder.literal(true))) {
    return;
  }

  // check that the agg is on the entire input
  if (!aggregate.getGroupSet().isEmpty()) {
    return;
  }

  List<AggregateCall> aggCalls = aggregate.getAggCallList();
  Set<Integer> isCount = new HashSet<>();

  // remember the count() positions
  int i = -1;
  for (AggregateCall aggCall : aggCalls) {
    ++i;
    if (aggCall.getAggregation() instanceof SqlCountAggFunction) {
      isCount.add(i);
    }
  }

  // now rewrite the plan to
  //
  // Project-A' (all LHS plus transformed original projections,
  //             replacing references to count() with case statement)
  //   Correlate(left correlation, condition = true)
  //     leftInput
  //     Aggregate(groupby (0), agg0(), agg1()...)
  //
  LogicalCorrelate newCorrelate =
      LogicalCorrelate.create(leftInput, aggregate,
          correlate.getCorrelationId(), correlate.getRequiredColumns(),
          correlate.getJoinType());

  // remember this rel so we don't fire rule on it again
  // REVIEW jhyde 29-Oct-2007: rules should not save state; rule
  // should recognize patterns where it does or does not need to do
  // work
  generatedCorRels.add(newCorrelate);

  // need to update the mapCorToCorRel Update the output position
  // for the corVars: only pass on the corVars that are not used in
  // the join key.
  if (cm.mapCorToCorRel.get(correlate.getCorrelationId()) == correlate) {
    cm.mapCorToCorRel.put(correlate.getCorrelationId(), newCorrelate);
  }

  RelNode newOutput =
      aggregateCorrelatorOutput(newCorrelate, aggOutputProject, isCount);

  call.transformTo(newOutput);
}
 
Example #29
Source File: EnumerableCorrelateRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an EnumerableCorrelateRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public EnumerableCorrelateRule(RelBuilderFactory relBuilderFactory) {
  super(LogicalCorrelate.class, (Predicate<RelNode>) r -> true,
      Convention.NONE, EnumerableConvention.INSTANCE, relBuilderFactory,
      "EnumerableCorrelateRule");
}
 
Example #30
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public RelNode visit(LogicalCorrelate correlate) {
  mapCorToCorRel.put(correlate.getCorrelationId(), correlate);
  return visitJoin(correlate);
}