summaryrefslogtreecommitdiffstats
path: root/tests/test_optimizer.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_optimizer.py')
-rw-r--r--tests/test_optimizer.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/tests/test_optimizer.py b/tests/test_optimizer.py
index cd0b9b1..64d7db7 100644
--- a/tests/test_optimizer.py
+++ b/tests/test_optimizer.py
@@ -242,7 +242,10 @@ class TestOptimizer(unittest.TestCase):
"CREATE FUNCTION `udfs`.`myTest`(`x` FLOAT64) AS (1)",
)
- self.check_file("qualify_columns", qualify_columns, execute=True, schema=self.schema)
+ self.check_file(
+ "qualify_columns", qualify_columns, execute=True, schema=self.schema, set_dialect=True
+ )
+ self.check_file("qualify_columns_ddl", qualify_columns, schema=self.schema)
def test_qualify_columns__with_invisible(self):
schema = MappingSchema(self.schema, {"x": {"a"}, "y": {"b"}, "z": {"b"}})
@@ -448,6 +451,23 @@ FROM READ_CSV('tests/fixtures/optimizer/tpc-h/nation.csv.gz', 'delimiter', '|')
)
self.assertEqual(set(scopes[3].sources), {""})
+ inner_query = "SELECT bar FROM baz"
+ for udtf in (f"UNNEST(({inner_query}))", f"LATERAL ({inner_query})"):
+ sql = f"SELECT a FROM foo CROSS JOIN {udtf}"
+ expression = parse_one(sql)
+
+ for scopes in traverse_scope(expression), list(build_scope(expression).traverse()):
+ self.assertEqual(len(scopes), 3)
+
+ self.assertEqual(scopes[0].expression.sql(), inner_query)
+ self.assertEqual(set(scopes[0].sources), {"baz"})
+
+ self.assertEqual(scopes[1].expression.sql(), udtf)
+ self.assertEqual(set(scopes[1].sources), {"", "foo"}) # foo is a lateral source
+
+ self.assertEqual(scopes[2].expression.sql(), f"SELECT a FROM foo CROSS JOIN {udtf}")
+ self.assertEqual(set(scopes[2].sources), {"", "foo"})
+
@patch("sqlglot.optimizer.scope.logger")
def test_scope_warning(self, logger):
self.assertEqual(len(traverse_scope(parse_one("WITH q AS (@y) SELECT * FROM q"))), 1)