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

The following examples show how to use org.apache.calcite.plan.RelOptUtil#disjunctions() . 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: GeodeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean matches(RelOptRuleCall call) {
  // Get the condition from the filter operation
  LogicalFilter filter = call.rel(0);
  RexNode condition = filter.getCondition();

  List<String> fieldNames = GeodeRules.geodeFieldNames(filter.getInput().getRowType());

  List<RexNode> disjunctions = RelOptUtil.disjunctions(condition);
  if (disjunctions.size() != 1) {
    return true;
  } else {
    // Check that all conjunctions are primary field conditions.
    condition = disjunctions.get(0);
    for (RexNode predicate : RelOptUtil.conjunctions(condition)) {
      if (!isEqualityOnKey(predicate, fieldNames)) {
        return false;
      }
    }
  }

  return true;
}
 
Example 2
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RexNode toDnf(RexNode rex) {
    final List<RexNode> operands;
    switch (rex.getKind()) {
    case AND:
        operands = flattenAnd(((RexCall) rex).getOperands());
        final RexNode head = operands.get(0);
        final RexNode headDnf = toDnf(head);
        final List<RexNode> headDnfs = RelOptUtil.disjunctions(headDnf);
        final RexNode tail = and(Util.skip(operands));
        final RexNode tailDnf = toDnf(tail);
        final List<RexNode> tailDnfs = RelOptUtil.disjunctions(tailDnf);
        final List<RexNode> list = new ArrayList<>();
        for (RexNode h : headDnfs) {
            for (RexNode t : tailDnfs) {
                list.add(and(ImmutableList.of(h, t)));
            }
        }
        return or(list);
    case OR:
        operands = flattenOr(((RexCall) rex).getOperands());
        return or(toDnfs(operands));
    case NOT:
        final RexNode arg = ((RexCall) rex).getOperands().get(0);
        switch (arg.getKind()) {
        case NOT:
            return toDnf(((RexCall) arg).getOperands().get(0));
        case OR:
            operands = ((RexCall) arg).getOperands();
            return toDnf(and(Lists.transform(flattenOr(operands), RexUtil::addNot)));
        case AND:
            operands = ((RexCall) arg).getOperands();
            return toDnf(or(Lists.transform(flattenAnd(operands), RexUtil::addNot)));
        default:
            return rex;
        }
    default:
        return rex;
    }
}
 
Example 3
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public QueryBuilder translate() {
    final List<RexNode> orNodes = RelOptUtil.disjunctions(condition);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (RexNode node : orNodes)
        boolQueryBuilder.should(translateAnd(node));
    return boolQueryBuilder;
}
 
Example 4
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public QueryBuilder translate() {
    final List<RexNode> orNodes = RelOptUtil.disjunctions(condition);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (RexNode node : orNodes)
        boolQueryBuilder.should(translateAnd(node));
    return boolQueryBuilder;
}
 
Example 5
Source File: FilterTranslator.java    From sql-gremlin with Apache License 2.0 5 votes vote down vote up
private GraphTraversal translateOr(RexNode condition) {
    List<GraphTraversal> list = new ArrayList<>();
    for (RexNode node : RelOptUtil.disjunctions(condition)) {
        list.addAll(translateAnd(node));
    }
    switch (list.size()) {
        case 1:
            return list.get(0);
        default:
            Map<String, Object> map = builder.map();
            map.put("$or", list);
            return __.__().or(list.toArray(new GraphTraversal[list.size()]));
    }
}
 
Example 6
Source File: GeodeFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Produce the OQL predicate string for the given condition.
 *
 * @param condition Condition to translate
 * @return OQL predicate string
 */
private String translateMatch(RexNode condition) {
  // Returns condition decomposed by OR
  List<RexNode> disjunctions = RelOptUtil.disjunctions(condition);
  if (disjunctions.size() == 1) {
    return translateAnd(disjunctions.get(0));
  } else {
    return translateOr(disjunctions);
  }
}
 
Example 7
Source File: CassandraFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Produce the CQL predicate string for the given condition.
 *
 * @param condition Condition to translate
 * @return CQL predicate string
 */
private String translateMatch(RexNode condition) {
  // CQL does not support disjunctions
  List<RexNode> disjunctions = RelOptUtil.disjunctions(condition);
  if (disjunctions.size() == 1) {
    return translateAnd(disjunctions.get(0));
  } else {
    throw new AssertionError("cannot translate " + condition);
  }
}
 
Example 8
Source File: CassandraRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean matches(RelOptRuleCall call) {
  // Get the condition from the filter operation
  LogicalFilter filter = call.rel(0);
  RexNode condition = filter.getCondition();

  // Get field names from the scan operation
  CassandraTableScan scan = call.rel(1);
  Pair<List<String>, List<String>> keyFields = scan.cassandraTable.getKeyFields();
  Set<String> partitionKeys = new HashSet<>(keyFields.left);
  List<String> fieldNames = CassandraRules.cassandraFieldNames(filter.getInput().getRowType());

  List<RexNode> disjunctions = RelOptUtil.disjunctions(condition);
  if (disjunctions.size() != 1) {
    return false;
  } else {
    // Check that all conjunctions are primary key equalities
    condition = disjunctions.get(0);
    for (RexNode predicate : RelOptUtil.conjunctions(condition)) {
      if (!isEqualityOnKey(predicate, fieldNames, partitionKeys, keyFields.right)) {
        return false;
      }
    }
  }

  // Either all of the partition keys must be specified or none
  return partitionKeys.size() == keyFields.left.size() || partitionKeys.size() == 0;
}
 
