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 16d46132 fix(parquet/file): return the number of rows Skip skipped
(#1585)
16d46132 is described below
commit 16d46132971bdc87d62a7ab76988bf026ae238ba
Author: Harrison Crosse <[email protected]>
AuthorDate: Tue Nov 17 11:06:52 2026 -0500
fix(parquet/file): return the number of rows Skip skipped (#1585)
### Rationale for this change
Typed `Skip` methods, such as `Int32ColumnChunkReader.Skip`, return the
requested count even when an error or the end of the column stops them
early. On a column with 5 rows, `Skip(20)` returns 20 and a nil error,
and a further `Skip(1)` returns 1.
I reproduced the end of column case on v18.8.0 and main, with Go 1.27.4
on macOS arm64.
### What changes are included in this PR?
`skipRows` now returns the number of rows that it skipped, and all typed
`Skip` methods return that count. After an error on a repeated column,
the count can include a row that continues onto the page that failed.
`SeekToRow` ignores the count.
For columns without repetition levels, the shorter count at the end of a
column matches C++:
https://github.com/apache/arrow/blob/976779b2aac587f51b87c3bd2e6ae4c36f75ab6c/cpp/src/parquet/column_reader.cc#L1458-L1481
### Are these changes tested?
This change adds count checks to `TestSkipStopsAtCorruptPage` and
`TestSkipEmptyRepeatedRows`, and adds `TestSkipPastEndOfColumn` and
`TestSkipStopsAtDecodeError`. All four pass with this change, and all
but `TestSkipEmptyRepeatedRows` fail on main.
`TestSkipEmptyRepeatedRows` passes on main because it requests exactly
as many rows as it skips.
Full `go test -race -tags assert,test ./parquet/...` and `golangci-lint`
2.13.2 passed on macOS arm64 with pinned test data.
### Are there any user-facing changes?
Yes. `Skip` returns a shorter count when it stops early. There is no API
change.
---
parquet/file/column_reader.go | 17 +++---
parquet/file/column_reader_test.go | 88 +++++++++++++++++++++++++++-
parquet/file/column_reader_types.gen.go | 72 ++++++++++++++++++-----
parquet/file/column_reader_types.gen.go.tmpl | 9 ++-
4 files changed, 158 insertions(+), 28 deletions(-)
diff --git a/parquet/file/column_reader.go b/parquet/file/column_reader.go
index e6617160..1d509945 100644
--- a/parquet/file/column_reader.go
+++ b/parquet/file/column_reader.go
@@ -651,10 +651,11 @@ func (c *columnChunkReader) SeekToRow(rowIdx int64) error
{
c.readNewPage()
}
- return c.skipRows(rowIdx - c.curPage.(DataPage).FirstRowIndex())
+ _, err = c.skipRows(rowIdx - c.curPage.(DataPage).FirstRowIndex())
+ return err
}
-func (c *columnChunkReader) skipRows(nrows int64) error {
+func (c *columnChunkReader) skipRows(nrows int64) (int64, error) {
toSkip := nrows
for c.HasNext() && toSkip > 0 {
// if there are no repetition levels, then this is easy! each
level
@@ -670,13 +671,13 @@ func (c *columnChunkReader) skipRows(nrows int64) error {
ndefs, nvals, err := c.determineNumToRead(toSkip, nil,
nil)
if err != nil {
c.err = err
- return err
+ return nrows - toSkip, err
}
skipped, err := c.curDecoder.Discard(int(nvals))
if err != nil {
c.err = err
- return err
+ return nrows - toSkip, err
}
skipped = max(ndefs, skipped)
@@ -693,7 +694,7 @@ func (c *columnChunkReader) skipRows(nrows int64) error {
nreps, _, err := c.repetitionDecoder.Decode(repLvls)
if err != nil {
c.err = err
- return err
+ return nrows - toSkip, err
}
rowsSkipped := int64(0)
@@ -720,7 +721,7 @@ func (c *columnChunkReader) skipRows(nrows int64) error {
_, valuesToSkip, err =
c.readDefinitionLevels(defLvls)
if err != nil {
c.err = err
- return err
+ return nrows - toSkip, err
}
} else {
valuesToSkip = int64(levelsToSkip)
@@ -729,14 +730,14 @@ func (c *columnChunkReader) skipRows(nrows int64) error {
_, err = c.curDecoder.Discard(int(valuesToSkip))
if err != nil {
c.err = err
- return err
+ return nrows - toSkip, err
}
toSkip -= rowsSkipped
c.consumeBufferedValues(int64(levelsToSkip))
}
}
- return c.err
+ return nrows - toSkip, c.err
}
type readerFunc func(int64, int64) (int, error)
diff --git a/parquet/file/column_reader_test.go
b/parquet/file/column_reader_test.go
index 9d3023d9..ebecba48 100644
--- a/parquet/file/column_reader_test.go
+++ b/parquet/file/column_reader_test.go
@@ -495,8 +495,9 @@ func TestSkipEmptyRepeatedRows(t *testing.T) {
col, err := r.RowGroup(0).Column(0)
require.NoError(t, err)
typed := col.(*file.Int32ColumnChunkReader)
- _, err = typed.Skip(2)
+ skipped, err := typed.Skip(2)
require.NoError(t, err)
+ assert.EqualValues(t, 2, skipped)
values := make([]int32, 1)
defs := make([]int16, 1)
@@ -510,6 +511,88 @@ func TestSkipEmptyRepeatedRows(t *testing.T) {
assert.Equal(t, []int16{0}, reps)
}
+func TestSkipStopsAtDecodeError(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+
+ node, err := schema.NewPrimitiveNode("value",
parquet.Repetitions.Required, parquet.Types.ByteArray, -1, -1)
+ require.NoError(t, err)
+ descr := schema.NewColumn(node, 0, 0)
+
+ t.Run("fails on the first value", func(t *testing.T) {
+ // the length prefix runs past the data, so only decoding fails
+ body := memory.NewBufferBytes([]byte{3, 0, 0, 0, 'a'})
+ page := file.NewDataPageV1(body, 20, parquet.Encodings.Plain,
parquet.Encodings.RLE, parquet.Encodings.RLE, int32(body.Len()))
+ defer page.Release()
+
+ pages := new(testutils.MockPageReader)
+ pages.Test(t)
+ pages.TestData().Set("pages", []file.Page{page})
+ pages.On("Err").Return((error)(nil))
+ r := file.NewColumnReader(descr, pages, mem,
nil).(*file.ByteArrayColumnChunkReader)
+ defer r.Close()
+
+ n, err := r.Skip(10)
+ require.Error(t, err)
+ assert.EqualValues(t, 0, n)
+ })
+
+ t.Run("valid value before the bad one", func(t *testing.T) {
+ first := memory.NewBufferBytes([]byte{1, 0, 0, 0, 'a', 1, 0, 0,
0, 'b'})
+ firstPage := file.NewDataPageV1(first, 2,
parquet.Encodings.Plain, parquet.Encodings.RLE, parquet.Encodings.RLE,
int32(first.Len()))
+ defer firstPage.Release()
+
+ // the length prefix runs past the data, so only decoding fails
+ second := memory.NewBufferBytes([]byte{1, 0, 0, 0, 'a', 3, 0,
0, 0})
+ secondPage := file.NewDataPageV1(second, 3,
parquet.Encodings.Plain, parquet.Encodings.RLE, parquet.Encodings.RLE,
int32(second.Len()))
+ defer secondPage.Release()
+
+ pages := new(testutils.MockPageReader)
+ pages.Test(t)
+ pages.TestData().Set("pages", []file.Page{firstPage,
secondPage})
+ pages.On("Err").Return((error)(nil))
+ r := file.NewColumnReader(descr, pages, mem,
nil).(*file.ByteArrayColumnChunkReader)
+ defer r.Close()
+
+ n, err := r.Skip(4)
+ require.Error(t, err)
+ assert.EqualValues(t, 2, n)
+ })
+}
+
+func TestSkipPastEndOfColumn(t *testing.T) {
+ sc := schema.NewSchema(schema.MustGroup(schema.NewGroupNode("schema",
parquet.Repetitions.Required, schema.FieldList{
+ schema.NewInt32Node("values", parquet.Repetitions.Required, -1),
+ }, -1)))
+
+ var out bytes.Buffer
+ w := file.NewParquetWriter(&out, sc.Root(), file.WithWriterProps(
+
parquet.NewWriterProperties(parquet.WithDictionaryDefault(false))))
+ rg := w.AppendRowGroup()
+ cw, err := rg.NextColumn()
+ require.NoError(t, err)
+ _, err = cw.(*file.Int32ColumnChunkWriter).WriteBatch([]int32{1, 2, 3,
4, 5}, nil, nil)
+ require.NoError(t, err)
+ require.NoError(t, cw.Close())
+ require.NoError(t, rg.Close())
+ require.NoError(t, w.Close())
+
+ r, err := file.NewParquetReader(bytes.NewReader(out.Bytes()))
+ require.NoError(t, err)
+ defer r.Close()
+ col, err := r.RowGroup(0).Column(0)
+ require.NoError(t, err)
+ icr := col.(*file.Int32ColumnChunkReader)
+
+ skipped, err := icr.Skip(15)
+ require.NoError(t, err)
+ assert.EqualValues(t, 5, skipped)
+
+ skipped, err = icr.Skip(1)
+ require.NoError(t, err)
+ assert.Zero(t, skipped)
+}
+
func (p *PrimitiveReaderSuite) TestReadBatchMultiPage() {
const (
levelsPerPage int = 100
@@ -1192,8 +1275,9 @@ func TestReadBatchStopsAtCorruptPage(t *testing.T) {
func TestSkipStopsAtCorruptPage(t *testing.T) {
icr := newCorruptPageReader(t)
- _, err := icr.Skip(25)
+ n, err := icr.Skip(25)
require.ErrorContains(t, err, "invalid checksum")
+ assert.EqualValues(t, 10, n)
}
func TestSeekToRowAfterCorruptPage(t *testing.T) {
diff --git a/parquet/file/column_reader_types.gen.go
b/parquet/file/column_reader_types.gen.go
index fe3e20e5..4e224716 100644
--- a/parquet/file/column_reader_types.gen.go
+++ b/parquet/file/column_reader_types.gen.go
@@ -31,9 +31,14 @@ type Int32ColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *Int32ColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -67,9 +72,14 @@ type Int64ColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *Int64ColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -103,9 +113,14 @@ type Int96ColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *Int96ColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -139,9 +154,14 @@ type Float32ColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *Float32ColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -175,9 +195,14 @@ type Float64ColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *Float64ColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -211,9 +236,14 @@ type BooleanColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *BooleanColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -247,9 +277,14 @@ type ByteArrayColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *ByteArrayColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
@@ -290,9 +325,14 @@ type FixedLenByteArrayColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *FixedLenByteArrayColumnChunkReader) Skip(nvalues int64) (int64,
error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
// ReadBatchInPage and ReadBatch are used to read batchSize values from the
column, currently they are
diff --git a/parquet/file/column_reader_types.gen.go.tmpl
b/parquet/file/column_reader_types.gen.go.tmpl
index 3c959ddc..bda24974 100644
--- a/parquet/file/column_reader_types.gen.go.tmpl
+++ b/parquet/file/column_reader_types.gen.go.tmpl
@@ -30,9 +30,14 @@ type {{.Name}}ColumnChunkReader struct {
// Skip skips the next nvalues so that the next call to ReadBatch
// will start reading *after* the skipped values.
+//
+// It returns the number of rows skipped before it stopped, which is less than
+// nvalues when the column ends or an error occurs first. On error, the count
+// covers the rows skipped before the step that failed, and the reader stops
+// at that error. For a repeated column, that count can include a row that
+// continues onto the page that failed.
func (cr *{{.Name}}ColumnChunkReader) Skip(nvalues int64) (int64, error) {
- err := cr.columnChunkReader.skipRows(nvalues)
- return nvalues, err
+ return cr.columnChunkReader.skipRows(nvalues)
}
{{- if .isByteArray}}