Java Code Examples for org.apache.calcite.sql.SqlOperator#getKind()

The following examples show how to use org.apache.calcite.sql.SqlOperator#getKind() . 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: TypeCoercionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Coerces operands in binary arithmetic expressions to NUMERIC types.
 *
 * <p>For binary arithmetic operators like [+, -, *, /, %]:
 * If the operand is VARCHAR,
 * coerce it to data type of the other operand if its data type is NUMERIC;
 * If the other operand is DECIMAL,
 * coerce the STRING operand to max precision/scale DECIMAL.
 */
public boolean binaryArithmeticCoercion(SqlCallBinding binding) {
  // Assume the operator has NUMERIC family operand type checker.
  SqlOperator operator = binding.getOperator();
  SqlKind kind = operator.getKind();
  boolean coerced = false;
  // Binary operator
  if (binding.getOperandCount() == 2) {
    final RelDataType type1 = binding.getOperandType(0);
    final RelDataType type2 = binding.getOperandType(1);
    // Special case for datetime + interval or datetime - interval
    if (kind == SqlKind.PLUS || kind == SqlKind.MINUS) {
      if (SqlTypeUtil.isInterval(type1) || SqlTypeUtil.isInterval(type2)) {
        return false;
      }
    }
    // Binary arithmetic operator like: + - * / %
    if (kind.belongsTo(SqlKind.BINARY_ARITHMETIC)) {
      coerced = binaryArithmeticWithStrings(binding, type1, type2);
    }
  }
  return coerced;
}
 
Example 2
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RexNode flattenComparison(RexBuilder rexBuilder, SqlOperator op, List<RexNode> exprs) {
    final List<Pair<RexNode, String>> flattenedExps = new ArrayList<>();
    flattenProjections(this, exprs, null, "", flattenedExps);
    int n = flattenedExps.size() / 2;
    boolean negate = false;
    if (op.getKind() == SqlKind.NOT_EQUALS) {
        negate = true;
        op = SqlStdOperatorTable.EQUALS;
    }
    if ((n > 1) && op.getKind() != SqlKind.EQUALS) {
        throw Util.needToImplement("inequality comparison for row types");
    }
    RexNode conjunction = null;
    for (int i = 0; i < n; ++i) {
        RexNode comparison = rexBuilder.makeCall(op, flattenedExps.get(i).left, flattenedExps.get(i + n).left);
        if (conjunction == null) {
            conjunction = comparison;
        } else {
            conjunction = rexBuilder.makeCall(SqlStdOperatorTable.AND, conjunction, comparison);
        }
    }
    if (negate) {
        return rexBuilder.makeCall(SqlStdOperatorTable.NOT, conjunction);
    } else {
        return conjunction;
    }
}
 
Example 3
Source File: JethroDataSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public boolean supportsFunction(SqlOperator operator,
    RelDataType type, List<RelDataType> paramTypes) {
  switch (operator.getKind()) {
  case IS_NOT_NULL:
  case IS_NULL:
  case AND:
  case OR:
  case NOT:
  case BETWEEN:
  case CASE:
  case CAST:
    return true;
  }
  final Set<JethroSupportedFunction> functions =
      info.supportedFunctions.get(operator.getName());

  if (functions != null) {
    for (JethroSupportedFunction f : functions) {
      if (f.argumentsMatch(paramTypes)) {
        return true;
      }
    }
  }
  LOGGER.debug("Unsupported function in jethro: " + operator + " with params "
      + paramTypes);
  return false;
}
 
Example 4
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 5
Source File: DrillCalciteSqlOperatorWrapper.java    From Bats with Apache License 2.0 5 votes vote down vote up
public DrillCalciteSqlOperatorWrapper(SqlOperator operator, final String rename, final List<DrillFuncHolder> functions) {
  super(
      operator.getName(),
      operator.getKind(),
      operator.getLeftPrec(),
      operator.getRightPrec(),
      TypeInferenceUtils.getDrillSqlReturnTypeInference(
          rename,
          functions),
      operator.getOperandTypeInference(),
      Checker.ANY_CHECKER);
  this.operator = operator;
}
 
Example 6
Source File: TupleExpressionVisitor.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public TupleExpression visitCall(RexCall call) {
    SqlOperator op = call.getOperator();
    if (op instanceof SqlCastFunction) {
        return call.getOperands().get(0).accept(this);
    } else if (op instanceof SqlUserDefinedFunction) {
        if (op.getName().equals("QUARTER")) {
            return visitFirstRexInputRef(call);
        }
    }

    TupleExpression tupleExpression;
    switch (op.getKind()) {
    case PLUS:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.PLUS);
        break;
    case MINUS:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.MINUS);
        break;
    case TIMES:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.MULTIPLE);
        break;
    case DIVIDE:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.DIVIDE);
        break;
    case CASE:
        tupleExpression = getCaseTupleExpression(call);
        break;
    default:
        tupleExpression = getRexCallTupleExpression(call);
    }
    if (ifVerify) {
        tupleExpression.verify();
    }
    return tupleExpression;
}
 
