-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support json_[string][pyarrow]
dtype and make pandas-gbq dtypes more independent from google-cloud-bigquery logic
#893
Open
tswast
wants to merge
4
commits into
main
Choose a base branch
from
b401630655-json-dtype
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+906
−14
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c57082
feat: make pandas-gbq dtypes more independent from google-cloud-bigqu…
tswast 8dcabdb
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 3a88427
temporarily avoid pyarrow.json_
tswast 4882483
Merge remote-tracking branch 'origin/b401630655-json-dtype' into b401…
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Copyright (c) 2025 pandas-gbq Authors All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
# Copyright (c) 2025 pandas-gbq Authors All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
from typing import Any, Union | ||
import warnings | ||
|
||
import db_dtypes | ||
from google.cloud import bigquery | ||
import pyarrow | ||
|
||
|
||
def pyarrow_datetime(): | ||
return pyarrow.timestamp("us", tz=None) | ||
|
||
|
||
def pyarrow_numeric(): | ||
return pyarrow.decimal128(38, 9) | ||
|
||
|
||
def pyarrow_bignumeric(): | ||
# 77th digit is partial. | ||
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types | ||
return pyarrow.decimal256(76, 38) | ||
|
||
|
||
def pyarrow_time(): | ||
return pyarrow.time64("us") | ||
|
||
|
||
def pyarrow_timestamp(): | ||
return pyarrow.timestamp("us", tz="UTC") | ||
|
||
|
||
# Prefer JSON type built-in to pyarrow (adding in 19.0.0), if available. | ||
# Otherwise, fallback to db-dtypes, where the JSONArrowType was added in 1.4.0, | ||
# but since they might have an older db-dtypes, have string as a fallback for that. | ||
if hasattr(pyarrow, "json_"): | ||
json_arrow_type = pyarrow.json_(pyarrow.string()) | ||
elif hasattr(db_dtypes, "JSONArrowType"): | ||
json_arrow_type = db_dtypes.JSONArrowType() | ||
else: | ||
json_arrow_type = pyarrow.string() | ||
|
||
|
||
# This dictionary is duplicated in bigquery_storage/test/unite/test_reader.py | ||
# When modifying it be sure to update it there as well. | ||
# Note(todo!!): type "BIGNUMERIC"'s matching pyarrow type is added in _pandas_helpers.py | ||
_BQ_TO_ARROW_SCALARS = { | ||
"BIGNUMERIC": pyarrow_bignumeric, | ||
"BOOL": pyarrow.bool_, | ||
"BOOLEAN": pyarrow.bool_, | ||
"BYTES": pyarrow.binary, | ||
"DATE": pyarrow.date32, | ||
"DATETIME": pyarrow_datetime, | ||
"FLOAT": pyarrow.float64, | ||
"FLOAT64": pyarrow.float64, | ||
"GEOGRAPHY": pyarrow.string, | ||
"INT64": pyarrow.int64, | ||
"INTEGER": pyarrow.int64, | ||
"JSON": json_arrow_type, | ||
"NUMERIC": pyarrow_numeric, | ||
"STRING": pyarrow.string, | ||
"TIME": pyarrow_time, | ||
"TIMESTAMP": pyarrow_timestamp, | ||
} | ||
|
||
_STRUCT_TYPES = ("RECORD", "STRUCT") | ||
|
||
|
||
def bq_to_arrow_scalars(bq_scalar: str): | ||
""" | ||
Returns: | ||
The Arrow scalar type that the input BigQuery scalar type maps to. | ||
If it cannot find the BigQuery scalar, return None. | ||
""" | ||
return _BQ_TO_ARROW_SCALARS.get(bq_scalar) | ||
|
||
|
||
BQ_FIELD_TYPE_TO_ARROW_FIELD_METADATA = { | ||
"GEOGRAPHY": { | ||
b"ARROW:extension:name": b"google:sqlType:geography", | ||
b"ARROW:extension:metadata": b'{"encoding": "WKT"}', | ||
}, | ||
"DATETIME": {b"ARROW:extension:name": b"google:sqlType:datetime"}, | ||
"JSON": {b"ARROW:extension:name": b"google:sqlType:json"}, | ||
} | ||
|
||
|
||
def bq_to_arrow_struct_data_type(field): | ||
arrow_fields = [] | ||
for subfield in field.fields: | ||
arrow_subfield = bq_to_arrow_field(subfield) | ||
if arrow_subfield: | ||
arrow_fields.append(arrow_subfield) | ||
else: | ||
# Could not determine a subfield type. Fallback to type | ||
# inference. | ||
return None | ||
return pyarrow.struct(arrow_fields) | ||
|
||
|
||
def bq_to_arrow_range_data_type(field): | ||
if field is None: | ||
raise ValueError( | ||
"Range element type cannot be None, must be one of " | ||
"DATE, DATETIME, or TIMESTAMP" | ||
) | ||
element_type = field.element_type.upper() | ||
arrow_element_type = bq_to_arrow_scalars(element_type)() | ||
return pyarrow.struct([("start", arrow_element_type), ("end", arrow_element_type)]) | ||
|
||
|
||
def bq_to_arrow_data_type(field): | ||
"""Return the Arrow data type, corresponding to a given BigQuery column. | ||
|
||
Returns: | ||
None: if default Arrow type inspection should be used. | ||
""" | ||
if field.mode is not None and field.mode.upper() == "REPEATED": | ||
inner_type = bq_to_arrow_data_type( | ||
bigquery.SchemaField(field.name, field.field_type, fields=field.fields) | ||
) | ||
if inner_type: | ||
return pyarrow.list_(inner_type) | ||
return None | ||
|
||
field_type_upper = field.field_type.upper() if field.field_type else "" | ||
if field_type_upper in _STRUCT_TYPES: | ||
return bq_to_arrow_struct_data_type(field) | ||
|
||
if field_type_upper == "RANGE": | ||
return bq_to_arrow_range_data_type(field.range_element_type) | ||
|
||
data_type_constructor = bq_to_arrow_scalars(field_type_upper) | ||
if data_type_constructor is None: | ||
return None | ||
return data_type_constructor() | ||
|
||
|
||
def bq_to_arrow_field(bq_field, array_type=None): | ||
"""Return the Arrow field, corresponding to a given BigQuery column. | ||
|
||
Returns: | ||
None: if the Arrow type cannot be determined. | ||
""" | ||
arrow_type = bq_to_arrow_data_type(bq_field) | ||
if arrow_type is not None: | ||
if array_type is not None: | ||
arrow_type = array_type # For GEOGRAPHY, at least initially | ||
metadata = BQ_FIELD_TYPE_TO_ARROW_FIELD_METADATA.get( | ||
bq_field.field_type.upper() if bq_field.field_type else "" | ||
) | ||
return pyarrow.field( | ||
bq_field.name, | ||
arrow_type, | ||
# Even if the remote schema is REQUIRED, there's a chance there's | ||
# local NULL values. Arrow will gladly interpret these NULL values | ||
# as non-NULL and give you an arbitrary value. See: | ||
# https://github.com/googleapis/python-bigquery/issues/1692 | ||
nullable=False if bq_field.mode.upper() == "REPEATED" else True, | ||
metadata=metadata, | ||
) | ||
|
||
warnings.warn( | ||
"Unable to determine Arrow type for field '{}'.".format(bq_field.name) | ||
) | ||
return None | ||
|
||
|
||
def bq_to_arrow_schema(bq_schema): | ||
"""Return the Arrow schema, corresponding to a given BigQuery schema. | ||
|
||
Returns: | ||
None: if any Arrow type cannot be determined. | ||
""" | ||
arrow_fields = [] | ||
for bq_field in bq_schema: | ||
arrow_field = bq_to_arrow_field(bq_field) | ||
if arrow_field is None: | ||
# Auto-detect the schema if there is an unknown field type. | ||
return None | ||
arrow_fields.append(arrow_field) | ||
return pyarrow.schema(arrow_fields) | ||
|
||
|
||
def default_types_mapper( | ||
date_as_object: bool = False, | ||
bool_dtype: Union[Any, None] = None, | ||
int_dtype: Union[Any, None] = None, | ||
float_dtype: Union[Any, None] = None, | ||
string_dtype: Union[Any, None] = None, | ||
date_dtype: Union[Any, None] = None, | ||
datetime_dtype: Union[Any, None] = None, | ||
time_dtype: Union[Any, None] = None, | ||
timestamp_dtype: Union[Any, None] = None, | ||
range_date_dtype: Union[Any, None] = None, | ||
range_datetime_dtype: Union[Any, None] = None, | ||
range_timestamp_dtype: Union[Any, None] = None, | ||
): | ||
"""Create a mapping from pyarrow types to pandas types. | ||
|
||
This overrides the pandas defaults to use null-safe extension types where | ||
available. | ||
|
||
See: https://arrow.apache.org/docs/python/api/datatypes.html for a list of | ||
data types. See: | ||
tests/unit/test__pandas_helpers.py::test_bq_to_arrow_data_type for | ||
BigQuery to Arrow type mapping. | ||
|
||
Note to google-cloud-bigquery developers: If you update the default dtypes, | ||
also update the docs at docs/usage/pandas.rst. | ||
""" | ||
|
||
def types_mapper(arrow_data_type): | ||
if bool_dtype is not None and pyarrow.types.is_boolean(arrow_data_type): | ||
return bool_dtype | ||
|
||
elif int_dtype is not None and pyarrow.types.is_integer(arrow_data_type): | ||
return int_dtype | ||
|
||
elif float_dtype is not None and pyarrow.types.is_floating(arrow_data_type): | ||
return float_dtype | ||
|
||
elif string_dtype is not None and pyarrow.types.is_string(arrow_data_type): | ||
return string_dtype | ||
|
||
elif ( | ||
# If date_as_object is True, we know some DATE columns are | ||
# out-of-bounds of what is supported by pandas. | ||
date_dtype is not None | ||
and not date_as_object | ||
and pyarrow.types.is_date(arrow_data_type) | ||
): | ||
return date_dtype | ||
|
||
elif ( | ||
datetime_dtype is not None | ||
and pyarrow.types.is_timestamp(arrow_data_type) | ||
and arrow_data_type.tz is None | ||
): | ||
return datetime_dtype | ||
|
||
elif ( | ||
timestamp_dtype is not None | ||
and pyarrow.types.is_timestamp(arrow_data_type) | ||
and arrow_data_type.tz is not None | ||
): | ||
return timestamp_dtype | ||
|
||
elif time_dtype is not None and pyarrow.types.is_time(arrow_data_type): | ||
return time_dtype | ||
|
||
elif pyarrow.types.is_struct(arrow_data_type): | ||
if range_datetime_dtype is not None and arrow_data_type.equals( | ||
range_datetime_dtype.pyarrow_dtype | ||
): | ||
return range_datetime_dtype | ||
|
||
elif range_date_dtype is not None and arrow_data_type.equals( | ||
range_date_dtype.pyarrow_dtype | ||
): | ||
return range_date_dtype | ||
|
||
# TODO: this section does not have a test yet OR at least not one that is | ||
# recognized by coverage, hence the pragma. See Issue: #2132 | ||
elif ( | ||
range_timestamp_dtype is not None | ||
and arrow_data_type.equals( # pragma: NO COVER | ||
range_timestamp_dtype.pyarrow_dtype | ||
) | ||
): | ||
return range_timestamp_dtype | ||
|
||
return types_mapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a change of heart in googleapis/python-bigquery#1876 For
to_arrow()
, we should emulate the BQ Storage Read API as closely as possible.For
read_gbq()
, that's where I'd like to use the extension type(s) if available.