Java Code Examples for org.apache.calcite.rel.logical.LogicalJoin#create()

The following examples show how to use org.apache.calcite.rel.logical.LogicalJoin#create() . 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: StreamRules.java    From Bats with Apache License 2.0 6 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.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.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 2
Source File: FlinkSemiAntiJoinFilterTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	LogicalJoin join = call.rel(0);
	LogicalFilter filter = call.rel(1);

	RelNode newJoin = LogicalJoin.create(
			filter.getInput(),
			join.getRight(),
			join.getCondition(),
			join.getVariablesSet(),
			join.getJoinType());

	final RelFactories.FilterFactory factory =
			RelFactories.DEFAULT_FILTER_FACTORY;
	RelNode newFilter =
			factory.createFilter(newJoin, filter.getCondition());

	call.transformTo(newFilter);
}
 
Example 3
Source File: FlinkSemiAntiJoinProjectTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	LogicalJoin join = call.rel(0);
	LogicalProject project = call.rel(1);

	// convert the semi/anti join condition to reflect the LHS with the project
	// pulled up
	RexNode newCondition = adjustCondition(project, join);
	Join newJoin = LogicalJoin.create(
			project.getInput(), join.getRight(), newCondition, join.getVariablesSet(), join.getJoinType());

	// Create the new projection. Note that the projection expressions
	// are the same as the original because they only reference the LHS
	// of the semi/anti join and the semi/anti join only projects out the LHS
	final RelBuilder relBuilder = call.builder();
	relBuilder.push(newJoin);
	relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());

	call.transformTo(relBuilder.build());
}
 
Example 4
Source File: FlinkSemiAntiJoinFilterTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	LogicalJoin join = call.rel(0);
	LogicalFilter filter = call.rel(1);

	RelNode newJoin = LogicalJoin.create(
			filter.getInput(),
			join.getRight(),
			join.getCondition(),
			join.getVariablesSet(),
			join.getJoinType());

	final RelFactories.FilterFactory factory =
			RelFactories.DEFAULT_FILTER_FACTORY;
	RelNode newFilter =
			factory.createFilter(newJoin, filter.getCondition());

	call.transformTo(newFilter);
}
 
Example 5
Source File: FlinkSemiAntiJoinProjectTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	LogicalJoin join = call.rel(0);
	LogicalProject project = call.rel(1);

	// convert the semi/anti join condition to reflect the LHS with the project
	// pulled up
	RexNode newCondition = adjustCondition(project, join);
	Join newJoin = LogicalJoin.create(
			project.getInput(), join.getRight(), newCondition, join.getVariablesSet(), join.getJoinType());

	// Create the new projection. Note that the projection expressions
	// are the same as the original because they only reference the LHS
	// of the semi/anti join and the semi/anti join only projects out the LHS
	final RelBuilder relBuilder = call.builder();
	relBuilder.push(newJoin);
	relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());

	call.transformTo(relBuilder.build());
}
 
Example 6
Source File: SemiJoinFilterTransposeRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalJoin semiJoin = call.rel(0);
  LogicalFilter filter = call.rel(1);

  RelNode newSemiJoin =
      LogicalJoin.create(filter.getInput(),
          semiJoin.getRight(),
          // No need to copy the hints, the framework would try to do that.
          ImmutableList.of(),
          semiJoin.getCondition(),
          ImmutableSet.of(),
          JoinRelType.SEMI);

  final RelFactories.FilterFactory factory =
      RelFactories.DEFAULT_FILTER_FACTORY;
  RelNode newFilter =
      factory.createFilter(newSemiJoin, filter.getCondition(),
          ImmutableSet.of());

  call.transformTo(newFilter);
}
 
Example 7
Source File: DrillJoinRelBase.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public double estimateRowCount(RelMetadataQuery mq) {
  if (this.condition.isAlwaysTrue()) {
    return joinRowFactor * this.getLeft().estimateRowCount(mq) * this.getRight().estimateRowCount(mq);
  }

  int[] joinFields = new int[2];

  LogicalJoin jr = LogicalJoin.create(this.getLeft(), this.getRight(), this.getCondition(),
          this.getVariablesSet(), this.getJoinType());

  if (!DrillRelOptUtil.guessRows(this)         //Statistics present for left and right side of the join
      && jr.getJoinType() == JoinRelType.INNER
      && DrillRelOptUtil.analyzeSimpleEquiJoin((Join)jr, joinFields)) {
    ImmutableBitSet leq = ImmutableBitSet.of(joinFields[0]);
    ImmutableBitSet req = ImmutableBitSet.of(joinFields[1]);

    Double ldrc = mq.getDistinctRowCount(this.getLeft(), leq, null);
    Double rdrc = mq.getDistinctRowCount(this.getRight(), req, null);

    Double lrc = mq.getRowCount(this.getLeft());
    Double rrc = mq.getRowCount(this.getRight());

    if (ldrc != null && rdrc != null && lrc != null && rrc != null) {
      // Join cardinality = (lrc * rrc) / Math.max(ldrc, rdrc). Avoid overflow by dividing earlier
      return (lrc / Math.max(ldrc, rdrc)) * rrc;
    }
  }

  return joinRowFactor * Math.max(
      mq.getRowCount(this.getLeft()),
      mq.getRowCount(this.getRight()));
}
 
