Java Code Examples for org.apache.calcite.sql.validate.SqlValidator#newValidationError()

The following examples show how to use org.apache.calcite.sql.validate.SqlValidator#newValidationError() . 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: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void validateCall(
    SqlCall call,
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlValidatorScope operandScope) {
  // per the SQL std, each string fragment must be on a different line
  final List<SqlNode> operandList = call.getOperandList();
  for (int i = 1; i < operandList.size(); i++) {
    SqlParserPos prevPos = operandList.get(i - 1).getParserPosition();
    final SqlNode operand = operandList.get(i);
    SqlParserPos pos = operand.getParserPosition();
    if (pos.getLineNum() <= prevPos.getLineNum()) {
      throw validator.newValidationError(operand,
          RESOURCE.stringFragsOnSameLine());
    }
  }
}
 
Example 2
Source File: SqlDataTypeSpec.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Throws an error if the type is not found.
 */
public RelDataType deriveType(SqlValidator validator) {
  RelDataType type = null;
  if (typeName.isSimple()) {
    if (null != collectionsTypeName) {
      final String collectionName = collectionsTypeName.getSimple();
      if (SqlTypeName.get(collectionName) == null) {
        throw validator.newValidationError(this,
            RESOURCE.unknownDatatypeName(collectionName));
      }
    }

    RelDataTypeFactory typeFactory = validator.getTypeFactory();
    type = deriveType(typeFactory);
  }
  if (type == null) {
    type = validator.getValidatedNodeType(typeName);
  }
  return type;
}
 
Example 3
Source File: SqlOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected List<SqlNode> constructOperandList(
    SqlValidator validator,
    SqlCall call,
    List<String> argNames) {
  if (argNames == null) {
    return call.getOperandList();
  }
  if (argNames.size() < call.getOperandList().size()) {
    throw validator.newValidationError(call,
        RESOURCE.someButNotAllArgumentsAreNamed());
  }
  final int duplicate = Util.firstDuplicate(argNames);
  if (duplicate >= 0) {
    throw validator.newValidationError(call,
        RESOURCE.duplicateArgumentName(argNames.get(duplicate)));
  }
  final ImmutableList.Builder<SqlNode> argBuilder = ImmutableList.builder();
  for (SqlNode operand : call.getOperandList()) {
    if (operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
      final List<SqlNode> operandList = ((SqlCall) operand).getOperandList();
      argBuilder.add(operandList.get(0));
    }
  }
  return argBuilder.build();
}
 
Example 4
Source File: SqlOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected List<SqlNode> constructOperandList(
    SqlValidator validator,
    SqlCall call,
    List<String> argNames) {
  if (argNames == null) {
    return call.getOperandList();
  }
  if (argNames.size() < call.getOperandList().size()) {
    throw validator.newValidationError(call,
        RESOURCE.someButNotAllArgumentsAreNamed());
  }
  final int duplicate = Util.firstDuplicate(argNames);
  if (duplicate >= 0) {
    throw validator.newValidationError(call,
        RESOURCE.duplicateArgumentName(argNames.get(duplicate)));
  }
  final ImmutableList.Builder<SqlNode> argBuilder = ImmutableList.builder();
  for (SqlNode operand : call.getOperandList()) {
    if (operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
      final List<SqlNode> operandList = ((SqlCall) operand).getOperandList();
      argBuilder.add(operandList.get(0));
    }
  }
  return argBuilder.build();
}
 
Example 5
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void validateCall(
    SqlCall call,
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlValidatorScope operandScope) {
  // per the SQL std, each string fragment must be on a different line
  final List<SqlNode> operandList = call.getOperandList();
  for (int i = 1; i < operandList.size(); i++) {
    SqlParserPos prevPos = operandList.get(i - 1).getParserPosition();
    final SqlNode operand = operandList.get(i);
    SqlParserPos pos = operand.getParserPosition();
    if (pos.getLineNum() <= prevPos.getLineNum()) {
      throw validator.newValidationError(operand,
          RESOURCE.stringFragsOnSameLine());
    }
  }
}
 
Example 6
Source File: SqlWindow.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static boolean setOperand(SqlNode clonedOperand, SqlNode thatOperand,
    SqlValidator validator) {
  if ((thatOperand != null) && !SqlNodeList.isEmptyList(thatOperand)) {
    if ((clonedOperand == null)
        || SqlNodeList.isEmptyList(clonedOperand)) {
      return true;
    } else {
      throw validator.newValidationError(clonedOperand,
          RESOURCE.cannotOverrideWindowAttribute());
    }
  }
  return false;
}
 
