chore(sql_parse): Provide more meaningful SQLGlot errors (#27858)
This commit is contained in:
parent
848a7ffbf3
commit
c38529741e
|
|
@ -752,11 +752,21 @@ class ParsedQuery:
|
||||||
statements = parse(self.stripped(), dialect=self._dialect)
|
statements = parse(self.stripped(), dialect=self._dialect)
|
||||||
except SqlglotError as ex:
|
except SqlglotError as ex:
|
||||||
logger.warning("Unable to parse SQL (%s): %s", self._dialect, self.sql)
|
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(
|
raise SupersetSecurityException(
|
||||||
SupersetError(
|
SupersetError(
|
||||||
error_type=SupersetErrorType.QUERY_SECURITY_ACCESS_ERROR,
|
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,
|
level=ErrorLevel.ERROR,
|
||||||
)
|
)
|
||||||
) from ex
|
) from ex
|
||||||
|
|
|
||||||
|
|
@ -277,26 +277,30 @@ def test_extract_tables_illdefined() -> None:
|
||||||
with pytest.raises(SupersetSecurityException) as excinfo:
|
with pytest.raises(SupersetSecurityException) as excinfo:
|
||||||
extract_tables("SELECT * FROM schemaname.")
|
extract_tables("SELECT * FROM schemaname.")
|
||||||
assert (
|
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:
|
with pytest.raises(SupersetSecurityException) as excinfo:
|
||||||
extract_tables("SELECT * FROM catalogname.schemaname.")
|
extract_tables("SELECT * FROM catalogname.schemaname.")
|
||||||
assert (
|
assert (
|
||||||
str(excinfo.value)
|
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:
|
with pytest.raises(SupersetSecurityException) as excinfo:
|
||||||
extract_tables("SELECT * FROM catalogname..")
|
extract_tables("SELECT * FROM catalogname..")
|
||||||
assert (
|
assert (
|
||||||
str(excinfo.value)
|
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:
|
with pytest.raises(SupersetSecurityException) as excinfo:
|
||||||
extract_tables('SELECT * FROM "tbname')
|
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
|
# odd edge case that works
|
||||||
assert extract_tables("SELECT * FROM catalogname..tbname") == {
|
assert extract_tables("SELECT * FROM catalogname..tbname") == {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue