Java Code Examples for org.apache.calcite.sql.SqlKind#IS_NOT_NULL

The following examples show how to use org.apache.calcite.sql.SqlKind#IS_NOT_NULL . 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: PredicateAnalyzer.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private QueryExpression postfix(RexCall call) {
  Preconditions.checkArgument(call.getKind() == SqlKind.IS_NULL || call.getKind() == SqlKind.IS_NOT_NULL);
  if (call.getOperands().size() != 1) {
    throw new PredicateAnalyzerException(format("Unsupported operator: [%s]", call));
  }
  Expression a = call.getOperands().get(0).accept(this);

  // Fields cannot be queried without being indexed
  final NamedFieldExpression fieldExpression = (NamedFieldExpression) a;
  if (fieldExpression.getAnnotation() != null && fieldExpression.getAnnotation().isNotIndexed()) {
    throw new PredicateAnalyzerException("Cannot handle " + call.getKind() + " expression for field not indexed, " + call);
  }

  // Elasticsearch does not want is null/is not null (exists query) for _id and _index, although it supports for all other metadata column
  isColumn(a, call, ElasticsearchConstants.ID, true);
  isColumn(a, call, ElasticsearchConstants.INDEX, true);
  QueryExpression operand = QueryExpression.create((TerminalExpression)a);
  return call.getKind() == SqlKind.IS_NOT_NULL ? operand.exists() : operand.notExists();
}
 
Example 2
Source File: DruidJsonFilter.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Nullable
private static DruidJsonFilter toIsNullKindDruidFilter(RexNode rexNode, RelDataType rowType,
    DruidQuery druidQuery) {
  if (rexNode.getKind() != SqlKind.IS_NULL && rexNode.getKind() != SqlKind.IS_NOT_NULL) {
    throw new AssertionError(
        DruidQuery.format("Expecting IS_NULL or IS_NOT_NULL but got [%s]", rexNode.getKind()));
  }
  final RexCall rexCall = (RexCall) rexNode;
  final RexNode refNode = rexCall.getOperands().get(0);
  Pair<String, ExtractionFunction> druidColumn = DruidQuery
      .toDruidColumn(refNode, rowType, druidQuery);
  final String columnName = druidColumn.left;
  final ExtractionFunction extractionFunction = druidColumn.right;
  if (columnName == null) {
    return null;
  }
  if (rexNode.getKind() == SqlKind.IS_NOT_NULL) {
    return toNotDruidFilter(new JsonSelector(columnName, null, extractionFunction));
  }
  return new JsonSelector(columnName, null, extractionFunction);
}
 
Example 3
Source File: RelMdUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns default estimates for selectivities, in the absence of stats.
 *
 * @param predicate      predicate for which selectivity will be computed;
 *                       null means true, so gives selectity of 1.0
 * @param artificialOnly return only the selectivity contribution from
 *                       artificial nodes
 * @return estimated selectivity
 */
public static double guessSelectivity(
    RexNode predicate,
    boolean artificialOnly) {
  double sel = 1.0;
  if ((predicate == null) || predicate.isAlwaysTrue()) {
    return sel;
  }

  double artificialSel = 1.0;

  for (RexNode pred : RelOptUtil.conjunctions(predicate)) {
    if (pred.getKind() == SqlKind.IS_NOT_NULL) {
      sel *= .9;
    } else if (
        (pred instanceof RexCall)
            && (((RexCall) pred).getOperator()
            == RelMdUtil.ARTIFICIAL_SELECTIVITY_FUNC)) {
      artificialSel *= RelMdUtil.getSelectivityValue(pred);
    } else if (pred.isA(SqlKind.EQUALS)) {
      sel *= .15;
    } else if (pred.isA(SqlKind.COMPARISON)) {
      sel *= .5;
    } else {
      sel *= .25;
    }
  }

  if (artificialOnly) {
    return artificialSel;
  } else {
    return sel * artificialSel;
  }
}
 
Example 4
Source File: PredicateAnalyzer.java    From calcite with Apache License 2.0 5 votes vote down vote up
private QueryExpression postfix(RexCall call) {
  Preconditions.checkArgument(call.getKind() == SqlKind.IS_NULL
      || call.getKind() == SqlKind.IS_NOT_NULL);
  if (call.getOperands().size() != 1) {
    String message = String.format(Locale.ROOT, "Unsupported operator: [%s]", call);
    throw new PredicateAnalyzerException(message);
  }
  Expression a = call.getOperands().get(0).accept(this);
  // Elasticsearch does not want is null/is not null (exists query)
  // for _id and _index, although it supports for all other metadata column
  isColumn(a, call, ElasticsearchConstants.ID, true);
  isColumn(a, call, ElasticsearchConstants.INDEX, true);
  QueryExpression operand = QueryExpression.create((TerminalExpression) a);
  return call.getKind() == SqlKind.IS_NOT_NULL ? operand.exists() : operand.notExists();
}
 
Example 5
Source File: RelMdUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns default estimates for selectivities, in the absence of stats.
 *
 * @param predicate      predicate for which selectivity will be computed;
 *                       null means true, so gives selectity of 1.0
 * @param artificialOnly return only the selectivity contribution from
 *                       artificial nodes
 * @return estimated selectivity
 */
public static double guessSelectivity(
    RexNode predicate,
    boolean artificialOnly) {
  double sel = 1.0;
  if ((predicate == null) || predicate.isAlwaysTrue()) {
    return sel;
  }

  double artificialSel = 1.0;

  for (RexNode pred : RelOptUtil.conjunctions(predicate)) {
    if (pred.getKind() == SqlKind.IS_NOT_NULL) {
      sel *= .9;
    } else if (
        (pred instanceof RexCall)
            && (((RexCall) pred).getOperator()
            == RelMdUtil.ARTIFICIAL_SELECTIVITY_FUNC)) {
      artificialSel *= RelMdUtil.getSelectivityValue(pred);
    } else if (pred.isA(SqlKind.EQUALS)) {
      sel *= .15;
    } else if (pred.isA(SqlKind.COMPARISON)) {
      sel *= .5;
    } else {
      sel *= .25;
    }
  }

  if (artificialOnly) {
    return artificialSel;
  } else {
    return sel * artificialSel;
  }
}
 
Example 6
Source File: RexSimplify.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Simplifies a list of terms and combines them into an OR.
 * Modifies the list in place. */
private RexNode simplifyOrs(List<RexNode> terms, RexUnknownAs unknownAs) {
  // CALCITE-3198 Auxiliary map to simplify cases like:
  //   X <> A OR X <> B => X IS NOT NULL or NULL
  // The map key will be the 'X'; and the value the first call 'X<>A' that is found,
  // or 'X IS NOT NULL' if a simplification takes place (because another 'X<>B' is found)
  final Map<RexNode, RexNode> notEqualsComparisonMap = new HashMap<>();
  final RexLiteral trueLiteral = rexBuilder.makeLiteral(true);
  for (int i = 0; i < terms.size(); i++) {
    final RexNode term = terms.get(i);
    switch (term.getKind()) {
    case LITERAL:
      if (RexLiteral.isNullLiteral(term)) {
        if (unknownAs == FALSE) {
          terms.remove(i);
          --i;
          continue;
        } else if (unknownAs == TRUE) {
          return trueLiteral;
        }
      } else {
        if (RexLiteral.booleanValue(term)) {
          return term; // true
        } else {
          terms.remove(i);
          --i;
          continue;
        }
      }
      break;
    case NOT_EQUALS:
      final Comparison notEqualsComparison = Comparison.of(term);
      if (notEqualsComparison != null) {
        // We are dealing with a X<>A term, check if we saw before another NOT_EQUALS involving X
        final RexNode prevNotEquals = notEqualsComparisonMap.get(notEqualsComparison.ref);
        if (prevNotEquals == null) {
          // This is the first NOT_EQUALS involving X, put it in the map
          notEqualsComparisonMap.put(notEqualsComparison.ref, term);
        } else {
          // There is already in the map another NOT_EQUALS involving X:
          //   - if it is already an IS_NOT_NULL: it was already simplified, ignore this term
          //   - if it is not an IS_NOT_NULL (i.e. it is a NOT_EQUALS): check comparison values
          if (prevNotEquals.getKind() != SqlKind.IS_NOT_NULL) {
            final Comparable comparable1 = notEqualsComparison.literal.getValue();
            final Comparable comparable2 = Comparison.of(prevNotEquals).literal.getValue();
            //noinspection unchecked
            if (comparable1.compareTo(comparable2) != 0) {
              // X <> A OR X <> B => X IS NOT NULL OR NULL
              final RexNode isNotNull =
                  rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, notEqualsComparison.ref);
              final RexNode constantNull =
                  rexBuilder.makeNullLiteral(trueLiteral.getType());
              final RexNode newCondition = simplify(
                  rexBuilder.makeCall(SqlStdOperatorTable.OR, isNotNull, constantNull),
                  unknownAs);
              if (newCondition.isAlwaysTrue()) {
                return trueLiteral;
              }
              notEqualsComparisonMap.put(notEqualsComparison.ref, isNotNull);
              final int pos = terms.indexOf(prevNotEquals);
              terms.set(pos, newCondition);
            }
          }
          terms.remove(i);
          --i;
          continue;
        }
      }
      break;
    }
    terms.set(i, term);
  }
  return RexUtil.composeDisjunction(rexBuilder, terms);
}