org.apache.calcite.plan.RelOptPredicateList Java Examples

The following examples show how to use org.apache.calcite.plan.RelOptPredicateList. 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: RelMdMaxRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMaxRowCount(Aggregate rel, RelMetadataQuery mq) {
  if (rel.getGroupSet().isEmpty()) {
    // Aggregate with no GROUP BY always returns 1 row (even on empty table).
    return 1D;
  }

  // Aggregate with constant GROUP BY always returns 1 row
  if (rel.getGroupType() == Aggregate.Group.SIMPLE) {
    final RelOptPredicateList predicateList =
        mq.getPulledUpPredicates(rel.getInput());
    if (predicateList != null
        && allGroupKeysAreConstant(rel, predicateList)) {
      return 1D;
    }
  }
  final Double rowCount = mq.getMaxRowCount(rel.getInput());
  if (rowCount == null) {
    return null;
  }
  return rowCount * rel.getGroupSets().size();
}
 
Example #2
Source File: RelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RelBuilder(Context context, RelOptCluster cluster,
    RelOptSchema relOptSchema) {
  this.cluster = cluster;
  this.relOptSchema = relOptSchema;
  if (context == null) {
    context = Contexts.EMPTY_CONTEXT;
  }
  this.config = getConfig(context);
  this.viewExpander = getViewExpander(cluster, context);
  this.struct =
      Objects.requireNonNull(RelFactories.Struct.fromContext(context));
  final RexExecutor executor =
      Util.first(context.unwrap(RexExecutor.class),
          Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR));
  final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
  this.simplifier =
      new RexSimplify(cluster.getRexBuilder(), predicates, executor);
}
 
Example #3
Source File: ReduceExpressionsRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Project project = call.rel(0);
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelOptPredicateList predicates =
      mq.getPulledUpPredicates(project.getInput());
  final List<RexNode> expList =
      Lists.newArrayList(project.getProjects());
  if (reduceExpressions(project, expList, predicates, false,
      matchNullability)) {
    call.transformTo(
        call.builder()
            .push(project.getInput())
            .project(expList, project.getRowType().getFieldNames())
            .build());

    // New plan is absolutely better than old plan.
    call.getPlanner().setImportance(project, 0.0);
  }
}
 
Example #4
Source File: RexProgramBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RexProgramBuilder create(
    RexBuilder rexBuilder,
    final RelDataType inputRowType,
    final List<RexNode> exprList,
    final List<? extends RexNode> projectList,
    final RexNode condition,
    final RelDataType outputRowType,
    boolean normalize,
    boolean simplify_) {
  RexSimplify simplify = null;
  if (simplify_) {
    simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY,
        RexUtil.EXECUTOR);
  }
  return new RexProgramBuilder(rexBuilder, inputRowType, exprList,
      projectList, condition, outputRowType, normalize, simplify);
}
 
Example #5
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testAllPredicatesCrossJoinMultiTable() {
  final String sql = "select x.sal from\n"
      + "(select a.deptno, c.sal from (select * from emp limit 7) as a\n"
      + "cross join (select * from dept limit 1) as b\n"
      + "cross join (select * from emp where empno = 5 limit 2) as c) as x";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
  assertThat(tableReferences,
      sortsAs("[[CATALOG, SALES, DEPT].#0, "
          + "[CATALOG, SALES, EMP].#0, "
          + "[CATALOG, SALES, EMP].#1]"));
  final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  // Note that we reference [CATALOG, SALES, EMP].#1 rather than [CATALOG, SALES, EMP].#0
  assertThat(inputSet.pulledUpPredicates,
      sortsAs("[=([CATALOG, SALES, EMP].#1.$0, 5), true, true]"));
}
 
