|
| 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 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import pathlib |
| 18 | +from typing import Generator, TYPE_CHECKING |
| 19 | + |
| 20 | +import pandas as pd |
| 21 | +import pandas.testing |
| 22 | +import pyarrow as pa # type: ignore |
| 23 | +import pytest |
| 24 | + |
| 25 | +import bigframes |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from bigframes.testing import polars_session |
| 29 | + |
| 30 | +pytest.importorskip("polars") |
| 31 | +pytest.importorskip("pandas", minversion="2.2.0") |
| 32 | + |
| 33 | +CURRENT_DIR = pathlib.Path(__file__).parent |
| 34 | +DATA_DIR = CURRENT_DIR.parent / "data" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(scope="module", autouse=True) |
| 38 | +def session() -> Generator[bigframes.Session, None, None]: |
| 39 | + import bigframes.core.global_session |
| 40 | + from bigframes.testing import polars_session |
| 41 | + |
| 42 | + session = polars_session.TestSession() |
| 43 | + with bigframes.core.global_session._GlobalSessionContext(session): |
| 44 | + yield session |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def struct_df(session: polars_session.TestSession): |
| 49 | + pa_type = pa.struct( |
| 50 | + [ |
| 51 | + ("str_field", pa.string()), |
| 52 | + ("int_field", pa.int64()), |
| 53 | + ] |
| 54 | + ) |
| 55 | + return session.DataFrame( |
| 56 | + { |
| 57 | + "struct_col": pd.Series( |
| 58 | + pa.array( |
| 59 | + [ |
| 60 | + { |
| 61 | + "str_field": "my string", |
| 62 | + "int_field": 1, |
| 63 | + }, |
| 64 | + { |
| 65 | + "str_field": None, |
| 66 | + "int_field": 2, |
| 67 | + }, |
| 68 | + { |
| 69 | + "str_field": "another string", |
| 70 | + "int_field": None, |
| 71 | + }, |
| 72 | + { |
| 73 | + "str_field": "some string", |
| 74 | + "int_field": 3, |
| 75 | + }, |
| 76 | + ], |
| 77 | + pa_type, |
| 78 | + ), |
| 79 | + dtype=pd.ArrowDtype(pa_type), |
| 80 | + ), |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture |
| 86 | +def struct_series(struct_df): |
| 87 | + return struct_df["struct_col"] |
| 88 | + |
| 89 | + |
| 90 | +def test_struct_dtypes(struct_series): |
| 91 | + bf_series = struct_series |
| 92 | + pd_series = struct_series.to_pandas() |
| 93 | + assert isinstance(pd_series.dtype, pd.ArrowDtype) |
| 94 | + |
| 95 | + bf_result = bf_series.struct.dtypes |
| 96 | + pd_result = pd_series.struct.dtypes |
| 97 | + |
| 98 | + pandas.testing.assert_series_equal(bf_result, pd_result) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + ("field_name", "common_dtype"), |
| 103 | + ( |
| 104 | + ("str_field", "string[pyarrow]"), |
| 105 | + ("int_field", "int64[pyarrow]"), |
| 106 | + # TODO(tswast): Support referencing fields by number, too. |
| 107 | + ), |
| 108 | +) |
| 109 | +def test_struct_field(struct_series, field_name, common_dtype): |
| 110 | + bf_series = struct_series |
| 111 | + pd_series = struct_series.to_pandas() |
| 112 | + assert isinstance(pd_series.dtype, pd.ArrowDtype) |
| 113 | + |
| 114 | + bf_result = bf_series.struct.field(field_name).to_pandas() |
| 115 | + pd_result = pd_series.struct.field(field_name) |
| 116 | + |
| 117 | + # TODO(tswast): if/when we support arrowdtype for int/string, we can remove |
| 118 | + # this cast. |
| 119 | + bf_result = bf_result.astype(common_dtype) |
| 120 | + pd_result = pd_result.astype(common_dtype) |
| 121 | + |
| 122 | + pandas.testing.assert_series_equal(bf_result, pd_result) |
| 123 | + |
| 124 | + |
| 125 | +def test_struct_explode(struct_series): |
| 126 | + bf_series = struct_series |
| 127 | + pd_series = struct_series.to_pandas() |
| 128 | + assert isinstance(pd_series.dtype, pd.ArrowDtype) |
| 129 | + |
| 130 | + bf_result = bf_series.struct.explode().to_pandas() |
| 131 | + pd_result = pd_series.struct.explode() |
| 132 | + |
| 133 | + pandas.testing.assert_frame_equal( |
| 134 | + bf_result, |
| 135 | + pd_result, |
| 136 | + # TODO(tswast): remove if/when we support arrowdtype for int/string. |
| 137 | + check_dtype=False, |
| 138 | + ) |
0 commit comments