|
4 | 4 |
|
5 | 5 | import re |
6 | 6 | import tracemalloc |
| 7 | +from unittest import mock |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 |
|
|
32 | 33 | StringType, |
33 | 34 | TimestampType, |
34 | 35 | ) |
| 36 | +import snowflake.snowpark.context as context |
35 | 37 | from tests.integ.scala.test_dataframe_reader_suite import get_reader |
36 | 38 | from tests.integ.utils.sql_counter import SqlCounter, sql_count_checker |
37 | 39 | from tests.utils import IS_IN_STORED_PROC_LOCALFS, TestFiles, Utils |
@@ -1313,3 +1315,40 @@ def test_table_select_cte(session): |
1313 | 1315 | union_count=1, |
1314 | 1316 | join_count=0, |
1315 | 1317 | ) |
| 1318 | + |
| 1319 | + |
| 1320 | +@pytest.mark.parametrize( |
| 1321 | + "reduce_describe_enabled,expected_describe_counts", |
| 1322 | + [ |
| 1323 | + (True, [1, 0]), # With caching: first call misses, second call hits cache |
| 1324 | + (False, [1, 1]), # Without caching: both calls issue describe queries |
| 1325 | + ], |
| 1326 | +) |
| 1327 | +def test_dataframe_queries_with_cte_reuses_schema_cache( |
| 1328 | + session, reduce_describe_enabled, expected_describe_counts |
| 1329 | +): |
| 1330 | + """Test that calling dataframe.queries (not same dataframe but same operation) multiple times with CTE optimization |
| 1331 | + does not issue extra DESCRIBE queries when reduce_describe_query_enabled is True. |
| 1332 | +
|
| 1333 | + This tests the deterministic CTE naming feature: when CTE optimization is enabled |
| 1334 | + and reduce_describe_query is enabled, repeated calls to df.queries should produce |
| 1335 | + identical SQL (with same CTE names), allowing the schema cache to hit. |
| 1336 | + """ |
| 1337 | + |
| 1338 | + def create_cte_dataframe(): |
| 1339 | + """Create a DataFrame that triggers CTE optimization (same df used twice).""" |
| 1340 | + df = session.create_dataframe([[1, 2], [3, 4]], schema=["a", "b"]) |
| 1341 | + return df.union_all(df) |
| 1342 | + |
| 1343 | + def access_queries_and_schema(df): |
| 1344 | + """Access both queries and schema properties.""" |
| 1345 | + _ = df.queries |
| 1346 | + _ = df.schema |
| 1347 | + |
| 1348 | + with mock.patch.object( |
| 1349 | + session, "_reduce_describe_query_enabled", reduce_describe_enabled |
| 1350 | + ), mock.patch.object(context, "_is_snowpark_connect_compatible_mode", True): |
| 1351 | + for expected_describe_count in expected_describe_counts: |
| 1352 | + df_union = create_cte_dataframe() |
| 1353 | + with SqlCounter(query_count=0, describe_count=expected_describe_count): |
| 1354 | + access_queries_and_schema(df_union) |
0 commit comments