-
Notifications
You must be signed in to change notification settings - Fork 280
fix(snowflake): recognise query sources with leading comments #377
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
Open
barveP
wants to merge
4
commits into
apache:main
Choose a base branch
from
barveP:fix/snowflake-query-source
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2a7ca18
fix(snowflake): recognise query sources with leading comments
barveP 368f365
fix(snowflake): do not treat SELECT$... identifiers as queries
barveP f67f582
fix(snowflake): classify query sources with SQLGlot
barveP 4ec6b28
refactor(snowflake): isolate existing source relation handling
barveP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ keywords = [ | |
| ] | ||
| dependencies = [ | ||
| "PyYAML>=5.0", | ||
| "sqlglot>=30.12.0", | ||
| ] | ||
|
|
||
| [project.scripts] | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ | |
| # under the License. | ||
|
|
||
| PyYAML>=5.0 | ||
| sqlglot>=30.12.0 | ||
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to avoid these regex as well with an approach like the following:
def try_parse_source_relation(source_stripped):
try:
table = parse_one(source_stripped, read="snowflake", into=exp.Table)
except ParseError:
return None
if table is None or len(table.parts) != 3 or not all(isinstance(p, exp.Identifier) for p in table.parts):
return None
return {
"database": _render_identifier(table.parts[0]),
"schema": _render_identifier(table.parts[1]),
"table": _render_identifier(table.parts[2]),
}
There is one nuance that the parse_one might be permissive to trailing semi-colons (so that might be a case to check in try_parse_source_relation => return None).
This helps keep the control flow a little cleaner:
if is_source(source_stripped):
return ...
parsed_relation = try_parse_relation(source_stripped)
if parsed_relation is not None:
return parsed_relation
throw_error
(ideas from this blog on the parse/don't validate pattern)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. took the control-flow shape in the latest commit.
i also tried
parse_one(..., into=exp.Table)for the relation path. it works with four guards: trailing;(as you noted), comments, extra args like an alias orAT (OFFSET => ...)that would otherwise be dropped silently, andisinstance(exp.Table), sincea.b.c; d.e.fparses to a Block. with those it passes every test exceptfrom.public.ordersanddb.public.qualifythose two are the real question. sqlglot decides which keywords can be identifiers from its own
ID_VAR_TOKENSlist (parser doc), so it rejects from andqualifybut acceptsorder,table,group, which snowflake reserves too (reserved-keywords). the regexes are snowflake's identifier grammar verbatim (identifiers): they check shape only and leave reserved words to snowflakeso it's partial enforcement via sqlglot, or none. i lean none because it's consistent but i'm fine either way and have the
parse_oneversion ready (with a bump to sqlglot 30.13.0, since 30.12.0 misparses"@".public.orders).which do we prefer?
cc: @khush-bhatia since you know the snowflake side best