Java Code Examples for org.apache.calcite.plan.RelOptUtil#splitFilters()

The following examples show how to use org.apache.calcite.plan.RelOptUtil#splitFilters() . 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: RelMdSelectivity.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Double getSelectivity(Aggregate rel, RelMetadataQuery mq,
    RexNode predicate) {
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      rel.getGroupSet(),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  Double selectivity = mq.getSelectivity(rel.getInput(), childPred);
  if (selectivity == null) {
    return null;
  } else {
    RexNode pred =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return selectivity * RelMdUtil.guessSelectivity(pred);
  }
}
 
Example 2
Source File: RelMdSelectivity.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getSelectivity(Aggregate rel, RelMetadataQuery mq,
    RexNode predicate) {
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      rel.getGroupSet(),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  Double selectivity = mq.getSelectivity(rel.getInput(), childPred);
  if (selectivity == null) {
    return null;
  } else {
    RexNode pred =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return selectivity * RelMdUtil.guessSelectivity(pred);
  }
}
 
Example 3
Source File: RelMdDistinctRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getDistinctRowCount(Aggregate rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  if (predicate == null || predicate.isAlwaysTrue()) {
    if (groupKey.isEmpty()) {
      return 1D;
    }
  }
  // determine which predicates can be applied on the child of the
  // aggregate
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      rel.getGroupSet(),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPreds =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  // set the bits as they correspond to the child input
  ImmutableBitSet.Builder childKey = ImmutableBitSet.builder();
  RelMdUtil.setAggChildKeys(groupKey, rel, childKey);

  Double distinctRowCount =
      mq.getDistinctRowCount(rel.getInput(), childKey.build(), childPreds);
  if (distinctRowCount == null) {
    return null;
  } else if (notPushable.isEmpty()) {
    return distinctRowCount;
  } else {
    RexNode preds =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return distinctRowCount * RelMdUtil.guessSelectivity(preds);
  }
}
 
Example 4
Source File: RelMdSelectivity.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getSelectivity(Project rel, RelMetadataQuery mq,
    RexNode predicate) {
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      ImmutableBitSet.range(rel.getRowType().getFieldCount()),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  RexNode modifiedPred;
  if (childPred == null) {
    modifiedPred = null;
  } else {
    modifiedPred = RelOptUtil.pushPastProject(childPred, rel);
  }
  Double selectivity = mq.getSelectivity(rel.getInput(), modifiedPred);
  if (selectivity == null) {
    return null;
  } else {
    RexNode pred =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return selectivity * RelMdUtil.guessSelectivity(pred);
  }
}
 
Example 5
Source File: RelMdDistinctRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getDistinctRowCount(Aggregate rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  if (predicate == null || predicate.isAlwaysTrue()) {
    if (groupKey.isEmpty()) {
      return 1D;
    }
  }
  // determine which predicates can be applied on the child of the
  // aggregate
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      rel.getGroupSet(),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPreds =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  // set the bits as they correspond to the child input
  ImmutableBitSet.Builder childKey = ImmutableBitSet.builder();
  RelMdUtil.setAggChildKeys(groupKey, rel, childKey);

  Double distinctRowCount =
      mq.getDistinctRowCount(rel.getInput(), childKey.build(), childPreds);
  if (distinctRowCount == null) {
    return null;
  } else if (notPushable.isEmpty()) {
    return distinctRowCount;
  } else {
    RexNode preds =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return distinctRowCount * RelMdUtil.guessSelectivity(preds);
  }
}
 
Example 6
Source File: RelMdSelectivity.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getSelectivity(Project rel, RelMetadataQuery mq,
    RexNode predicate) {
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      ImmutableBitSet.range(rel.getRowType().getFieldCount()),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  RexNode modifiedPred;
  if (childPred == null) {
    modifiedPred = null;
  } else {
    modifiedPred = RelOptUtil.pushPastProject(childPred, rel);
  }
  Double selectivity = mq.getSelectivity(rel.getInput(), modifiedPred);
  if (selectivity == null) {
    return null;
  } else {
    RexNode pred =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return selectivity * RelMdUtil.guessSelectivity(pred);
  }
}
 
