This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 1b78e7ae fix(parquet/variant): clear totalDictSize on Builder.Reset 
(#1677)
1b78e7ae is described below

commit 1b78e7ae81b5b647a0c0e9121bee6b2a66ad9d8b
Author: 小哥不太逍遥 <[email protected]>
AuthorDate: Thu Nov 26 02:40:02 2026 +0800

    fix(parquet/variant): clear totalDictSize on Builder.Reset (#1677)
    
    ### Rationale for this change
    
    `(*variant.Builder).Reset()` resets `b.buf`, `b.dict`, and `b.dictKeys`,
    but does not reset `b.totalDictSize`.
    
    When a `variant.Builder` is reused across `Reset()` calls, `Build()`
    computes `maxSize`, `offsetSize`, and `metadataSize` using the
    accumulated `b.totalDictSize` from all prior builds:
    
    1. Subsequent `Metadata.Bytes()` slices are padded with trailing zero
    bytes (`len(m.Bytes()) > m.SizeBytes()`), and `OffsetSize()` is inflated
    above 1 byte once prior keys exceed 255 bytes even when building a
    primitive or small-key object.
    2. Reusing a `Builder` in a loop eventually causes `Build()` to fail
    with `metadata size too large` once cumulative historical key bytes
    exceed 128 MiB, even if each individual value has a tiny dictionary.
    
    ### What changes are included in this PR?
    
    - Reset `b.totalDictSize = 0` in `(*variant.Builder).Reset()`
    (`parquet/variant/builder.go`).
    - Add `TestBuilderResetClearsTotalDictSize` in
    `parquet/variant/builder_test.go` covering empty metadata after a
    large-key build, small-key metadata after reset, and repeated reset
    reuse.
    
    ### Are these changes tested?
    
    Yes. Confirmed `TestBuilderResetClearsTotalDictSize` fails before the
    fix (`len(m.Bytes()) == 305`, `OffsetSize() == 2` instead of `3` / `1`)
    and passes after the fix under:
    
    - `go test -race -count=1 -tags "assert,test" ./parquet/variant/...`
    - `go test -count=1 -tags "noasm,assert,test" ./parquet/variant/...`
    - `go vet ./parquet/variant/...` and `gofmt -l`
    
    ### Are there any user-facing changes?
    
    No breaking API changes; `Builder.Reset()` now produces compact metadata
    matching freshly constructed builders and no longer accumulates
    dictionary byte counts across resets.
---
 parquet/variant/builder.go      |  1 +
 parquet/variant/builder_test.go | 46 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/parquet/variant/builder.go b/parquet/variant/builder.go
index cf6a6ce8..efba27a6 100644
--- a/parquet/variant/builder.go
+++ b/parquet/variant/builder.go
@@ -821,6 +821,7 @@ func (b *Builder) Reset() {
                b.dictKeys[i] = nil
        }
        b.dictKeys = b.dictKeys[:0]
+       b.totalDictSize = 0
 }
 
 // BuildWithoutMeta returns just the raw variant bytes that were built without
diff --git a/parquet/variant/builder_test.go b/parquet/variant/builder_test.go
index 98a4d7f7..e7d8bf42 100644
--- a/parquet/variant/builder_test.go
+++ b/parquet/variant/builder_test.go
@@ -18,6 +18,7 @@ package variant_test
 
 import (
        "encoding/json"
+       "strings"
        "testing"
        "time"
 
@@ -784,6 +785,51 @@ func TestAppendReset(t *testing.T) {
        assert.JSONEq(t, `{"key": "value"}`, string(out3))
 }
 
+func TestBuilderResetClearsTotalDictSize(t *testing.T) {
+       var b variant.Builder
+
+       longKey := strings.Repeat("k", 300)
+       require.NoError(t, b.Append(map[string]any{longKey: 1}))
+       v1, err := b.Build()
+       require.NoError(t, err)
+       require.Greater(t, v1.Metadata().OffsetSize(), uint8(1))
+       v1Len := len(v1.Metadata().Bytes())
+
+       // After Reset, building a primitive must produce compact empty metadata
+       // with no trailing padding or inflated offset width from prior builds.
+       b.Reset()
+       require.NoError(t, b.AppendInt(42))
+       v2, err := b.Build()
+       require.NoError(t, err)
+
+       m2 := v2.Metadata()
+       assert.Equal(t, uint8(1), m2.OffsetSize())
+       assert.Equal(t, variant.EmptyMetadataBytes[:], m2.Bytes())
+       assert.Equal(t, len(m2.Bytes()), m2.SizeBytes())
+
+       // Building an object with a short key after Reset must also use 1-byte 
offsets
+       // and exact-length metadata bytes.
+       b.Reset()
+       require.NoError(t, b.Append(map[string]any{"a": 7}))
+       v3, err := b.Build()
+       require.NoError(t, err)
+
+       m3 := v3.Metadata()
+       assert.Equal(t, uint8(1), m3.OffsetSize())
+       assert.Equal(t, len(m3.Bytes()), m3.SizeBytes())
+
+       // Repeated resets with the same key must keep metadata length constant
+       // instead of accumulating totalDictSize across builds.
+       for range 5 {
+               b.Reset()
+               require.NoError(t, b.Append(map[string]any{longKey: 1}))
+               v, err := b.Build()
+               require.NoError(t, err)
+               assert.Equal(t, v1Len, len(v.Metadata().Bytes()))
+               assert.Equal(t, len(v.Metadata().Bytes()), 
v.Metadata().SizeBytes())
+       }
+}
+
 func TestBuilderFromJSON(t *testing.T) {
        tests := []struct {
                name  string

Reply via email to