|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import pytest |
| 15 | + |
| 16 | +import bigframes |
| 17 | +from bigframes.testing.utils import assert_pandas_df_equal |
| 18 | + |
| 19 | +polars = pytest.importorskip("polars", reason="polars is required for this test") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def session_w_polars(): |
| 24 | + context = bigframes.BigQueryOptions(location="US", enable_polars_execution=True) |
| 25 | + session = bigframes.Session(context=context) |
| 26 | + yield session |
| 27 | + session.close() # close generated session at cleanup time |
| 28 | + |
| 29 | + |
| 30 | +def test_polar_execution_sorted(session_w_polars, scalars_pandas_df_index): |
| 31 | + execution_count_before = session_w_polars._metrics.execution_count |
| 32 | + bf_df = session_w_polars.read_pandas(scalars_pandas_df_index) |
| 33 | + |
| 34 | + pd_result = scalars_pandas_df_index.sort_index(ascending=False)[ |
| 35 | + ["int64_too", "bool_col"] |
| 36 | + ] |
| 37 | + bf_result = bf_df.sort_index(ascending=False)[["int64_too", "bool_col"]].to_pandas() |
| 38 | + |
| 39 | + assert session_w_polars._metrics.execution_count == execution_count_before |
| 40 | + assert_pandas_df_equal(bf_result, pd_result) |
| 41 | + |
| 42 | + |
| 43 | +def test_polar_execution_sorted_filtered(session_w_polars, scalars_pandas_df_index): |
| 44 | + execution_count_before = session_w_polars._metrics.execution_count |
| 45 | + bf_df = session_w_polars.read_pandas(scalars_pandas_df_index) |
| 46 | + |
| 47 | + pd_result = scalars_pandas_df_index.sort_index(ascending=False).dropna( |
| 48 | + subset=["int64_col", "string_col"] |
| 49 | + ) |
| 50 | + bf_result = ( |
| 51 | + bf_df.sort_index(ascending=False) |
| 52 | + .dropna(subset=["int64_col", "string_col"]) |
| 53 | + .to_pandas() |
| 54 | + ) |
| 55 | + |
| 56 | + # Filter and isnull not supported by polar engine yet, so falls back to bq execution |
| 57 | + assert session_w_polars._metrics.execution_count == (execution_count_before + 1) |
| 58 | + assert_pandas_df_equal(bf_result, pd_result) |
| 59 | + |
| 60 | + |
| 61 | +def test_polar_execution_unsupported_sql_fallback( |
| 62 | + session_w_polars, scalars_pandas_df_index |
| 63 | +): |
| 64 | + execution_count_before = session_w_polars._metrics.execution_count |
| 65 | + bf_df = session_w_polars.read_pandas(scalars_pandas_df_index) |
| 66 | + |
| 67 | + pd_df = scalars_pandas_df_index.copy() |
| 68 | + pd_df["str_len_col"] = pd_df.string_col.str.len() |
| 69 | + pd_result = pd_df |
| 70 | + |
| 71 | + bf_df["str_len_col"] = bf_df.string_col.str.len() |
| 72 | + bf_result = bf_df.to_pandas() |
| 73 | + |
| 74 | + # str len not supported by polar engine yet, so falls back to bq execution |
| 75 | + assert session_w_polars._metrics.execution_count == (execution_count_before + 1) |
| 76 | + assert_pandas_df_equal(bf_result, pd_result) |
0 commit comments