@Test public void testParsingExceptionPositionInfo() { try { SQL_PARSER.createStatement("select *\nfrom x\nwhere from"); fail("expected exception"); } catch (ParsingException e) { assertTrue(e.getMessage().startsWith("line 3:7: mismatched input 'from'")); assertTrue(e.getErrorMessage().startsWith("mismatched input 'from'")); assertEquals(e.getLineNumber(), 3); assertEquals(e.getColumnNumber(), 7); } }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { throw new ParsingException(message, e, line, charPositionInLine); } };
@Override public String getMessage() { return format("line %s:%s: %s", getLineNumber(), getColumnNumber(), getErrorMessage()); } }
@Nullable private static ErrorLocation getErrorLocation(Throwable throwable) { // TODO: this is a big hack if (throwable instanceof ParsingException) { ParsingException e = (ParsingException) throwable; return new ErrorLocation(e.getLineNumber(), e.getColumnNumber()); } else if (throwable instanceof SemanticException) { SemanticException e = (SemanticException) throwable; if (e.getNode().getLocation().isPresent()) { NodeLocation nodeLocation = e.getNode().getLocation().get(); return new ErrorLocation(nodeLocation.getLineNumber(), nodeLocation.getColumnNumber()); } } return null; }
query = buildQuery(context, sqlQuery, sample, defaultSchema, limit, materializedViews, sessionParameters); } catch (ParsingException e) { QueryError error = new QueryError(e.getMessage(), null, null, e.getLineNumber(), e.getColumnNumber()); LogUtil.logQueryError(sqlQuery, error, executor.getClass()); return QueryExecution.completedQueryExecution(sqlQuery, QueryResult.errorResult(error, sqlQuery));
private static Statement parseFormatted(SqlParser sqlParser, ParsingOptions parsingOptions, String sql, Node tree) { try { return sqlParser.createStatement(sql, parsingOptions); } catch (ParsingException e) { throw new AssertionError(format( "failed to parse formatted SQL: %s\nerror: %s\ntree: %s", sql, e.getMessage(), tree)); } }
private static void assertInvalidExpression(String expression, String expectedErrorMessageRegex) { try { Expression result = SQL_PARSER.createExpression(expression); fail("Expected to throw ParsingException for input:[" + expression + "], but got: " + result); } catch (ParsingException e) { if (!e.getErrorMessage().matches(expectedErrorMessageRegex)) { fail(format("Expected error message to match '%s', but was: '%s'", expectedErrorMessageRegex, e.getErrorMessage())); } } }
@Override public String getMessage() { return format("line %s:%s: %s", getLineNumber(), getColumnNumber(), getErrorMessage()); } }
private Query parseView(String view, QualifiedObjectName name, Node node) { try { Statement statement = sqlParser.createStatement(view, createParsingOptions(session)); return (Query) statement; } catch (ParsingException e) { throw new SemanticException(VIEW_PARSE_ERROR, node, "Failed parsing stored view '%s': %s", name, e.getMessage()); } }
@Nullable private static ErrorLocation getErrorLocation(Throwable throwable) { // TODO: this is a big hack if (throwable instanceof ParsingException) { ParsingException e = (ParsingException) throwable; return new ErrorLocation(e.getLineNumber(), e.getColumnNumber()); } else if (throwable instanceof SemanticException) { SemanticException e = (SemanticException) throwable; if (e.getNode().getLocation().isPresent()) { NodeLocation nodeLocation = e.getNode().getLocation().get(); return new ErrorLocation(nodeLocation.getLineNumber(), nodeLocation.getColumnNumber()); } } return null; }
} catch (ParsingException e) { request.response(encode(errorMessage(format("Couldn't parse %s: %s", collection.filter, e.getErrorMessage()), BAD_REQUEST))).end(); throw e;
@Test public void testParsingExceptionPositionInfo() { try { SQL_PARSER.createStatement("select *\nfrom x\nwhere from"); fail("expected exception"); } catch (ParsingException e) { assertEquals(e.getMessage(), "line 3:7: no viable alternative at input 'from'"); assertEquals(e.getErrorMessage(), "no viable alternative at input 'from'"); assertEquals(e.getLineNumber(), 3); assertEquals(e.getColumnNumber(), 7); } }
@Override public String getMessage() { return format("line %s:%s: %s", getLineNumber(), getColumnNumber(), getErrorMessage()); } }
@Test(dataProvider = "statements") public void testStatement(String sql, String error) { try { SQL_PARSER.createStatement(sql); fail("Expected parsing to fail"); } catch (ParsingException e) { assertEquals(e.getMessage(), error, "Error message mismatch for query:\n\n" + sql + "\n\n"); } }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { throw new ParsingException(message, e, line, charPositionInLine); } };
private static void assertInvalidExpression(String expression, String expectedErrorMessageRegex) { try { Expression result = SQL_PARSER.createExpression(expression); fail("Expected to throw ParsingException for input:[" + expression + "], but got: " + result); } catch (ParsingException e) { if (!e.getErrorMessage().matches(expectedErrorMessageRegex)) { fail(format("Expected error message to match '%s', but was: '%s'", expectedErrorMessageRegex, e.getErrorMessage())); } } }
@Test public void testParsingExceptionPositionInfo() { try { SQL_PARSER.createStatement("select *\nfrom x\nwhere from"); fail("expected exception"); } catch (ParsingException e) { assertTrue(e.getMessage().startsWith("line 3:7: mismatched input 'from'")); assertTrue(e.getErrorMessage().startsWith("mismatched input 'from'")); assertEquals(e.getLineNumber(), 3); assertEquals(e.getColumnNumber(), 7); } }
private Query parseView(String view, QualifiedObjectName name, Node node) { try { return (Query) sqlParser.createStatement(view, createParsingOptions(session)); } catch (ParsingException e) { throw new SemanticException(VIEW_PARSE_ERROR, node, "Failed parsing stored view '%s': %s", name, e.getMessage()); } }
private LongLiteral(Optional<NodeLocation> location, String value) { super(location); requireNonNull(value, "value is null"); try { this.value = Long.parseLong(value); } catch (NumberFormatException e) { throw new ParsingException("Invalid numeric literal: " + value); } }
private static void assertInvalidExpression(String expression, String expectedErrorMessageRegex) { try { Expression result = SQL_PARSER.createExpression(expression); fail("Expected to throw ParsingException for input:[" + expression + "], but got: " + result); } catch (ParsingException e) { if (!e.getErrorMessage().matches(expectedErrorMessageRegex)) { fail(format("Expected error message to match '%s', but was: '%s'", expectedErrorMessageRegex, e.getErrorMessage())); } } }