Example 7
Source File: SqlJdbcFunctionCall.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode rewriteCall(SqlValidator validator,
    SqlCall call) {
  if (null == lookupMakeCallObj) {
    throw validator.newValidationError(call,
        RESOURCE.functionUndefined(getName()));
  }
  return lookupMakeCallObj.getOperator().rewriteCall(validator, call);
}
 
Example 8
Source File: SqlOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void checkOperandCount(
    SqlValidator validator,
    SqlOperandTypeChecker argType,
    SqlCall call) {
  SqlOperandCountRange od = call.getOperator().getOperandCountRange();
  if (od.isValidCount(call.operandCount())) {
    return;
  }
  if (od.getMin() == od.getMax()) {
    throw validator.newValidationError(call,
        RESOURCE.invalidArgCount(call.getOperator().getName(), od.getMin()));
  } else {
    throw validator.newValidationError(call, RESOURCE.wrongNumOfArguments());
  }
}
 
Example 9
Source File: SqlFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Throws a validation error if a DISTINCT or ALL quantifier is present but
 * not allowed.
 */
protected void validateQuantifier(SqlValidator validator, SqlCall call) {
  if ((null != call.getFunctionQuantifier()) && !isQuantifierAllowed()) {
    throw validator.newValidationError(call.getFunctionQuantifier(),
        RESOURCE.functionQuantifierNotAllowed(call.getOperator().getName()));
  }
}
 
Example 10
Source File: SqlJdbcFunctionCall.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode rewriteCall(SqlValidator validator,
    SqlCall call) {
  if (null == lookupMakeCallObj) {
    throw validator.newValidationError(call,
        RESOURCE.functionUndefined(getName()));
  }
  return lookupMakeCallObj.getOperator().rewriteCall(validator, call);
}
 
Example 11
Source File: SqlWindow.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static boolean setOperand(SqlNode clonedOperand, SqlNode thatOperand,
    SqlValidator validator) {
  if ((thatOperand != null) && !SqlNodeList.isEmptyList(thatOperand)) {
    if ((clonedOperand == null)
        || SqlNodeList.isEmptyList(clonedOperand)) {
      return true;
    } else {
      throw validator.newValidationError(clonedOperand,
          RESOURCE.cannotOverrideWindowAttribute());
    }
  }
  return false;
}
 
Example 12
Source File: SqlOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected void checkOperandCount(
    SqlValidator validator,
    SqlOperandTypeChecker argType,
    SqlCall call) {
  SqlOperandCountRange od = call.getOperator().getOperandCountRange();
  if (od.isValidCount(call.operandCount())) {
    return;
  }
  if (od.getMin() == od.getMax()) {
    throw validator.newValidationError(call,
        RESOURCE.invalidArgCount(call.getOperator().getName(), od.getMin()));
  } else {
    throw validator.newValidationError(call, RESOURCE.wrongNumOfArguments());
  }
}
 
Example 13
Source File: SqlFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Throws a validation error if a DISTINCT or ALL quantifier is present but
 * not allowed.
 */
protected void validateQuantifier(SqlValidator validator, SqlCall call) {
  if ((null != call.getFunctionQuantifier()) && !isQuantifierAllowed()) {
    throw validator.newValidationError(call.getFunctionQuantifier(),
        RESOURCE.functionQuantifierNotAllowed(call.getOperator().getName()));
  }
}
 
Example 14
Source File: SqlWindow.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new window by combining this one with another.
 *
 * <p>For example,
 *
 * <blockquote><pre>WINDOW (w PARTITION BY x ORDER BY y)
 *   overlay
 *   WINDOW w AS (PARTITION BY z)</pre></blockquote>
 *
 * <p>yields
 *
 * <blockquote><pre>WINDOW (PARTITION BY z ORDER BY y)</pre></blockquote>
 *
 * <p>Does not alter this or the other window.
 *
 * @return A new window
 */
