Skip to content

feat(rest): preserve column bounds and key metadata during REST scan planning - #4544

Open
SurenderguliaEnginner wants to merge 2 commits into
apache:mainfrom
SurenderguliaEnginner:fix/4528-rest-scan-planning-metadata
Open

SurenderguliaEnginner wants to merge 2 commits into
apache:mainfrom
SurenderguliaEnginner:fix/4528-rest-scan-planning-metadata

Conversation

@SurenderguliaEnginner

Copy link
Copy Markdown

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_file in pyiceberg/table/__init__.py did not pass these fields to DataFile.from_args(), causing the converted DataFile to always drop column bounds and key metadata.

This caused two main issues:

  1. Pruning/skipping on planned tasks failed because column bounds were missing.
  2. Encrypted tables failed to read because key metadata was not retained to construct decryption properties.

Changes:

  1. pyiceberg/table/__init__.py:
    • Added _serialize_bounds helper using pyiceberg.conversions.to_bytes and field types from table_schema.
    • Decoded key_metadata from base64 string to bytes via base64.b64decode(validate=True) (consistent with table/metadata.py).
    • Forwarded lower_bounds, upper_bounds, and key_metadata to DataFile.from_args().
    • Threaded table_schema through FileScanTask.from_rest_response and DataScan._plan_files_server_side.
  2. pyiceberg/catalog/rest/__init__.py:
    • Forwarded table_schema through _plan_scan_result and _expand_plan_tasks into FileScanTask.from_rest_response.
  3. tests/catalog/test_scan_planning_models.py:
    • Added test_rest_task_preserves_bounds_and_key_metadata to verify bounds serialization and key metadata decoding.

Closes #4528

Tests

  • pytest tests/catalog/test_scan_planning_models.py (63 passed)

…planning

Signed-off-by: Surender Gulia <shriomgulia5@gmail.com>
Signed-off-by: Surender Gulia <shriomgulia5@gmail.com>
@SurenderguliaEnginner
SurenderguliaEnginner force-pushed the fix/4528-rest-scan-planning-metadata branch from eddc546 to c61e652 Compare December 1, 2026 10:56

@rahulsmahadev rahulsmahadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 bytes

Hex 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_result no longer checks isinstance(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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Suggested change
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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REST scan planning drops data file lower/upper bounds and key metadata

2 participants