lliangyu-lin commented on code in PR #468: URL: https://github.com/apache/iceberg-go/pull/468#discussion_r2167743712
########## partitions.go: ########## @@ -293,3 +293,77 @@ func AssignFreshPartitionSpecIDs(spec *PartitionSpec, old, fresh *Schema) (Parti return NewPartitionSpec(newFields...), nil } + +type PartitionSpecVisitor[T any] interface { + Identity(fieldId int, sourceName string, sourceId int) T + Bucket(fieldId int, sourceName string, sourceId int, numBuckets int) T + Truncate(fieldId int, sourceName string, sourceId int, width int) T + Year(fieldId int, sourceName string, sourceId int) T + Month(fieldId int, sourceName string, sourceId int) T + Day(fieldId int, sourceName string, sourceId int) T + Hour(fieldId int, sourceName string, sourceId int) T + Void(fieldId int, sourceName string, sourceId int) T +} + +type PartitionNameGenerator struct{} + +func (g PartitionNameGenerator) Identity(fieldID int, sourceName string, sourceID int) string { + return sourceName +} + +func (g PartitionNameGenerator) Bucket(fieldID int, sourceName string, sourceID int, numBuckets int) string { + return fmt.Sprintf("%s_bucket_%d", sourceName, numBuckets) +} + +func (g PartitionNameGenerator) Truncate(fieldID int, sourceName string, sourceID int, width int) string { + return fmt.Sprintf("%s_trunc_%d", sourceName, width) +} + +func (g PartitionNameGenerator) Year(fieldID int, sourceName string, sourceID int) string { + return sourceName + "_year" +} + +func (g PartitionNameGenerator) Month(fieldID int, sourceName string, sourceID int) string { + return sourceName + "_month" +} + +func (g PartitionNameGenerator) Day(fieldID int, sourceName string, sourceID int) string { + return sourceName + "_day" +} + +func (g PartitionNameGenerator) Hour(fieldID int, sourceName string, sourceID int) string { + return sourceName + "_hour" +} + +func (g PartitionNameGenerator) Void(fieldID int, sourceName string, sourceID int) string { + return sourceName + "_null" +} + +func VisitPartitionField[R any](schema *Schema, field PartitionField, visitor PartitionSpecVisitor[R]) (R, error) { + sourceName, exists := schema.FindColumnName(field.SourceID) + var empty R + if !exists { + return empty, fmt.Errorf("could not find field with id %d", field.SourceID) + } + transform := field.Transform + switch t := transform.(type) { + case IdentityTransform: + return visitor.Identity(field.FieldID, sourceName, field.SourceID), nil + case BucketTransform: + return visitor.Bucket(field.FieldID, sourceName, field.SourceID, t.NumBuckets), nil + case TruncateTransform: + return visitor.Truncate(field.FieldID, sourceName, field.SourceID, t.Width), nil + case DayTransform: + return visitor.Day(field.FieldID, sourceName, field.SourceID), nil + case HourTransform: + return visitor.Hour(field.FieldID, sourceName, field.SourceID), nil + case MonthTransform: + return visitor.Month(field.FieldID, sourceName, field.SourceID), nil + case YearTransform: + return visitor.Year(field.FieldID, sourceName, field.SourceID), nil + case VoidTransform: + return visitor.Void(field.FieldID, sourceName, field.SourceID), nil + default: + return empty, fmt.Errorf("unknown transform type: %T", transform) Review Comment: Maybe we should just panic here. Would see what other people thinks -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org