Repository navigation
feat(encryption): [13/N] Write encrypted Parquet data files #4328
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
kevinjqliu
merged 4 commits into
apache:main
from
xanderbailey:xb/encryption-write-parquet
Nov 13, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0c0a7dd
Write encrypted Parquet data files
xanderbailey 53e2f94
Only pass key_metadata to create_writer for encrypted tables
xanderbailey dba6ed9
Always pass key_metadata to create_writer
kevinjqliu ad8eb51
Handle None key_metadata in _parquet_encryption_properties
kevinjqliu 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,7 @@ | |
|
|
||
| from pyiceberg.conversions import to_bytes | ||
| from pyiceberg.encryption.key_metadata import StandardKeyMetadata | ||
| from pyiceberg.encryption.manager import _new_data_file_key_metadata | ||
| from pyiceberg.exceptions import ResolveError | ||
| from pyiceberg.expressions import AlwaysTrue, BooleanExpression, BoundIsNaN, BoundIsNull, BoundTerm, Not, Or | ||
| from pyiceberg.expressions.literals import Literal | ||
|
|
@@ -1175,6 +1176,19 @@ def _parquet_decryption_properties(key_metadata: bytes) -> pq.FileDecryptionProp | |
| return create_decryption_properties(metadata.encryption_key, aad_prefix=metadata.aad_prefix) | ||
|
|
||
|
|
||
| def _parquet_encryption_properties(key_metadata: StandardKeyMetadata | None) -> pq.FileEncryptionProperties | None: | ||
| if key_metadata is None: | ||
| return None | ||
|
|
||
|
Comment on lines
+1179
to
+1182
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. this is a nit change, i prefer to keep the logic inside |
||
| try: | ||
| from pyarrow.parquet.encryption import create_encryption_properties | ||
| except ImportError as e: | ||
| raise ImportError(f"pyarrow >= 25.0.0 required to write encrypted Parquet files, found {pyarrow.__version__}") from e | ||
|
|
||
| # Java does not store the AAD prefix in the file, readers take it from key metadata | ||
| return create_encryption_properties(key_metadata.encryption_key, aad_prefix=key_metadata.aad_prefix, store_aad_prefix=False) | ||
|
|
||
|
|
||
| def _read_deletes(io: FileIO, data_file: DataFile) -> dict[str, pa.ChunkedArray]: | ||
| _check_encryption_supported(data_file) | ||
| if data_file.file_format == FileFormat.PARQUET: | ||
|
|
@@ -2764,13 +2778,20 @@ def data_file_statistics_from_parquet_metadata( | |
| class ParquetFormatWriter(FileFormatWriter): | ||
| """Writes Arrow tables to a Parquet file.""" | ||
|
|
||
| def __init__(self, output_file: OutputFile, file_schema: Schema, properties: Properties) -> None: | ||
| def __init__( | ||
| self, | ||
| output_file: OutputFile, | ||
| file_schema: Schema, | ||
| properties: Properties, | ||
| key_metadata: StandardKeyMetadata | None = None, | ||
| ) -> None: | ||
| self._output_file = output_file | ||
| self._file_schema = file_schema | ||
| self._properties = properties | ||
| self._writer: pq.ParquetWriter | None = None | ||
| self._fos: OutputStream | None = None | ||
| self._parquet_writer_kwargs = _get_parquet_writer_kwargs(properties) | ||
| self._parquet_writer_kwargs["encryption_properties"] = _parquet_encryption_properties(key_metadata) | ||
| self._row_group_size = property_as_int( | ||
| properties=properties, | ||
| property_name=TableProperties.PARQUET_ROW_GROUP_LIMIT, | ||
|
|
@@ -2823,8 +2844,9 @@ def create_writer( | |
| output_file: OutputFile, | ||
| file_schema: Schema, | ||
| properties: Properties, | ||
| key_metadata: StandardKeyMetadata | None = None, | ||
| ) -> ParquetFormatWriter: | ||
| return ParquetFormatWriter(output_file, file_schema, properties) | ||
| return ParquetFormatWriter(output_file, file_schema, properties, key_metadata) | ||
|
|
||
| def add_field_metadata(self, field: NestedField, metadata: dict[bytes, bytes], include_field_ids: bool) -> None: | ||
| if include_field_ids: | ||
|
|
@@ -2871,7 +2893,8 @@ def write_data_file(task: WriteTask) -> DataFile: | |
| partition_key=task.partition_key, | ||
| ) | ||
| fo = io.new_output(file_path) | ||
| writer = format_model.create_writer(fo, file_schema, table_metadata.properties) | ||
| key_metadata = _new_data_file_key_metadata(table_metadata) | ||
| writer = format_model.create_writer(fo, file_schema, table_metadata.properties, key_metadata=key_metadata) | ||
| with writer: | ||
| writer.write(arrow_table) | ||
| statistics = writer.result() | ||
|
|
@@ -2889,7 +2912,7 @@ def write_data_file(task: WriteTask) -> DataFile: | |
| # Just copy these from the table for now | ||
| spec_id=table_metadata.default_spec_id, | ||
| equality_ids=None, | ||
| key_metadata=None, | ||
| key_metadata=key_metadata.to_bytes() if key_metadata is not None else None, | ||
| **statistics.to_serialized_dict(), | ||
| ) | ||
|
|
||
|
|
||
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.
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.