@Override public String getMessage() { return format("line %s:%s: %s", getLineNumber(), getColumnNumber(), getErrorMessage()); } }
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)); } }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { throw new ParsingException(message, e, line, charPositionInLine); } };
@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); } }
@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; }
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())); } } }
@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; }
@Override public String getMessage() { return format("line %s:%s: %s", getLineNumber(), getColumnNumber(), getErrorMessage()); } }
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)); } }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { throw new ParsingException(message, e, line, charPositionInLine); } };
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()); } }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { throw new ParsingException(message, e, line, charPositionInLine); } };
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()); } }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { throw new ParsingException(message, e, line, charPositionInLine); } };
@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"); } }
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 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 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 GenericLiteral(Optional<NodeLocation> location, String type, String value) { super(location); requireNonNull(type, "type is null"); requireNonNull(value, "value is null"); if (type.equalsIgnoreCase("X")) { // we explicitly disallow "X" as type name, so if the user arrived here, // it must be because that he intended to give a binaryLiteral instead, but // added whitespace between the X and quote throw new ParsingException("Spaces are not allowed between 'X' and the starting quote of a binary literal", location.get()); } this.type = type; this.value = value; }