Example #6
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testAllPredicatesAndTableUnion() {
  final String sql = "select a.deptno, c.sal from (select * from emp limit 7) as a\n"
      + "cross join (select * from dept limit 1) as b\n"
      + "inner join (select * from emp limit 2) as c\n"
      + "on a.deptno = c.deptno\n"
      + "union all\n"
      + "select a.deptno, c.sal from (select * from emp limit 7) as a\n"
      + "cross join (select * from dept limit 1) as b\n"
      + "inner join (select * from emp limit 2) as c\n"
      + "on a.deptno = c.deptno";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  assertThat(inputSet.pulledUpPredicates,
      sortsAs("[=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#1.$7),"
          + " =([CATALOG, SALES, EMP].#2.$7, [CATALOG, SALES, EMP].#3.$7), "
          + "true, "
          + "true]"));
  final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
  assertThat(tableReferences.toString(),
      equalTo("[[CATALOG, SALES, DEPT].#0, [CATALOG, SALES, DEPT].#1, "
          + "[CATALOG, SALES, EMP].#0, [CATALOG, SALES, EMP].#1, "
          + "[CATALOG, SALES, EMP].#2, [CATALOG, SALES, EMP].#3]"));
}
 
Example #7
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testAllPredicatesAggregate1() {
  final String sql = "select a, max(b) from (\n"
      + "  select empno as a, sal as b from emp where empno = 5)subq\n"
      + "group by a";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
  assertThat(pulledUpPredicates.size(), is(1));
  RexCall call = (RexCall) pulledUpPredicates.get(0);
  assertThat(call.getOperands().size(), is(2));
  final RexTableInputRef inputRef1 = (RexTableInputRef) call.getOperands().get(0);
  assertThat(inputRef1.getQualifiedName(), is(EMP_QNAME));
  assertThat(inputRef1.getIndex(), is(0));
  final RexLiteral constant = (RexLiteral) call.getOperands().get(1);
  assertThat(constant.toString(), is("5"));
}
 
Example #8
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testAllPredicatesUnionMultiTable() {
  final String sql = "select x.sal from\n"
      + "(select a.deptno, a.sal from (select * from emp) as a\n"
      + "union all select emp.deptno, emp.sal from emp\n"
      + "union all select emp.deptno, emp.sal from emp where empno = 5) as x";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
  assertThat(tableReferences,
      sortsAs("[[CATALOG, SALES, EMP].#0, "
          + "[CATALOG, SALES, EMP].#1, "
          + "[CATALOG, SALES, EMP].#2]"));
  // Note that we reference [CATALOG, SALES, EMP].#2 rather than
  // [CATALOG, SALES, EMP].#0 or [CATALOG, SALES, EMP].#1
  final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  assertThat(inputSet.pulledUpPredicates,
      sortsAs("[=([CATALOG, SALES, EMP].#2.$0, 5)]"));
}
 
Example #9
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** @see RelMetadataQuery#getPulledUpPredicates(RelNode) */
public RelOptPredicateList getPredicates(RelSubset r,
    RelMetadataQuery mq) {
  if (!Bug.CALCITE_1048_FIXED) {
    return RelOptPredicateList.EMPTY;
  }
  final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
  RelOptPredicateList list = null;
  for (RelNode r2 : r.getRels()) {
    RelOptPredicateList list2 = mq.getPulledUpPredicates(r2);
    if (list2 != null) {
      list = list == null ? list2 : list.union(rexBuilder, list2);
    }
  }
  return Util.first(list, RelOptPredicateList.EMPTY);
}
 
Example #10
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 #11
Source File: RexSimplify.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a RexSimplify the same as this but with a specified
 * {@link #predicates} value. */
public RexSimplify withPredicates(RelOptPredicateList predicates) {
  return predicates == this.predicates
      ? this
      : new RexSimplify(rexBuilder, predicates, defaultUnknownAs,
          predicateElimination, paranoid, executor);
}
 
Example #12
Source File: RelMdAllPredicates.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Add the Filter condition to the list obtained from the input.
 */
public RelOptPredicateList getAllPredicates(Filter filter, RelMetadataQuery mq) {
  final RelNode input = filter.getInput();
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RexNode pred = filter.getCondition();

  final RelOptPredicateList predsBelow = mq.getAllPredicates(input);
  if (predsBelow == null) {
    // Safety check
    return null;
  }

  // Extract input fields referenced by Filter condition
  final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>();
  final RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields);
  pred.accept(inputFinder);
  final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();

  // Infer column origin expressions for given references
  final Map<RexInputRef, Set<RexNode>> mapping = new LinkedHashMap<>();
  for (int idx : inputFieldsUsed) {
    final RexInputRef ref = RexInputRef.of(idx, filter.getRowType().getFieldList());
    final Set<RexNode> originalExprs = mq.getExpressionLineage(filter, ref);
    if (originalExprs == null) {
      // Bail out
      return null;
    }
    mapping.put(ref, originalExprs);
  }

  // Replace with new expressions and return union of predicates
  final Set<RexNode> allExprs =
      RelMdExpressionLineage.createAllPossibleExpressions(rexBuilder, pred, mapping);
  if (allExprs == null) {
    return null;
  }
  return predsBelow.union(rexBuilder, RelOptPredicateList.of(rexBuilder, allExprs));
}
 
Example #13
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testAllPredicatesAggregate3() {
  final String sql = "select * from (select a, max(b) as b from (\n"
      + "  select empno as a, sal as b from emp)subq\n"
      + "group by a)\n"
      + "where b = 5";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  // Filter on aggregate, we cannot infer lineage
  assertNull(inputSet);
}
 
Example #14
Source File: RelMetadataQuery.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the
 * {@link BuiltInMetadata.AllPredicates#getAllPredicates()}
 * statistic.
 *
 * @param rel the relational expression
 * @return All predicates within and below this RelNode
 */
public RelOptPredicateList getAllPredicates(RelNode rel) {
  for (;;) {
    try {
      return allPredicatesHandler.getAllPredicates(rel, this);
    } catch (JaninoRelMetadataProvider.NoHandler e) {
      allPredicatesHandler = revise(e.relClass, BuiltInMetadata.AllPredicates.DEF);
    }
  }
}
 
Example #15
Source File: RexAnalyzer.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a RexAnalyzer. */
public RexAnalyzer(RexNode e, RelOptPredicateList predicates) {
  this.e = e;
  final VariableCollector variableCollector = new VariableCollector();
  e.accept(variableCollector);
  variableCollector.visitEach(predicates.pulledUpPredicates);
  variables = ImmutableList.copyOf(variableCollector.builder);
  unsupportedCount = variableCollector.unsupportedCount;
}
 
Example #16
Source File: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RexNode simplifyOrs(RexBuilder rexBuilder,
    List<RexNode> terms) {
  return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, EXECUTOR)
      .simplifyUnknownAs(RexUtil.composeDisjunction(rexBuilder, terms),
          RexUnknownAs.UNKNOWN);
}
 
Example #17
Source File: RelMetadataQuery.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the
 * {@link BuiltInMetadata.Predicates#getPredicates()}
 * statistic.
 *
 * @param rel the relational expression
 * @return Predicates that can be pulled above this RelNode
 */
public RelOptPredicateList getPulledUpPredicates(RelNode rel) {
  for (;;) {
    try {
      return predicatesHandler.getPredicates(rel, this);
    } catch (JaninoRelMetadataProvider.NoHandler e) {
      predicatesHandler = revise(e.relClass, BuiltInMetadata.Predicates.DEF);
    }
  }
}
 
Example #18
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Join join = call.rel(0);
  final List<RexNode> expList = Lists.newArrayList(join.getCondition());
  final int fieldCount = join.getLeft().getRowType().getFieldCount();
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelOptPredicateList leftPredicates =
      mq.getPulledUpPredicates(join.getLeft());
  final RelOptPredicateList rightPredicates =
      mq.getPulledUpPredicates(join.getRight());
  final RexBuilder rexBuilder = join.getCluster().getRexBuilder();
  final RelOptPredicateList predicates =
      leftPredicates.union(rexBuilder,
          rightPredicates.shift(rexBuilder, fieldCount));
  if (!reduceExpressions(join, expList, predicates, true,
      matchNullability)) {
    return;
  }
  call.transformTo(
      join.copy(
          join.getTraitSet(),
          expList.get(0),
          join.getLeft(),
          join.getRight(),
          join.getJoinType(),
          join.isSemiJoinDone()));

  // New plan is absolutely better than old plan.
  call.getPlanner().prune(join);
}
 