Example 7
Source File: RelMdDistinctRowCount.java    From Bats with Apache License 2.0 4 votes vote down vote up
public Double getDistinctRowCount(Project rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  if (predicate == null || predicate.isAlwaysTrue()) {
    if (groupKey.isEmpty()) {
      return 1D;
    }
  }
  ImmutableBitSet.Builder baseCols = ImmutableBitSet.builder();
  ImmutableBitSet.Builder projCols = ImmutableBitSet.builder();
  List<RexNode> projExprs = rel.getProjects();
  RelMdUtil.splitCols(projExprs, groupKey, baseCols, projCols);

  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      ImmutableBitSet.range(rel.getRowType().getFieldCount()),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();

  // get the distinct row count of the child input, passing in the
  // columns and filters that only reference the child; convert the
  // filter to reference the children projection expressions
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);
  RexNode modifiedPred;
  if (childPred == null) {
    modifiedPred = null;
  } else {
    modifiedPred = RelOptUtil.pushPastProject(childPred, rel);
  }
  Double distinctRowCount =
      mq.getDistinctRowCount(rel.getInput(), baseCols.build(),
          modifiedPred);

  if (distinctRowCount == null) {
    return null;
  } else if (!notPushable.isEmpty()) {
    RexNode preds =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    distinctRowCount *= RelMdUtil.guessSelectivity(preds);
  }

  // No further computation required if the projection expressions
  // are all column references
  if (projCols.cardinality() == 0) {
    return distinctRowCount;
  }

  // multiply by the cardinality of the non-child projection expressions
  for (int bit : projCols.build()) {
    Double subRowCount =
        RelMdUtil.cardOfProjExpr(mq, rel, projExprs.get(bit));
    if (subRowCount == null) {
      return null;
    }
    distinctRowCount *= subRowCount;
  }

  return RelMdUtil.numDistinctVals(distinctRowCount, mq.getRowCount(rel));
}
 
Example 8
Source File: RelMdDistinctRowCount.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Double getDistinctRowCount(Project rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  if (predicate == null || predicate.isAlwaysTrue()) {
    if (groupKey.isEmpty()) {
      return 1D;
    }
  }
  ImmutableBitSet.Builder baseCols = ImmutableBitSet.builder();
  ImmutableBitSet.Builder projCols = ImmutableBitSet.builder();
  List<RexNode> projExprs = rel.getProjects();
  RelMdUtil.splitCols(projExprs, groupKey, baseCols, projCols);

  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      ImmutableBitSet.range(rel.getRowType().getFieldCount()),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();

  // get the distinct row count of the child input, passing in the
  // columns and filters that only reference the child; convert the
  // filter to reference the children projection expressions
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);
  RexNode modifiedPred;
  if (childPred == null) {
    modifiedPred = null;
  } else {
    modifiedPred = RelOptUtil.pushPastProject(childPred, rel);
  }
  Double distinctRowCount =
      mq.getDistinctRowCount(rel.getInput(), baseCols.build(),
          modifiedPred);

  if (distinctRowCount == null) {
    return null;
  } else if (!notPushable.isEmpty()) {
    RexNode preds =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    distinctRowCount *= RelMdUtil.guessSelectivity(preds);
  }

  // No further computation required if the projection expressions
  // are all column references
  if (projCols.cardinality() == 0) {
    return distinctRowCount;
  }

  // multiply by the cardinality of the non-child projection expressions
  for (int bit : projCols.build()) {
    Double subRowCount =
        RelMdUtil.cardOfProjExpr(mq, rel, projExprs.get(bit));
    if (subRowCount == null) {
      return null;
    }
    distinctRowCount *= subRowCount;
  }

  return RelMdUtil.numDistinctVals(distinctRowCount, mq.getRowCount(rel));
}