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)