Example #19
Source File: RelMetadataQuery.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the
 * {@link BuiltInMetadata.Predicates#getPredicates()}
 * statistic.
 *
 * @param rel the relational expression
 * @return Predicates that can be pulled above this RelNode
 */
public RelOptPredicateList getPulledUpPredicates(RelNode rel) {
  for (;;) {
    try {
      return predicatesHandler.getPredicates(rel, this);
    } catch (JaninoRelMetadataProvider.NoHandler e) {
      predicatesHandler = revise(e.relClass, BuiltInMetadata.Predicates.DEF);
    }
  }
}
 
Example #20
Source File: RexProgramBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Registers an expression in the list of common sub-expressions, and
 * returns a reference to that expression.
 *
 * <p>If an equivalent sub-expression already exists, creates another
 * expression only if <code>force</code> is true.
 *
 * @param expr  Expression to register
 * @param force Whether to create a new sub-expression if an equivalent
 *              sub-expression exists.
 */
private RexLocalRef registerInternal(RexNode expr, boolean force) {
    final RexSimplify simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
    expr = simplify.simplifyPreservingType(expr);

    RexLocalRef ref;
    final Pair<RexNode, String> key;
    if (expr instanceof RexLocalRef) {
        key = null;
        ref = (RexLocalRef) expr;
    } else {
        key = RexUtil.makeKey(expr);
        ref = exprMap.get(key);
    }
    if (ref == null) {
        if (validating) {
            validate(expr, exprList.size());
        }

        // Add expression to list, and return a new reference to it.
        ref = addExpr(expr);
        exprMap.put(key, ref);
    } else {
        if (force) {
            // Add expression to list, but return the previous ref.
            addExpr(expr);
        }
    }

    for (;;) {
        int index = ref.getIndex();
        final RexNode expr2 = exprList.get(index);
        if (expr2 instanceof RexLocalRef) {
            ref = (RexLocalRef) expr2;
        } else {
            return ref;
        }
    }
}
 
Example #21
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1960">[CALCITE-1960]
 * RelMdPredicates.getPredicates is slow if there are many equivalent
 * columns</a>. There are much less duplicates after
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2205">[CALCITE-2205]</a>.
 * Since this is a performance problem, the test result does not
 * change, but takes over 15 minutes before the fix and 6 seconds after. */
@Test void testPullUpPredicatesForExprsItr() {
  final String sql = "select a.EMPNO, a.ENAME\n"
      + "from (select * from sales.emp ) a\n"
      + "join (select * from sales.emp  ) b\n"
      + "on a.empno = b.deptno\n"
      + "  and a.comm = b.comm\n"
      + "  and a.mgr=b.mgr\n"
      + "  and (a.empno < 10 or a.comm < 3 or a.deptno < 10\n"
      + "    or a.job ='abc' or a.ename='abc' or a.sal='30' or a.mgr >3\n"
      + "    or a.slacker is not null  or a.HIREDATE is not null\n"
      + "    or b.empno < 9 or b.comm < 3 or b.deptno < 10 or b.job ='abc'\n"
      + "    or b.ename='abc' or b.sal='30' or b.mgr >3 or b.slacker )\n"
      + "join emp c\n"
      + "on b.mgr =a.mgr and a.empno =b.deptno and a.comm=b.comm\n"
      + "  and a.deptno=b.deptno and a.job=b.job and a.ename=b.ename\n"
      + "  and a.mgr=b.deptno and a.slacker=b.slacker";
  // Lock to ensure that only one test is using this method at a time.
  try (JdbcAdapterTest.LockWrapper ignore =
           JdbcAdapterTest.LockWrapper.lock(LOCK)) {
    // FIXME: fix timeout when enable implicit type coercion.
    final RelNode rel = convertSql(sql, false);
    final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel.getInput(0));
    assertThat(inputSet.pulledUpPredicates.size(), is(11));
  }
}
 
Example #22
Source File: RexProgramBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RexProgramBuilder create(RexBuilder rexBuilder, final RelDataType inputRowType,
        final List<RexNode> exprList, final List<? extends RexNode> projectList, final RexNode condition,
        final RelDataType outputRowType, boolean normalize, boolean simplify_) {
    RexSimplify simplify = null;
    if (simplify_) {
        simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
    }
    return new RexProgramBuilder(rexBuilder, inputRowType, exprList, projectList, condition, outputRowType,
            normalize, simplify);
}
 
Example #23
Source File: RexProgramTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testSimplifyNotEqual() {
  RexNode ref = input(tInt(), 0);
  RelOptPredicateList relOptPredicateList = RelOptPredicateList.of(rexBuilder,
      ImmutableList.of(eq(ref, literal(9))));
  checkSimplifyFilter(ne(ref, literal(9)), relOptPredicateList, "false");
  checkSimplifyFilter(ne(ref, literal(5)), relOptPredicateList, "true");

  ref = input(tInt(true), 0);
  checkSimplifyFilter(ne(ref, literal(9)), relOptPredicateList,
      "AND(null, IS NULL($0))");
  checkSimplifyFilter(ne(ref, literal(5)), relOptPredicateList,
      "OR(null, IS NOT NULL($0))");
}
 
