Skip to content

feat(partitions): Add support for get partition field name - #468

Merged
zeroshade merged 9 commits into
apache:mainfrom
lliangyu-lin:partition-visitor
Jun 30, 2025
Merged

zeroshade merged 9 commits into
apache:mainfrom
lliangyu-lin:partition-visitor

Conversation

@lliangyu-lin

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

Copy link
Copy Markdown
Contributor

Description

Testing

Added unit tests TestGetPartitionFieldName

@lliangyu-lin
lliangyu-lin marked this pull request as ready for review June 25, 2025 22:19
Comment thread partitions.go Outdated
case VoidTransform:
return visitor.Void(field.FieldID, sourceName, field.SourceID), nil
default:
return empty, fmt.Errorf("unknown transform type: %T", transform)

@lliangyu-lin lliangyu-lin Jun 25, 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.

Maybe we should just panic here. Will see what other people thinks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

error here seems just about right

Comment thread partitions.go Outdated
return NewPartitionSpec(newFields...), nil
}

type PartitionSpecVisitor[T any] interface {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this interface is quite wierd, what's a point for this interface to be generic?

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.

same response below. #468 (comment)
Mostly for extensibility

Comment thread partitions.go Outdated
return sourceName + "_null"
}

func VisitPartitionField[R any](schema *Schema, field PartitionField, visitor PartitionSpecVisitor[R]) (R, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i don't really get why this needed, can you elaborate what is a purpose of this visitor?
why not just pass a function?

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's mainly for aligning with iceberg java and iceberg python implementations

The generic visitor approach could be extended and there are more visitor implementations in future use case such as building sort order within partitions https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/Partitioning.java#L133

Although I don't think it will be needed any time soon, and I think just have a simple function that creates partition field name is fine as well. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

matching same patterns in different language seems to me a bit odd, so if you can simplify it - let's do it.
this seems like an overhead.

@lliangyu-lin lliangyu-lin Jun 26, 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.

Updated to just have a function for getting a default partition field name. This should be enough to help with cases like when adding partition field name with empty string "" and rename it with the default name.

@lliangyu-lin lliangyu-lin changed the title feat(partitions): Add support for partition field visitor feat(partitions): Add support for get partition field name Jun 26, 2025
Comment thread partitions.go Outdated
Comment on lines +306 to +317
case BucketTransform:
return fmt.Sprintf("%s_bucket_%d", sourceName, t.NumBuckets), nil
case TruncateTransform:
return fmt.Sprintf("%s_trunc_%d", sourceName, t.Width), nil
case YearTransform:
return sourceName + "_year", nil
case MonthTransform:
return sourceName + "_month", nil
case DayTransform:
return sourceName + "_day", nil
case HourTransform:
return sourceName + "_hour", nil

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.

all of these could likely be replaced by calling .String() on the transform:

switch t := transform.(type) {
case IdentityTransform:
        return sourceName, nil
case VoidTransform:
        return sourceName + "_null", nil
default:
        return sourcecName + "_" + t.String(), nil
}

@lliangyu-lin lliangyu-lin Jun 27, 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.

Bucket and Truncate .String() returns bucket[%d] and truncate[%d]. Are we okay with using these as partition field name or should we handle these two transforms separately?

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'm okay with using those as partition field names, is there an existing example of the generated names in the java implementation?

@lliangyu-lin lliangyu-lin Jun 27, 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.

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.

damn, okay i guess we have to leave those separately so we can be consistent with the others.

Can you add a comment linking to the java impl so we remember why we're not just using the .String() method?

@lliangyu-lin
lliangyu-lin requested a review from zeroshade June 27, 2025 23:51

@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 last nitpick! Thanks so much for this.

Comment thread partitions_test.go Outdated
Comment on lines +219 to +229
name, err = iceberg.GeneratePartitionFieldName(schema, field)
assert.NoError(t, err)
assert.Equal(t, "other_str", name)

field = iceberg.PartitionField{
SourceID: 3, FieldID: 1001,
Transform: iceberg.VoidTransform{}, Name: "name",
}
name, err = iceberg.GeneratePartitionFieldName(schema, field)
assert.NoError(t, err)
assert.Equal(t, "int_null", name)

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.

Can we add tests for the rest of the transform types? Or at least one that hits the default condition?

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.

Yes! Added in the new commit

Comment thread partitions.go
//
// The default names are aligned with other client implementations
// https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java#L518-L563
func GeneratePartitionFieldName(schema *Schema, field PartitionField) (string, 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.

Looking at the rest of the Java implementation, it looks like it only uses the generated name like this if field.Name is empty. Should we add something like if len(field.Name) > 0 { return field.Name, nil } ? and have the tests leave out the Name member?

@lliangyu-lin lliangyu-lin Jun 30, 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.

Based on other client implementations, I think the method should work for both field with name and empty name?
In python implementation, it gets the partition field name with a tmp field with name unassigned_field_name.
https://github.com/apache/iceberg-python/blob/main/pyiceberg/table/update/spec.py#L281-L282

Although I don't think there will be any issue if we check empty name inside the method to limit the use case to empty name only as well. 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.

Sounds good!

@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 last thing to do: add a test case that does set the partition field Name and confirms that it returns that name 😄

@zeroshade
zeroshade merged commit 914e7b4 into apache:main Jun 30, 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.

3 participants