Skip to content

Commit 40fdbd1

Browse files
john-bodleymichael-s-molina
authored andcommitted
chore(sql_parse): Provide more meaningful SQLGlot errors (#27858)
(cherry picked from commit c385297)
1 parent a39971a commit 40fdbd1

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
@@ -297,11 +297,21 @@ def _extract_tables_from_sql(self) -> set[Table]:
297297
statements = parse(self.stripped(), dialect=self._dialect)
298298
except SqlglotError as ex:
299299
logger.warning("Unable to parse SQL (%s): %s", self._dialect, self.sql)
300-
dialect = self._dialect or "generic"
300+
301+
message = (
302+
"Error parsing near '{highlight}' at line {line}:{col}".format( # pylint: disable=consider-using-f-string
303+
**ex.errors[0]
304+
)
305+
if isinstance(ex, ParseError)
306+
else str(ex)
307+
)
308+
301309
raise SupersetSecurityException(
302310
SupersetError(
303311
error_type=SupersetErrorType.QUERY_SECURITY_ACCESS_ERROR,
304-
message=__(f"Unable to parse SQL ({dialect}): {self.sql}"),
312+
message=__(
313+
f"You may have an error in your SQL statement. {message}"
314+
),
305315
level=ErrorLevel.ERROR,
306316
)
307317
) from ex

tests/unit_tests/sql_parse_tests.py

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

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

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

293294
with pytest.raises(SupersetSecurityException) as excinfo:
294295
extract_tables('SELECT * FROM "tbname')
295-
assert str(excinfo.value) == 'Unable to parse SQL (generic): SELECT * FROM "tbname'
296+
assert (
297+
str(excinfo.value)
298+
== "You may have an error in your SQL statement. Error tokenizing 'SELECT * FROM \"tbnam'"
299+
)
296300

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

0 commit comments

Comments
 (0)