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

The following examples show how to use org.apache.calcite.sql.SqlKind#OR . 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: RewriteAsBinaryOperators.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitCall(RexCall call) {
  SqlOperator op = call.getOperator();
  SqlKind kind = op.getKind();
  RelDataType type = call.getType();
  if (kind == SqlKind.OR || kind == SqlKind.AND) {
    if (call.getOperands().size() > 2) {
      List<RexNode> children = new ArrayList<>(call.getOperands());
      RexNode left = children.remove(0).accept(this);
      RexNode right = builder.makeCall(type, op, children).accept(this);
      return builder.makeCall(type, op, ImmutableList.of(left, right));
    }
  }
  return builder.makeCall(type, op, visitChildren(call));
}
 
Example 2
Source File: RewriteAsBinaryOperators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitCall(RexCall call) {
  SqlOperator op = call.getOperator();
  SqlKind kind = op.getKind();
  RelDataType type = call.getType();
  if (kind == SqlKind.OR || kind == SqlKind.AND) {
    if (call.getOperands().size() > 2) {
      List<RexNode> children = new ArrayList<>(call.getOperands());
      RexNode left = children.remove(0).accept(this);
      RexNode right = builder.makeCall(type, op, children).accept(this);
      return builder.makeCall(type, op, ImmutableList.of(left, right));
    }
  }
  return builder.makeCall(type, op, visitChildren(call));
}
 
