Skip to content

feat(table): add fanout partition writer and rolling data writer - #524

Merged
zeroshade merged 47 commits into
apache:mainfrom
badalprasadsingh:feat/partitioned-fanout-writer
Sep 23, 2025
Merged

zeroshade merged 47 commits into
apache:mainfrom
badalprasadsingh:feat/partitioned-fanout-writer

Conversation

@badalprasadsingh

Copy link
Copy Markdown
Contributor

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

  • Introduced parallel processing of arrow.Record 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.

Tests Performed

  • Compatible with all partition transforms
  • Handled null values in partition columns
  • Validated compatibility with partition spec evolution
  • Verified correctness for non-linear transformation cases
  • Confirmed schema evolution compatibility
  • Partition pruning verified

@zeroshade — would appreciate your review when you get a chance!

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]>
@kevinjqliu

Copy link
Copy Markdown
Contributor

Thanks for the PR @badalprasadsingh could you address the linter issue in CI?

@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.

Did a first pass and left some comments for start. Will need to do another tomorrow

Comment thread schema_conversions.go
switch typ := f.Type.(type) {
case Int32Type:
sc = internal.IntSchema
sc = internal.NullableSchema(internal.IntSchema)

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.

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.

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 a test case for this then?

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.

Sure

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.

test added

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 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?

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 we can do that.

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.

Was a test ever added for this?

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, in the schema_conversions_test.go file

@badalprasadsingh badalprasadsingh Sep 21, 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.

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.

Comment thread table/arrow_utils.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
Comment thread table/partitioned_fanout_writer.go Resolved
return iceberg.NewLiteral(iceberg.Time(arr.Value(row))), nil
case *array.Timestamp:
return iceberg.NewLiteral(iceberg.Timestamp(arr.Value(row))), nil
case *array.Decimal128:

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.

Decimal32/Decimal64?

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.

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.

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]>
@badalprasadsingh

Copy link
Copy Markdown
Contributor Author

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]>
@zeroshade

Copy link
Copy Markdown
Member

Hey @badalprasadsingh I'm having some Internet issues but I should be able to review this tomorrow

Signed-off-by: badalprasadsingh <[email protected]>
@badalprasadsingh

Copy link
Copy Markdown
Contributor Author

hi @zeroshade , please do provide a second review to the PR, once you get free.

Comment thread manifest.go Resolved
Comment thread manifest.go Resolved
Comment thread manifest.go
Comment on lines +1551 to +1560
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
}

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.

is this to ensure consistency?

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.

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.

@zeroshade

Copy link
Copy Markdown
Member

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.

Comment thread exprs.go Outdated
Comment on lines +487 to +507
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

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 the conversion is happening in avroPartitionData now, then we can remove this function, right?

Comment thread exprs.go Outdated
Comment on lines +516 to +551
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}
}
}
}
}
}

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 the conversion is happening in avroPartitionData then why would we have a map[string]any here?

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.

Totally agree! my apologies for making the type conversions so messy.

Comment thread manifest.go Resolved
Comment thread manifest.go Outdated
Comment on lines +1040 to +1069
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)
}
}

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 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?

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.

Totally agree! Fixed 🙌

Comment thread exprs.go Outdated
Comment on lines +487 to +507
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

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.

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.

Comment thread manifest.go Outdated
}

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 {

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.

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.

@badalprasadsingh badalprasadsingh Sep 12, 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.

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.

Comment thread manifest.go Outdated
if err != nil {
return v
}
fixedArray := convertToFixedArray(padOrTruncateBytes(bytes, fixedSize), fixedSize)

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 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.

Comment thread manifest.go
Comment on lines +1627 to +1628
arr := reflect.New(reflect.ArrayOf(size, reflect.TypeOf(byte(0)))).Elem()
reflect.Copy(arr, reflect.ValueOf(bytes))

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.

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.

Comment thread schema_conversions.go
switch typ := f.Type.(type) {
case Int32Type:
sc = internal.IntSchema
sc = internal.NullableSchema(internal.IntSchema)

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.

Was a test ever added for this?

Comment thread table/arrow_utils.go
return
}

partitionVal := statistics.PartitionValue(field, currentSchema)

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.

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.

@badalprasadsingh

badalprasadsingh commented Sep 12, 2025 •

Copy link
Copy Markdown
Contributor Author

My apologies @zeroshade, for messing up the expression handling.

Well we do have a test for internal.NullableSchema(internal.IntSchema) in schema_conversions_test.go file. I have even updated it to show how it fails in case of a non-nullable schema.

And as we need it, in hamba/avro library we can check the tests here, it depicts how map[string]any are used to encode into avro format.

And, thanks for pointing out the messing conversion logics.

Signed-off-by: badalprasadsingh <[email protected]>
Signed-off-by: badalprasadsingh <[email protected]>
Comment thread manifest.go Outdated
Comment on lines +1704 to +1709
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())

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.

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.

@badalprasadsingh badalprasadsingh 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.

okey

Comment thread manifest.go Resolved
Comment thread manifest_test.go Resolved
@badalprasadsingh

Copy link
Copy Markdown
Contributor Author

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 addEntry() function, we call avroPartitionData(), which converts the data into the respective maps.

While reading manifests, the decoded Avro types come as Go types, and we use the convertAvroValueToIcebergType function inside initializeMapData() to convert them back into the respective Iceberg literals.

The getFieldIDMap() func is used both during writing manifests and during reading manifests to get the fieldNameToID, fieldIDToType, fieldIDToSize (only during reading) maps. Thus, we need to have int type as a case as well (during writing).

@badalprasadsingh

Copy link
Copy Markdown
Contributor Author

For certain types like decimal and uuid, their primitive Avro types are fixed, which is absolutely correct according to the official Iceberg documentation. However, according to hamba/avro, their preferred primitive Avro types should be bytes and string, respectively. Therefore, to work with the fixed schema of both types, the use of maps becomes a necessity in a union schema.

Specifically, for decimal and uuid in the convertAvroValueToIcebergType function, we get the decoded values as a map[string]interface{} for the same reason. For decimal, the decoded value is returned as a big.Rat. To convert a big.Rat into a DecimalLiteral, we need to know its scale, which is why we store the decimal scale in the fieldIDToFixedSize map before encoding.

@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.

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!

Comment thread manifest.go
Comment on lines +1767 to +1782
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,
}
}
}
}

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 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.

@badalprasadsingh badalprasadsingh Sep 23, 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.

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?

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.

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

Comment thread schema_conversions.go
Comment on lines +57 to +58
// 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

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 file an issue on hamba to get this fixed?

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.

Sure.

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.

Update this comment with a link to the issue and then we're good to merge 😄

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.

Hey, @zeroshade , here is the link to the issue. 🙌

@zeroshade
zeroshade merged commit 4f01042 into apache:main Sep 23, 2025
@badalprasadsingh

Copy link
Copy Markdown
Contributor Author

Thank you so much @zeroshade ! This means a lot 🙇.

@badalprasadsingh
badalprasadsingh deleted the feat/partitioned-fanout-writer branch May 12, 2026 07:45
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