Java Code Examples for org.apache.calcite.tools.RelBuilder#join()

The following examples show how to use org.apache.calcite.tools.RelBuilder#join() . 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: SubQueryRemoveRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites an EXISTS RexSubQuery into a {@link Join}.
 *
 * @param e            EXISTS sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param logic        Logic for evaluating
 * @param builder      Builder
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteExists(RexSubQuery e, Set<CorrelationId> variablesSet, RelOptUtil.Logic logic,
        RelBuilder builder) {
    builder.push(e.getRel());

    builder.project(builder.alias(builder.literal(true), "i"));
    switch (logic) {
    case TRUE:
        // Handles queries with single EXISTS in filter condition:
        // select e.deptno from emp as e
        // where exists (select deptno from emp)
        builder.aggregate(builder.groupKey(0));
        builder.as("dt");
        builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
        return builder.literal(true);
    default:
        builder.distinct();
    }

    builder.as("dt");

    builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);

    return builder.isNotNull(Util.last(builder.fields()));
}
 
Example 2
Source File: JoinRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Join join = call.rel(0);
  final RelNode left = join.getLeft();
  final RelNode right = join.getRight();
  final RelNode convertedLeft = convertIfNecessary(left);
  final RelNode convertedRight = convertIfNecessary(right);
  final RelBuilder builder = factory.create(join.getCluster(), null);

  builder.pushAll(ImmutableList.of(convertedLeft, convertedRight));
  builder.join(join.getJoinType(), join.getCondition());

  final RelNode newJoin = builder.build();
  if(newJoin != null) {
    call.transformTo(newJoin);
  }
}
 
Example 3
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites an EXISTS RexSubQuery into a {@link Join}.
 *
 * @param e            EXISTS sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param logic        Logic for evaluating
 * @param builder      Builder
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteExists(RexSubQuery e, Set<CorrelationId> variablesSet,
    RelOptUtil.Logic logic, RelBuilder builder) {
  builder.push(e.rel);

  builder.project(builder.alias(builder.literal(true), "i"));
  switch (logic) {
  case TRUE:
    // Handles queries with single EXISTS in filter condition:
    // select e.deptno from emp as e
    // where exists (select deptno from emp)
    builder.aggregate(builder.groupKey(0));
    builder.as("dt");
    builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
    return builder.literal(true);
  default:
    builder.distinct();
  }

  builder.as("dt");

  builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);

  return builder.isNotNull(Util.last(builder.fields()));
}
 
Example 4
Source File: SubQueryRemoveRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrites a scalar sub-query into an
 * {@link org.apache.calcite.rel.core.Aggregate}.
 *
 * @param e            IN sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param builder      Builder
 * @param offset       Offset to shift {@link RexInputRef}
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteScalarQuery(RexSubQuery e, Set<CorrelationId> variablesSet, RelBuilder builder,
        int inputCount, int offset) {
    builder.push(e.getRel());
    final RelMetadataQuery mq = e.getRel().getCluster().getMetadataQuery();
    final Boolean unique = mq.areColumnsUnique(builder.peek(), ImmutableBitSet.of());
    if (unique == null || !unique) {
        builder.aggregate(builder.groupKey(),
                builder.aggregateCall(SqlStdOperatorTable.SINGLE_VALUE, builder.field(0)));
    }
    builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
    return field(builder, inputCount, offset);
}
 
Example 5
Source File: JoinFilterCanonicalizationRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create a join operator with a canonicalized version of {@code joinCondition}
 *
 * @param builder
 * @param joinType
 * @param joinCondition
 * @param left
 * @param right
 * @return the new join operator, or {@code null} if {@code joinCondition} hasn't changed.
 */
