org.apache.calcite.runtime.CalciteContextException Java Examples

The following examples show how to use org.apache.calcite.runtime.CalciteContextException. 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: CollectionTypeTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testInvalidAccessUseStringForIndexOnArrayWithAnyType() throws Exception {
  Connection connection = setupConnectionWithNestedTable();

  final Statement statement = connection.createStatement();

  try {
    final String sql = "select \"ID\", \"MAPFIELD\","
        + " \"NESTEDMAPFIELD\", \"ARRAYFIELD\" "
        + "from \"s\".\"nested\" "
        + "where CAST(\"ARRAYFIELD\"['a'] AS INTEGER) = 200";
    statement.executeQuery(sql);

    fail("This query shouldn't be evaluated properly");
  } catch (SQLException e) {
    Throwable e2 = e.getCause();
    assertThat(e2, is(instanceOf(CalciteContextException.class)));
  }
}
 
Example #2
Source File: SqlExceptionHelper.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static UserException.Builder planError(String query, Exception ex) {
  UserException.Builder b = UserException.planError(ex)
    .addContext(SQL_QUERY_CONTEXT, query);

  // CalciteContextException alters the error message including the start/end positions
  // we need to extract the original error message and add the remaining information as context
  if (ex instanceof CalciteContextException) {
    CalciteContextException cce = (CalciteContextException) ex;
    b.message(cce.getMessage())
            .addContext(START_LINE_CONTEXT, cce.getPosLine())
            .addContext(START_COLUMN_CONTEXT, cce.getPosColumn())
            .addContext(END_LINE_CONTEXT, cce.getEndPosLine())
            .addContext(END_COLUMN_CONTEXT, cce.getEndPosColumn());
  }
  return b;
}
 
Example #3
Source File: SqlExceptionHelper.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static UserException.Builder validationError(String query, ValidationException ex) {
  Throwable cause = ex;
  if (ex.getCause() != null) {
    // ValidationException generally wraps the "real" cause that we are interested in
    cause = ex.getCause();
  }
  UserException.Builder b = UserException.validationError(cause)
    .addContext(SQL_QUERY_CONTEXT, query);

  // CalciteContextException alters the error message including the start/end positions
  // we need to extract the original error message and add the remaining information as context
  if (cause instanceof CalciteContextException && cause.getCause() != null) {
    CalciteContextException cce = (CalciteContextException) cause;
    b.message(cce.getCause().getMessage())
            .addContext(START_LINE_CONTEXT, cce.getPosLine())
            .addContext(START_COLUMN_CONTEXT, cce.getPosColumn())
            .addContext(END_LINE_CONTEXT, cce.getEndPosLine())
            .addContext(END_COLUMN_CONTEXT, cce.getEndPosColumn());
  }
  return b;
}
 
Example #4
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that a source value does not violate the constraint of the target
 * column.
 *
 * @param sourceValue      The insert value being validated
 * @param targetConstraint The constraint applied to sourceValue for validation
 * @param errorSupplier    The function to apply when validation fails
 */
public static void validateValueAgainstConstraint(SqlNode sourceValue,
    RexNode targetConstraint,
    Supplier<CalciteContextException> errorSupplier) {
  if (!(sourceValue instanceof SqlLiteral)) {
    // We cannot guarantee that the value satisfies the constraint.
    throw errorSupplier.get();
  }
  final SqlLiteral insertValue = (SqlLiteral) sourceValue;
  final RexLiteral columnConstraint = (RexLiteral) targetConstraint;

  final RexSqlStandardConvertletTable convertletTable =
      new RexSqlStandardConvertletTable();
  final RexToSqlNodeConverter sqlNodeToRexConverter =
      new RexToSqlNodeConverterImpl(convertletTable);
  final SqlLiteral constraintValue =
      (SqlLiteral) sqlNodeToRexConverter.convertLiteral(columnConstraint);

  if (!insertValue.equals(constraintValue)) {
    // The value does not satisfy the constraint.
    throw errorSupplier.get();
  }
}
 