Example 8
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 9
Source File: JoinAddRedundantSemiJoinRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  Join origJoinRel = call.rel(0);
  if (origJoinRel.isSemiJoinDone()) {
    return;
  }

  // can't process outer joins using semijoins
  if (origJoinRel.getJoinType() != JoinRelType.INNER) {
    return;
  }

  // determine if we have a valid join condition
  final JoinInfo joinInfo = origJoinRel.analyzeCondition();
  if (joinInfo.leftKeys.size() == 0) {
    return;
  }

  RelNode semiJoin =
      LogicalJoin.create(origJoinRel.getLeft(),
          origJoinRel.getRight(),
          ImmutableList.of(),
          origJoinRel.getCondition(),
          ImmutableSet.of(),
          JoinRelType.SEMI);

  RelNode newJoinRel =
      origJoinRel.copy(
          origJoinRel.getTraitSet(),
          origJoinRel.getCondition(),
          semiJoin,
          origJoinRel.getRight(),
          JoinRelType.INNER,
          true);

  call.transformTo(newJoinRel);
}
 
Example 10
Source File: SemiJoinProjectTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalJoin semiJoin = call.rel(0);
  LogicalProject project = call.rel(1);

  // Convert the LHS semi-join keys to reference the child projection
  // expression; all projection expressions must be RexInputRefs,
  // otherwise, we wouldn't have created this semi-join.

  // convert the semijoin condition to reflect the LHS with the project
  // pulled up
  RexNode newCondition = adjustCondition(project, semiJoin);

  LogicalJoin newSemiJoin =
      LogicalJoin.create(project.getInput(),
          semiJoin.getRight(),
          // No need to copy the hints, the framework would try to do that.
          ImmutableList.of(),
          newCondition,
          ImmutableSet.of(), JoinRelType.SEMI);

  // Create the new projection.  Note that the projection expressions
  // are the same as the original because they only reference the LHS
  // of the semijoin and the semijoin only projects out the LHS
  final RelBuilder relBuilder = call.builder();
  relBuilder.push(newSemiJoin);
  relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());

  call.transformTo(relBuilder.build());
}
 
Example 11
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalJoin rel) {
  final LogicalJoin newRel =
      LogicalJoin.create(getNewForOldRel(rel.getLeft()),
          getNewForOldRel(rel.getRight()),
          rel.getHints(),
          rel.getCondition().accept(new RewriteRexShuttle()),
          rel.getVariablesSet(), rel.getJoinType());
  setNewForOldRel(rel, newRel);
}
 
Example 12
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTableReferencesJoinUnknownNode() {
  final String sql = "select * from emp limit 10";
  final RelNode node = convertSql(sql);
  final RelNode nodeWithUnknown = new DummyRelNode(
      node.getCluster(), node.getTraitSet(), node);
  final RexBuilder rexBuilder = node.getCluster().getRexBuilder();
  // Join
  final LogicalJoin join =
      LogicalJoin.create(nodeWithUnknown, node, ImmutableList.of(),
          rexBuilder.makeLiteral(true), ImmutableSet.of(), JoinRelType.INNER);
  final RelMetadataQuery mq = node.getCluster().getMetadataQuery();
  final Set<RelTableRef> tableReferences = mq.getTableReferences(join);
  assertNull(tableReferences);
}
 
Example 13
Source File: RelFactories.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelNode createJoin(RelNode left, RelNode right,
    RexNode condition, Set<CorrelationId> variablesSet,
    JoinRelType joinType, boolean semiJoinDone) {
  return LogicalJoin.create(left, right, condition, variablesSet, joinType,
      semiJoinDone, ImmutableList.of());
}
 
Example 14
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void rewriteRel(LogicalJoin rel) {
    LogicalJoin newRel = LogicalJoin.create(getNewForOldRel(rel.getLeft()), getNewForOldRel(rel.getRight()),
            rel.getCondition().accept(new RewriteRexShuttle()), rel.getVariablesSet(), rel.getJoinType());
    setNewForOldRel(rel, newRel);
}
 
Example 15
Source File: RelFactories.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode createJoin(RelNode left, RelNode right, List<RelHint> hints,
    RexNode condition, Set<CorrelationId> variablesSet,
    JoinRelType joinType, boolean semiJoinDone) {
  return LogicalJoin.create(left, right, hints, condition, variablesSet, joinType,
      semiJoinDone, ImmutableList.of());
}
 
Example 16
Source File: RelFactories.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode createSemiJoin(RelNode left, RelNode right,
    RexNode condition) {
  return LogicalJoin.create(left, right, ImmutableList.of(), condition,
      ImmutableSet.of(), JoinRelType.SEMI, false, ImmutableList.of());
}