Skip to content

feat(metadata): PartitionSpec builder & validations - #558

Merged
zeroshade merged 10 commits into
apache:mainfrom
twuebi:tp/partition-specs
Sep 17, 2025
Merged

zeroshade merged 10 commits into
apache:mainfrom
twuebi:tp/partition-specs

Conversation

@twuebi

@twuebi twuebi commented Sep 8, 2025 •

Copy link
Copy Markdown
Contributor

This PR:

  1. adds a funct-opts builder for PartitionSpec
  2. implements the remove PartitionSpec Update
  3. ports test relating to PartitionSpecs from iceberg-rust
  4. port missing validations from iceberg-rust to metadataBuilder

It's mostly contained within partitions, there's a more fundamental change to json deserialization in there which initializes the deserialization target with -1 in all int fields so that we are able to determine if we received / did not receive a certain field and are consequently able to reject requests where mandatory IDs are missing.

There's currently still some duplication between the funct-opts constructor of PartitionSpec and update_spec.go. I'm still working on that one, I hope to arrive at something close to java's apply() of BaseUpdatePartitionSpec

Comment thread partitions.go
const (
PartitionDataIDStart = 1000
InitialPartitionSpecID = 0
unassignedFieldID = 0

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.

should this be -1 instead of 0?

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.

field-ids semantic space starts at 1000, so 0 for unassigned is fine? Can also change to -1 if preferred

Comment thread partitions.go
Comment on lines +90 to +92
type PartitionOption func(*PartitionSpec) error

func NewPartitionSpecOpts(opts ...PartitionOption) (PartitionSpec, 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.

these need docstring comments to explain why someone would use this instead of NewPartitionSpec etc.

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 think it'd make sense to eventually get rid of NewPartitionSpec since it allows to construct invalid partition specs?

Comment thread partitions.go Outdated
Comment on lines +171 to +173
func (p *PartitionSpec) SetID(id int) {
p.id = id
}

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.

if we're adding a SetID function, we might as well just make the ID field a public field instead of having SetID and ID() methods. That's more idiomatic for Go.

That said, I would rather this instead be a WithID(id int) *PartitionSpec method which returns a copy of the partition spec with the updated ID. That way we maintain that the PartitionSpec object itself can still be immutable

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.

removed, only callsite was better served by moving all of it into the new method PartitionSpec.BindToSchema

Comment thread partitions.go Resolved
Comment thread partitions.go Resolved
Comment thread partitions.go Outdated
p.id = id
}

func (ps *PartitionSpec) AssignPartitionFieldIds(lastAssignedFieldIDPtr *int) 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.

same argument as above, I'd rather keep a PartitionSpec as an immutable object, so my preference is this should return a copy of the PartitionSpec with the new IDs

@twuebi twuebi Sep 9, 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.

Made it an unexported function + a PartitionOption, maybe don't even need that, we're maybe ok with just calling it from PartitionSpec.BindToSchema?

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.

I'd be fine with that

Comment thread table/metadata.go Resolved
Comment thread table/metadata.go Resolved
Comment thread table/metadata.go Resolved
Comment thread table/metadata.go Resolved
Comment thread table/updates.go
type removeSpecUpdate struct {
baseUpdate
SpecIds []int64 `json:"spec-ids"`
SpecIds []int `json:"spec-ids"`

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.

why this change?

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.

specIds are int not int64, I made a mistake when originally adding the updates

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.

technically spec ids are JSON ints according to the spec, which means that an int64 would be valid. But I'm fine with changing them back to just being an int for now

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.

java uses int which is signed 32bit, rust uses i32, I think for cross language compatibility's sake it makes sense to keep it 32bit

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.

Then you should use int32. On a 64-bit system, int in Go is 64-bit. 😄

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.

ughhh 😅

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.

fixing that now will have a lot of changes all over the place, I'll create an issue and we'll do it as a follow up?

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.

- BindToSchema function on PartitionSpec to a) validate a partition spec on addSpec and b) do the field re-assignment in the constructor
@twuebi
twuebi force-pushed the tp/partition-specs branch from 8d51586 to 891ac47 Compare September 9, 2025 15:25
- move reassigning of spec ids into metadata constructor
- construct all metadata through builder to have a single point of validation
Comment thread catalog/rest/rest.go
Comment on lines -753 to -766
freshSchema, err := iceberg.AssignFreshSchemaIDs(schema, nil)
if err != nil {
return nil, err
}

