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

The following examples show how to use org.apache.calcite.sql.SqlKind#CASE . 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: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RexCall doCollapseExpandedIsNotDistinctFromCaseExpr(final RexCall call,
    final RexBuilder rexBuilder) {
  if (call.getKind() != SqlKind.CASE || call.getOperands().size() != 5) {
    return call;
  }

  final RexNode op0 = call.getOperands().get(0);
  final RexNode op1 = call.getOperands().get(1);
  final RexNode op2 = call.getOperands().get(2);
  final RexNode op3 = call.getOperands().get(3);
  final RexNode op4 = call.getOperands().get(4);

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

  RexCall ifCall = (RexCall) op0;
  RexCall thenCall = (RexCall) op1;
  RexCall elseIfCall = (RexCall) op2;
  RexCall elseIfThenCall = (RexCall) op3;
  RexCall elseCall = (RexCall) op4;

  if (ifCall.getKind() != SqlKind.IS_NULL
      || thenCall.getKind() != SqlKind.IS_NULL
      || elseIfCall.getKind() != SqlKind.IS_NULL
      || elseIfThenCall.getKind() != SqlKind.IS_NULL
      || elseCall.getKind() != SqlKind.EQUALS) {
    return call;
  }

  if (!ifCall.equals(elseIfThenCall)
      || !thenCall.equals(elseIfCall)) {
    return call;
  }

  return doCollapseExpandedIsNotDistinctFrom(rexBuilder, call, ifCall, elseIfCall, elseCall);
}
 
Example 2
Source File: SqlCaseOperator.java    From Bats with Apache License 2.0 4 votes vote down vote up
private SqlCaseOperator() {
  super("CASE", SqlKind.CASE, MDX_PRECEDENCE, true, null,
      InferTypes.RETURN_TYPE, null);
}
 
Example 3
Source File: SqlCase.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public SqlKind getKind() {
  return SqlKind.CASE;
}
 
Example 4
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Pushes predicates into a CASE.
 *
 * <p>We have a loose definition of 'predicate': any boolean expression will
 * do, except CASE. For example '(CASE ...) = 5' or '(CASE ...) IS NULL'.
 */
public static RexCall pushPredicateIntoCase(RexCall call) {
  if (call.getType().getSqlTypeName() != SqlTypeName.BOOLEAN) {
    return call;
  }
  switch (call.getKind()) {
  case CASE:
  case AND:
  case OR:
    return call; // don't push CASE into CASE!
  case EQUALS: {
    // checks that the EQUALS operands may be splitted and
    // doesn't push EQUALS into CASE
    List<RexNode> equalsOperands = call.getOperands();
    ImmutableBitSet left = RelOptUtil.InputFinder.bits(equalsOperands.get(0));
    ImmutableBitSet right = RelOptUtil.InputFinder.bits(equalsOperands.get(1));
    if (!left.isEmpty() && !right.isEmpty() && left.intersect(right).isEmpty()) {
      return call;
    }
  }
  }
  int caseOrdinal = -1;
  final List<RexNode> operands = call.getOperands();
  for (int i = 0; i < operands.size(); i++) {
    RexNode operand = operands.get(i);
    if (operand.getKind() == SqlKind.CASE) {
      caseOrdinal = i;
    }
  }
  if (caseOrdinal < 0) {
    return call;
  }
  // Convert
  //   f(CASE WHEN p1 THEN v1 ... END, arg)
  // to
  //   CASE WHEN p1 THEN f(v1, arg) ... END
  final RexCall case_ = (RexCall) operands.get(caseOrdinal);
  final List<RexNode> nodes = new ArrayList<>();
  for (int i = 0; i < case_.getOperands().size(); i++) {
    RexNode node = case_.getOperands().get(i);
    if (!RexUtil.isCasePredicate(case_, i)) {
      node = substitute(call, caseOrdinal, node);
    }
    nodes.add(node);
  }
  return case_.clone(call.getType(), nodes);
}
 
Example 5
Source File: AggregateCaseToFilterRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static boolean isThreeArgCase(final RexNode rexNode) {
  return rexNode.getKind() == SqlKind.CASE
      && ((RexCall) rexNode).operands.size() == 3;
}
 
Example 6
Source File: SqlCaseOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
private SqlCaseOperator() {
  super("CASE", SqlKind.CASE, MDX_PRECEDENCE, true, null,
      InferTypes.RETURN_TYPE, null);
}
 
Example 7
Source File: SqlCase.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlKind getKind() {
  return SqlKind.CASE;
}