Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sql_parse): Provide more meaningful SQLGlot errors #27858

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,21 @@ def _extract_tables_from_sql(self) -> set[Table]:
statements = parse(self.stripped(), dialect=self._dialect)
except SqlglotError as ex:
logger.warning("Unable to parse SQL (%s): %s", self._dialect, self.sql)
dialect = self._dialect or "generic"

message = (
"Error parsing near '{highlight}' at line {line}:{col}".format( # pylint: disable=consider-using-f-string
**ex.errors[0]
)
if isinstance(ex, ParseError)
else str(ex)
)

raise SupersetSecurityException(
SupersetError(
error_type=SupersetErrorType.QUERY_SECURITY_ACCESS_ERROR,
message=__(f"Unable to parse SQL ({dialect}): {self.sql}"),
message=__(
f"You may have an error in your SQL statement. {message}"
),
level=ErrorLevel.ERROR,
)
) from ex
Expand Down
12 changes: 8 additions & 4 deletions tests/unit_tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,30 @@ def test_extract_tables_illdefined() -> None:
with pytest.raises(SupersetSecurityException) as excinfo:
extract_tables("SELECT * FROM schemaname.")
assert (
str(excinfo.value) == "Unable to parse SQL (generic): SELECT * FROM schemaname."
str(excinfo.value)
== "You may have an error in your SQL statement. Error parsing near '.' at line 1:25"
)

with pytest.raises(SupersetSecurityException) as excinfo:
extract_tables("SELECT * FROM catalogname.schemaname.")
assert (
str(excinfo.value)
== "Unable to parse SQL (generic): SELECT * FROM catalogname.schemaname."
== "You may have an error in your SQL statement. Error parsing near '.' at line 1:37"
)

with pytest.raises(SupersetSecurityException) as excinfo:
extract_tables("SELECT * FROM catalogname..")
assert (
str(excinfo.value)
== "Unable to parse SQL (generic): SELECT * FROM catalogname.."
== "You may have an error in your SQL statement. Error parsing near '.' at line 1:27"
)

with pytest.raises(SupersetSecurityException) as excinfo:
extract_tables('SELECT * FROM "tbname')
assert str(excinfo.value) == 'Unable to parse SQL (generic): SELECT * FROM "tbname'
assert (
str(excinfo.value)
== "You may have an error in your SQL statement. Error tokenizing 'SELECT * FROM \"tbnam'"
)

# odd edge case that works
assert extract_tables("SELECT * FROM catalogname..tbname") == {
Expand Down
Loading