Repository navigation
feat(partitions): Add support for get partition field name - #468
Conversation
| case VoidTransform: | ||
| return visitor.Void(field.FieldID, sourceName, field.SourceID), nil | ||
| default: | ||
| return empty, fmt.Errorf("unknown transform type: %T", transform) |
There was a problem hiding this comment.
Maybe we should just panic here. Will see what other people thinks
There was a problem hiding this comment.
error here seems just about right
| return NewPartitionSpec(newFields...), nil | ||
| } | ||
|
|
||
| type PartitionSpecVisitor[T any] interface { |
There was a problem hiding this comment.
this interface is quite wierd, what's a point for this interface to be generic?
There was a problem hiding this comment.
same response below. #468 (comment)
Mostly for extensibility
| return sourceName + "_null" | ||
| } | ||
|
|
||
| func VisitPartitionField[R any](schema *Schema, field PartitionField, visitor PartitionSpecVisitor[R]) (R, error) { |
There was a problem hiding this comment.
i don't really get why this needed, can you elaborate what is a purpose of this visitor?
why not just pass a function?
There was a problem hiding this comment.
It's mainly for aligning with iceberg java and iceberg python implementations
- https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/transforms/PartitionSpecVisitor.java
- https://github.com/apache/iceberg-python/blob/main/pyiceberg/partitioning.py#L351
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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
}There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I'm okay with using those as partition field names, is there an existing example of the generated names in the java implementation?
There was a problem hiding this comment.
Java uses the same partition name generator as python.
- https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java#L213-L214
- https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java#L518-L563
For bucket and truncate, the names will be sourceName + "_bucket_" + numBuckets and sourceName + "_trunc_" + width
There was a problem hiding this comment.
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?
zeroshade
left a comment
There was a problem hiding this comment.
Only one last nitpick! Thanks so much for this.
| 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) |
There was a problem hiding this comment.
Can we add tests for the rest of the transform types? Or at least one that hits the default condition?
There was a problem hiding this comment.
Yes! Added in the new commit
| // | ||
| // 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
zeroshade
left a comment
There was a problem hiding this comment.
Only one last thing to do: add a test case that does set the partition field Name and confirms that it returns that name 😄
Description
AddIdentity) and creates default partition field nameTesting
Added unit tests
TestGetPartitionFieldName