org.apache.calcite.rel.core.Intersect Java Examples

The following examples show how to use org.apache.calcite.rel.core.Intersect. 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: 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 #2
Source File: RelMdMaxRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(Intersect rel, RelMetadataQuery mq) {
  // max row count is the smallest of the inputs
  Double rowCount = null;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getMaxRowCount(input);
    if (rowCount == null
        || partialRowCount != null && partialRowCount < rowCount) {
      rowCount = partialRowCount;
    }
  }
  return rowCount;
}
 
Example #3
Source File: RelMdColumnUniqueness.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
    ImmutableBitSet columns, boolean ignoreNulls) {
  if (areColumnsUnique((SetOp) rel, mq, columns, ignoreNulls)) {
    return true;
  }
  for (RelNode input : rel.getInputs()) {
    Boolean b = mq.areColumnsUnique(input, columns, ignoreNulls);
    if (b != null && b) {
      return true;
    }
  }
  return false;
}
 
Example #4
Source File: RelMdRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getRowCount(Intersect rel, RelMetadataQuery mq) {
  Double rowCount = null;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getRowCount(input);
    if (rowCount == null
        || partialRowCount != null && partialRowCount < rowCount) {
      rowCount = partialRowCount;
    }
  }
  return rowCount;
}
 
Example #5
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Infers predicates for a Intersect.
 */
public RelOptPredicateList getPredicates(Intersect intersect, RelMetadataQuery mq) {
  final RexBuilder rexBuilder = intersect.getCluster().getRexBuilder();

  final RexExecutor executor =
      Util.first(intersect.getCluster().getPlanner().getExecutor(), RexUtil.EXECUTOR);

  final RexImplicationChecker rexImplicationChecker =
      new RexImplicationChecker(rexBuilder, executor, intersect.getRowType());

  Set<RexNode> finalPredicates = new HashSet<>();

  for (Ord<RelNode> input : Ord.zip(intersect.getInputs())) {
    RelOptPredicateList info = mq.getPulledUpPredicates(input.e);
    if (info == null || info.pulledUpPredicates.isEmpty()) {
      continue;
    }

    for (RexNode pred: info.pulledUpPredicates) {
      if (finalPredicates.stream().anyMatch(
          finalPred -> rexImplicationChecker.implies(finalPred, pred))) {
        // There's already a stricter predicate in finalPredicates,
        // thus no need to count this one.
        continue;
      }
      // Remove looser predicate and add this one into finalPredicates
      finalPredicates = finalPredicates.stream()
          .filter(finalPred -> !rexImplicationChecker.implies(pred, finalPred))
          .collect(Collectors.toSet());
      finalPredicates.add(pred);
    }
  }

  return RelOptPredicateList.of(rexBuilder, finalPredicates);
}
 
Example #6
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
public Result visitChild(int i, RelNode e) {
  if (e instanceof Union) {
    return visitUnion((Union) e);
  } else if (e instanceof Join) {
    return visitJoin((Join) e);
  } else if (e instanceof Filter) {
    return visitFilter((Filter) e);
  } else if (e instanceof Project) {
    return visitProject((Project) e);
  } else if (e instanceof Aggregate) {
    return visitAggregate((Aggregate) e);
  } else if (e instanceof TableScan) {
    return visitTableScan((TableScan) e);
  } else if (e instanceof Intersect) {
    return visitIntersect((Intersect) e);
  } else if (e instanceof Minus) {
    return visitMinus((Minus) e);
  } else if (e instanceof Calc) {
    return visitCalc((Calc) e);
  } else if (e instanceof Sort) {
    return visitSort((Sort) e);
  } else if (e instanceof TableModify) {
    return visitTableModify((TableModify) e);
  } else if (e instanceof Limit) {
    return visitLimit((Limit) e);
  } else {
    throw new AssertionError("Need to Implement for " + e.getClass().getName()); // TODO:
  }
}
 
Example #7
Source File: RelMdRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getRowCount(Intersect rel, RelMetadataQuery mq) {
  Double rowCount = null;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getRowCount(input);
    if (rowCount == null
        || partialRowCount != null && partialRowCount < rowCount) {
      rowCount = partialRowCount;
    }
  }
  if (rowCount == null || !rel.all) {
    return rowCount;
  } else {
    return rowCount * 2;
  }
}
 
