Repository navigation
Add update_partition_statistics() to Table and Transaction - #4397
Conversation
Add an UpdatePartitionStatistics builder with set_partition_statistics() and remove_partition_statistics(), so callers no longer build update objects by hand. Sets apply before removals and absent removals are no-ops, as in Java. The RemovePartitionStatisticsUpdate handler therefore no longer raises. Co-authored-by: Isaac <no-reply@databricks.com>
|
@rambleraptor, this adds Table and Transaction builders for the partition-statistics updates you introduced in #2192. Could you take a look at the API, and at the missing-file and operation-order semantics, when you have a chance? |
|
Zooming out a bit more, wouldn't it make more sense to have PyIceberg compute the partition statistics? |
|
Good point. Java splits the two: |
| self._sets = {} | ||
| self._removals = {} | ||
|
|
||
| def set_partition_statistics(self, partition_statistics_file: PartitionStatisticsFile | None) -> "UpdatePartitionStatistics": |
There was a problem hiding this comment.
Let's avoid passing in the PartitionStatisticsFile itself. Let's pass in the parameters to construct a PartitionStatisticsFile instead, so the object can evolve separately from the API here.
Also, let's make sure to add docstrings here and at remove_partition_statistics, since it is a public API
There was a problem hiding this comment.
Done in 964b6f0. set_partition_statistics now takes snapshot_id, statistics_path and file_size_in_bytes and builds the PartitionStatisticsFile itself, and both public methods have docstrings. The docs and tests use the new signature; I dropped the None no-op, since there is no file argument any more.
Review feedback: set_partition_statistics takes snapshot_id, statistics_path and file_size_in_bytes and builds the PartitionStatisticsFile, so the model can evolve separately from the API. Add docstrings to both public methods. Co-authored-by: Isaac <no-reply@databricks.com>
Fokko
left a comment
There was a problem hiding this comment.
Thanks @rahulsmahadev for working on this 🚀
Rationale for this change
PyIceberg already models partition statistics in the table metadata:
PartitionStatisticsFile,TableMetadata.partition_statistics, and theSetPartitionStatisticsUpdateandRemovePartitionStatisticsUpdatetable updates (#2192). Table and Transaction have no builder for these updates; callers currently need to construct the update objects and commit them through the catalog API.Java has this API.
Table.updatePartitionStatistics()andTransaction.updatePartitionStatistics()return anUpdatePartitionStatisticswithsetPartitionStatistics(file)andremovePartitionStatistics(snapshotId)(api/src/main/java/org/apache/iceberg/UpdatePartitionStatistics.java,core/src/main/java/org/apache/iceberg/SetPartitionStatistics.java).This PR adds the same API to PyIceberg:
Table.update_partition_statistics()andTransaction.update_partition_statistics(), next toupdate_statistics().UpdatePartitionStatisticsinpyiceberg/table/update/partition_statistics.py, modelled onUpdateStatistics. Pending operations are applied on commit, for chained andwithblock operations alike, with sets applied before removals as in Java.set_partition_statistics(snapshot_id, statistics_path, file_size_in_bytes)builds thePartitionStatisticsFileand replaces the file already set for that snapshot. It takes the fields rather than the file object, soPartitionStatisticsFilecan evolve separately from this API. Java'ssetPartitionStatisticstakes the file object.remove_partition_statistics(snapshot_id)uses the existingRemovePartitionStatisticsUpdate.Related open PRs: #4087 fixes
UpdateStatistics.remove_statistics, which replaces the pending operations instead of appending to them. This PR does not copy that pattern. #4364 removes partition statistics when their snapshots are removed. This PR changes only theRemovePartitionStatisticsUpdatehandler inpyiceberg/table/update/__init__.py, a different part of that file from the snapshot removal handler that #4364 changes, and does not touch the snapshot expiry code. #4364 only removes entries that exist, so the no-op change does not affect its path.Are these changes tested?
Yes. Four tests in
tests/table/test_statistics.pyuse thecatalogfixture (in-memory and SQL catalogs) and go through the public API:test_update_partition_statistics: set, replace the file for the same snapshot, set in awithblock, remove, and reload the table from the catalog.test_update_partition_statistics_set_and_remove_in_one_update: operations on different snapshots are both applied. On the same snapshot the removal wins in either call order, with or without an existing file. Repeated sets keep the last file, and repeated removals are no-ops.test_update_partition_statistics_in_transaction: pending changes are visible intransaction.table_metadata, but not intable.metadataor a catalog reload, until the transaction commits. An exception rolls them back.test_remove_partition_statistics_without_file_is_noop: removing a snapshot without a file, once or repeated, commits and leaves the metadata unchanged.The low-level handler is covered by
test_remove_partition_statistics_update_without_file_is_noopintests/table/test_init.py.All four tests fail on main with
AttributeError: the transaction test reports thatTransactionhas noupdate_partition_statisticsattribute, and the other three report the missing attribute onTable. All four pass with this change. The low-level test intests/table/test_init.pyfails on main withValueErrorand passes with this change. I also checked that a copy of theUpdateStatisticspattern, where removal discards the pending sets, fails the set-and-remove test.Are there any user-facing changes?
Yes. New methods
Table.update_partition_statistics()andTransaction.update_partition_statistics(), and a newUpdatePartitionStatisticsclass. The docs are inmkdocs/docs/api.md. Two behaviours follow Java:RemovePartitionStatisticsUpdatehandler, which previously raisedValueError.Please add the
changeloglabel.This pull request and its description were written by Isaac.