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 3735d553 fix(parquet/file): return an error when a page holds fewer
values than its header claims (#1565)
3735d553 is described below
commit 3735d5532b43c8b9db6b563fa1b9150fdbdd7f98
Author: Harrison Crosse <[email protected]>
AuthorDate: Tue Nov 17 14:31:14 2026 -0500
fix(parquet/file): return an error when a page holds fewer values than its
header claims (#1565)
### Rationale for this change
On a malformed required page that claims more values than it holds,
`ReadBatch`, `Skip` and `SeekToRow` loop forever, including seeks
through `pqarrow`'s `RecordReader`. On main, `pqarrow.ReadTable` returns
an error.
I reproduced this on v18.8.0 and main, with Go 1.26.10 on macOS arm64. I
wrote ten required `int32` values in one uncompressed
`DELTA_BINARY_PACKED` page and set the delta header's count to 2:
```go
// delta header: block size 128, 4 miniblocks, 10 values
i := bytes.Index(data, []byte{0x80, 0x01, 0x04, 0x0a})
if i < 0 {
panic("delta header not found")
}
data[i+3] = 0x02
rdr, _ := file.NewParquetReader(bytes.NewReader(data))
col, _ := rdr.RowGroup(0).Column(0)
vals := make([]int32, 10)
total, n, err := col.(*file.Int32ColumnChunkReader).ReadBatch(10, vals,
nil, nil)
```
On both versions, `ReadBatch(10)` did not return within five seconds.
### What changes are included in this PR?
`readBatchInPage` and `skipRows` return an error when a nonempty batch
makes no progress. The read guard matches C++:
https://github.com/apache/arrow/blob/976779b2aac587f51b87c3bd2e6ae4c36f75ab6c/cpp/src/parquet/column_reader.cc#L1666-L1674
`Skip` fails immediately. C++ fails on the next `ReadBatch`. As in C++,
optional columns are unchanged. The guard adds one comparison per batch.
### Are these changes tested?
`TestReadBatchPageHoldsFewerValuesThanHeader`,
`TestReadBatchInPageReportsNoProgress` and
`TestSkipPageHoldsFewerValuesThanHeader` fail on main.
`TestShortPageGuardKeepsDecoderError` and
`TestShortPageGuardLeavesConsistentPagesAlone` check decoder errors,
null pages and zero sized requests. All five pass with this change.
Full `go test -race ./parquet/...` passed on linux/amd64 with pinned
test data, and focused tests and `golangci-lint` 2.13.2 passed on macOS
arm64.
### Are there any user-facing changes?
Yes. `ReadBatch`, `ReadBatchInPage`, `Skip` and `SeekToRow` return
errors for these pages. There is no API change.
---
parquet/file/column_reader.go | 13 ++
.../file/column_reader_short_page_internal_test.go | 227 +++++++++++++++++++++
2 files changed, 240 insertions(+)
diff --git a/parquet/file/column_reader.go b/parquet/file/column_reader.go
index e3a64d15..827f3382 100644
--- a/parquet/file/column_reader.go
+++ b/parquet/file/column_reader.go
@@ -697,6 +697,10 @@ func (c *columnChunkReader) skipRows(nrows int64) (int64,
error) {
}
skipped = max(ndefs, skipped)
+ if skipped == 0 {
+ c.err = errNoProgress(min(toSkip,
c.numAvailValues()))
+ return nrows - toSkip, c.err
+ }
toSkip -= int64(skipped)
c.consumeBufferedValues(int64(skipped))
@@ -815,7 +819,16 @@ func (c *columnChunkReader) readBatchInPage(batchSize
int64, totalRead int64, de
// levels stored with it and `read` will be the number of values,
otherwise
// we use ndefs since it will be equal to or greater than read.
lvls = int64(utils.Max(ndefs, read))
+ // A decoder can hold fewer values than the page header claims. Zero
progress
+ // without an error would leave callers that loop on HasNext spinning.
+ if lvls == 0 && err == nil && batchSize > 0 {
+ return 0, 0, errNoProgress(min(batchSize, c.numAvailValues()))
+ }
c.consumeBufferedValues(lvls)
return lvls, read, err
}
+
+func errNoProgress(expected int64) error {
+ return fmt.Errorf("parquet: decoded no values, expected %d: the page
holds fewer values than its header claims", expected)
+}
diff --git a/parquet/file/column_reader_short_page_internal_test.go
b/parquet/file/column_reader_short_page_internal_test.go
new file mode 100644
index 00000000..d9a515b4
--- /dev/null
+++ b/parquet/file/column_reader_short_page_internal_test.go
@@ -0,0 +1,227 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package file
+
+import (
+ "encoding/binary"
+ "testing"
+
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ "github.com/apache/arrow-go/v18/parquet"
+ "github.com/apache/arrow-go/v18/parquet/compress"
+ "github.com/apache/arrow-go/v18/parquet/internal/encoding"
+ format "github.com/apache/arrow-go/v18/parquet/internal/gen-go/parquet"
+ "github.com/apache/arrow-go/v18/parquet/schema"
+ "github.com/stretchr/testify/require"
+)
+
+const shortPageMessage = "the page holds fewer values than its header claims"
+
+// maxDecoderCalls bounds how often a reader may call its decoder. A reader
+// that spins on a decoder returning nothing exceeds it and fails the test
+// instead of hanging it.
+const maxDecoderCalls = 16
+
+type countingInt32Decoder struct {
+ encoding.Int32Decoder
+ t *testing.T
+ calls int
+}
+
+func (d *countingInt32Decoder) tick() {
+ d.t.Helper()
+ if d.calls++; d.calls > maxDecoderCalls {
+ d.t.Fatalf("decoder called more than %d times: the reader is
not making progress", maxDecoderCalls)
+ }
+}
+
+func (d *countingInt32Decoder) Decode(out []int32) (int, error) {
+ d.tick()
+ return d.Int32Decoder.Decode(out)
+}
+
+func (d *countingInt32Decoder) Discard(n int) (int, error) {
+ d.tick()
+ return d.Int32Decoder.Discard(n)
+}
+
+type onePagePager struct {
+ page Page
+ read bool
+}
+
+func (p *onePagePager) SetMaxPageHeaderSize(int) {}
+func (p *onePagePager) Page() Page {
+ if !p.read {
+ return nil
+ }
+ return p.page
+}
+func (p *onePagePager) Next() bool {
+ was := p.read
+ p.read = true
+ return !was
+}
+func (p *onePagePager) Err() error { return nil }
+func (p *onePagePager) Reset(parquet.BufferedReader, int64,
compress.Compression, *CryptoContext) {
+}
+func (p *onePagePager) GetDictionaryPage() (*DictionaryPage, error) { return
nil, nil }
+func (p *onePagePager) SeekToPageWithRow(int64) error {
+ p.read = true
+ return nil
+}
+func (p *onePagePager) Close() error { return nil }
+
+func shortPageDescr(t *testing.T, rep parquet.Repetition) *schema.Column {
+ t.Helper()
+ node, err := schema.NewPrimitiveNode("v", rep, parquet.Types.Int32, -1,
-1)
+ require.NoError(t, err)
+ maxDef := int16(0)
+ if rep == parquet.Repetitions.Optional {
+ maxDef = 1
+ }
+ return schema.NewColumn(node, maxDef, 0)
+}
+
+// shortPage returns a v1 data page whose header claims numValues values while
+// its payload encodes only vals. A non-nil defLevels is written as an RLE
level block.
+func shortPage(t *testing.T, descr *schema.Column, enc parquet.Encoding,
numValues int, defLevels []int16, vals []int32) *DataPageV1 {
+ t.Helper()
+ mem := memory.DefaultAllocator
+ var body []byte
+ if defLevels != nil {
+ buf :=
encoding.NewBufferWriter(encoding.LevelEncodingMaxBufferSize(parquet.Encodings.RLE,
1, len(defLevels)), mem)
+ var lvlEnc encoding.LevelEncoder
+ lvlEnc.Init(parquet.Encodings.RLE, 1, buf)
+ _, err := lvlEnc.Encode(defLevels)
+ require.NoError(t, err)
+ body = binary.LittleEndian.AppendUint32(nil,
uint32(lvlEnc.Len()))
+ body = append(body, buf.Bytes()[:lvlEnc.Len()]...)
+ }
+
+ valEnc := encoding.NewEncoder(parquet.Types.Int32, enc, false, descr,
mem)
+ defer valEnc.Release()
+ valEnc.(encoding.Int32Encoder).Put(vals)
+ encoded, err := valEnc.FlushValues()
+ require.NoError(t, err)
+ defer encoded.Release()
+ body = append(body, encoded.Bytes()...)
+
+ return NewDataPageV1WithConfig(memory.NewBufferBytes(body),
parquet.Encodings.RLE, parquet.Encodings.RLE,
+ DataPageConfig{Num: int32(numValues), Encoding: enc,
UncompressedSize: int32(len(body))})
+}
+
+func newInt32Reader(t *testing.T, descr *schema.Column, enc parquet.Encoding,
page Page) *Int32ColumnChunkReader {
+ t.Helper()
+ r := NewColumnReader(descr, &onePagePager{page: page},
memory.DefaultAllocator, nil).(*Int32ColumnChunkReader)
+ r.decoders[format.Encoding(enc)] = &countingInt32Decoder{
+ Int32Decoder: encoding.NewDecoder(parquet.Types.Int32, enc,
descr, memory.DefaultAllocator).(encoding.Int32Decoder),
+ t: t,
+ }
+ return r
+}
+
+func TestReadBatchPageHoldsFewerValuesThanHeader(t *testing.T) {
+ for name, enc := range map[string]parquet.Encoding{
+ "delta binary packed": parquet.Encodings.DeltaBinaryPacked,
+ "byte stream split": parquet.Encodings.ByteStreamSplit,
+ } {
+ t.Run(name, func(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Required)
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr,
enc, 10, nil, []int32{7, 8}))
+ out := make([]int32, 10)
+ total, read, err := r.ReadBatch(10, out, nil, nil)
+ require.ErrorContains(t, err, shortPageMessage)
+ require.Equal(t, int64(2), total)
+ require.Equal(t, []int32{7, 8}, out[:read])
+ })
+ }
+}
+
+func TestReadBatchInPageReportsNoProgress(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Required)
+ enc := parquet.Encodings.DeltaBinaryPacked
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr, enc, 10, nil,
[]int32{7, 8}))
+ out := make([]int32, 10)
+
+ total, read, err := r.ReadBatchInPage(10, out, nil, nil)
+ require.NoError(t, err)
+ require.Equal(t, int64(2), total)
+ require.Equal(t, []int32{7, 8}, out[:read])
+
+ total, read, err = r.ReadBatchInPage(10, out, nil, nil)
+ require.ErrorContains(t, err, shortPageMessage)
+ require.ErrorContains(t, err, "expected 8")
+ require.Zero(t, total)
+ require.Zero(t, read)
+}
+
+func TestSkipPageHoldsFewerValuesThanHeader(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Required)
+ enc := parquet.Encodings.DeltaBinaryPacked
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr, enc, 10, nil,
[]int32{7, 8}))
+
+ _, err := r.Skip(5)
+ require.ErrorContains(t, err, shortPageMessage)
+ require.ErrorContains(t, err, "expected 3")
+ require.ErrorContains(t, r.Err(), shortPageMessage)
+}
+
+func TestSeekToRowPageHoldsFewerValuesThanHeader(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Required)
+ enc := parquet.Encodings.DeltaBinaryPacked
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr, enc, 10, nil,
[]int32{7, 8}))
+
+ err := r.SeekToRow(5)
+ require.ErrorContains(t, err, shortPageMessage)
+ require.ErrorContains(t, err, "expected 3")
+ require.ErrorContains(t, r.Err(), shortPageMessage)
+}
+
+func TestShortPageGuardKeepsDecoderError(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Required)
+ enc := parquet.Encodings.Plain
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr, enc, 10, nil,
[]int32{7, 8}))
+
+ _, _, err := r.ReadBatch(10, make([]int32, 10), nil, nil)
+ require.ErrorContains(t, err, "eof exception")
+ require.NotContains(t, err.Error(), shortPageMessage)
+}
+
+func TestShortPageGuardLeavesConsistentPagesAlone(t *testing.T) {
+ enc := parquet.Encodings.DeltaBinaryPacked
+
+ t.Run("optional page of only nulls", func(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Optional)
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr, enc, 10,
make([]int16, 10), nil))
+ _, err := r.Skip(3)
+ require.NoError(t, err)
+ total, read, err := r.ReadBatch(10, make([]int32, 10),
make([]int16, 10), nil)
+ require.NoError(t, err)
+ require.Equal(t, int64(7), total)
+ require.Zero(t, read)
+ })
+
+ t.Run("zero sized batch", func(t *testing.T) {
+ descr := shortPageDescr(t, parquet.Repetitions.Required)
+ r := newInt32Reader(t, descr, enc, shortPage(t, descr, enc, 10,
nil, []int32{0, 3, 6, 9, 12, 15, 18, 21, 24, 27}))
+ total, read, err := r.ReadBatchInPage(0, nil, nil, nil)
+ require.NoError(t, err)
+ require.Zero(t, total)
+ require.Zero(t, read)
+ })
+}