freshPartitionSpec, err := iceberg.AssignFreshPartitionSpecIDs(cfg.PartitionSpec, schema, freshSchema)
if err != nil {
return nil, err
}

freshSortOrder, err := table.AssignFreshSortOrderIDs(cfg.SortOrder, schema, freshSchema)
if err != nil {
return nil, err
}

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.

neither java, nor rust are doing this, I'd prefer to get rid of it to remove the last callsite of iceberg.AssignFreshPartitionSpecIDs. Any rest catalog needs to recheck this on createTable, servers can't trust clients to do this beforehand so I think this is largely redundant

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.

I think this is why the REST catalog integration is failing, maybe something needs to change (or get updated?) with the iceberg rest catalog image?

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.

Will take a look

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.

It makes very little sense for view or namespace changes to fail based on these changes?

    --- PASS: TestRestIntegration/TestCreateNamespace (0.03s)
    --- FAIL: TestRestIntegration/TestCreateTable (0.02s)
    --- FAIL: TestRestIntegration/TestCreateView (0.01s)
    --- PASS: TestRestIntegration/TestLoadNamespaceProps (0.01s)
    --- PASS: TestRestIntegration/TestUpdateNamespaceProps (0.03s)
    --- FAIL: TestRestIntegration/TestWriteCommitTable (0.02s)

maybe test setup is already failing?

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.

I'll rerun the test on main, but it doesn't appear to be failing. The test failing with these changes is likely due to the field/partition/schema ID values

@twuebi twuebi Sep 17, 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.

Yeah, it's just the zero value of var cfg catalog.CreateTableCfg ruining the day

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.

yea, the default in CreateTableCfg is the zero value which has nil for the slice instead of an empty slice. The rest-fixture errors on the null.

You can fix it by adding:

if cfg.SortOrder.Fields == nil {
    cfg.SortOrder = table.UnsortedSortOrder
}

before the payload is created

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.

eventually we can shift to jsonv2 which will use an empty slice as the default instead of nil

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 was about to add a constructor which would put sane defaults?

func NewCreateTableCfg() CreateTableCfg {
	return CreateTableCfg{
		 Location:      "",
		 PartitionSpec: nil,
		 SortOrder:     table.UnsortedSortOrder,
		 Properties:    nil,
	}
}

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.

Not sure how much of a go-ism that'd be - I thought it'd make sense to make those fields private so that mutators would protect users from building stuff that won't work?

That'd somewhat conflict with funct-opts being consumer-extendable I guess?

Comment thread table/metadata.go
Comment on lines +1417 to +1455
_, err = builder.SetUUID(tableUuid)
if err != nil {
return nil, err
}

formatVersion := DefaultFormatVersion
if props != nil {
verStr, ok := props["format-version"]
if ok {
if formatVersion, err = strconv.Atoi(verStr); err != nil {
formatVersion = DefaultFormatVersion
}
delete(props, "format-version")
_, err = builder.AddSortOrder(&reassignedIds.sortOrder, true)
if err != nil {
return nil, err
}

_, err = builder.SetDefaultSortOrderID(-1)
if err != nil {
return nil, err
}

_, err = builder.AddSchema(reassignedIds.schema)
if err != nil {
return nil, err
}

_, err = builder.SetCurrentSchemaID(-1)
if err != nil {
return nil, err
}

_, err = builder.AddPartitionSpec(reassignedIds.partitionSpec, true)
if err != nil {
return nil, err
}

_, err = builder.SetLoc(location)
if err != nil {
return nil, err
}

_, err = builder.SetProperties(props)
if err != nil {
return nil, err
}

@twuebi twuebi Sep 10, 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.

We should make this a funct-opts thing eventually, I think. I went for the builder to replace the struct literal so that there's a single place of validation

@zeroshade

Copy link
Copy Markdown
Member

Hmm, i wonder why the REST catalog integration tests are failing. It's likely somehow related to this change as they don't seem to be failing on main.

Comment thread catalog/rest/rest.go Resolved
@zeroshade
zeroshade merged commit 038457b into apache:main Sep 17, 2025
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