|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved. |
| 4 | +# |
| 5 | + |
| 6 | +import pytest |
| 7 | +import snowflake.snowpark.context as context |
| 8 | +from copy import copy |
| 9 | +from unittest.mock import patch, Mock |
| 10 | + |
| 11 | + |
| 12 | +from snowflake.snowpark.functions import col, lit, max |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.skipif( |
| 16 | + "config.getoption('local_testing_mode', default=False)", |
| 17 | + reason="debug_mode not used in local testing mode", |
| 18 | +) |
| 19 | +@pytest.mark.parametrize("debug_mode", [True, False]) |
| 20 | +@pytest.mark.parametrize( |
| 21 | + "transform", |
| 22 | + [ |
| 23 | + pytest.param(lambda x: copy(x), id="copy"), |
| 24 | + pytest.param(lambda x: x.to_df(["C", "D"]), id="to_df"), |
| 25 | + pytest.param(lambda x: x.distinct(), id="distinct"), |
| 26 | + pytest.param(lambda x: x.drop_duplicates(), id="drop_duplicates"), |
| 27 | + pytest.param(lambda x: x.limit(1), id="limit"), |
| 28 | + pytest.param(lambda x: x.union(x), id="union"), |
| 29 | + pytest.param(lambda x: x.union_all(x), id="union_all"), |
| 30 | + pytest.param(lambda x: x.union_by_name(x), id="union_by_name"), |
| 31 | + pytest.param(lambda x: x.union_all_by_name(x), id="union_all_by_name"), |
| 32 | + pytest.param(lambda x: x.intersect(x), id="intersect"), |
| 33 | + pytest.param(lambda x: x.natural_join(x), id="natural_join"), |
| 34 | + pytest.param(lambda x: x.cross_join(x), id="cross_join"), |
| 35 | + pytest.param(lambda x: x.sample(n=1), id="sample"), |
| 36 | + pytest.param( |
| 37 | + lambda x: x.with_column_renamed(col("A"), "B"), id="with_column_renamed" |
| 38 | + ), |
| 39 | + # Unpivot already validates names |
| 40 | + pytest.param(lambda x: x.unpivot("x", "y", ["A"]), id="unpivot"), |
| 41 | + # The following functions do not error early because their schema_query do not contain |
| 42 | + # information about the transformation being called. |
| 43 | + pytest.param(lambda x: x.drop(col("A")), id="drop"), |
| 44 | + pytest.param(lambda x: x.filter(col("A") == lit(1)), id="filter"), |
| 45 | + pytest.param(lambda x: x.sort(col("A").desc()), id="sort"), |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_early_attributes(session, transform, debug_mode): |
| 49 | + with patch.object(context, "_debug_eager_schema_validation", debug_mode): |
| 50 | + df = session.create_dataframe([(1, "A"), (2, "B"), (3, "C")], ["A", "B"]) |
| 51 | + |
| 52 | + transformed = transform(df) |
| 53 | + |
| 54 | + # When debug mode is enabled the dataframe plan attributes are populated early |
| 55 | + if debug_mode: |
| 56 | + assert transformed._plan._metadata.attributes is not None |
| 57 | + else: |
| 58 | + assert transformed._plan._metadata.attributes is None |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.skipif( |
| 62 | + "config.getoption('local_testing_mode', default=False)", |
| 63 | + reason="debug_mode not used in local testing mode", |
| 64 | +) |
| 65 | +@pytest.mark.parametrize("debug_mode", [True, False]) |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "transform", |
| 68 | + [ |
| 69 | + pytest.param(lambda x: x.select("B"), id="select"), |
| 70 | + pytest.param(lambda x: x.select_expr("cast(b as str)"), id="select_expr"), |
| 71 | + pytest.param(lambda x: x.agg(max("B")), id="agg"), |
| 72 | + pytest.param(lambda x: x.join(copy(x), on=(col("A") == col("B"))), id="join"), |
| 73 | + pytest.param( |
| 74 | + lambda x: x.join_table_function("flatten", col("B")), |
| 75 | + id="join_table_function", |
| 76 | + ), |
| 77 | + pytest.param(lambda x: x.with_column("C", col("B")), id="with_column"), |
| 78 | + pytest.param(lambda x: x.with_columns(["C"], [col("B")]), id="with_columns"), |
| 79 | + ], |
| 80 | +) |
| 81 | +def test_early_error(session, transform, debug_mode): |
| 82 | + with patch.object(context, "_debug_eager_schema_validation", debug_mode): |
| 83 | + df = session.create_dataframe([1, 2, 3], ["A"]) |
| 84 | + |
| 85 | + show_mock = Mock() |
| 86 | + show_mock.__qualname__ = "show" |
| 87 | + show_mock.__name__ = "show" |
| 88 | + |
| 89 | + with patch("snowflake.snowpark.dataframe.DataFrame.show", show_mock): |
| 90 | + try: |
| 91 | + transformed = transform(df) |
| 92 | + transformed.show() |
| 93 | + except Exception: |
| 94 | + pass |
| 95 | + # When debug mode is enabled the error is thrown before reaching show. |
| 96 | + # Without debug mode the error only shows up once show is called. |
| 97 | + if debug_mode: |
| 98 | + show_mock.assert_not_called() |
| 99 | + assert df._plan._metadata.attributes is not None |
| 100 | + else: |
| 101 | + show_mock.assert_called() |
| 102 | + assert df._plan._metadata.attributes is None |
0 commit comments