Skip to content

Commit c385297

Browse files
authored
chore(sql_parse): Provide more meaningful SQLGlot errors (#27858)
1 parent 848a7ff commit c385297

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

superset/sql_parse.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,21 @@ def _extract_tables_from_sql(self) -> set[Table]:
752752
statements = parse(self.stripped(), dialect=self._dialect)
753753
except SqlglotError as ex:
754754
logger.warning("Unable to parse SQL (%s): %s", self._dialect, self.sql)
755-
dialect = self._dialect or "generic"
755+
756+
message = (
757+
"Error parsing near '{highlight}' at line {line}:{col}".format( # pylint: disable=consider-using-f-string
758+
**ex.errors[0]
759+
)
760+
if isinstance(ex, ParseError)
761+
else str(ex)
762+
)
763+
756764
raise SupersetSecurityException(
757765
SupersetError(
758766
error_type=SupersetErrorType.QUERY_SECURITY_ACCESS_ERROR,
759-
message=__(f"Unable to parse SQL ({dialect}): {self.sql}"),
767+
message=__(
768+
f"You may have an error in your SQL statement. {message}"
769+
),
760770
level=ErrorLevel.ERROR,
761771
)
762772
) from ex

tests/unit_tests/sql_parse_tests.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,26 +277,30 @@ def test_extract_tables_illdefined() -> None:
277277
with pytest.raises(SupersetSecurityException) as excinfo:
278278
extract_tables("SELECT * FROM schemaname.")
279279
assert (
280-
str(excinfo.value) == "Unable to parse SQL (generic): SELECT * FROM schemaname."
280+
str(excinfo.value)
281+
== "You may have an error in your SQL statement. Error parsing near '.' at line 1:25"
281282
)
282283

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

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

297298
with pytest.raises(SupersetSecurityException) as excinfo:
298299
extract_tables('SELECT * FROM "tbname')
299-
assert str(excinfo.value) == 'Unable to parse SQL (generic): SELECT * FROM "tbname'
300+
assert (
301+
str(excinfo.value)
302+
== "You may have an error in your SQL statement. Error tokenizing 'SELECT * FROM \"tbnam'"
303+
)
300304

301305
# odd edge case that works
302306
assert extract_tables("SELECT * FROM catalogname..tbname") == {

0 commit comments

Comments
 (0)