public SqlWindow overlay(SqlWindow that, SqlValidator validator) {
  // check 7.11 rule 10c
  final SqlNodeList partitions = getPartitionList();
  if (0 != partitions.size()) {
    throw validator.newValidationError(partitions.get(0),
        RESOURCE.partitionNotAllowed());
  }

  // 7.11 rule 10d
  final SqlNodeList baseOrder = getOrderList();
  final SqlNodeList refOrder = that.getOrderList();
  if ((0 != baseOrder.size()) && (0 != refOrder.size())) {
    throw validator.newValidationError(baseOrder.get(0),
        RESOURCE.orderByOverlap());
  }

  // 711 rule 10e
  final SqlNode lowerBound = that.getLowerBound();
  final SqlNode upperBound = that.getUpperBound();
  if ((null != lowerBound) || (null != upperBound)) {
    throw validator.newValidationError(that.isRows,
        RESOURCE.refWindowWithFrame());
  }

  SqlIdentifier declNameNew = declName;
  SqlIdentifier refNameNew = refName;
  SqlNodeList partitionListNew = partitionList;
  SqlNodeList orderListNew = orderList;
  SqlLiteral isRowsNew = isRows;
  SqlNode lowerBoundNew = lowerBound;
  SqlNode upperBoundNew = upperBound;
  SqlLiteral allowPartialNew = allowPartial;

  // Clear the reference window, because the reference is now resolved.
  // The overlaying window may have its own reference, of course.
  refNameNew = null;

  // Overlay other parameters.
  if (setOperand(partitionListNew, that.partitionList, validator)) {
    partitionListNew = that.partitionList;
  }
  if (setOperand(orderListNew, that.orderList, validator)) {
    orderListNew = that.orderList;
  }
  if (setOperand(lowerBoundNew, that.lowerBound, validator)) {
    lowerBoundNew = that.lowerBound;
  }
  if (setOperand(upperBoundNew, that.upperBound, validator)) {
    upperBoundNew = that.upperBound;
  }
  return new SqlWindow(
      SqlParserPos.ZERO,
      declNameNew,
      refNameNew,
      partitionListNew,
      orderListNew,
      isRowsNew,
      lowerBoundNew,
      upperBoundNew,
      allowPartialNew);
}
 
Example 15
Source File: SqlWindow.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** @see Util#deprecated(Object, boolean) */
static void checkSpecialLiterals(SqlWindow window, SqlValidator validator) {
  final SqlNode lowerBound = window.getLowerBound();
  final SqlNode upperBound = window.getUpperBound();
  Object lowerLitType = null;
  Object upperLitType = null;
  SqlOperator lowerOp = null;
  SqlOperator upperOp = null;
  if (null != lowerBound) {
    if (lowerBound.getKind() == SqlKind.LITERAL) {
      lowerLitType = ((SqlLiteral) lowerBound).getValue();
      if (Bound.UNBOUNDED_FOLLOWING == lowerLitType) {
        throw validator.newValidationError(lowerBound,
            RESOURCE.badLowerBoundary());
      }
    } else if (lowerBound instanceof SqlCall) {
      lowerOp = ((SqlCall) lowerBound).getOperator();
    }
  }
  if (null != upperBound) {
    if (upperBound.getKind() == SqlKind.LITERAL) {
      upperLitType = ((SqlLiteral) upperBound).getValue();
      if (Bound.UNBOUNDED_PRECEDING == upperLitType) {
        throw validator.newValidationError(upperBound,
            RESOURCE.badUpperBoundary());
      }
    } else if (upperBound instanceof SqlCall) {
      upperOp = ((SqlCall) upperBound).getOperator();
    }
  }

  if (Bound.CURRENT_ROW == lowerLitType) {
    if (null != upperOp) {
      if (upperOp == PRECEDING_OPERATOR) {
        throw validator.newValidationError(upperBound,
            RESOURCE.currentRowPrecedingError());
      }
    }
  } else if (null != lowerOp) {
    if (lowerOp == FOLLOWING_OPERATOR) {
      if (null != upperOp) {
        if (upperOp == PRECEDING_OPERATOR) {
          throw validator.newValidationError(upperBound,
              RESOURCE.followingBeforePrecedingError());
        }
      } else if (null != upperLitType) {
        if (Bound.CURRENT_ROW == upperLitType) {
          throw validator.newValidationError(upperBound,
              RESOURCE.currentRowFollowingError());
        }
      }
    }
  }
}
 
