Fix crash on malformed SQL like "{ =" - #673
Open
eeshsaxena wants to merge 1 commit into
Open
Conversation
DialectParser runs sqlglot in WARN mode so it returns a best-effort AST instead
of raising. For a handful of short inputs like "{ =" sqlglot instead raises an
AttributeError while assembling that partial tree (a node whose key is None).
That was neither ParseError nor TokenError, so it escaped _try_dialects and
every public accessor (tables, columns, query_type, ...) crashed with a raw
AttributeError.
Catch any such sqlglot-side failure in _parse_with_dialect and treat it as 'this
dialect produced no result', so the query is reported through the normal
InvalidQueryDefinition path. ParseError/TokenError are still re-raised so the
existing syntax-error handling is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Every public accessor crashes on the same three-character input:
.tables,.columns,.query_type,.tables_aliases,.columns_dict.SELECT { =andx { =behave the same.Why
DialectParser._parse_with_dialectruns sqlglot withErrorLevel.WARNso it returns a best-effort partial AST instead of raising on the first problem. For{ =sqlglot builds a bracket/map node whose key isNoneand then raisesAttributeErrorwhile evaluatinge.this.nameon it. That is neitherParseErrornorTokenError, so_try_dialects(which only catches those two) never sees it and the exception propagates all the way out to the accessors, even though the method is documented to raiseInvalidQueryDefinitionwhen no dialect produces a usable AST.The fix
Catch any sqlglot-side failure in
_parse_with_dialectand treat it as 'this dialect produced no result' (returnsNone), so a malformed query is reported through the normalInvalidQueryDefinitionpath rather than crashing.ParseError/TokenErrorare re-raised so the existing syntax-error handling is untouched.After the fix
{ =no longer crashes:query_type/tablesraiseInvalidQueryDefinitionand the best-effort accessors come back empty.Tests
Added
test/test_malformed_input.pycovering the three inputs across the accessors. Full suite passes anddialect_parser.pystays at 100% coverage.