forked from tobymao/sqlglot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert: "Feat!(schema, snowflake): normalize snowflake identifiers to…
… uppercase" (tobymao#1680) * Revert "Feat!(schema, snowflake): normalize snowflake identifiers to uppercase (tobymao#1666)" This reverts commit 028fa35. * Keep schema associated changes, snowflake logic excluded
- Loading branch information
1 parent
f54d230
commit d8edd2c
Showing
13 changed files
with
110 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from sqlglot import exp | ||
|
||
|
||
def lower_identities(expression): | ||
""" | ||
Convert all unquoted identifiers to lower case. | ||
Assuming the schema is all lower case, this essentially makes identifiers case-insensitive. | ||
Example: | ||
>>> import sqlglot | ||
>>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar') | ||
>>> lower_identities(expression).sql() | ||
'SELECT bar.a AS A FROM "Foo".bar' | ||
Args: | ||
expression (sqlglot.Expression): expression to quote | ||
Returns: | ||
sqlglot.Expression: quoted expression | ||
""" | ||
# We need to leave the output aliases unchanged, so the selects need special handling | ||
_lower_selects(expression) | ||
|
||
# These clauses can reference output aliases and also need special handling | ||
_lower_order(expression) | ||
_lower_having(expression) | ||
|
||
# We've already handled these args, so don't traverse into them | ||
traversed = {"expressions", "order", "having"} | ||
|
||
if isinstance(expression, exp.Subquery): | ||
# Root subquery, e.g. (SELECT A AS A FROM X) LIMIT 1 | ||
lower_identities(expression.this) | ||
traversed |= {"this"} | ||
|
||
if isinstance(expression, exp.Union): | ||
# Union, e.g. SELECT A AS A FROM X UNION SELECT A AS A FROM X | ||
lower_identities(expression.left) | ||
lower_identities(expression.right) | ||
traversed |= {"this", "expression"} | ||
|
||
for k, v in expression.iter_expressions(): | ||
if k in traversed: | ||
continue | ||
v.transform(_lower, copy=False) | ||
|
||
return expression | ||
|
||
|
||
def _lower_selects(expression): | ||
for e in expression.expressions: | ||
# Leave output aliases as-is | ||
e.unalias().transform(_lower, copy=False) | ||
|
||
|
||
def _lower_order(expression): | ||
order = expression.args.get("order") | ||
|
||
if not order: | ||
return | ||
|
||
output_aliases = {e.alias for e in expression.expressions if isinstance(e, exp.Alias)} | ||
|
||
for ordered in order.expressions: | ||
# Don't lower references to output aliases | ||
if not ( | ||
isinstance(ordered.this, exp.Column) | ||
and not ordered.this.table | ||
and ordered.this.name in output_aliases | ||
): | ||
ordered.transform(_lower, copy=False) | ||
|
||
|
||
def _lower_having(expression): | ||
having = expression.args.get("having") | ||
|
||
if not having: | ||
return | ||
|
||
# Don't lower references to output aliases | ||
for agg in having.find_all(exp.AggFunc): | ||
agg.transform(_lower, copy=False) | ||
|
||
|
||
def _lower(node): | ||
if isinstance(node, exp.Identifier) and not node.quoted: | ||
node.set("this", node.this.lower()) | ||
return node |
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
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
16 changes: 0 additions & 16 deletions
16
...tures/optimizer/normalize_identifiers.sql → ...s/fixtures/optimizer/lower_identities.sql
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.