Repository navigation
feat(transaction): Add initial support for update spec in transaction API - #467
Conversation
### Description * Added method to get default partition field name based on transform type * This method will be a helper for update spec when the partition field is not given (such as `AddIdentity`) and creates default partition field name * https://github.com/apache/iceberg-go/pull/467/files#diff-eb806166d1b9d570a36b8506d2b9ada56126a32ca94b1eb12d6de03973e292c2R147 * https://github.com/apache/iceberg-python/blob/main/pyiceberg/partitioning.py#L275-L376 * #467 ### Testing Added unit tests `TestGetPartitionFieldName `
zeroshade
left a comment
There was a problem hiding this comment.
first pass, still need to review the tests
| Transform string | ||
| } | ||
|
|
||
| func (us *UpdateSpec) AddField(sourceColName string, transform iceberg.Transform, partitionFieldName string) (*UpdateSpec, error) { |
There was a problem hiding this comment.
The whole benefit to returning *UpdateSpec is so that you can chain these calls. If we're also returning the error then we, by definition, are preventing the chaining unless someone creates a must function which seems bad.
So either we should change the signatures to just return only an error instead of *UpdateSpec or we should change this to be more of a Builder pattern where the actual function calls just populate the list of operations (AddField, RemoveField, Rename, etc.) but don't actually attempt to perform the functions until something like UpdateSpec.Build() is called, at which point it returns the error.
Basically, I'm proposing we should modify this to one of these two patterns:
- Change all the methods to return only
errorinstead of(*UpdateSpec, error) - Change the structure of the
UpdateSpecstruct to facilitate something like:err := (&UpdateSpec{}).AddField(...).RemoveField(...).RenameField(...).Build().
I think both of those would be cleaner than this current function signature.
There was a problem hiding this comment.
I agree with you. I realized Go will not allow chaining when you return Error as part of the result when I started writing the unit tests...
For Change all the methods to return only error, this still won't allow chaining right? Or do you mean returning only *UpdateSpec.
For Builder approach, how should handle the errors if one of the chained methods failed? This applies to any approach for chaining I guess, I think we might need to maintain an error message field within the struct?
What do you think?
There was a problem hiding this 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:
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?
There was a problem hiding this comment.
I see. Thanks for the clarifications!
I like the builder approach in this case. Will try to update this locally and do some testings.
| return us.txn.apply(updates, requirements) | ||
| } | ||
|
|
||
| func (us *UpdateSpec) CommitUpdates() ([]Update, []Requirement, error) { |
There was a problem hiding this comment.
it doesn't look like there's anything that can return an error here, can we remove the error from the signature?
There was a problem hiding this comment.
CommitUpdates and BuildChanges() seems like performing the same logic expect BuildChanges goes through the operation queue, so I only kept the BuildUpdates with the error in the signature.
fix appends Co-authored-by: Matt Topol <[email protected]>
zeroshade
left a comment
There was a problem hiding this comment.
Only one nitpick is left and then we're good! Thanks for this!
| "github.com/apache/iceberg-go" | ||
| ) | ||
|
|
||
| type UpdateSpec struct { |
Description
Usage
Testing
update_spec_test.go