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 cdcf4605 fix(parquet/pqarrow): round-trip nested dictionaries without 
values (#1658)
cdcf4605 is described below

commit cdcf4605eb7f973747e03314c7f6a01e38219331
Author: Shivam Sharma <[email protected]>
AuthorDate: Mon Nov 23 23:22:39 2026 +0530

    fix(parquet/pqarrow): round-trip nested dictionaries without values (#1658)
    
    ### Rationale for this change
    
    Writing a record batch with a list, large list or map of dictionary
    values fails when all the lists in the batch are null or empty:
    
    ```
    unknown error type: runtime error: invalid memory address or nil pointer 
dereference
    ```
    
    `dictEncoder.PutIndices` reads the values buffer of the indices, which
    an empty array doesn't have.
    
    Once such a file can be written, reading it back gives dictionary values
    without a dictionary. `array.ValidateFull` accepts them, but printing
    the array panics with a nil dereference. `chunksToSingle` builds an
    array with empty buffers when there are no chunks, and a dictionary
    array also needs a dictionary.
    
    ### What changes are included in this PR?
    
    - `PutIndices` returns early for an empty array.
    - `chunksToSingle` uses `array.MakeArrayOfNull` for the empty array,
    which sets an empty dictionary for dictionary types.
    
    ### Are these changes tested?
    
    Yes. `TestArrowWriteNestedDictionaryWithoutValues` round-trips `[null]`,
    `[[]]` and `[null, [], null]` for `list<dictionary>` (int32 and int8
    indices), `large_list<dictionary>` and `map<string, dictionary>`, and
    compares the printed arrays. Without the writer change all 12 cases fail
    with the error above. Without the reader change, the 8 cases with an
    empty list fail.
    
    On Windows amd64 with Go 1.26.9 and the pinned `parquet-testing` data,
    `go test -tags assert` passes for `./parquet/pqarrow/...`,
    `./parquet/internal/encoding/...` and `./parquet/file/...`, and `go vet`
    is clean for the changed packages. The full test suite was not run
    locally.
    
    ### Are there any user-facing changes?
    
    These batches can now be written, and their values have an empty
    dictionary when read back. No API changes.
    
    This PR was prepared with AI assistance (Claude Code).
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 parquet/internal/encoding/encoder.go      |  5 ++++
 parquet/pqarrow/column_readers.go         |  8 +++++-
 parquet/pqarrow/encode_dictionary_test.go | 46 +++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/parquet/internal/encoding/encoder.go 
b/parquet/internal/encoding/encoder.go
index 09009246..b94a723d 100644
--- a/parquet/internal/encoding/encoder.go
+++ b/parquet/internal/encoding/encoder.go
@@ -288,6 +288,11 @@ func (d *dictEncoder) PutIndices(data arrow.Array) error {
                return fmt.Errorf("%w: passed non-integer array to PutIndices", 
arrow.ErrInvalid)
        }
 
+       if data.Len() == 0 {
+               // an empty array may not have a values buffer
+               return nil
+       }
+
        newValues := data.Len() - data.NullN()
        curPos := len(d.idxValues)
        newLen := newValues + curPos
diff --git a/parquet/pqarrow/column_readers.go 
b/parquet/pqarrow/column_readers.go
index 5a9be4ba..e70f674c 100644
--- a/parquet/pqarrow/column_readers.go
+++ b/parquet/pqarrow/column_readers.go
@@ -705,7 +705,13 @@ func newFixedSizeListReader(rctx *readerCtx, field 
*arrow.Field, info file.Level
 func chunksToSingle(chunked *arrow.Chunked, mem memory.Allocator) 
(arrow.ArrayData, error) {
        switch len(chunked.Chunks()) {
        case 0:
-               return array.NewData(chunked.DataType(), 0, 
[]*memory.Buffer{nil, nil}, nil, 0, 0), nil
+               // not just empty buffers: a dictionary array also needs a 
dictionary
+               empty := array.MakeArrayOfNull(mem, chunked.DataType(), 0)
+               defer empty.Release()
+
+               data := empty.Data()
+               data.Retain()
+               return data, nil
        case 1:
                data := chunked.Chunk(0).Data()
                data.Retain() // we pass control to the caller
diff --git a/parquet/pqarrow/encode_dictionary_test.go 
b/parquet/pqarrow/encode_dictionary_test.go
index 1e2a65bc..fca48d3c 100644
--- a/parquet/pqarrow/encode_dictionary_test.go
+++ b/parquet/pqarrow/encode_dictionary_test.go
@@ -1006,6 +1006,52 @@ func TestArrowWriteNestedSubfieldDictionary(t 
*testing.T) {
        assert.Truef(t, array.TableEqual(tbl, actual), "expected: %s\ngot: %s", 
tbl, actual)
 }
 
+func TestArrowWriteNestedDictionaryWithoutValues(t *testing.T) {
+       dictType := &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int32, ValueType: arrow.BinaryTypes.String}
+       smallDictType := &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int8, ValueType: arrow.BinaryTypes.String}
+
+       for _, dt := range []arrow.DataType{
+               arrow.ListOf(dictType),
+               arrow.ListOf(smallDictType),
+               arrow.LargeListOf(dictType),
+               arrow.MapOf(arrow.BinaryTypes.String, dictType),
+       } {
+               for _, json := range []string{`[null]`, `[[]]`, `[null, [], 
null]`} {
+                       t.Run(dt.String()+" "+json, func(t *testing.T) {
+                               mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                               defer mem.AssertSize(t, 0)
+
+                               arr, _, err := array.FromJSON(mem, dt, 
strings.NewReader(json))
+                               require.NoError(t, err)
+                               defer arr.Release()
+
+                               schema := arrow.NewSchema([]arrow.Field{{Name: 
"a", Type: dt, Nullable: true}}, nil)
+                               rec := array.NewRecordBatch(schema, 
[]arrow.Array{arr}, int64(arr.Len()))
+                               defer rec.Release()
+
+                               var buf bytes.Buffer
+                               wr, err := pqarrow.NewFileWriter(schema, &buf, 
parquet.NewWriterProperties(parquet.WithAllocator(mem)),
+                                       
pqarrow.NewArrowWriterProperties(pqarrow.WithStoreSchema(), 
pqarrow.WithAllocator(mem)))
+                               require.NoError(t, err)
+                               require.NoError(t, wr.Write(rec))
+                               require.NoError(t, wr.Close())
+
+                               tbl, err := 
pqarrow.ReadTable(context.Background(), bytes.NewReader(buf.Bytes()),
+                                       parquet.NewReaderProperties(mem), 
pqarrow.ArrowReadProperties{}, mem)
+                               require.NoError(t, err)
+                               defer tbl.Release()
+
+                               require.EqualValues(t, arr.Len(), tbl.NumRows())
+                               require.Len(t, tbl.Column(0).Data().Chunks(), 1)
+                               got := tbl.Column(0).Data().Chunk(0)
+                               require.NoError(t, array.ValidateFull(got))
+                               // the values have a dictionary, so they can be 
printed
+                               assert.Equal(t, arr.String(), got.String())
+                       })
+               }
+       }
+}
+
 func TestDictOfEmptyStringsRoundtrip(t *testing.T) {
        mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
        defer mem.AssertSize(t, 0)

Reply via email to