Merging upstream version 21.0.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
3759c601a7
commit
96b10de29a
115 changed files with 66603 additions and 60920 deletions
|
@ -1,3 +1,5 @@
|
|||
# ruff: noqa: F401
|
||||
|
||||
from sqlglot.optimizer.optimizer import RULES, optimize
|
||||
from sqlglot.optimizer.scope import (
|
||||
Scope,
|
||||
|
|
|
@ -10,11 +10,13 @@ if t.TYPE_CHECKING:
|
|||
|
||||
|
||||
@t.overload
|
||||
def normalize_identifiers(expression: E, dialect: DialectType = None) -> E: ...
|
||||
def normalize_identifiers(expression: E, dialect: DialectType = None) -> E:
|
||||
...
|
||||
|
||||
|
||||
@t.overload
|
||||
def normalize_identifiers(expression: str, dialect: DialectType = None) -> exp.Identifier: ...
|
||||
def normalize_identifiers(expression: str, dialect: DialectType = None) -> exp.Identifier:
|
||||
...
|
||||
|
||||
|
||||
def normalize_identifiers(expression, dialect=None):
|
||||
|
|
|
@ -8,10 +8,10 @@ from sqlglot.schema import ensure_schema
|
|||
# Sentinel value that means an outer query selecting ALL columns
|
||||
SELECT_ALL = object()
|
||||
|
||||
|
||||
# Selection to use if selection list is empty
|
||||
DEFAULT_SELECTION = lambda is_agg: alias(
|
||||
exp.Max(this=exp.Literal.number(1)) if is_agg else "1", "_"
|
||||
)
|
||||
def default_selection(is_agg: bool) -> exp.Alias:
|
||||
return alias(exp.Max(this=exp.Literal.number(1)) if is_agg else "1", "_")
|
||||
|
||||
|
||||
def pushdown_projections(expression, schema=None, remove_unused_selections=True):
|
||||
|
@ -129,7 +129,7 @@ def _remove_unused_selections(scope, parent_selections, schema, alias_count):
|
|||
|
||||
# If there are no remaining selections, just select a single constant
|
||||
if not new_selections:
|
||||
new_selections.append(DEFAULT_SELECTION(is_agg))
|
||||
new_selections.append(default_selection(is_agg))
|
||||
|
||||
scope.expression.select(*new_selections, append=False, copy=False)
|
||||
|
||||
|
|
|
@ -104,7 +104,6 @@ def simplify(
|
|||
|
||||
if root:
|
||||
expression.replace(node)
|
||||
|
||||
return node
|
||||
|
||||
expression = while_changing(expression, _simplify)
|
||||
|
@ -174,16 +173,20 @@ def simplify_not(expression):
|
|||
if isinstance(this, exp.Paren):
|
||||
condition = this.unnest()
|
||||
if isinstance(condition, exp.And):
|
||||
return exp.or_(
|
||||
exp.not_(condition.left, copy=False),
|
||||
exp.not_(condition.right, copy=False),
|
||||
copy=False,
|
||||
return exp.paren(
|
||||
exp.or_(
|
||||
exp.not_(condition.left, copy=False),
|
||||
exp.not_(condition.right, copy=False),
|
||||
copy=False,
|
||||
)
|
||||
)
|
||||
if isinstance(condition, exp.Or):
|
||||
return exp.and_(
|
||||
exp.not_(condition.left, copy=False),
|
||||
exp.not_(condition.right, copy=False),
|
||||
copy=False,
|
||||
return exp.paren(
|
||||
exp.and_(
|
||||
exp.not_(condition.left, copy=False),
|
||||
exp.not_(condition.right, copy=False),
|
||||
copy=False,
|
||||
)
|
||||
)
|
||||
if is_null(condition):
|
||||
return exp.null()
|
||||
|
@ -490,7 +493,7 @@ def simplify_equality(expression: exp.Expression) -> exp.Expression:
|
|||
if isinstance(expression, COMPARISONS):
|
||||
l, r = expression.left, expression.right
|
||||
|
||||
if not l.__class__ in INVERSE_OPS:
|
||||
if l.__class__ not in INVERSE_OPS:
|
||||
return expression
|
||||
|
||||
if r.is_number:
|
||||
|
@ -714,8 +717,7 @@ def simplify_concat(expression):
|
|||
"""Reduces all groups that contain string literals by concatenating them."""
|
||||
if not isinstance(expression, CONCATS) or (
|
||||
# We can't reduce a CONCAT_WS call if we don't statically know the separator
|
||||
isinstance(expression, exp.ConcatWs)
|
||||
and not expression.expressions[0].is_string
|
||||
isinstance(expression, exp.ConcatWs) and not expression.expressions[0].is_string
|
||||
):
|
||||
return expression
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue