Repository navigation
feat(rest): preserve column bounds and key metadata during REST scan planning - #4544
SurenderguliaEnginner wants to merge 2 commits into
Conversation
…planning Signed-off-by: Surender Gulia <shriomgulia5@gmail.com>
Signed-off-by: Surender Gulia <shriomgulia5@gmail.com>
eddc546 to
c61e652
Compare
rahulsmahadev
left a comment
There was a problem hiding this comment.
Thanks @SurenderguliaEnginner! The new test fails on main and passes here.
I'd recommend decoding the bounds and key-metadata as hex, which is how Java's ContentFileParser writes them: bounds as a map of field id to binary, key metadata as a BinaryTypeValue (TestContentFileParser pins "values":["01000000","02000000"], as in #2792). With that JSON, scans that work on main fail here:
rest = {"spec-id": 0, "content": "data", "file-path": "s3://b/f.parquet", "file-format": "parquet",
"file-size-in-bytes": 1, "record-count": 1, "lower-bounds": {"keys": [1], "values": ["01000000"]}}
task = RESTFileScanTask.model_validate({"data-file": rest})
FileScanTask.from_rest_response(task, [], table_schema=Schema(NestedField(1, "id", IntegerType())))
# struct.error: required argument is not an integer; plan_files(), to_arrow() and count() raise the same
# "key-metadata": "ABCDEF" raises binascii.Error: Incorrect padding; 32 hex zeros decode to 24 other bytesHex needs no schema, so the table_schema threading, the load_table fallback (an extra loadTable on every plan_scan()) and the ValueError for a bound on a dropped column would go away.
A couple of smaller suggestions:
_plan_scan_resultno longer checksisinstance(response, PlanCompleted), which seems unrelated here.- The new test pins typed values and base64; Java's JSON through
table.scan().plan_files()would pin the real format and the catalog path.
| elif schema is not None: | ||
| field = schema.find_field(field_id) | ||
| if field is not None and isinstance(field.field_type, PrimitiveType): | ||
| serialized[field_id] = to_bytes(field.field_type, val) |
There was a problem hiding this comment.
One detail for the hex version: RestCatalog parses responses with validate_json, where pydantic turns a 32-digit hex value into a UUID, so 16-byte bounds need a str-valued map. With KeyValueMap[str] for lower_bounds/upper_bounds and {field_id: bytes.fromhex(value) for ...}, a scratch copy got the bytes pyiceberg writes in manifests for all 14 primitive types, including uuid, decimal and fixed[16].
| lower_bounds = None | ||
| upper_bounds = None | ||
|
|
||
| key_metadata = base64.b64decode(rest_file.key_metadata, validate=True) if rest_file.key_metadata else None |
There was a problem hiding this comment.
The REST spec types key-metadata as a BinaryTypeValue (uppercase hex); the base64 in table/metadata.py is for the table metadata's encrypted-key-metadata. Something like (and drop the base64 import):
| key_metadata = base64.b64decode(rest_file.key_metadata, validate=True) if rest_file.key_metadata else None | |
| key_metadata = bytes.fromhex(rest_file.key_metadata) if rest_file.key_metadata else None |
Description
When a REST catalog plans a scan server-side, the server can return per-column lower and upper bounds (
lower-bounds,upper-bounds) and encryption key metadata (key-metadata) on each data file. Previously,_rest_file_to_data_fileinpyiceberg/table/__init__.pydid not pass these fields toDataFile.from_args(), causing the convertedDataFileto always drop column bounds and key metadata.This caused two main issues:
Changes:
pyiceberg/table/__init__.py:_serialize_boundshelper usingpyiceberg.conversions.to_bytesand field types fromtable_schema.key_metadatafrom base64 string to bytes viabase64.b64decode(validate=True)(consistent withtable/metadata.py).lower_bounds,upper_bounds, andkey_metadatatoDataFile.from_args().table_schemathroughFileScanTask.from_rest_responseandDataScan._plan_files_server_side.pyiceberg/catalog/rest/__init__.py:table_schemathrough_plan_scan_resultand_expand_plan_tasksintoFileScanTask.from_rest_response.tests/catalog/test_scan_planning_models.py:test_rest_task_preserves_bounds_and_key_metadatato verify bounds serialization and key metadata decoding.Closes #4528
Tests
pytest tests/catalog/test_scan_planning_models.py(63 passed)