-
Notifications
You must be signed in to change notification settings - Fork 113
feat: Python Support for Large Binary #4100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
99d324a
Add support for Python
kunwp1 7333dae
Fix format
kunwp1 47692b7
Address comments
kunwp1 6a5cc33
Add support for Python
kunwp1 e42c10d
Fix format
kunwp1 d35253f
Address comments
kunwp1 830d983
Rename
kunwp1 ed92f98
flake8
kunwp1 aab7329
Merge branch 'main' into chris-big-object-python
kunwp1 b8e37ae
Rename to Large Binary
kunwp1 eb4e1c0
Format files
kunwp1 a0a4d1c
Address comments
kunwp1 3ed7fb6
Address comments
kunwp1 3a64dd1
Format files
kunwp1 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 hidden or 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 |
|---|---|---|
|
|
@@ -23,3 +23,4 @@ pybase64==1.3.2 | |
| torch==2.8.0 | ||
| scikit-learn==1.5.0 | ||
| transformers==4.57.3 | ||
| boto3==1.40.53 | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
63 changes: 63 additions & 0 deletions
63
amber/src/main/python/core/models/schema/arrow_schema_utils.py
This file contains hidden or 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,63 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| Utilities for converting between Arrow schemas and Amber schemas, | ||
| handling LARGE_BINARY metadata preservation. | ||
| """ | ||
|
|
||
| import pyarrow as pa | ||
| from typing import Mapping | ||
|
|
||
| from core.models.schema.attribute_type import AttributeType | ||
| from core.models.schema.attribute_type_utils import ( | ||
| detect_attribute_type_from_arrow_field, | ||
| create_arrow_field_with_metadata, | ||
| ) | ||
|
|
||
|
|
||
| def arrow_schema_to_attr_types(arrow_schema: pa.Schema) -> dict[str, AttributeType]: | ||
| """ | ||
| Converts an Arrow schema to a dictionary of attribute name to AttributeType. | ||
| Handles LARGE_BINARY metadata detection. | ||
|
|
||
| :param arrow_schema: PyArrow schema that may contain LARGE_BINARY metadata | ||
| :return: Dictionary mapping attribute names to AttributeTypes | ||
| """ | ||
| attr_types = {} | ||
| for attr_name in arrow_schema.names: | ||
| field = arrow_schema.field(attr_name) | ||
| attr_types[attr_name] = detect_attribute_type_from_arrow_field(field) | ||
| return attr_types | ||
|
|
||
|
|
||
| def attr_types_to_arrow_schema( | ||
| attr_types: Mapping[str, AttributeType], | ||
| ) -> pa.Schema: | ||
| """ | ||
| Converts a mapping of attribute name to AttributeType into an Arrow schema. | ||
| Adds metadata for LARGE_BINARY types. | ||
| Preserves the order of attributes from the input mapping. | ||
|
|
||
| :param attr_types: Mapping of attribute names to AttributeTypes (e.g., OrderedDict) | ||
| :return: PyArrow schema with metadata for LARGE_BINARY types | ||
| """ | ||
| fields = [ | ||
| create_arrow_field_with_metadata(attr_name, attr_type) | ||
| for attr_name, attr_type in attr_types.items() | ||
| ] | ||
| return pa.schema(fields) |
This file contains hidden or 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
72 changes: 72 additions & 0 deletions
72
amber/src/main/python/core/models/schema/attribute_type_utils.py
This file contains hidden or 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,72 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| Utilities for converting between AttributeTypes and Arrow field types, | ||
| handling LARGE_BINARY metadata preservation. | ||
| """ | ||
|
|
||
| import pyarrow as pa | ||
|
|
||
| from core.models.schema.attribute_type import ( | ||
| AttributeType, | ||
| FROM_ARROW_MAPPING, | ||
| TO_ARROW_MAPPING, | ||
| ) | ||
|
|
||
| # Metadata key used to mark LARGE_BINARY fields in Arrow schemas | ||
| TEXERA_TYPE_METADATA_KEY = b"texera_type" | ||
| LARGE_BINARY_METADATA_VALUE = b"LARGE_BINARY" | ||
|
|
||
|
|
||
| def detect_attribute_type_from_arrow_field(field: pa.Field) -> AttributeType: | ||
| """ | ||
| Detects the AttributeType from an Arrow field, checking metadata for LARGE_BINARY. | ||
|
|
||
| :param field: PyArrow field that may contain metadata | ||
| :return: The detected AttributeType | ||
| """ | ||
| # Check metadata for LARGE_BINARY type | ||
| # (can be stored by either Scala ArrowUtils or Python) | ||
| is_large_binary = ( | ||
| field.metadata | ||
| and field.metadata.get(TEXERA_TYPE_METADATA_KEY) == LARGE_BINARY_METADATA_VALUE | ||
| ) | ||
|
|
||
| if is_large_binary: | ||
| return AttributeType.LARGE_BINARY | ||
| else: | ||
| return FROM_ARROW_MAPPING[field.type.id] | ||
|
|
||
|
|
||
| def create_arrow_field_with_metadata( | ||
| attr_name: str, attr_type: AttributeType | ||
| ) -> pa.Field: | ||
| """ | ||
| Creates a PyArrow field with appropriate metadata for the given AttributeType. | ||
|
|
||
| :param attr_name: Name of the attribute | ||
| :param attr_type: The AttributeType | ||
| :return: PyArrow field with metadata if needed | ||
| """ | ||
| metadata = ( | ||
| {TEXERA_TYPE_METADATA_KEY: LARGE_BINARY_METADATA_VALUE} | ||
| if attr_type == AttributeType.LARGE_BINARY | ||
| else None | ||
| ) | ||
|
|
||
| return pa.field(attr_name, TO_ARROW_MAPPING[attr_type], metadata=metadata) |
This file contains hidden or 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
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.