Repository navigation
feat(table): add fanout partition writer and rolling data writer - #524
Conversation
This completes the implementation of partitioned writing with support for rolling data files in append mode. It enables efficient, parallelized ingestion into partitioned tables while maintaining manifest and snapshot correctness. Details - Introduced parallel processing of `arrow.Record`s using a user-defined number of goroutines. - Each goroutine maintains its own hash table to map partition keys to row indices. - After partitioning, `compute.Take()` is used to extract per-partition data slices. - Integrated dedicated rolling writers per partition to manage data file size thresholds and output constraints. Signed-off-by: badalprasadsingh <[email protected]>
|
Thanks for the PR @badalprasadsingh could you address the linter issue in CI? |
zeroshade
left a comment
There was a problem hiding this comment.
Did a first pass and left some comments for start. Will need to do another tomorrow
| switch typ := f.Type.(type) { | ||
| case Int32Type: | ||
| sc = internal.IntSchema | ||
| sc = internal.NullableSchema(internal.IntSchema) |
There was a problem hiding this comment.
This change ensures the schema allows nullable data. Without it, if the entire column contains null values, it triggers the error: PartitionData: col: avro: *avro.null. Wrapping inside internal.NullableSchema prevents this issue.
There was a problem hiding this comment.
Can we add a test case for this then?
There was a problem hiding this comment.
test added
There was a problem hiding this comment.
Can we update the tests that were failing to allow the nullable schema? Alternately, can we detect whether or not there are nulls and only use the nullable schema when nulls are present?
There was a problem hiding this comment.
Yes we can do that.
There was a problem hiding this comment.
Was a test ever added for this?
There was a problem hiding this comment.
Yes, in the schema_conversions_test.go file
There was a problem hiding this comment.
well @zeroshade , in avro, if we want a field to allow null values, then we must define its type as a union that includes null.
and, partitioned columns are always kept optional which means they are nullable, so I think we can simply keep all the columns in partitionTypeToAvroSchema as nullable.
| return iceberg.NewLiteral(iceberg.Time(arr.Value(row))), nil | ||
| case *array.Timestamp: | ||
| return iceberg.NewLiteral(iceberg.Timestamp(arr.Value(row))), nil | ||
| case *array.Decimal128: |
There was a problem hiding this comment.
well, arrow-go currently does not supports compute.Take on a Decimal64 or Decimal32 column. It throws the error: function 'array_take' has no kernel matching input types (decimal32(9, 2), int64). Yet, added the case.
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Updated Avro serialization logic to correctly handle decimal logical types and applied suggested code refactoring from PR review. Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
|
Hey @zeroshade , I’ve addressed all the review comments. Could you please take another look at the PR when you get a chance? |
Signed-off-by: badalprasadsingh <[email protected]>
|
Hey @badalprasadsingh I'm having some Internet issues but I should be able to review this tomorrow |
Signed-off-by: badalprasadsingh <[email protected]>
|
hi @zeroshade , please do provide a second review to the PR, once you get free. |
| func convertDateValue(v any) any { | ||
| if t, ok := v.(time.Time); ok { | ||
| return map[string]any{"int.date": int32(t.Truncate(24*time.Hour).Unix() / int64((time.Hour * 24).Seconds()))} | ||
| } | ||
| if d, ok := v.(Date); ok { | ||
| return map[string]any{"int.date": int32(d)} | ||
| } | ||
|
|
||
| return v | ||
| } |
There was a problem hiding this comment.
is this to ensure consistency?
There was a problem hiding this comment.
Well, since we are using nullable schema, the map pattern like
map[string]any{"int.date": int32(value)}was created as a way to explicitly tag which union member should be used:
return map[string]any{"int.date": int32(d)}The key "int.date" serves as a type hint to resolve the union ambiguity.
|
we're almost there! just a few more comments left. Thanks for your persistence and work on this @badalprasadsingh Once we get this merged I think it'll be good for us to cut a release. |
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
fix race condition in partitioned fanout writer Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
| if m, ok := v.(map[string]any); ok { | ||
| if val, exists := m["long.timestamp-micros"]; exists { | ||
| if microseconds, ok := val.(int64); ok { | ||
| return Timestamp(microseconds) | ||
| } | ||
| } | ||
|
|
||
| if val, exists := m["int.date"]; exists { | ||
| if days, ok := val.(int32); ok { | ||
| return days | ||
| } | ||
| } | ||
|
|
||
| if val, exists := m["long.time-micros"]; exists { | ||
| if microseconds, ok := val.(int64); ok { | ||
| return Time(microseconds) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return v |
There was a problem hiding this comment.
if the conversion is happening in avroPartitionData now, then we can remove this function, right?
| if unionMap, ok := v.(map[string]any); ok { | ||
| switch b.field.Type.(type) { | ||
| case DecimalType: | ||
| if fixedVal, exists := unionMap["fixed"]; exists { | ||
| if fixedArray := reflect.ValueOf(fixedVal); fixedArray.Kind() == reflect.Array { | ||
| bytes := make([]byte, fixedArray.Len()) | ||
| for i := range bytes { | ||
| bytes[i] = fixedArray.Index(i).Interface().(byte) | ||
| } | ||
|
|
||
| var decimal DecimalLiteral | ||
| if err := decimal.UnmarshalBinary(bytes); err == nil { | ||
| if decType, ok := b.field.Type.(DecimalType); ok { | ||
| result := Decimal{Val: decimal.Val, Scale: decType.Scale()} | ||
| if converted, ok := any(result).(T); ok { | ||
| return Optional[T]{Valid: true, Val: converted} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| case UUIDType: | ||
| if uuidVal, exists := unionMap["uuid"]; exists { | ||
| if uuidArray := reflect.ValueOf(uuidVal); uuidArray.Kind() == reflect.Array && uuidArray.Len() == 16 { | ||
| var uuidBytes [16]byte | ||
| for i := 0; i < 16; i++ { | ||
| uuidBytes[i] = uuidArray.Index(i).Interface().(byte) | ||
| } | ||
| result := uuid.UUID(uuidBytes) | ||
| if converted, ok := any(result).(T); ok { | ||
| return Optional[T]{Valid: true, Val: converted} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
if the conversion is happening in avroPartitionData then why would we have a map[string]any here?
There was a problem hiding this comment.
Totally agree! my apologies for making the type conversions so messy.
| if unionMap, ok := value.(map[string]interface{}); ok { | ||
| switch field.Type.(type) { | ||
| case DecimalType: | ||
| decType := field.Type.(DecimalType) | ||
| if fixedBytes, ok := unionMap["fixed"]; ok { | ||
| if bytes := extractBytesFromFixed(fixedBytes); bytes != nil { | ||
| decLit := DecimalLiteral{Scale: decType.Scale()} | ||
| if err := decLit.UnmarshalBinary(bytes); err == nil { | ||
| value = decLit.Value() | ||
| } | ||
| } | ||
| } | ||
| case TimeType: | ||
| if longVal, ok := unionMap["long.time-micros"]; ok { | ||
| if microseconds, ok := longVal.(int64); ok { | ||
| value = Time(microseconds) | ||
| } | ||
| } | ||
| case TimestampType: | ||
| if longVal, ok := unionMap["long.timestamp-micros"]; ok { | ||
| if microseconds, ok := longVal.(int64); ok { | ||
| value = Timestamp(microseconds) | ||
| } | ||
| } | ||
| case TimestampTzType: | ||
| if longVal, ok := unionMap["long.timestamp-micros"]; ok { | ||
| if microseconds, ok := longVal.(int64); ok { | ||
| value = Timestamp(microseconds) | ||
| } | ||
| } |
There was a problem hiding this comment.
If the conversion is happening during read and write of avroPartitionData, shouldn't we never have a map here? We should always have the properly typed values right?
There was a problem hiding this comment.
Totally agree! Fixed 🙌
| if m, ok := v.(map[string]any); ok { | ||
| if val, exists := m["long.timestamp-micros"]; exists { | ||
| if microseconds, ok := val.(int64); ok { | ||
| return Timestamp(microseconds) | ||
| } | ||
| } | ||
|
|
||
| if val, exists := m["int.date"]; exists { | ||
| if days, ok := val.(int32); ok { | ||
| return days | ||
| } | ||
| } | ||
|
|
||
| if val, exists := m["long.time-micros"]; exists { | ||
| if microseconds, ok := val.(int64); ok { | ||
| return Time(microseconds) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return v |
There was a problem hiding this comment.
the conversion should happen inside of ToDataFile not here. We should never have the map representation here. Specifically, it should be happening in DataFileStatistics.PartitionValue, that's where we should convert from the literal to the map representation.
| } | ||
|
|
||
| func avroPartitionData(input map[int]any, logicalTypes map[int]avro.LogicalType) map[int]any { | ||
| func avroPartitionData(input map[int]any, logicalTypes map[int]avro.LogicalType, fixedSizes map[int]int) map[int]any { |
There was a problem hiding this comment.
this function is only used for reading the partition data (not writing it) so the values in the map should be the actual typed values, not map[string]any.
There was a problem hiding this comment.
Well sorry for the misunderstanding, I have updated my implementation.
The avroPartitionData func is now used only to write into the manifest. In the addEntry func which would ultimately do the avro encoding at w.writer.Encode(toEncode)
So, converting the partition values into map format become a necessity here.
| if err != nil { | ||
| return v | ||
| } | ||
| fixedArray := convertToFixedArray(padOrTruncateBytes(bytes, fixedSize), fixedSize) |
There was a problem hiding this comment.
Then that should be happening inside of DataFileStatistics.PartitionValue or in DataFileStatistics.ToDataFile. That's where we perform the writes. This code path only happens on read, so I don't know why we're doing this here.
| arr := reflect.New(reflect.ArrayOf(size, reflect.TypeOf(byte(0)))).Elem() | ||
| reflect.Copy(arr, reflect.ValueOf(bytes)) |
There was a problem hiding this comment.
as with the other sections, I'm still unsure here. This codepath seems to only be used during read, so we should never have to produce the map representation here. The avro library itself should perform the conversions for us when dealing with fixed size array things.
| switch typ := f.Type.(type) { | ||
| case Int32Type: | ||
| sc = internal.IntSchema | ||
| sc = internal.NullableSchema(internal.IntSchema) |
There was a problem hiding this comment.
Was a test ever added for this?
| return | ||
| } | ||
|
|
||
| partitionVal := statistics.PartitionValue(field, currentSchema) |
There was a problem hiding this comment.
this is where the conversion should happen if we need the map representation, though we could also just register the Date/Timestamp/Decimal etc. types to use the appropriate fields right? That would enable us to avoid having to create the map representations ourselves.
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
|
My apologies @zeroshade, for messing up the expression handling. Well we do have a test for And as we need it, in And, thanks for pointing out the messing conversion logics. |
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
| case avro.Date: | ||
| days := int32(t.Truncate(24*time.Hour).Unix() / int64((time.Hour * 24).Seconds())) | ||
|
|
||
| return Date(days) | ||
| case avro.TimestampMicros: | ||
| return Timestamp(t.UTC().UnixMicro()) |
There was a problem hiding this comment.
what about TimeMillis, TimeMicros, and TimestampMillis? Would make more sense to grab the old implementation of avroPartitionData and use that since that is already what we were using for reading.
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
|
Hey @zeroshade, I have updated the workflow. While writing a manifest, we need to encode it into Avro types. So, just before encoding in the While reading manifests, the decoded Avro types come as Go types, and we use the The |
|
For certain types like Specifically, for |
zeroshade
left a comment
There was a problem hiding this comment.
Just two comments.
I think we're probably okay to merge at this point even if it's not perfect. This is a large enough PR and most of the remaining things I can think of are all performance optimizations and slight refactors that could be done later on while people really want this functionality now. So we can get this out, and then optimize it further afterwards.
So please just address the two comments below and then we can get this merged.
Once again, thanks so much for staying on top of this. I know it's been a long back-and-forth but this is definitely a great step in the right direction for this project! Thanks!
| if unionMap, ok := v.(map[string]interface{}); ok { | ||
| if val, ok := unionMap["fixed"]; ok { | ||
| if bigRatValue, ok := val.(*big.Rat); ok { | ||
| scale := d.fieldIDToFixedSize[fieldID] | ||
| scaleFactor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil) | ||
| unscaled := new(big.Int).Mul(bigRatValue.Num(), scaleFactor) | ||
| unscaled = unscaled.Div(unscaled, bigRatValue.Denom()) | ||
| decimal128Val := decimal128.FromBigInt(unscaled) | ||
|
|
||
| return DecimalLiteral{ | ||
| Scale: scale, | ||
| Val: decimal128Val, | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I think it would be better and more performant if we instead register []byte as the type for reading in a decimal and then just used UnmarshalBinary etc. for the decimal. It doesn't make sense for hamba avro to do all the computation to generate the Rat only for us to undo that.
There was a problem hiding this comment.
Well, according to hamba/avro, for both fixed.Decimal and bytes.Decimal, a decimal is returned in the form of *big.Rat.
So, we have to convert it to iceberg.Decimal from *big.Rat anyway.
One way to avoid *big.Rat is to define the Avro union schema of decimal as:
["null", "bytes"]With this, we can simply use (scale is still needed to be stored while writing):
decLit.UnmarshalBinary(decoded.([]byte))But then it would no longer be a logical type, making it difficult to identify as a decimal schema while reading in the getFieldIDMap() function.
So, should we do it? or, can we pick it up as a performance improvement later?
There was a problem hiding this comment.
We can pick it up as a performance improvement later on, but I was more considering overriding the registered type so that we connect it to []byte instead of big.Rat
| // Currently the hamba/avro library couldn't resolve the [n]byte array types for fixed schemas in unions. | ||
| // TODO: Create the proper Fixed Schema for Avro that can match the use case |
There was a problem hiding this comment.
can we file an issue on hamba to get this fixed?
There was a problem hiding this comment.
Update this comment with a link to the issue and then we're good to merge 😄
There was a problem hiding this comment.
Hey, @zeroshade , here is the link to the issue. 🙌
Updated TODO comment for Fixed Schema in Avro.
|
Thank you so much @zeroshade ! This means a lot 🙇. |
Partitioned Fanout Writer with Rolling Data File Support (Append Mode)
This PR completes the implementation of partitioned writing with support for rolling data files in append mode. It enables efficient, parallelized ingestion into partitioned tables while maintaining manifest and snapshot correctness.
Slack Thread Discussion: Link
Proposal Document: Google Drive
Details
arrow.Recordusing a user-defined number of goroutines.compute.Take()is used to extract per-partition data slices.Tests Performed
@zeroshade — would appreciate your review when you get a chance!