Example #5
Source File: SqlValidatorUtilTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
@Test void testCheckingDuplicatesWithCompoundIdentifiers() {
  final List<SqlNode> newList = new ArrayList<>(2);
  newList.add(new SqlIdentifier(Arrays.asList("f0", "c0"), SqlParserPos.ZERO));
  newList.add(new SqlIdentifier(Arrays.asList("f0", "c0"), SqlParserPos.ZERO));
  final SqlTester tester =
      new SqlValidatorTester(SqlTestFactory.INSTANCE);
  final SqlValidatorImpl validator =
      (SqlValidatorImpl) tester.getValidator();
  try {
    SqlValidatorUtil.checkIdentifierListForDuplicates(newList,
        validator.getValidationErrorFunction());
    fail("expected exception");
  } catch (CalciteContextException e) {
    // ok
  }
  // should not throw
  newList.set(1, new SqlIdentifier(Arrays.asList("f0", "c1"), SqlParserPos.ZERO));
  SqlValidatorUtil.checkIdentifierListForDuplicates(newList, null);
}
 
Example #6
Source File: SqlValidatorFeatureTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected void validateFeature(
    Feature feature,
    SqlParserPos context) {
  if (feature.equals(disabledFeature)) {
    CalciteException ex =
        new CalciteException(
            FEATURE_DISABLED,
            null);
    if (context == null) {
      throw ex;
    }
    throw new CalciteContextException(
        "location",
        ex,
        context.getLineNum(),
        context.getColumnNum(),
        context.getEndLineNum(),
        context.getEndColumnNum());
  }
}
 
Example #7
Source File: CollectionTypeTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testInvalidAccessUseStringForIndexOnArray() throws Exception {
  Connection connection = setupConnectionWithNestedTable();

  final Statement statement = connection.createStatement();

  try {
    final String sql = "select \"ID\","
        + " \"MAPFIELD\", \"NESTEDMAPFIELD\", \"ARRAYFIELD\" "
        + "from \"s\".\"nested\" "
        + "where \"ARRAYFIELD\"['a'] = 200";
    statement.executeQuery(sql);

    fail("This query shouldn't be evaluated properly");
  } catch (SQLException e) {
    Throwable e2 = e.getCause();
    assertThat(e2, is(instanceOf(CalciteContextException.class)));
  }
}
 
Example #8
Source File: SqlUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps an exception with context.
 */
public static CalciteContextException newContextException(
    int line,
    int col,
    int endLine,
    int endCol,
    Resources.ExInst<?> e) {
  CalciteContextException contextExcn =
      (line == endLine && col == endCol
          ? RESOURCE.validatorContextPoint(line, col)
          : RESOURCE.validatorContext(line, col, endLine, endCol)).ex(e.ex());
  contextExcn.setPosition(line, col, endLine, endCol);
  return contextExcn;
}
 
Example #9
Source File: SqlValidatorImpl.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void validateAggregateParams(SqlCall aggCall, SqlNode filter, SqlValidatorScope scope) {
  if (filter != null) {
    Exception e = new SqlValidatorException("Dremio does not currently support aggregate functions with a filter clause", null);
    SqlParserPos pos = filter.getParserPosition();
    CalciteContextException ex = RESOURCE.validatorContextPoint(pos.getLineNum(), pos.getColumnNum()).ex(e);
    ex.setPosition(pos.getLineNum(), pos.getColumnNum());
    throw ex;
  }
  super.validateAggregateParams(aggCall, filter, scope);
}
 
Example #10
Source File: SqlQueryParser.java    From quark with Apache License 2.0 5 votes vote down vote up
private RelNode parseInternal(String sql) throws SQLException {
  try {
    //final CalcitePrepare.Context prepareContext = context.getPrepareContext();
    //Class elementType = Object[].class;
    //RelNode relNode = new QuarkPrepare().prepare(prepareContext, sql, elementType, -1);
    RelNode relNode =  this.worker.parse(sql);
    LOG.info("\n" + RelOptUtil.dumpPlan(
        "", relNode, false, SqlExplainLevel.ALL_ATTRIBUTES));
    return relNode;
  } catch (CalciteContextException e) {
    throw new SQLException(e.getMessage(), e);
  }
}
 
Example #11
Source File: SqlParser.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a SQL expression.
 *
 * @throws SqlParseException if there is a parse error
 */
public SqlNode parseExpression() throws SqlParseException {
  try {
    return parser.parseSqlExpressionEof();
  } catch (Throwable ex) {
    if (ex instanceof CalciteContextException) {
      final String originalSql = parser.getOriginalSql();
      if (originalSql != null) {
        ((CalciteContextException) ex).setOriginalStatement(originalSql);
      }
    }
    throw parser.normalizeException(ex);
  }
}
 
Example #12
Source File: SqlParser.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Normalizes a SQL exception. */
private SqlParseException handleException(Throwable ex) {
  if (ex instanceof CalciteContextException) {
    final String originalSql = parser.getOriginalSql();
    if (originalSql != null) {
      ((CalciteContextException) ex).setOriginalStatement(originalSql);
    }
  }
  return parser.normalizeException(ex);
}
 
Example #13
Source File: SqlUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps an exception with context.
 */
public static CalciteContextException newContextException(
    final SqlParserPos pos,
    Resources.ExInst<?> e) {
  int line = pos.getLineNum();
  int col = pos.getColumnNum();
  int endLine = pos.getEndLineNum();
  int endCol = pos.getEndColumnNum();
  return newContextException(line, col, endLine, endCol, e);
}
 
Example #14
Source File: SqlUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps an exception with context.
 */
public static CalciteException newContextException(
    final SqlParserPos pos,
    Resources.ExInst<?> e,
    String inputText) {
  CalciteContextException ex = newContextException(pos, e);
  ex.setOriginalStatement(inputText);
  return ex;
}
 
Example #15
Source File: SqlIntervalQualifier.java    From calcite with Apache License 2.0 5 votes vote down vote up
private CalciteContextException fieldExceedsPrecisionException(
    SqlParserPos pos, int sign, BigDecimal value, TimeUnit type,
    int precision) {
  if (sign == -1) {
    value = value.negate();
  }
  return SqlUtil.newContextException(pos,
      RESOURCE.intervalFieldExceedsPrecision(
          value, type.name() + "(" + precision + ")"));
}
 
Example #16
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ValidateErrorInfo with an CalciteContextException.
 *
 * @param e Exception
 */
public ValidateErrorInfo(
    CalciteContextException e) {
  this.startLineNum = e.getPosLine();
  this.startColumnNum = e.getPosColumn();
  this.endLineNum = e.getEndPosLine();
  this.endColumnNum = e.getEndPosColumn();
  this.errorMsg = e.getCause().getMessage();
}
 
Example #17
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override public CalciteContextException apply(
	SqlNode v0, Resources.ExInst<SqlValidatorException> v1) {
	return newValidationError(v0, v1);
}
 
Example #18
Source File: SqlIntervalQualifier.java    From calcite with Apache License 2.0 4 votes vote down vote up
private CalciteContextException invalidValueException(SqlParserPos pos,
    String value) {
  return SqlUtil.newContextException(pos,
      RESOURCE.unsupportedIntervalLiteral(
          "'" + value + "'", "INTERVAL " + toString()));
}
 
Example #19
Source File: SqlValidatorImpl.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private void throwException(SqlParserPos parserPos) {
  throw new CalciteContextException("Failure parsing the query",
                                    new SqlValidatorException("Flatten is not supported as part of join condition", null),
                                    parserPos.getLineNum(), parserPos.getEndLineNum(),
                                    parserPos.getColumnNum(), parserPos.getEndColumnNum());
}
 
Example #20
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public CalciteContextException newValidationError(SqlNode node,
	Resources.ExInst<SqlValidatorException> e) {
	assert node != null;
	final SqlParserPos pos = node.getParserPosition();
	return SqlUtil.newContextException(pos, e);
}
 
Example #21
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public CalciteContextException apply(
	SqlNode v0, Resources.ExInst<SqlValidatorException> v1) {
	return newValidationError(v0, v1);
}
 
Example #22
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public CalciteContextException get() {
	return newValidationError(sqlNode, validatorException);
}
 
Example #23
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public CalciteContextException newValidationError(SqlNode node,
	Resources.ExInst<SqlValidatorException> e) {
	assert node != null;
	final SqlParserPos pos = node.getParserPosition();
	return SqlUtil.newContextException(pos, e);
}
 
Example #24
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public CalciteContextException get() {
	return newValidationError(sqlNode, validatorException);
}
 
Example #25
Source File: SqlValidator.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Adds "line x, column y" context to a validator exception.
 *
 * <p>Note that the input exception is checked (it derives from
 * {@link Exception}) and the output exception is unchecked (it derives from
 * {@link RuntimeException}). This is intentional -- it should remind code
 * authors to provide context for their validation errors.</p>
 *
 * @param node The place where the exception occurred, not null
 * @param e    The validation error
 * @return Exception containing positional information, never null
 */
CalciteContextException newValidationError(
    SqlNode node,
    Resources.ExInst<SqlValidatorException> e);
 
Example #26
Source File: SqlParser.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the warnings that were generated by the previous invocation
 * of the parser.
 */
public List<CalciteContextException> getWarnings() {
  return parser.warnings;
}