Example 7
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 8
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public TupleExpression visitCall(RexCall call) {
    SqlOperator op = call.getOperator();
    if (op instanceof SqlCastFunction) {
        return call.getOperands().get(0).accept(this);
    } else if (op instanceof SqlUserDefinedFunction) {
        if (op.getName().equals("QUARTER")) {
            return visitFirstRexInputRef(call);
        }
    }

    TupleExpression tupleExpression;
    switch (op.getKind()) {
    case PLUS:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.PLUS);
        break;
    case MINUS:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.MINUS);
        break;
    case TIMES:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.MULTIPLE);
        break;
    case DIVIDE:
        tupleExpression = getBinaryTupleExpression(call, TupleExpression.ExpressionOperatorEnum.DIVIDE);
        break;
    case CASE:
        tupleExpression = getCaseTupleExpression(call);
        break;
    default:
        tupleExpression = getRexCallTupleExpression(call);
    }
    if (ifVerify) {
        tupleExpression.verify();
    }
    return tupleExpression;
}
 
Example 9
Source File: SplunkPushDownRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private boolean getFilter(SqlOperator op, List<RexNode> operands,
    StringBuilder s, List<String> fieldNames) {
  if (!valid(op.getKind())) {
    return false;
  }

  boolean like = false;
  switch (op.getKind()) {
  case NOT:
    // NOT op pre-pended
    s = s.append(" NOT ");
    break;
  case CAST:
    return asd(false, operands, s, fieldNames, 0);
  case LIKE:
    like = true;
    break;
  }

  for (int i = 0; i < operands.size(); i++) {
    if (!asd(like, operands, s, fieldNames, i)) {
      return false;
    }
    if (op instanceof SqlBinaryOperator && i == 0) {
      s.append(" ").append(op).append(" ");
    }
  }
  return true;
}
 
Example 10
Source File: JethroDataSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean supportsFunction(SqlOperator operator,
    RelDataType type, List<RelDataType> paramTypes) {
  switch (operator.getKind()) {
  case IS_NOT_NULL:
  case IS_NULL:
  case AND:
  case OR:
  case NOT:
  case BETWEEN:
  case CASE:
  case CAST:
    return true;
  }
  final Set<JethroSupportedFunction> functions =
      info.supportedFunctions.get(operator.getName());

  if (functions != null) {
    for (JethroSupportedFunction f : functions) {
      if (f.argumentsMatch(paramTypes)) {
        return true;
      }
    }
  }
  LOGGER.debug("Unsupported function in jethro: " + operator + " with params "
      + paramTypes);
  return false;
}
 
Example 11
Source File: SqlStdOperatorTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the binary operator that corresponds to this operator but in the opposite
 * direction. Or returns this, if its kind is not reversible.
 *
 * <p>For example, {@code reverse(GREATER_THAN)} returns {@link #LESS_THAN}.
 */
public static SqlOperator reverse(SqlOperator operator) {
  switch (operator.getKind()) {
  case GREATER_THAN:
    return LESS_THAN;
  case GREATER_THAN_OR_EQUAL:
    return LESS_THAN_OR_EQUAL;
  case LESS_THAN:
    return GREATER_THAN;
  case LESS_THAN_OR_EQUAL:
    return GREATER_THAN_OR_EQUAL;
  default:
    return operator;
  }
}
 
Example 12
Source File: EqualityConvertlet.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static RexNode convertEquality(SqlRexContext cx, SqlCall call) {

    final List<SqlNode> operands = call.getOperandList();
    final RexBuilder rexBuilder = cx.getRexBuilder();
    final List<RexNode> exprs =
      convertExpressionList(cx, operands);
    SqlOperator op = call.getOperator();
    RelDataType type = rexBuilder.deriveReturnType(op, exprs);
    RexCall convertedCall = (RexCall) rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));

    // The following is copied from Calcite's StdConvertletTable.convertEqualsOrNotEquals()
    final RexNode op0 = convertedCall.getOperands().get(0);
    final RexNode op1 = convertedCall.getOperands().get(1);
    final RexNode booleanOp;
    final RexNode integerOp;

    if (op0.getType().getSqlTypeName() == SqlTypeName.BOOLEAN
      && SqlTypeName.INT_TYPES.contains(op1.getType().getSqlTypeName())) {
      booleanOp = op0;
      integerOp = op1;
    } else if (SqlTypeName.INT_TYPES.contains(op0.getType().getSqlTypeName())
      && op1.getType().getSqlTypeName() == SqlTypeName.BOOLEAN) {
      booleanOp = op1;
      integerOp = op0;
    } else {
      return convertedCall;
    }

    SqlOperator newOp = (op.getKind() == SqlKind.EQUALS || op.getKind() == SqlKind.NOT_EQUALS) ?
      SqlStdOperatorTable.EQUALS :
      SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;

    return rexBuilder.makeCall(call.getOperator(),
      booleanOp,
      rexBuilder.makeCall(
        SqlStdOperatorTable.CASE,
        rexBuilder.makeCall(
          newOp,
          integerOp,
          rexBuilder.makeZeroLiteral(integerOp.getType())),
        rexBuilder.makeLiteral(false),
        rexBuilder.makeLiteral(true)));
  }
 