Example #8
Source File: RelMdColumnUniqueness.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
    ImmutableBitSet columns, boolean ignoreNulls) {
  columns = decorateWithConstantColumnsFromPredicates(columns, rel, mq);
  if (areColumnsUnique((SetOp) rel, mq, columns, ignoreNulls)) {
    return true;
  }
  for (RelNode input : rel.getInputs()) {
    Boolean b = mq.areColumnsUnique(input, columns, ignoreNulls);
    if (b != null && b) {
      return true;
    }
  }
  return false;
}
 
Example #9
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(Intersect rel, RelMetadataQuery mq) {
  // max row count is the smallest of the inputs
  Double rowCount = null;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getMaxRowCount(input);
    if (rowCount == null
        || partialRowCount != null && partialRowCount < rowCount) {
      rowCount = partialRowCount;
    }
  }
  return rowCount;
}
 
Example #10
Source File: RelMdNodeTypes.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Multimap<Class<? extends RelNode>, RelNode> getNodeTypes(Intersect rel,
    RelMetadataQuery mq) {
  return getNodeTypes(rel, Intersect.class, mq);
}
 
Example #11
Source File: JdbcRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates a JdbcIntersectRule. */
private JdbcIntersectRule(JdbcConvention out,
    RelBuilderFactory relBuilderFactory) {
  super(Intersect.class, (Predicate<RelNode>) r -> true, Convention.NONE,
      out, relBuilderFactory, "JdbcIntersectRule");
}
 
Example #12
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** @see #dispatch */
public Result visit(Intersect e) {
  return setOpToSql(e.all
      ? SqlStdOperatorTable.INTERSECT_ALL
      : SqlStdOperatorTable.INTERSECT, e);
}
 
Example #13
Source File: IntersectToDistinctRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates an IntersectToDistinctRule. */
public IntersectToDistinctRule(Class<? extends Intersect> intersectClazz,
    RelBuilderFactory relBuilderFactory) {
  super(operand(intersectClazz, any()), relBuilderFactory, null);
}
 
Example #14
Source File: UnionMergeRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final SetOp topOp = call.rel(0);
  @SuppressWarnings("unchecked") final Class<? extends SetOp> setOpClass =
      (Class) operands.get(0).getMatchedClass();

  // For Union and Intersect, we want to combine the set-op that's in the
  // second input first.
  //
  // For example, we reduce
  //    Union(Union(a, b), Union(c, d))
  // to
  //    Union(Union(a, b), c, d)
  // in preference to
  //    Union(a, b, Union(c, d))
  //
  // But for Minus, we can only reduce the left input. It is not valid to
  // reduce
  //    Minus(a, Minus(b, c))
  // to
  //    Minus(a, b, c)
  //
  // Hence, that's why the rule pattern matches on generic RelNodes rather
  // than explicit sub-classes of SetOp.  By doing so, and firing this rule
  // in a bottom-up order, it allows us to only specify a single
  // pattern for this rule.
  final SetOp bottomOp;
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    bottomOp = call.rel(2);
  } else if (setOpClass.isInstance(call.rel(1))) {
    bottomOp = call.rel(1);
  } else {
    return;
  }

  // Can only combine (1) if all operators are ALL,
  // or (2) top operator is DISTINCT (i.e. not ALL).
  // In case (2), all operators become DISTINCT.
  if (topOp.all && !bottomOp.all) {
    return;
  }

  // Combine the inputs from the bottom set-op with the other inputs from
  // the top set-op.
  final RelBuilder relBuilder = call.builder();
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    relBuilder.push(topOp.getInput(0));
    relBuilder.pushAll(bottomOp.getInputs());
    // topOp.getInputs().size() may be more than 2
    for (int index = 2; index < topOp.getInputs().size(); index++) {
      relBuilder.push(topOp.getInput(index));
    }
  } else {
    relBuilder.pushAll(bottomOp.getInputs());
    relBuilder.pushAll(Util.skip(topOp.getInputs()));
  }
  int n = bottomOp.getInputs().size()
      + topOp.getInputs().size()
      - 1;
  if (topOp instanceof Union) {
    relBuilder.union(topOp.all, n);
  } else if (topOp instanceof Intersect) {
    relBuilder.intersect(topOp.all, n);
  } else if (topOp instanceof Minus) {
    relBuilder.minus(topOp.all, n);
  }
  call.transformTo(relBuilder.build());
}
 
Example #15
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Double getMinRowCount(Intersect rel, RelMetadataQuery mq) {
  return 0d; // no lower bound
}
 
