ahmedabu98 commented on code in PR #40080:
URL: https://github.com/apache/beam/pull/40080#discussion_r4019717284


##########
sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/AssignDestinationsAndPartitions.java:
##########
@@ -111,58 +139,83 @@ public void processElement(
           dynamicDestinations.getTableStringIdentifier(
               ValueInSingleWindow.of(element, timestamp, window, paneInfo));
 
+      String canonicalTableId;
+      try {
+        canonicalTableId =
+            IcebergUtils.tableIdentifierToString(
+                IcebergUtils.parseTableIdentifier(tableIdentifier));
+      } catch (Exception e) {
+        canonicalTableId = tableIdentifier.trim();
+      }

Review Comment:
   I don't think this is necessary?



##########
sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/AssignDestinationsAndPartitions.java:
##########
@@ -111,58 +139,83 @@ public void processElement(
           dynamicDestinations.getTableStringIdentifier(
               ValueInSingleWindow.of(element, timestamp, window, paneInfo));
 
+      String canonicalTableId;
+      try {
+        canonicalTableId =
+            IcebergUtils.tableIdentifierToString(
+                IcebergUtils.parseTableIdentifier(tableIdentifier));
+      } catch (Exception e) {
+        canonicalTableId = tableIdentifier.trim();
+      }
+
+      SerializableTableSpec tableSpec = null;
+      if (metadataView != null) {
+        Map<String, SerializableTableSpec> viewMap = c.sideInput(metadataView);
+        if (viewMap != null) {
+          tableSpec = viewMap.get(canonicalTableId);
+          if (tableSpec == null) {
+            tableSpec = viewMap.get(tableIdentifier);
+          }
+        }
+      }
+
       Row data = dynamicDestinations.getData(element);
 
       @Nullable PartitionKey partitionKey = 
checkStateNotNull(partitionKeys).get(tableIdentifier);
-
       @Nullable BeamRowWrapper wrapper = 
checkStateNotNull(wrappers).get(tableIdentifier);
-
       @Nullable Instant lastRefresh = 
checkStateNotNull(lastRefreshTimes).get(tableIdentifier);
+      @Nullable Integer cachedSpecId = 
checkStateNotNull(cachedSpecIds).get(tableIdentifier);
 
       Instant now = Instant.now();
 
+      boolean specChanged =
+          tableSpec != null
+              && (cachedSpecId == null || 
!cachedSpecId.equals(tableSpec.getSpecId()));
+
       boolean shouldRefresh =
           partitionKey == null
               || wrapper == null
-              || lastRefresh == null
-              || now.isAfter(lastRefresh.plus(REFRESH_INTERVAL));
+              || specChanged
+              || (tableSpec == null
+                  && (lastRefresh == null || 
now.isAfter(lastRefresh.plus(REFRESH_INTERVAL))));
 
       if (shouldRefresh) {
 
         PartitionSpec spec = PartitionSpec.unpartitioned();
-
         Schema schema = 
IcebergUtils.beamSchemaToIcebergSchema(data.getSchema());
 
         @Nullable IcebergTableCreateConfig createConfig =
             
dynamicDestinations.instantiateDestination(tableIdentifier).getTableCreateConfig();
 
         if (createConfig != null && createConfig.getPartitionFields() != null) 
{
-
           spec =
               
PartitionUtils.toPartitionSpec(createConfig.getPartitionFields(), 
data.getSchema());
-
+        } else if (tableSpec != null) {
+          spec = tableSpec.getPartitionSpec();
+          if (data.getSchema().getFieldCount() == 
tableSpec.getSchema().columns().size()) {
+            schema = tableSpec.getSchema();
+          }
+          checkStateNotNull(cachedSpecIds).put(tableIdentifier, 
tableSpec.getSpecId());

Review Comment:
   Can we move this to be the first check? So we can make sure `cachedSpecIds` 
is populated even when the user gives a `createConfig`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to