Skip to content

Commit

Permalink
Improve heuristics of assigning an error to its statement
Browse files Browse the repository at this point in the history
  • Loading branch information
surister committed Jun 10, 2024
1 parent b946f76 commit f6ad0ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cratedb_sqlparse_py/cratedb_sqlparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def original_query_with_error_marked(self):
query = self.offending_token.source[1].strdata
offending_token_text: str = query[self.offending_token.start : self.offending_token.stop + 1]
query_lines: list = query.split("\n")

offending_line: str = query_lines[self.line - 1]

# White spaces from the beginning of the offending line to the offending text, so the '^'
Expand All @@ -72,10 +71,10 @@ def line(self):
return self.offending_token.line

def __repr__(self):
return f"{type(self.e).__qualname__}"
return type(self.e).__qualname__

def __str__(self):
return repr(self)
return self.error_message


class CaseInsensitiveStream(InputStream):
Expand All @@ -91,6 +90,8 @@ class ExceptionErrorListener(ErrorListener):
Error listener that raises the antlr4 error as a Python exception.
"""

errors = () # Dummy for passing tests, should not be used.

def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
error = ParsingException(
msg=msg,
Expand Down Expand Up @@ -166,12 +167,15 @@ def __repr__(self):
def find_suitable_error(statement, errors):
for error in errors[:]:
# We clean the error_query of ';' and spaces because ironically,
# we can get the full query in the error handler but not in the context.
# we can get the full query in the error but not in the parsed statement.
error_query = error.query
if error_query.endswith(";"):
error_query = error_query[: len(error_query) - 1]

if error_query.lstrip().rstrip() == statement.query:
error_query = error_query.lstrip().rstrip()

# If a good match error_query contains statement.query
if statement.query in error_query:
statement.exception = error
errors.pop(errors.index(error))

Expand Down Expand Up @@ -222,7 +226,7 @@ def sqlparse(query: str, raise_exception: bool = False) -> List[Statement]:

if len(error_listener.errors) > 1:
logging.error(
"Could not match errors to queries, too much ambiguity, open an issue with this error and the query."
"Could not match errors to queries, too much ambiguity, open an issue with this " "error and the query."
)

return statements
6 changes: 6 additions & 0 deletions cratedb_sqlparse_py/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_sqlparse_raises_exception():
sqlparse(query, raise_exception=True)


def test_sqlparse_not_raise_exception():
from cratedb_sqlparse import sqlparse

sqlparse("SELECT COUNT(*) FROM doc.tbl f WHERE f.id = 1;", raise_exception=True)


def test_sqlparse_collects_exception():
from cratedb_sqlparse import sqlparse

Expand Down

0 comments on commit f6ad0ac

Please sign in to comment.