Example 9
Source File: MongoFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Object translateOr(RexNode condition) {
  List<Object> list = new ArrayList<>();
  for (RexNode node : RelOptUtil.disjunctions(condition)) {
    list.add(translateAnd(node));
  }
  switch (list.size()) {
  case 1:
    return list.get(0);
  default:
    Map<String, Object> map = builder.map();
    map.put("$or", list);
    return map;
  }
}
 
Example 10
Source File: DrillRelMdSelectivity.java    From Bats with Apache License 2.0 4 votes vote down vote up
private double getScanSelectivityInternal(TableMetadata tableMetadata, RexNode predicate, List<SchemaPath> fieldNames) {
  double sel = 1.0;
  if ((predicate == null) || predicate.isAlwaysTrue()) {
    return sel;
  }
  for (RexNode pred : RelOptUtil.conjunctions(predicate)) {
    double orSel = 0;
    for (RexNode orPred : RelOptUtil.disjunctions(pred)) {
      if (isMultiColumnPredicate(orPred)) {
        orSel += RelMdUtil.guessSelectivity(orPred);  //CALCITE guess
      } else if (orPred.isA(SqlKind.EQUALS)) {
        orSel += computeEqualsSelectivity(tableMetadata, orPred, fieldNames);
      } else if (orPred.isA(RANGE_PREDICATE)) {
        orSel += computeRangeSelectivity(tableMetadata, orPred, fieldNames);
      } else if (orPred.isA(SqlKind.NOT_EQUALS)) {
        orSel += 1.0 - computeEqualsSelectivity(tableMetadata, orPred, fieldNames);
      } else if (orPred.isA(SqlKind.LIKE)) {
        // LIKE selectivity is 5% more than a similar equality predicate, capped at CALCITE guess
        orSel +=  Math.min(computeEqualsSelectivity(tableMetadata, orPred, fieldNames) + LIKE_PREDICATE_SELECTIVITY,
            guessSelectivity(orPred));
      } else if (orPred.isA(SqlKind.NOT)) {
        if (orPred instanceof RexCall) {
          // LIKE selectivity is 5% more than a similar equality predicate, capped at CALCITE guess
          RexNode childOp = ((RexCall) orPred).getOperands().get(0);
          if (childOp.isA(SqlKind.LIKE)) {
            orSel += 1.0 - Math.min(computeEqualsSelectivity(tableMetadata, childOp, fieldNames) + LIKE_PREDICATE_SELECTIVITY,
                    guessSelectivity(childOp));
          } else {
            orSel += 1.0 - guessSelectivity(orPred);
          }
        }
      } else if (orPred.isA(SqlKind.IS_NULL)) {
        orSel += 1.0 - computeIsNotNullSelectivity(tableMetadata, orPred, fieldNames);
      } else if (orPred.isA(SqlKind.IS_NOT_NULL)) {
        orSel += computeIsNotNullSelectivity(tableMetadata, orPred, fieldNames);
      } else {
        // Use the CALCITE guess.
        orSel += guessSelectivity(orPred);
      }
    }
    sel *= orSel;
  }
  // Cap selectivity if it exceeds 1.0
  return (sel > 1.0) ? 1.0 : sel;
}
 
Example 11
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RexNode toDnf(RexNode rex) {
  final List<RexNode> operands;
  switch (rex.getKind()) {
  case AND:
    operands = flattenAnd(((RexCall) rex).getOperands());
    final RexNode head = operands.get(0);
    final RexNode headDnf = toDnf(head);
    final List<RexNode> headDnfs = RelOptUtil.disjunctions(headDnf);
    final RexNode tail = and(Util.skip(operands));
    final RexNode tailDnf = toDnf(tail);
    final List<RexNode> tailDnfs = RelOptUtil.disjunctions(tailDnf);
    final List<RexNode> list = new ArrayList<>();
    for (RexNode h : headDnfs) {
      for (RexNode t : tailDnfs) {
        list.add(and(ImmutableList.of(h, t)));
      }
    }
    return or(list);
  case OR:
    operands = flattenOr(((RexCall) rex).getOperands());
    return or(toDnfs(operands));
  case NOT:
    final RexNode arg = ((RexCall) rex).getOperands().get(0);
    switch (arg.getKind()) {
    case NOT:
      return toDnf(((RexCall) arg).getOperands().get(0));
    case OR:
      operands = ((RexCall) arg).getOperands();
      return toDnf(
          and(Lists.transform(flattenOr(operands), RexUtil::addNot)));
    case AND:
      operands = ((RexCall) arg).getOperands();
      return toDnf(
          or(Lists.transform(flattenAnd(operands), RexUtil::addNot)));
    default:
      return rex;
    }
  default:
    return rex;
  }
}