Example #24
Source File: ExchangeRemoveConstantKeysRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Exchange exchange = call.rel(0);
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelNode input = exchange.getInput();
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(input);
  if (predicates == null) {
    return;
  }

  final Set<Integer> constants = new HashSet<>();
  predicates.constantMap.keySet().forEach(key -> {
    if (key instanceof RexInputRef) {
      constants.add(((RexInputRef) key).getIndex());
    }
  });
  if (constants.isEmpty()) {
    return;
  }

  final List<Integer> distributionKeys = simplifyDistributionKeys(
      exchange.getDistribution(), constants);

  if (distributionKeys.size() != exchange.getDistribution().getKeys()
      .size()) {
    call.transformTo(call.builder()
        .push(exchange.getInput())
        .exchange(distributionKeys.isEmpty()
            ? RelDistributions.SINGLETON
            : RelDistributions.hash(distributionKeys))
        .build());
    call.getPlanner().prune(exchange);
  }
}
 
Example #25
Source File: SortRemoveConstantKeysRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelNode input = sort.getInput();
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(input);
  if (predicates == null) {
    return;
  }

  final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
  final List<RelFieldCollation> collationsList =
      sort.getCollation().getFieldCollations().stream()
          .filter(fc ->
              !predicates.constantMap.containsKey(
                  rexBuilder.makeInputRef(input, fc.getFieldIndex())))
          .collect(Collectors.toList());

  if (collationsList.size() == sort.collation.getFieldCollations().size()) {
    return;
  }

  // No active collations. Remove the sort completely
  if (collationsList.isEmpty() && sort.offset == null && sort.fetch == null) {
    call.transformTo(input);
    call.getPlanner().setImportance(sort, 0.0);
    return;
  }

  final Sort result =
      sort.copy(sort.getTraitSet(), input, RelCollations.of(collationsList));
  call.transformTo(result);
  call.getPlanner().setImportance(sort, 0.0);
}
 
Example #26
Source File: RelBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected RelBuilder(Context context, RelOptCluster cluster, RelOptSchema relOptSchema) {
    this.cluster = cluster;
    this.relOptSchema = relOptSchema;
    if (context == null) {
        context = Contexts.EMPTY_CONTEXT;
    }
    this.simplify = Hook.REL_BUILDER_SIMPLIFY.get(true);
    this.aggregateFactory = Util.first(context.unwrap(RelFactories.AggregateFactory.class),
            RelFactories.DEFAULT_AGGREGATE_FACTORY);
    this.filterFactory = Util.first(context.unwrap(RelFactories.FilterFactory.class),
            RelFactories.DEFAULT_FILTER_FACTORY);
    this.projectFactory = Util.first(context.unwrap(RelFactories.ProjectFactory.class),
            RelFactories.DEFAULT_PROJECT_FACTORY);
    this.sortFactory = Util.first(context.unwrap(RelFactories.SortFactory.class),
            RelFactories.DEFAULT_SORT_FACTORY);
    this.exchangeFactory = Util.first(context.unwrap(RelFactories.ExchangeFactory.class),
            RelFactories.DEFAULT_EXCHANGE_FACTORY);
    this.sortExchangeFactory = Util.first(context.unwrap(RelFactories.SortExchangeFactory.class),
            RelFactories.DEFAULT_SORT_EXCHANGE_FACTORY);
    this.setOpFactory = Util.first(context.unwrap(RelFactories.SetOpFactory.class),
            RelFactories.DEFAULT_SET_OP_FACTORY);
    this.joinFactory = Util.first(context.unwrap(RelFactories.JoinFactory.class),
            RelFactories.DEFAULT_JOIN_FACTORY);
    this.semiJoinFactory = Util.first(context.unwrap(RelFactories.SemiJoinFactory.class),
            RelFactories.DEFAULT_SEMI_JOIN_FACTORY);
    this.correlateFactory = Util.first(context.unwrap(RelFactories.CorrelateFactory.class),
            RelFactories.DEFAULT_CORRELATE_FACTORY);
    this.valuesFactory = Util.first(context.unwrap(RelFactories.ValuesFactory.class),
            RelFactories.DEFAULT_VALUES_FACTORY);
    this.scanFactory = Util.first(context.unwrap(RelFactories.TableScanFactory.class),
            RelFactories.DEFAULT_TABLE_SCAN_FACTORY);
    this.snapshotFactory = Util.first(context.unwrap(RelFactories.SnapshotFactory.class),
            RelFactories.DEFAULT_SNAPSHOT_FACTORY);
    this.matchFactory = Util.first(context.unwrap(RelFactories.MatchFactory.class),
            RelFactories.DEFAULT_MATCH_FACTORY);
    final RexExecutor executor = Util.first(context.unwrap(RexExecutor.class),
            Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR));
    final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
    this.simplifier = new RexSimplify(cluster.getRexBuilder(), predicates, executor);
}
 