Example 13
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RexNode flattenComparison(
    RexBuilder rexBuilder,
    SqlOperator op,
    List<RexNode> exprs) {
  final List<Pair<RexNode, String>> flattenedExps = new ArrayList<>();
  flattenProjections(this, exprs, null, "", flattenedExps);
  int n = flattenedExps.size() / 2;
  boolean negate = false;
  if (op.getKind() == SqlKind.NOT_EQUALS) {
    negate = true;
    op = SqlStdOperatorTable.EQUALS;
  }
  if ((n > 1) && op.getKind() != SqlKind.EQUALS) {
    throw Util.needToImplement(
        "inequality comparison for row types");
  }
  RexNode conjunction = null;
  for (int i = 0; i < n; ++i) {
    RexNode comparison =
        rexBuilder.makeCall(
            op,
            flattenedExps.get(i).left,
            flattenedExps.get(i + n).left);
    if (conjunction == null) {
      conjunction = comparison;
    } else {
      conjunction =
          rexBuilder.makeCall(
              SqlStdOperatorTable.AND,
              conjunction,
              comparison);
    }
  }
  if (negate) {
    return rexBuilder.makeCall(
        SqlStdOperatorTable.NOT,
        conjunction);
  } else {
    return conjunction;
  }
}
 
Example 14
Source File: RexCall.java    From calcite with Apache License 2.0 4 votes vote down vote up
private Pair<SqlOperator, ImmutableList<RexNode>> normalize(
    SqlOperator operator,
    List<? extends RexNode> operands) {
  final ImmutableList<RexNode> oldOperands = ImmutableList.copyOf(operands);
  final Pair<SqlOperator, ImmutableList<RexNode>> original = Pair.of(operator, oldOperands);
  if (!needNormalize()) {
    return original;
  }
  if (operands.size() != 2
      || !operands.stream().allMatch(operand -> operand.getClass() == RexInputRef.class)) {
    return original;
  }
  final RexInputRef operand0 = (RexInputRef) operands.get(0);
  final RexInputRef operand1 = (RexInputRef) operands.get(1);
  final SqlKind kind = operator.getKind();
  if (operand0.getIndex() < operand1.getIndex()) {
    return original;
  }
  // If arguments are the same, then we normalize < vs >
  // '<' == 60, '>' == 62, so we prefer <.
  if (operand0.getIndex() == operand1.getIndex()) {
    if (kind.reverse().compareTo(kind) < 0) {
      return Pair.of(SqlStdOperatorTable.reverse(operator), oldOperands);
    } else {
      return original;
    }
  }

  if (SqlKind.SYMMETRICAL_SAME_ARG_TYPE.contains(kind)) {
    final RelDataType firstType = operands.get(0).getType();
    for (int i = 1; i < operands.size(); i++) {
      if (!equalSansNullability(firstType, operands.get(i).getType())) {
        // Arguments have different type, thus they must not be sorted
        return original;
      }
    }
    // fall through: order arguments below
  } else if (!SqlKind.SYMMETRICAL.contains(kind)
      && kind == kind.reverse()) {
    // The operations have to be either symmetrical or reversible
    // Nothing matched => we skip argument sorting
    return original;
  }
  // $0=$1 is the same as $1=$0, so we make sure the digest is the same for them.

  // When $1 > $0 is normalized, the operation needs to be flipped
  // So we sort arguments first, then flip the sign.
  final SqlOperator newOp = SqlStdOperatorTable.reverse(operator);
  final ImmutableList<RexNode> newOperands = ImmutableList.of(operand1, operand0);
  return Pair.of(newOp, newOperands);
}
 
Example 15
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 16
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;
}