Example 16
Source File: SqlWindow.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** @see Util#deprecated(Object, boolean) */
static void checkSpecialLiterals(SqlWindow window, SqlValidator validator) {
  final SqlNode lowerBound = window.getLowerBound();
  final SqlNode upperBound = window.getUpperBound();
  Object lowerLitType = null;
  Object upperLitType = null;
  SqlOperator lowerOp = null;
  SqlOperator upperOp = null;
  if (null != lowerBound) {
    if (lowerBound.getKind() == SqlKind.LITERAL) {
      lowerLitType = ((SqlLiteral) lowerBound).getValue();
      if (Bound.UNBOUNDED_FOLLOWING == lowerLitType) {
        throw validator.newValidationError(lowerBound,
            RESOURCE.badLowerBoundary());
      }
    } else if (lowerBound instanceof SqlCall) {
      lowerOp = ((SqlCall) lowerBound).getOperator();
    }
  }
  if (null != upperBound) {
    if (upperBound.getKind() == SqlKind.LITERAL) {
      upperLitType = ((SqlLiteral) upperBound).getValue();
      if (Bound.UNBOUNDED_PRECEDING == upperLitType) {
        throw validator.newValidationError(upperBound,
            RESOURCE.badUpperBoundary());
      }
    } else if (upperBound instanceof SqlCall) {
      upperOp = ((SqlCall) upperBound).getOperator();
    }
  }

  if (Bound.CURRENT_ROW == lowerLitType) {
    if (null != upperOp) {
      if (upperOp == PRECEDING_OPERATOR) {
        throw validator.newValidationError(upperBound,
            RESOURCE.currentRowPrecedingError());
      }
    }
  } else if (null != lowerOp) {
    if (lowerOp == FOLLOWING_OPERATOR) {
      if (null != upperOp) {
        if (upperOp == PRECEDING_OPERATOR) {
          throw validator.newValidationError(upperBound,
              RESOURCE.followingBeforePrecedingError());
        }
      } else if (null != upperLitType) {
        if (Bound.CURRENT_ROW == upperLitType) {
          throw validator.newValidationError(upperBound,
              RESOURCE.currentRowFollowingError());
        }
      }
    }
  }
}
 
Example 17
Source File: SqlWindow.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new window by combining this one with another.
 *
 * <p>For example,
 *
 * <blockquote><pre>WINDOW (w PARTITION BY x ORDER BY y)
 *   overlay
 *   WINDOW w AS (PARTITION BY z)</pre></blockquote>
 *
 * <p>yields
 *
 * <blockquote><pre>WINDOW (PARTITION BY z ORDER BY y)</pre></blockquote>
 *
 * <p>Does not alter this or the other window.
 *
 * @return A new window
 */
public SqlWindow overlay(SqlWindow that, SqlValidator validator) {
  // check 7.11 rule 10c
  final SqlNodeList partitions = getPartitionList();
  if (0 != partitions.size()) {
    throw validator.newValidationError(partitions.get(0),
        RESOURCE.partitionNotAllowed());
  }

  // 7.11 rule 10d
  final SqlNodeList baseOrder = getOrderList();
  final SqlNodeList refOrder = that.getOrderList();
  if ((0 != baseOrder.size()) && (0 != refOrder.size())) {
    throw validator.newValidationError(baseOrder.get(0),
        RESOURCE.orderByOverlap());
  }

  // 711 rule 10e
  final SqlNode lowerBound = that.getLowerBound();
  final SqlNode upperBound = that.getUpperBound();
  if ((null != lowerBound) || (null != upperBound)) {
    throw validator.newValidationError(that.isRows,
        RESOURCE.refWindowWithFrame());
  }

  SqlIdentifier declNameNew = declName;
  SqlIdentifier refNameNew = refName;
  SqlNodeList partitionListNew = partitionList;
  SqlNodeList orderListNew = orderList;
  SqlLiteral isRowsNew = isRows;
  SqlNode lowerBoundNew = lowerBound;
  SqlNode upperBoundNew = upperBound;
  SqlLiteral allowPartialNew = allowPartial;

  // Clear the reference window, because the reference is now resolved.
  // The overlaying window may have its own reference, of course.
  refNameNew = null;

  // Overlay other parameters.
  if (setOperand(partitionListNew, that.partitionList, validator)) {
    partitionListNew = that.partitionList;
  }
  if (setOperand(orderListNew, that.orderList, validator)) {
    orderListNew = that.orderList;
  }
  if (setOperand(lowerBoundNew, that.lowerBound, validator)) {
    lowerBoundNew = that.lowerBound;
  }
  if (setOperand(upperBoundNew, that.upperBound, validator)) {
    upperBoundNew = that.upperBound;
  }
  return new SqlWindow(
      SqlParserPos.ZERO,
      declNameNew,
      refNameNew,
      partitionListNew,
      orderListNew,
      isRowsNew,
      lowerBoundNew,
      upperBoundNew,
      allowPartialNew);
}