Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 325
feat: support RANGE in queries Part 2: Arrow#1868
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
5dd6b2474fb1d3a67e1aa75a985553635bc5dfd65e73a50016a735cad54336a8dc4ae51b2d68f6f93d8e005d409839eafe58a0e18cc12e1b691710c6d5ce1b3ddfbf8b7c42eaf54a1d7b716f98c46c65cb8401d24b96ee82b7095d790b3d10be9fb6b7f3779edc8b5c2a0d518a0d01f72c9782f40afa27203e0c0bb17b3be58739ac3db3c92211dd0e2a95520357b6f4c20bd7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -142,6 +142,17 @@ def bq_to_arrow_struct_data_type(field): | ||
| 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 = _pyarrow_helpers.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. | ||
| @@ -160,6 +171,9 @@ def bq_to_arrow_data_type(field): | ||
| if field_type_upper in schema._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 = _pyarrow_helpers.bq_to_arrow_scalars(field_type_upper) | ||
| if data_type_constructor is None: | ||
| return None | ||
| @@ -220,6 +234,9 @@ def default_types_mapper( | ||
| 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. | ||
| @@ -274,6 +291,22 @@ def types_mapper(arrow_data_type): | ||
| 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): | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to handle structs more generally here, or is that logic elsewhere? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! Indeed, our types mapper function doesn't seem to do any conversion for STRUCT or ARRAY. This function is used as the parameter | ||
| 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 | ||
| elif range_timestamp_dtype is not None and arrow_data_type.equals( | ||
| range_timestamp_dtype.pyarrow_dtype | ||
| ): | ||
| return range_timestamp_dtype | ||
| return types_mapper | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
do we need to do validation here? None-check?
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.
Great point, I will add a None-check here
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 added it, as well as the unit tests in
test__pandas_helpers.py.