Example #16
Source File: RelMdSize.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<Double> averageColumnSizes(Intersect rel, RelMetadataQuery mq) {
  return mq.getAverageColumnSizes(rel.getInput(0));
}
 
Example #17
Source File: RelMdNodeTypes.java    From Bats with Apache License 2.0 4 votes vote down vote up
public Multimap<Class<? extends RelNode>, RelNode> getNodeTypes(Intersect rel,
    RelMetadataQuery mq) {
  return getNodeTypes(rel, Intersect.class, mq);
}
 
Example #18
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 4 votes vote down vote up
public Result visitIntersect(Intersect e) {
  return setOpToSql(e.all
      ? SqlStdOperatorTable.INTERSECT_ALL
      : SqlStdOperatorTable.INTERSECT, e);
}
 
Example #19
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * @see #dispatch
 */
public Result visit(Intersect e) {
  return setOpToSql(e.all
    ? SqlStdOperatorTable.INTERSECT_ALL
    : SqlStdOperatorTable.INTERSECT, e);
}
 
Example #20
Source File: RelToSqlConverter.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** @see #dispatch */
public Result visit(Intersect e) {
  return setOpToSql(e.all
      ? SqlStdOperatorTable.INTERSECT_ALL
      : SqlStdOperatorTable.INTERSECT, e);
}
 
Example #21
Source File: IntersectToDistinctRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Creates an IntersectToDistinctRule. */
public IntersectToDistinctRule(Class<? extends Intersect> intersectClazz,
    RelBuilderFactory relBuilderFactory) {
  super(operand(intersectClazz, any()), relBuilderFactory, null);
}
 
Example #22
Source File: UnionMergeRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final SetOp topOp = call.rel(0);
  @SuppressWarnings("unchecked") final Class<? extends SetOp> setOpClass =
      (Class) operands.get(0).getMatchedClass();

  // For Union and Intersect, we want to combine the set-op that's in the
  // second input first.
  //
  // For example, we reduce
  //    Union(Union(a, b), Union(c, d))
  // to
  //    Union(Union(a, b), c, d)
  // in preference to
  //    Union(a, b, Union(c, d))
  //
  // But for Minus, we can only reduce the left input. It is not valid to
  // reduce
  //    Minus(a, Minus(b, c))
  // to
  //    Minus(a, b, c)
  //
  // Hence, that's why the rule pattern matches on generic RelNodes rather
  // than explicit sub-classes of SetOp.  By doing so, and firing this rule
  // in a bottom-up order, it allows us to only specify a single
  // pattern for this rule.
  final SetOp bottomOp;
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    bottomOp = call.rel(2);
  } else if (setOpClass.isInstance(call.rel(1))) {
    bottomOp = call.rel(1);
  } else {
    return;
  }

  // Can only combine (1) if all operators are ALL,
  // or (2) top operator is DISTINCT (i.e. not ALL).
  // In case (2), all operators become DISTINCT.
  if (topOp.all && !bottomOp.all) {
    return;
  }

  // Combine the inputs from the bottom set-op with the other inputs from
  // the top set-op.
  final RelBuilder relBuilder = call.builder();
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    relBuilder.push(topOp.getInput(0));
    relBuilder.pushAll(bottomOp.getInputs());
    // topOp.getInputs().size() may be more than 2
    for (int index = 2; index < topOp.getInputs().size(); index++) {
      relBuilder.push(topOp.getInput(index));
    }
  } else {
    relBuilder.pushAll(bottomOp.getInputs());
    relBuilder.pushAll(Util.skip(topOp.getInputs()));
  }
  int n = bottomOp.getInputs().size()
      + topOp.getInputs().size()
      - 1;
  if (topOp instanceof Union) {
    relBuilder.union(topOp.all, n);
  } else if (topOp instanceof Intersect) {
    relBuilder.intersect(topOp.all, n);
  } else if (topOp instanceof Minus) {
    relBuilder.minus(topOp.all, n);
  }
  call.transformTo(relBuilder.build());
}
 
Example #23
Source File: RelMdMinRowCount.java    From Bats with Apache License 2.0 4 votes vote down vote up
public Double getMinRowCount(Intersect rel, RelMetadataQuery mq) {
  return 0d; // no lower bound
}
 
Example #24
Source File: RelMdSize.java    From Bats with Apache License 2.0 4 votes vote down vote up
public List<Double> averageColumnSizes(Intersect rel, RelMetadataQuery mq) {
  return mq.getAverageColumnSizes(rel.getInput(0));
}