From 012d0864217d093230def603e977fc71059e5fba Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Thu, 1 Aug 2024 21:14:35 -0700 Subject: [PATCH] fix(parser): none check on parent_predicate (#2) --- sqlglot/optimizer/scope.py | 5 +++-- sqlglot/optimizer/unnest_subqueries.py | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/sqlglot/optimizer/scope.py b/sqlglot/optimizer/scope.py index 6826bf93b4..fd44658c50 100644 --- a/sqlglot/optimizer/scope.py +++ b/sqlglot/optimizer/scope.py @@ -470,8 +470,9 @@ def traverse(self): scope.subquery_scopes, ) ) - if len(result) > MAX_SCOPE_DEPTH: - raise OptimizeError("Scope depth limit exceeded") + actual_depth = len(result) + if actual_depth > MAX_SCOPE_DEPTH: + raise OptimizeError(f"Scope depth limit({actual_depth}) exceeded max depth limit({MAX_SCOPE_DEPTH})") yield from reversed(result) diff --git a/sqlglot/optimizer/unnest_subqueries.py b/sqlglot/optimizer/unnest_subqueries.py index dcb2f59f51..094dada310 100644 --- a/sqlglot/optimizer/unnest_subqueries.py +++ b/sqlglot/optimizer/unnest_subqueries.py @@ -261,16 +261,19 @@ def remove_aggs(node): if key in group_by: key.replace(nested) elif isinstance(predicate, exp.EQ): - parent_predicate = _replace( - parent_predicate, - f"({parent_predicate} AND ARRAY_CONTAINS({nested}, {column}))", - ) + if parent_predicate: + parent_predicate = _replace( + parent_predicate, + f"({parent_predicate} AND ARRAY_CONTAINS({nested}, {column}))", + ) else: key.replace(exp.to_identifier("_x")) - parent_predicate = _replace( - parent_predicate, - f"({parent_predicate} AND ARRAY_ANY({nested}, _x -> {predicate}))", - ) + + if parent_predicate: + parent_predicate = _replace( + parent_predicate, + f"({parent_predicate} AND ARRAY_ANY({nested}, _x -> {predicate}))", + ) parent_select.join( select.group_by(*group_by, copy=False),