Java Code Examples for org.apache.calcite.sql.advise.SqlAdvisor#ValidateErrorInfo

The following examples show how to use org.apache.calcite.sql.advise.SqlAdvisor#ValidateErrorInfo . 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: SQLResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@POST
@Path("/analyze/validate")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ValidationResponse validateSQL(AnalyzeRequest analyzeRequest) {

  final String sql = analyzeRequest.getSql();
  final List<String> context = analyzeRequest.getContext();

  // Setup dependencies and execute validation
  SQLAnalyzer SQLAnalyzer =
    SQLAnalyzerFactory.createSQLAnalyzer(
      securityContext.getUserPrincipal().getName(), sabotContext, context, false, projectOptionManager);

  List<SqlAdvisor.ValidateErrorInfo> validationErrors = SQLAnalyzer.validate(sql);

  // Build response object and return
  return buildValidationResponse(validationErrors);
}
 
Example 2
Source File: SQLResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Builds the response object for query validation.
 *
 * @param errorList  The list of query errors returned from the SqlAdvisor.
 *
 * @return The built ValidationResponse object or null if there are no available validation errors.
 */
protected ValidationResponse buildValidationResponse(List<SqlAdvisor.ValidateErrorInfo> errorList) {

  // Return empty response in REST request
  if (errorList == null || errorList.isEmpty()) {
    return null;
  }

  // Create and populate error response list
  List<QueryError> sqlErrors = new ArrayList<>();
  for (SqlAdvisor.ValidateErrorInfo error : errorList) {
    sqlErrors.add(
      new QueryError(error.getMessage(),
      new QueryError.Range(error.getStartLineNum(),
        error.getStartColumnNum(),
        error.getEndLineNum() + 1,
        error.getEndColumnNum() + 1)));
  }

  ValidationResponse response = new ValidationResponse(sqlErrors);
  return response;
}
 
Example 3
Source File: TestSQLAnalyzer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidation() {
  List<SqlAdvisor.ValidateErrorInfo> validationErrors = sqlAnalyzer.validate("select * from");
  assertEquals(1, validationErrors.size());
  assertEquals(10, validationErrors.get(0).getStartColumnNum());
  assertEquals(13, validationErrors.get(0).getEndColumnNum());
}
 
Example 4
Source File: SQLAnalyzer.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Pass the SqlValidatorWithHints implementation to Calcite's SqlAdvisor
 * for query validation.
 *
 * @param sql             The SQL being evaluated.
 * @return List<SqlAdvisor.ValidateErrorInfo> that represents parser or validation errors.
 */
public List<SqlAdvisor.ValidateErrorInfo> validate(String sql) {
  SqlAdvisor sqlAdvisor = new SqlAdvisor(validator);
  return sqlAdvisor.validate(sql);
}