- 
                Notifications
    You must be signed in to change notification settings 
- Fork 326
Add properties from TableMetadata into Table entity internalProperties #2735
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -44,6 +44,7 @@ | |||||
| import java.lang.reflect.Method; | ||||||
| import java.time.Clock; | ||||||
| import java.util.Arrays; | ||||||
| import java.util.Comparator; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.List; | ||||||
| import java.util.Map; | ||||||
|  | @@ -62,6 +63,7 @@ | |||||
| import org.apache.iceberg.FileMetadata; | ||||||
| import org.apache.iceberg.FileScanTask; | ||||||
| import org.apache.iceberg.MetadataUpdate; | ||||||
| import org.apache.iceberg.NullOrder; | ||||||
| import org.apache.iceberg.PartitionSpec; | ||||||
| import org.apache.iceberg.RowDelta; | ||||||
| import org.apache.iceberg.Schema; | ||||||
|  | @@ -103,12 +105,14 @@ | |||||
| import org.apache.polaris.core.context.CallContext; | ||||||
| import org.apache.polaris.core.context.RealmContext; | ||||||
| import org.apache.polaris.core.entity.CatalogEntity; | ||||||
| import org.apache.polaris.core.entity.NamespaceEntity; | ||||||
| import org.apache.polaris.core.entity.PolarisBaseEntity; | ||||||
| import org.apache.polaris.core.entity.PolarisEntity; | ||||||
| import org.apache.polaris.core.entity.PolarisEntitySubType; | ||||||
| import org.apache.polaris.core.entity.PolarisEntityType; | ||||||
| import org.apache.polaris.core.entity.PrincipalEntity; | ||||||
| import org.apache.polaris.core.entity.TaskEntity; | ||||||
| import org.apache.polaris.core.entity.table.IcebergTableLikeEntity; | ||||||
| import org.apache.polaris.core.exceptions.CommitConflictException; | ||||||
| import org.apache.polaris.core.persistence.MetaStoreManagerFactory; | ||||||
| import org.apache.polaris.core.persistence.PolarisMetaStoreManager; | ||||||
|  | @@ -154,6 +158,7 @@ | |||||
| import org.apache.polaris.service.types.TableUpdateNotification; | ||||||
| import org.assertj.core.api.AbstractCollectionAssert; | ||||||
| import org.assertj.core.api.Assertions; | ||||||
| import org.assertj.core.api.InstanceOfAssertFactories; | ||||||
| import org.assertj.core.api.ListAssert; | ||||||
| import org.assertj.core.api.ThrowableAssert.ThrowingCallable; | ||||||
| import org.assertj.core.configuration.PreferredAssumptionException; | ||||||
|  | @@ -2282,6 +2287,96 @@ public void testTableOperationsDoesNotRefreshAfterCommit(boolean updateMetadataO | |||||
| } | ||||||
| } | ||||||
|  | ||||||
| @Test | ||||||
| public void testTableInternalPropertiesStoredOnCommit() { | ||||||
| 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. [Non-blocker] Since we are testing the extraction of metadata fields, it would be good if we could add/or parametrize this test with different format-version (1,2,3). Technically the  | ||||||
| Assumptions.assumeTrue( | ||||||
| requiresNamespaceCreate(), | ||||||
| "Only applicable if namespaces must be created before adding children"); | ||||||
|  | ||||||
| catalog.createNamespace(NS); | ||||||
| catalog.buildTable(TABLE, SCHEMA).create(); | ||||||
| catalog.loadTable(TABLE).newFastAppend().appendFile(FILE_A).commit(); | ||||||
| Table afterAppend = catalog.loadTable(TABLE); | ||||||
| EntityResult schemaResult = | ||||||
| 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. 
        Suggested change
       
 [nit] Let's use  | ||||||
| metaStoreManager.readEntityByName( | ||||||
| polarisContext, | ||||||
| List.of(catalogEntity), | ||||||
| PolarisEntityType.NAMESPACE, | ||||||
| PolarisEntitySubType.NULL_SUBTYPE, | ||||||
| NS.toString()); | ||||||
| Assertions.assertThat(schemaResult).returns(true, EntityResult::isSuccess); | ||||||
| EntityResult tableResult = | ||||||
| metaStoreManager.readEntityByName( | ||||||
| polarisContext, | ||||||
| List.of(catalogEntity, schemaResult.getEntity()), | ||||||
| PolarisEntityType.TABLE_LIKE, | ||||||
| PolarisEntitySubType.ICEBERG_TABLE, | ||||||
| TABLE.name()); | ||||||
| Assertions.assertThat(tableResult) | ||||||
| .returns(true, EntityResult::isSuccess) | ||||||
| .extracting(er -> PolarisEntity.of(er.getEntity())) | ||||||
| .extracting(PolarisEntity::getInternalPropertiesAsMap) | ||||||
| .asInstanceOf(InstanceOfAssertFactories.map(String.class, String.class)) | ||||||
| .containsEntry(NamespaceEntity.PARENT_NAMESPACE_KEY, NS.toString()) | ||||||
| .containsEntry( | ||||||
| IcebergTableLikeEntity.CURRENT_SNAPSHOT_ID, | ||||||
| String.valueOf(afterAppend.currentSnapshot().snapshotId())) | ||||||
| .containsEntry(IcebergTableLikeEntity.LOCATION, afterAppend.location()) | ||||||
| .containsEntry(IcebergTableLikeEntity.TABLE_UUID, afterAppend.uuid().toString()) | ||||||
| .containsEntry( | ||||||
| IcebergTableLikeEntity.CURRENT_SCHEMA_ID, | ||||||
| String.valueOf(afterAppend.schema().schemaId())) | ||||||
| .containsEntry( | ||||||
| IcebergTableLikeEntity.LAST_COLUMN_ID, | ||||||
| afterAppend.schema().columns().stream() | ||||||
| .max(Comparator.comparing(Types.NestedField::fieldId)) | ||||||
| .map(Types.NestedField::fieldId) | ||||||
| .orElse(0) | ||||||
| .toString()) | ||||||
| .containsEntry( | ||||||
| IcebergTableLikeEntity.LAST_SEQUENCE_NUMBER, | ||||||
| String.valueOf(afterAppend.currentSnapshot().sequenceNumber())); | ||||||
|  | ||||||
| catalog.loadTable(TABLE).refresh(); | ||||||
| catalog.loadTable(TABLE).newFastAppend().appendFile(FILE_B).commit(); | ||||||
| validatePropertiesUpdated( | ||||||
| schemaResult, | ||||||
| IcebergTableLikeEntity.CURRENT_SNAPSHOT_ID, | ||||||
| tbl -> String.valueOf(tbl.currentSnapshot().snapshotId())); | ||||||
|  | ||||||
| catalog.loadTable(TABLE).refresh(); | ||||||
| catalog.loadTable(TABLE).updateSchema().addColumn("new_col", Types.LongType.get()).commit(); | ||||||
| validatePropertiesUpdated( | ||||||
| schemaResult, | ||||||
| IcebergTableLikeEntity.CURRENT_SCHEMA_ID, | ||||||
| tbl -> String.valueOf(tbl.schema().schemaId())); | ||||||
|  | ||||||
| catalog.loadTable(TABLE).refresh(); | ||||||
| catalog.loadTable(TABLE).replaceSortOrder().desc("new_col", NullOrder.NULLS_FIRST).commit(); | ||||||
| validatePropertiesUpdated( | ||||||
| schemaResult, | ||||||
| IcebergTableLikeEntity.DEFAULT_SORT_ORDER_ID, | ||||||
| table -> String.valueOf(table.sortOrder().orderId())); | ||||||
| } | ||||||
|  | ||||||
| private void validatePropertiesUpdated( | ||||||
| EntityResult schemaResult, String key, Function<Table, String> expectedValue) { | ||||||
| Table afterUpdate = catalog.loadTable(TABLE); | ||||||
| EntityResult tableResult = | ||||||
| metaStoreManager.readEntityByName( | ||||||
| polarisContext, | ||||||
| List.of(catalogEntity, schemaResult.getEntity()), | ||||||
| PolarisEntityType.TABLE_LIKE, | ||||||
| PolarisEntitySubType.ICEBERG_TABLE, | ||||||
| TABLE.name()); | ||||||
| Assertions.assertThat(tableResult) | ||||||
| .returns(true, EntityResult::isSuccess) | ||||||
| .extracting(er -> PolarisEntity.of(er.getEntity())) | ||||||
| .extracting(PolarisEntity::getInternalPropertiesAsMap) | ||||||
| .asInstanceOf(InstanceOfAssertFactories.map(String.class, String.class)) | ||||||
| .containsEntry(key, expectedValue.apply(afterUpdate)); | ||||||
| } | ||||||
|  | ||||||
| @Test | ||||||
| public void testEventsAreEmitted() { | ||||||
| IcebergCatalog catalog = catalog(); | ||||||
|  | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: If we're using the builder pattern, why have rich constructor parameters? Why not call
.setABC()?In this case the
Mapis empty, so this parameter is redundant?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is in setting the
internalPropertiesmap in the builder methods because we have these helper methods, such assetMetadataLocationthat aren't fields themselves, but modify entries in the map. If we do call the following, we're finebut if we reverse the order and call the following, we're broken:
It's not obvious from the caller's perspective, but
setMetadataLocationmodifies the underlying map, then thesetInternalPropertiescall completely overwrites the map, losing the value set in the previous call.With the existing constructor, it's impossible to order the setting of the
metadataLocationand theinternalPropertiesvia the builder methods. If we used the builder for all properties all the time, it would be fine, but because we pass in thenewLocationparameter as a constructor arg, if we set theinternalPropertiesfield using the builder method, we lose the location parameter we just passed in.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, TIL 🤔 However, having this kind of effects in the codebase it pretty risky in the long term maintenance perspective, IMHO. Would it be reasonable to refactor the builders / related code to allow for more intuitive usage (in another PR, of course)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was thinking about making
metadataLocationand other settable map entries into distinct fields in the Builder so that they can just be added to the map in thebuildcall, but... yeah, a future PR