Skip to content

feat(transaction): Add initial support for update spec in transaction API - #467

Merged
zeroshade merged 12 commits into
apache:mainfrom
lliangyu-lin:update-spec
Jul 14, 2025
Merged

zeroshade merged 12 commits into
apache:mainfrom
lliangyu-lin:update-spec

Conversation

@lliangyu-lin

@lliangyu-lin lliangyu-lin commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor

Description

Usage

specUpdates := txn.UpdateSpec()

specUpdates, _ = specUpdates.AddField("ts", iceberg.YearTransform{}, "year_transform")
specUpdates, _ = specUpdates.RenameField("id_identity", "new_id_identity")
specUpdates, _ = specUpdates.RemoveFiled("field")

specUpdates.Commit()
txn.Commit()

Testing

  • update_spec_test.go

@lliangyu-lin
lliangyu-lin marked this pull request as ready for review June 25, 2025 23:07
@lliangyu-lin lliangyu-lin changed the title [WIP] feat(transaction): Add initial support for update spec in transaction API feat(transaction): Add initial support for update spec in transaction API Jun 25, 2025
zeroshade pushed a commit that referenced this pull request Jun 30, 2025
### 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 zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first pass, still need to review the tests

Comment thread partitions.go Resolved
Comment thread table/update_spec.go Resolved
Comment thread table/update_spec.go Resolved
Comment thread table/update_spec.go Outdated
Transform string
}

func (us *UpdateSpec) AddField(sourceColName string, transform iceberg.Transform, partitionFieldName string) (*UpdateSpec, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Change all the methods to return only error instead of (*UpdateSpec, error)
  2. Change the structure of the UpdateSpec struct to facilitate something like: err := (&UpdateSpec{}).AddField(...).RemoveField(...).RenameField(...).Build().

I think both of those would be cleaner than this current function signature.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thanks for the clarifications!
I like the builder approach in this case. Will try to update this locally and do some testings.

Comment thread table/update_spec.go Resolved
Comment thread table/update_spec.go Outdated
return us.txn.apply(updates, requirements)
}

func (us *UpdateSpec) CommitUpdates() ([]Update, []Requirement, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't look like there's anything that can return an error here, can we remove the error from the signature?

@lliangyu-lin lliangyu-lin Jul 14, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread table/update_spec.go Resolved
Comment thread table/update_spec.go Resolved
Comment thread table/update_spec.go Resolved
Comment thread table/update_spec.go Resolved

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one nitpick is left and then we're good! Thanks for this!

Comment thread table/update_spec.go
"github.com/apache/iceberg-go"
)

type UpdateSpec struct {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a docstring

@zeroshade
zeroshade merged commit 194a45f into apache:main Jul 14, 2025
@lliangyu-lin
lliangyu-lin deleted the update-spec branch July 14, 2025 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants