Skip to content

Commit 5cf754d

Browse files
committed
fix unit tests
1 parent fb3cdda commit 5cf754d

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

bigframes/core/backports.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
"""Helpers for working across versions of different depenencies."""
16+
17+
from typing import List
18+
19+
import pyarrow
20+
21+
22+
def pyarrow_struct_type_fields(struct_type: pyarrow.StructType) -> List[pyarrow.Field]:
23+
"""StructType.fields was added in pyarrow 18.
24+
25+
See: https://arrow.apache.org/docs/18.0/python/generated/pyarrow.StructType.html
26+
"""
27+
28+
if hasattr(struct_type, "fields"):
29+
return struct_type.fields
30+
31+
return [
32+
struct_type.field(field_index) for field_index in range(struct_type.num_fields)
33+
]

bigframes/dtypes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import pyarrow as pa
3232
import shapely.geometry # type: ignore
3333

34+
import bigframes.core.backports
3435
import bigframes.exceptions
3536

3637
# Type hints for Pandas dtypes supported by BigQuery DataFrame
@@ -372,8 +373,7 @@ def get_struct_fields(type_: ExpressionType) -> dict[str, Dtype]:
372373
assert isinstance(type_.pyarrow_dtype, pa.StructType)
373374
struct_type = type_.pyarrow_dtype
374375
result: dict[str, Dtype] = {}
375-
for field_no in range(struct_type.num_fields):
376-
field = struct_type.field(field_no)
376+
for field in bigframes.core.backports.pyarrow_struct_type_fields(struct_type):
377377
result[field.name] = arrow_dtype_to_bigframes_dtype(field.type)
378378
return result
379379

@@ -551,7 +551,8 @@ def arrow_type_to_literal(
551551
return [arrow_type_to_literal(arrow_type.value_type)]
552552
if pa.types.is_struct(arrow_type):
553553
return {
554-
field.name: arrow_type_to_literal(field.type) for field in arrow_type.fields
554+
field.name: arrow_type_to_literal(field.type)
555+
for field in bigframes.core.backports.pyarrow_struct_type_fields(arrow_type)
555556
}
556557
if pa.types.is_string(arrow_type):
557558
return "string"
@@ -930,7 +931,8 @@ def contains_db_dtypes_json_arrow_type(type_):
930931

931932
if isinstance(type_, pa.StructType):
932933
return any(
933-
contains_db_dtypes_json_arrow_type(field.type) for field in type_.fields
934+
contains_db_dtypes_json_arrow_type(field.type)
935+
for field in bigframes.core.backports.pyarrow_struct_type_fields(type_)
934936
)
935937
return False
936938

0 commit comments

Comments
 (0)