Example 3
Source File: RelOptUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for
 * {@link #splitJoinCondition(RexBuilder, int, RexNode, List, List, List, List)} and
 * {@link #splitJoinCondition(List, List, RexNode, List, List, List, List)}.
 *
 * <p>If the given expr <code>call</code> is an expanded version of
 * IS NOT DISTINCT FROM function call, collapse it and return a
 * IS NOT DISTINCT FROM function call.
 *
 * <p>For example: {@code t1.key IS NOT DISTINCT FROM t2.key}
 * can rewritten in expanded form as
 * {@code t1.key = t2.key OR (t1.key IS NULL AND t2.key IS NULL)}.
 *
 * @param call       Function expression to try collapsing.
 * @param rexBuilder {@link RexBuilder} instance to create new {@link RexCall} instances.
 * @return If the given function is an expanded IS NOT DISTINCT FROM function call,
 *         return a IS NOT DISTINCT FROM function call. Otherwise return the input
 *         function call as it is.
 */
private static RexCall collapseExpandedIsNotDistinctFromExpr(final RexCall call, final RexBuilder rexBuilder) {
    if (call.getKind() != SqlKind.OR || call.getOperands().size() != 2) {
        return call;
    }

    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (!(op0 instanceof RexCall) || !(op1 instanceof RexCall)) {
        return call;
    }

    RexCall opEqCall = (RexCall) op0;
    RexCall opNullEqCall = (RexCall) op1;

    if (opEqCall.getKind() == SqlKind.AND && opNullEqCall.getKind() == SqlKind.EQUALS) {
        RexCall temp = opEqCall;
        opEqCall = opNullEqCall;
        opNullEqCall = temp;
    }

    if (opNullEqCall.getKind() != SqlKind.AND || opNullEqCall.getOperands().size() != 2
            || opEqCall.getKind() != SqlKind.EQUALS) {
        return call;
    }

    final RexNode op10 = opNullEqCall.getOperands().get(0);
    final RexNode op11 = opNullEqCall.getOperands().get(1);
    if (op10.getKind() != SqlKind.IS_NULL || op11.getKind() != SqlKind.IS_NULL) {
        return call;
    }
    final RexNode isNullInput0 = ((RexCall) op10).getOperands().get(0);
    final RexNode isNullInput1 = ((RexCall) op11).getOperands().get(0);

    final String isNullInput0Digest = isNullInput0.toString();
    final String isNullInput1Digest = isNullInput1.toString();
    final String equalsInput0Digest = opEqCall.getOperands().get(0).toString();
    final String equalsInput1Digest = opEqCall.getOperands().get(1).toString();

    if ((isNullInput0Digest.equals(equalsInput0Digest) && isNullInput1Digest.equals(equalsInput1Digest))
            || (isNullInput1Digest.equals(equalsInput0Digest) && isNullInput0Digest.equals(equalsInput1Digest))) {
        return (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
                ImmutableList.of(isNullInput0, isNullInput1));
    }

    return call;
}
 
Example 4
Source File: PredicateAnalyzer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Process a call which is a binary operation, transforming into an equivalent
 * query expression. Note that the incoming call may be either a simple binary
 * expression, such as 'foo > 5', or it may be several simple expressions connected
 * by 'AND' or 'OR' operators, such as 'foo > 5 AND bar = 'abc' AND 'rot' < 1'.
 */
private QueryExpression binary(RexCall call) {

  // if AND/OR, do special handling
  if (call.getKind() == SqlKind.AND || call.getKind() == SqlKind.OR) {
    return andOr(call);
  }

  checkForIncompatibleDateTimeOperands(call);

  Preconditions.checkState(call.getOperands().size() == 2);
  final Expression a = call.getOperands().get(0).accept(this);
  final Expression b = call.getOperands().get(1).accept(this);

  final SwapResult pair = swap(a, b);
  final boolean swapped = pair.isSwapped();

  // For _id and _index columns, only equals/not_equals work!
  if (isColumn(pair.getKey(), call, ElasticsearchConstants.ID, false)
    || isColumn(pair.getKey(), call, ElasticsearchConstants.INDEX, false)
    || isColumn(pair.getKey(), call, ElasticsearchConstants.UID, false)) {
    switch (call.getKind()) {
      case EQUALS:
      case NOT_EQUALS:
        break;
      default:
        throw new PredicateAnalyzerException("Cannot handle " + call.getKind() + " expression for _id field, " + call);
    }
  }

 // Fields cannot be queried without being indexed
  final NamedFieldExpression fieldExpression = (NamedFieldExpression) pair.getKey();
  if (fieldExpression.getAnnotation() != null && fieldExpression.getAnnotation().isNotIndexed()) {
    throw new PredicateAnalyzerException("Cannot handle " + call.getKind() + " expression because indexing is disabled, " + call);
  }

  // Analyzed text fields and normalized keyword fields cannot be pushed down unless allowed in settings
  if (!config.isAllowPushdownOnNormalizedOrAnalyzedFields() &&
      fieldExpression.getAnnotation() != null && fieldExpression.getType().isText() &&
      (fieldExpression.getAnnotation().isAnalyzed() || fieldExpression.getAnnotation().isNormalized())) {
    throw new PredicateAnalyzerException(
        "Cannot handle " + call.getKind() + " expression because text or keyword field is analyzed or normalized, " + call);
  }

  switch (call.getKind()) {
    case LIKE:
      // LIKE/regexp cannot handle metadata columns
      isMeta(pair.getKey(), call, true);
      String sqlRegex = RegexpUtil.sqlToRegexLike(pair.getValue().stringValue());
      RexLiteral sqlRegexLiteral = rexBuilder.makeLiteral(sqlRegex);
      LiteralExpression sqlRegexExpression = new LiteralExpression(sqlRegexLiteral);
      SqlLikeOperator likeOperator = (SqlLikeOperator) call.getOperator();
      if (likeOperator.isNegated()) {
        return QueryExpression.create(pair.getKey()).notLike(sqlRegexExpression);
      } else {
        return QueryExpression.create(pair.getKey()).like(sqlRegexExpression);
      }
    case EQUALS:
      return QueryExpression.create(pair.getKey()).equals(pair.getValue());
    case NOT_EQUALS:
      return QueryExpression.create(pair.getKey()).notEquals(pair.getValue());
    case GREATER_THAN:
      if (swapped) {
        return QueryExpression.create(pair.getKey()).lt(pair.getValue());
      }
      return QueryExpression.create(pair.getKey()).gt(pair.getValue());
    case GREATER_THAN_OR_EQUAL:
      if (swapped) {
        return QueryExpression.create(pair.getKey()).lte(pair.getValue());
      }
      return QueryExpression.create(pair.getKey()).gte(pair.getValue());
    case LESS_THAN:
      if (swapped) {
        return QueryExpression.create(pair.getKey()).gt(pair.getValue());
      }
      return QueryExpression.create(pair.getKey()).lt(pair.getValue());
    case LESS_THAN_OR_EQUAL:
      if (swapped) {
        return QueryExpression.create(pair.getKey()).gte(pair.getValue());
      }
      return QueryExpression.create(pair.getKey()).lte(pair.getValue());
    default:
      break;
  }
  throw new PredicateAnalyzerException(format("Unable to handle call: [%s]", call));
}
 
Example 5
Source File: PredicateAnalyzer.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Process a call which is a binary operation, transforming into an equivalent
 * query expression. Note that the incoming call may be either a simple binary
 * expression, such as {@code foo > 5}, or it may be several simple expressions connected
 * by {@code AND} or {@code OR} operators, such as {@code foo > 5 AND bar = 'abc' AND 'rot' < 1}
 *
 * @param call existing call
 * @return evaluated expression
 */
private QueryExpression binary(RexCall call) {

  // if AND/OR, do special handling
  if (call.getKind() == SqlKind.AND || call.getKind() == SqlKind.OR) {
    return andOr(call);
  }

  checkForIncompatibleDateTimeOperands(call);

  Preconditions.checkState(call.getOperands().size() == 2);
  final Expression a = call.getOperands().get(0).accept(this);
  final Expression b = call.getOperands().get(1).accept(this);

  final SwapResult pair = swap(a, b);
  final boolean swapped = pair.isSwapped();

  // For _id and _index columns, only equals/not_equals work!
  if (isColumn(pair.getKey(), call, ElasticsearchConstants.ID, false)
      || isColumn(pair.getKey(), call, ElasticsearchConstants.INDEX, false)
      || isColumn(pair.getKey(), call, ElasticsearchConstants.UID, false)) {
    switch (call.getKind()) {
    case EQUALS:
    case NOT_EQUALS:
      break;
    default:
      throw new PredicateAnalyzerException(
          "Cannot handle " + call.getKind() + " expression for _id field, " + call);
    }
  }

  switch (call.getKind()) {
  case CONTAINS:
    return QueryExpression.create(pair.getKey()).contains(pair.getValue());
  case LIKE:
    throw new UnsupportedOperationException("LIKE not yet supported");
  case EQUALS:
    return QueryExpression.create(pair.getKey()).equals(pair.getValue());
  case NOT_EQUALS:
    return QueryExpression.create(pair.getKey()).notEquals(pair.getValue());
  case GREATER_THAN:
    if (swapped) {
      return QueryExpression.create(pair.getKey()).lt(pair.getValue());
    }
    return QueryExpression.create(pair.getKey()).gt(pair.getValue());
  case GREATER_THAN_OR_EQUAL:
    if (swapped) {
      return QueryExpression.create(pair.getKey()).lte(pair.getValue());
    }
    return QueryExpression.create(pair.getKey()).gte(pair.getValue());
  case LESS_THAN:
    if (swapped) {
      return QueryExpression.create(pair.getKey()).gt(pair.getValue());
    }
    return QueryExpression.create(pair.getKey()).lt(pair.getValue());
  case LESS_THAN_OR_EQUAL:
    if (swapped) {
      return QueryExpression.create(pair.getKey()).gte(pair.getValue());
    }
    return QueryExpression.create(pair.getKey()).lte(pair.getValue());
  default:
    break;
  }
  String message = String.format(Locale.ROOT, "Unable to handle call: [%s]", call);
  throw new PredicateAnalyzerException(message);
}
 
Example 6
Source File: RexUtil.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Returns whether an operator is associative. AND is associative,
 * which means that "(x AND y) and z" is equivalent to "x AND (y AND z)".
 * We might well flatten the tree, and write "AND(x, y, z)".
 */
private static boolean isAssociative(SqlOperator op) {
    return op.getKind() == SqlKind.AND || op.getKind() == SqlKind.OR;
}
 
Example 7
Source File: RexUtil.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns whether an operator is associative. AND is associative,
 * which means that "(x AND y) and z" is equivalent to "x AND (y AND z)".
 * We might well flatten the tree, and write "AND(x, y, z)".
 */
private static boolean isAssociative(SqlOperator op) {
  return op.getKind() == SqlKind.AND
      || op.getKind() == SqlKind.OR;
}