-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat!(snowflake): normalize identifiers as uppercase (#1697)
* Feat!(snowflake): normalize identifiers as uppercase * fixup * fixup * Fix\!: lower aliases as well * Fix: maintain quotes in aliased names * Apply suggestions from code review Co-authored-by: Jo <[email protected]> * Simplify normalize_identifiers. * Update sqlglot/helper.py Co-authored-by: Jo <[email protected]> * default identify to true --------- Co-authored-by: George Sittas <[email protected]> Co-authored-by: Jo <[email protected]>
- Loading branch information
1 parent
c2c955c
commit dcfe67f
Showing
18 changed files
with
303 additions
and
148 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from sqlglot import exp | ||
from sqlglot.dialects.dialect import RESOLVES_IDENTIFIERS_AS_UPPERCASE, DialectType | ||
|
||
|
||
def normalize_identifiers( | ||
expression: exp.Expression, dialect: DialectType = None | ||
) -> exp.Expression: | ||
""" | ||
Normalize all unquoted identifiers to either lower or upper case, depending on | ||
the dialect. This essentially makes those identifiers case-insensitive. | ||
Example: | ||
>>> import sqlglot | ||
>>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar') | ||
>>> normalize_identifiers(expression).sql() | ||
'SELECT bar.a AS a FROM "Foo".bar' | ||
Args: | ||
expression: The expression to transform. | ||
dialect: The dialect to use in order to decide how to normalize identifiers. | ||
Returns: | ||
The transformed expression. | ||
""" | ||
return expression.transform(_normalize, dialect, copy=False) | ||
|
||
|
||
def _normalize(node: exp.Expression, dialect: DialectType = None) -> exp.Expression: | ||
if isinstance(node, exp.Identifier) and not node.quoted: | ||
node.set( | ||
"this", | ||
node.this.upper() | ||
if dialect in RESOLVES_IDENTIFIERS_AS_UPPERCASE | ||
else node.this.lower(), | ||
) | ||
|
||
return node |
This file contains 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
Oops, something went wrong.