zeroshade commented on code in PR #467:
URL: https://github.com/apache/iceberg-go/pull/467#discussion_r2195814663


##########
table/update_spec.go:
##########
@@ -0,0 +1,367 @@
+// 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 (
+       "errors"
+       "fmt"
+
+       "github.com/apache/iceberg-go"
+)
+
+type UpdateSpec struct {
+       txn                   *Transaction
+       nameToField           map[string]iceberg.PartitionField
+       nameToAddedField      map[string]iceberg.PartitionField
+       transformToField      map[transformKey]iceberg.PartitionField
+       transformToAddedField map[transformKey]iceberg.PartitionField
+       renames               map[string]string
+       addedTimeFields       map[int]iceberg.PartitionField
+       caseSensitive         bool
+       adds                  []iceberg.PartitionField
+       deletes               map[int]bool
+       lastAssignedFieldId   int
+}
+
+func NewUpdateSpec(t *Transaction, caseSensitive bool) *UpdateSpec {
+       transformToField := make(map[transformKey]iceberg.PartitionField)
+       nameToField := make(map[string]iceberg.PartitionField)
+       partitionSpec := t.tbl.Metadata().PartitionSpec()
+       for partitionField := range partitionSpec.Fields() {
+               transformToField[transformKey{
+                       SourceId:  partitionField.SourceID,
+                       Transform: partitionField.Transform.String(),
+               }] = partitionField
+               nameToField[partitionField.Name] = partitionField
+       }
+       lastAssignedFieldId := t.tbl.Metadata().LastPartitionSpecID()
+       if lastAssignedFieldId == nil {
+               v := iceberg.PartitionDataIDStart - 1
+               lastAssignedFieldId = &v
+       }
+
+       return &UpdateSpec{
+               txn:                   t,
+               nameToField:           nameToField,
+               nameToAddedField:      make(map[string]iceberg.PartitionField),
+               transformToField:      transformToField,
+               transformToAddedField: 
make(map[transformKey]iceberg.PartitionField),
+               renames:               make(map[string]string),
+               addedTimeFields:       make(map[int]iceberg.PartitionField),
+               caseSensitive:         caseSensitive,
+               adds:                  make([]iceberg.PartitionField, 0),
+               deletes:               make(map[int]bool),
+               lastAssignedFieldId:   *lastAssignedFieldId,
+       }
+}
+
+type transformKey struct {
+       SourceId  int
+       Transform string
+}
+
+func (us *UpdateSpec) AddField(sourceColName string, transform 
iceberg.Transform, partitionFieldName string) (*UpdateSpec, error) {

Review Comment:
   > For Change all the methods to return only error, this still won't allow 
chaining right?
   
   Correct, that would not allow chaining. If we're going to return the error 
individually from the methods then we should simplify it and just return the 
error since we can't do chaining that way anyways.
   
   To do chaining with the Builder approach, the methods wouldn't actually 
*perform* the operations, they would essentially create a stack of operations 
to perform that we delay executing until `Build()` is called. `Build` can then 
just go through the list of chained operations until either they all complete 
or one of them returns an error.
   
   For instance something maybe like:
   
   ```go
   func (b *UpdateSpec) AddField(...) *UpdateSpec {
        b.operations = append(b.operations, b.addField(...))
        return b
   }
   
   ...
   
   func (b *UpdateSpec) BuildUpdates() ([]Update, []Requirement, error) {
           for _, op := range b.operations {
                   if err := op(); err != nil {
                          return nil, nil, err
                    }
            }
   
            // build updates and requirements and return them
   }
   ```
   
   Does that kinda show what I mean?



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to