laskoviymishka commented on code in PR #2381:
URL: https://github.com/apache/iceberg-go/pull/2381#discussion_r4490455355


##########
table/dynamic_partition_overwrite.go:
##########
@@ -0,0 +1,445 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package table
+
+import (
+       "context"
+       "fmt"
+       "maps"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/google/uuid"
+)
+
+// dynamicPartitionKeys normalizes all partition values before comparing them.
+// A manifest decoder may represent the same logical day as an int32 while the
+// writer uses iceberg.Date. The shared canonical encoder handles both forms,
+// decimals, bytes, UUIDs, and nulls without lossy string formatting.
+func dynamicPartitionKeys(files []iceberg.DataFile) (map[string]struct{}, 
error) {
+       keys := make(map[string]struct{}, len(files))
+       for _, file := range files {
+               key, err := canonicalPartitionKey(file.SpecID(), 
dataFilePartition(file))
+               if err != nil {
+                       return nil, fmt.Errorf("dynamic partition overwrite 
file %q: %w", file.FilePath(), err)
+               }
+               keys[key] = struct{}{}
+       }
+
+       return keys, nil
+}
+
+func (t *Transaction) dynamicOverwriteSpec() (*iceberg.PartitionSpec, error) {
+       meta, err := t.txnMeta()
+       if err != nil {
+               return nil, err
+       }
+
+       if meta.formatVersion > 3 {
+               return nil, fmt.Errorf("%w: dynamic partition overwrite does 
not support format v%d", ErrInvalidOperation, meta.formatVersion)
+       }
+
+       spec, err := meta.CurrentSpec()
+       if err != nil {
+               return nil, err
+       }
+       if spec == nil {
+               return nil, fmt.Errorf("%w: dynamic partition overwrite 
requires a current partition spec", ErrInvalidOperation)
+       }
+       if err := checkNoUnknownTransform(spec); err != nil {
+               return nil, err
+       }
+
+       return spec, nil
+}
+
+// checkDynamicOverwriteExistingSpecs rejects live files under older specs
+// before an Arrow writer creates data files. Different partition transforms
+// cannot be compared safely by comparing their stored partition tuples.
+func (t *Transaction) checkDynamicOverwriteExistingSpecs(ctx context.Context, 
fs iceio.IO, specID int) error {
+       snapshot := t.planningSnapshot(t.meta)
+       if snapshot == nil {
+               return nil
+       }
+       manifests, err := snapshot.Manifests(ctx, fs)
+       if err != nil {
+               return fmt.Errorf("dynamic partition overwrite: read manifests: 
%w", err)
+       }
+       for _, manifest := range manifests {
+               if err := ctx.Err(); err != nil {
+                       return err
+               }
+               if int(manifest.PartitionSpecID()) == specID {
+                       continue
+               }
+               for entry, err := range manifest.Entries(ctx, fs, true) {
+                       if err != nil {
+                               return fmt.Errorf("dynamic partition overwrite: 
read manifest %s: %w", manifest.FilePath(), err)
+                       }
+
+                       return fmt.Errorf("%w: dynamic partition overwrite 
cannot replace live file %q under partition spec %d (current %d)",

Review Comment:
   Could we skip files under older specs here instead of rejecting the 
operation? One live file under any non-current spec fails every dynamic 
overwrite, though the keys include the spec ID, so it can never match a touched 
partition. Java's `BaseReplacePartitions` drops by `(specId, partition)` and 
keeps other-spec files: 
https://github.com/apache/iceberg/blob/2114bf631e49af532d66e2ce148ee49dd1dd1f1f/core/src/main/java/org/apache/iceberg/BaseReplacePartitions.java#L50-L56
   
   The planning loop has the same check: 
https://github.com/apache/iceberg-go/blob/bda09cde1967a5f5784b228ac764f959697d6291/table/dynamic_partition_overwrite.go#L229-L235



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to