Example #27
Source File: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Simplifies a conjunction of boolean expressions.
 *
 * @deprecated Use
 * {@link RexSimplify#simplifyAnds(Iterable, RexUnknownAs)}.
 */
@Deprecated // to be removed before 2.0
public static RexNode simplifyAnds(RexBuilder rexBuilder,
    Iterable<? extends RexNode> nodes) {
  return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, EXECUTOR)
      .simplifyAnds(nodes, RexUnknownAs.UNKNOWN);
}
 
Example #28
Source File: SortRemoveConstantKeysRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelNode input = sort.getInput();
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(input);
  if (predicates == null) {
    return;
  }

  final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
  final List<RelFieldCollation> collationsList =
      sort.getCollation().getFieldCollations().stream()
          .filter(fc ->
              !predicates.constantMap.containsKey(
                  rexBuilder.makeInputRef(input, fc.getFieldIndex())))
          .collect(Collectors.toList());

  if (collationsList.size() == sort.collation.getFieldCollations().size()) {
    return;
  }

  // No active collations. Remove the sort completely
  if (collationsList.isEmpty() && sort.offset == null && sort.fetch == null) {
    call.transformTo(input);
    call.getPlanner().prune(sort);
    return;
  }

  final Sort result =
      sort.copy(sort.getTraitSet(), input, RelCollations.of(collationsList));
  call.transformTo(result);
  call.getPlanner().prune(sort);
}
 
Example #29
Source File: RexAnalyzer.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a RexAnalyzer. */
public RexAnalyzer(RexNode e, RelOptPredicateList predicates) {
  this.e = e;
  final VariableCollector variableCollector = new VariableCollector();
  e.accept(variableCollector);
  predicates.pulledUpPredicates.forEach(p -> p.accept(variableCollector));
  variables = ImmutableList.copyOf(variableCollector.builder);
  unsupportedCount = variableCollector.unsupportedCount;
}
 
Example #30
Source File: RelMdAllPredicates.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Add the Filter condition to the list obtained from the input.
 */
public RelOptPredicateList getAllPredicates(Filter filter, RelMetadataQuery mq) {
  final RelNode input = filter.getInput();
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RexNode pred = filter.getCondition();

  final RelOptPredicateList predsBelow = mq.getAllPredicates(input);
  if (predsBelow == null) {
    // Safety check
    return null;
  }

  // Extract input fields referenced by Filter condition
  final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>();
  final RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields);
  pred.accept(inputFinder);
  final ImmutableBitSet inputFieldsUsed = inputFinder.build();

  // Infer column origin expressions for given references
  final Map<RexInputRef, Set<RexNode>> mapping = new LinkedHashMap<>();
  for (int idx : inputFieldsUsed) {
    final RexInputRef ref = RexInputRef.of(idx, filter.getRowType().getFieldList());
    final Set<RexNode> originalExprs = mq.getExpressionLineage(filter, ref);
    if (originalExprs == null) {
      // Bail out
      return null;
    }
    mapping.put(ref, originalExprs);
  }

  // Replace with new expressions and return union of predicates
  final Set<RexNode> allExprs =
      RelMdExpressionLineage.createAllPossibleExpressions(rexBuilder, pred, mapping);
  if (allExprs == null) {
    return null;
  }
  return predsBelow.union(rexBuilder, RelOptPredicateList.of(rexBuilder, allExprs));
}