|
| 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 | +import datetime |
| 16 | + |
| 17 | +import pyarrow as pa |
| 18 | +import pytest |
| 19 | + |
| 20 | +import bigframes.pandas as bpd |
| 21 | +from bigframes.testing import mocks |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module") |
| 25 | +def session(): |
| 26 | + # Use the mock session from bigframes.testing |
| 27 | + return mocks.create_bigquery_session() |
| 28 | + |
| 29 | + |
| 30 | +def test_read_arrow_empty_table(session): |
| 31 | + empty_table = pa.Table.from_pydict( |
| 32 | + { |
| 33 | + "col_a": pa.array([], type=pa.int64()), |
| 34 | + "col_b": pa.array([], type=pa.string()), |
| 35 | + } |
| 36 | + ) |
| 37 | + df = session.read_arrow(empty_table) |
| 38 | + assert isinstance(df, bpd.DataFrame) |
| 39 | + assert df.shape == (0, 2) |
| 40 | + assert list(df.columns) == ["col_a", "col_b"] |
| 41 | + pd_df = df.to_pandas() |
| 42 | + assert pd_df.empty |
| 43 | + assert list(pd_df.columns) == ["col_a", "col_b"] |
| 44 | + assert pd_df["col_a"].dtype == "Int64" |
| 45 | + assert pd_df["col_b"].dtype == "string[pyarrow]" |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.parametrize( |
| 49 | + "data,arrow_type,expected_bq_type_kind", |
| 50 | + [ |
| 51 | + ([1, 2], pa.int8(), "INTEGER"), |
| 52 | + ([1, 2], pa.int16(), "INTEGER"), |
| 53 | + ([1, 2], pa.int32(), "INTEGER"), |
| 54 | + ([1, 2], pa.int64(), "INTEGER"), |
| 55 | + ([1.0, 2.0], pa.float32(), "FLOAT"), |
| 56 | + ([1.0, 2.0], pa.float64(), "FLOAT"), |
| 57 | + ([True, False], pa.bool_(), "BOOLEAN"), |
| 58 | + (["a", "b"], pa.string(), "STRING"), |
| 59 | + (["a", "b"], pa.large_string(), "STRING"), |
| 60 | + ([b"a", b"b"], pa.binary(), "BYTES"), |
| 61 | + ([b"a", b"b"], pa.large_binary(), "BYTES"), |
| 62 | + ( |
| 63 | + [ |
| 64 | + pa.scalar(1000, type=pa.duration("s")), |
| 65 | + pa.scalar(2000, type=pa.duration("s")), |
| 66 | + ], |
| 67 | + pa.duration("s"), |
| 68 | + "INTEGER", |
| 69 | + ), |
| 70 | + ([datetime.date(2023, 1, 1)], pa.date32(), "DATE"), |
| 71 | + ( |
| 72 | + [datetime.datetime(2023, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)], |
| 73 | + pa.timestamp("s", tz="UTC"), |
| 74 | + "TIMESTAMP", |
| 75 | + ), |
| 76 | + ( |
| 77 | + [datetime.datetime(2023, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)], |
| 78 | + pa.timestamp("ms", tz="UTC"), |
| 79 | + "TIMESTAMP", |
| 80 | + ), |
| 81 | + ( |
| 82 | + [datetime.datetime(2023, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)], |
| 83 | + pa.timestamp("us", tz="UTC"), |
| 84 | + "TIMESTAMP", |
| 85 | + ), |
| 86 | + ([datetime.time(12, 34, 56, 789000)], pa.time64("us"), "TIME"), |
| 87 | + ], |
| 88 | +) |
| 89 | +def test_read_arrow_type_mappings(session, data, arrow_type, expected_bq_type_kind): |
| 90 | + """ |
| 91 | + Tests that various arrow types are mapped to the expected BigQuery types. |
| 92 | + This is an indirect check via the resulting DataFrame's schema. |
| 93 | + """ |
| 94 | + pa_table = pa.Table.from_arrays([pa.array(data, type=arrow_type)], names=["col"]) |
| 95 | + df = session.read_arrow(pa_table) |
| 96 | + |
| 97 | + bigquery_schema = df._block.expr.schema.to_bigquery() |
| 98 | + assert len(bigquery_schema) == 2 # offsets + value |
| 99 | + field = bigquery_schema[-1] |
| 100 | + assert field.field_type.upper() == expected_bq_type_kind |
| 101 | + |
| 102 | + # Also check pandas dtype after conversion for good measure |
| 103 | + pd_df = df.to_pandas() |
| 104 | + assert pd_df["col"].shape == (len(data),) |
| 105 | + |
| 106 | + |
| 107 | +def test_read_arrow_list_type(session): |
| 108 | + pa_table = pa.Table.from_arrays( |
| 109 | + [pa.array([[1, 2], [3, 4, 5]], type=pa.list_(pa.int64()))], names=["list_col"] |
| 110 | + ) |
| 111 | + df = session.read_arrow(pa_table) |
| 112 | + |
| 113 | + bigquery_schema = df._block.expr.schema.to_bigquery() |
| 114 | + assert len(bigquery_schema) == 2 # offsets + value |
| 115 | + field = bigquery_schema[-1] |
| 116 | + assert field.mode.upper() == "REPEATED" |
| 117 | + assert field.field_type.upper() == "INTEGER" |
| 118 | + |
| 119 | + |
| 120 | +def test_read_arrow_struct_type(session): |
| 121 | + struct_type = pa.struct([("a", pa.int64()), ("b", pa.string())]) |
| 122 | + pa_table = pa.Table.from_arrays( |
| 123 | + [pa.array([{"a": 1, "b": "x"}, {"a": 2, "b": "y"}], type=struct_type)], |
| 124 | + names=["struct_col"], |
| 125 | + ) |
| 126 | + df = session.read_arrow(pa_table) |
| 127 | + |
| 128 | + bigquery_schema = df._block.expr.schema.to_bigquery() |
| 129 | + assert len(bigquery_schema) == 2 # offsets + value |
| 130 | + field = bigquery_schema[-1] |
| 131 | + assert field.field_type.upper() == "RECORD" |
| 132 | + assert field.fields[0].name == "a" |
| 133 | + assert field.fields[1].name == "b" |
0 commit comments