private RelNode canonicalizeJoinCondition(
    RelBuilder builder,
    JoinRelType joinType,
    RexNode joinCondition,
    RelNode left,
    RelNode right) {
  final List<Integer> leftKeys = Lists.newArrayList();
  final List<Integer> rightKeys = Lists.newArrayList();
  final List<Boolean> filterNulls = Lists.newArrayList();

  final RexNode remaining = RelOptUtil.splitJoinCondition(left, right, joinCondition, leftKeys, rightKeys, filterNulls);

  // Create a normalized join condition
  final RexNode newPartialJoinCondition = buildJoinCondition(builder.getRexBuilder(), left.getRowType(), right.getRowType(), leftKeys, rightKeys, filterNulls);
  // Add the remaining filter condition
  final RexNode newJoinCondition = RelOptUtil.andJoinFilters(builder.getRexBuilder(), newPartialJoinCondition, remaining);

  // terminate if the same condition as previously
  if (RexUtil.eq(joinCondition, newJoinCondition)) {
    return null;
  }

  builder.pushAll(ImmutableList.of(left, right));
  builder.join(joinType, newJoinCondition);

  return builder.build();
}
 
Example 6
Source File: SubQueryRemoveRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrites a scalar sub-query into an
 * {@link org.apache.calcite.rel.core.Aggregate}.
 *
 * @param e            IN sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param builder      Builder
 * @param offset       Offset to shift {@link RexInputRef}
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteScalarQuery(RexSubQuery e, Set<CorrelationId> variablesSet,
    RelBuilder builder, int inputCount, int offset) {
  builder.push(e.rel);
  final RelMetadataQuery mq = e.rel.getCluster().getMetadataQuery();
  final Boolean unique = mq.areColumnsUnique(builder.peek(),
      ImmutableBitSet.of());
  if (unique == null || !unique) {
    builder.aggregate(builder.groupKey(),
        builder.aggregateCall(SqlStdOperatorTable.SINGLE_VALUE,
            builder.field(0)));
  }
  builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
  return field(builder, inputCount, offset);
}
 
Example 7
Source File: JoinNormalizationRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Attempt to create a new join with a canonicalized join expression, and a possible filter
 * on top
 * @param builder
 * @param join
 * @return a new join tree (or same as original argument if no change)
 */
private RelNode getNewJoinCondition(RelBuilder builder, Join join) {
  final List<Integer> leftKeys = Lists.newArrayList();
  final List<Integer> rightKeys = Lists.newArrayList();
  final List<Boolean> filterNulls = Lists.newArrayList();

  final RexNode remaining = RelOptUtil.splitJoinCondition(join.getLeft(), join.getRight(), join.getCondition(), leftKeys, rightKeys, filterNulls);
  final boolean hasEquiJoins = leftKeys.size() == rightKeys.size() && leftKeys.size() > 0 ;
  final JoinRelType joinType = join.getJoinType();

  // If join has no equi-join condition, do not transform
  if (!hasEquiJoins) {
    return join;
  }

  // Create a new partial condition for the equi-join
  final RexNode partialCondition = JoinFilterCanonicalizationRule.buildJoinCondition(
      builder.getRexBuilder(),
      join.getLeft().getRowType(),
      join.getRight().getRowType(),
      leftKeys,
      rightKeys,
      filterNulls);

  // We do not know how to add filter for non-INNER joins (see DRILL-1337)
  if (joinType != JoinRelType.INNER) {
    final RexNode newJoinCondition = RexUtil.composeConjunction(builder.getRexBuilder(), ImmutableList.of(partialCondition, remaining), false);
    if (RexUtil.eq(join.getCondition(), newJoinCondition)) {
      // Condition is the same, do not create a new rel
      return join;
    }
    builder.pushAll(ImmutableList.of(join.getLeft(), join.getRight()));
    builder.join(joinType, newJoinCondition);

    return builder.build();
  }

  // Check if join condition has changed if pure equi-join
  if (remaining.isAlwaysTrue() && RexUtil.eq(join.getCondition(), partialCondition)) {
    return join;
  }

  // Return the new join with a filter on top
  builder.pushAll(ImmutableList.of(join.getLeft(), join.getRight()));
  builder.join(joinType, partialCondition);
  builder.filter(remaining);
  return builder.build();
}