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 14cb2235 fix(parquet/pqarrow): read duration columns (#1607)
14cb2235 is described below
commit 14cb223519b8e83c51d39c01af5ddba2cb7c1e20
Author: Sankalp Thakur <[email protected]>
AuthorDate: Wed Nov 18 22:46:09 2026 +0400
fix(parquet/pqarrow): read duration columns (#1607)
### Rationale for this change
Reading a Parquet file with a duration column fails for the whole file in
v18.8.0:
```
no support for reading columns of type: duration
```
That covers pandas `timedelta64` columns written through pyarrow, pyarrow
`duration` columns, and anything else that keeps the Arrow schema in the file.
Parquet has no duration type, so those writers store INT64 and put the Arrow
type in the `ARROW:schema` metadata. Before #1095 the column came back as int64.
#1095 made the reader restore the duration type from that metadata
(`parquet/pqarrow/schema.go:1234`), but the zero-copy case in
`transferColumnData` (`parquet/pqarrow/column_readers.go:743`) only lists
`INT32`, `INT64`, `FLOAT32` and `FLOAT64`. A duration column falls through to
the default case at line 796. The test in #1095 covers the schema step only.
### What changes are included in this PR?
Add `arrow.DURATION` to that case. `transferZeroCopy` already builds the
array with the restored type from the INT64 buffer.
### Are these changes tested?
Yes. `TestReadDurationFromStoredSchema` (`parquet/pqarrow/schema_test.go`,
next to `TestRestoreDurationFromStoredSchema`) builds a file the way pyarrow
does, an INT64 column plus `ARROW:schema` holding a duration field, for all
four units and with a null in the middle. It reads the file with `pqarrow` and
checks the type and the values. It fails on main with the error above and
passes with the change.
I did not use a round trip through the Go writer: arrow-go does not write
durations (`TestUnsupportedTypes` lists `DURATION` as unsupported) and I did
not want to change that in a bug fix. I did not commit a binary fixture either,
since there is no Parquet testdata directory under `parquet/pqarrow`.
- `go test ./parquet/pqarrow -run
'TestReadDurationFromStoredSchema|TestRestoreDurationFromStoredSchema|TestUnsupportedTypes'
-count=1`: ok
- `PARQUET_TEST_DATA=$PWD/parquet-testing/data go test ./parquet/pqarrow
-short -count=1`: ok
- `go vet ./parquet/pqarrow` and `gofmt -l` on the changed files: clean
### Are there any user-facing changes?
Yes. Files with duration columns can be read again, as `arrow.DURATION`
with the unit from the stored schema. Writing durations is still unsupported.
### AI/LLM disclosure
- AI coding tools were used to help draft or modify code and this PR
description.
- I reviewed the complete change, understand the reasoning, and ran the
reported local tests before submitting.
---
parquet/pqarrow/column_readers.go | 2 +-
parquet/pqarrow/schema_test.go | 45 +++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/parquet/pqarrow/column_readers.go
b/parquet/pqarrow/column_readers.go
index 237df7d8..c1705b9c 100644
--- a/parquet/pqarrow/column_readers.go
+++ b/parquet/pqarrow/column_readers.go
@@ -740,7 +740,7 @@ func transferColumnData(rdr file.RecordReader, valueType
arrow.DataType, descr *
return transferDictionary(rdr, valueType, mem)
case arrow.NULL:
return arrow.NewChunked(arrow.Null,
[]arrow.Array{array.NewNull(rdr.ValuesWritten())}), nil
- case arrow.INT32, arrow.INT64, arrow.FLOAT32, arrow.FLOAT64:
+ case arrow.INT32, arrow.INT64, arrow.FLOAT32, arrow.FLOAT64,
arrow.DURATION:
data = transferZeroCopy(rdr, valueType) // can just reference
the raw data without copying
case arrow.BOOL:
data = transferBool(rdr)
diff --git a/parquet/pqarrow/schema_test.go b/parquet/pqarrow/schema_test.go
index 7e308d70..7feae925 100644
--- a/parquet/pqarrow/schema_test.go
+++ b/parquet/pqarrow/schema_test.go
@@ -199,6 +199,51 @@ func TestRestoreDurationFromStoredSchema(t *testing.T) {
}
}
+// Parquet has no duration type. pyarrow, pandas timedelta64 columns and
arrow-rs store a
+// duration as a plain INT64 column and keep the Arrow type in the
ARROW:schema metadata,
+// which the reader uses to restore it. Build a file with that layout and read
it back.
+func TestReadDurationFromStoredSchema(t *testing.T) {
+ for _, unit := range []arrow.TimeUnit{arrow.Second, arrow.Millisecond,
arrow.Microsecond, arrow.Nanosecond} {
+ t.Run(unit.String(), func(t *testing.T) {
+ dtype := &arrow.DurationType{Unit: unit}
+ stored := arrow.NewSchema([]arrow.Field{{Name: "d",
Type: dtype, Nullable: true}}, nil)
+ kv := metadata.NewKeyValueMetadata()
+ kv.Append("ARROW:schema",
base64.StdEncoding.EncodeToString(flight.SerializeSchema(stored,
memory.DefaultAllocator)))
+
+ root := schema.MustGroup(schema.NewGroupNode("schema",
parquet.Repetitions.Required, schema.FieldList{
+
schema.MustPrimitive(schema.NewPrimitiveNode("d", parquet.Repetitions.Optional,
parquet.Types.Int64, -1, -1)),
+ }, -1))
+ var buf bytes.Buffer
+ writer := file.NewParquetWriter(&buf, root,
file.WithWriteMetadata(kv))
+ rgw, err := writer.AppendRowGroupChecked()
+ require.NoError(t, err)
+ cw, err := rgw.NextColumn()
+ require.NoError(t, err)
+ _, err =
cw.(*file.Int64ColumnChunkWriter).WriteBatch([]int64{1, 3}, []int16{1, 0, 1},
nil)
+ require.NoError(t, err)
+ require.NoError(t, cw.Close())
+ require.NoError(t, rgw.Close())
+ require.NoError(t, writer.Close())
+
+ rdr, err :=
file.NewParquetReader(bytes.NewReader(buf.Bytes()))
+ require.NoError(t, err)
+ defer rdr.Close()
+ fr, err := pqarrow.NewFileReader(rdr,
pqarrow.ArrowReadProperties{}, memory.DefaultAllocator)
+ require.NoError(t, err)
+ tbl, err := fr.ReadTable(context.Background())
+ require.NoError(t, err)
+ defer tbl.Release()
+
+ require.True(t, arrow.TypeEqual(dtype,
tbl.Column(0).DataType()), "got %s", tbl.Column(0).DataType())
+ col := tbl.Column(0).Data().Chunk(0).(*array.Duration)
+ require.Equal(t, 3, col.Len())
+ assert.Equal(t, arrow.Duration(1), col.Value(0))
+ assert.True(t, col.IsNull(1))
+ assert.Equal(t, arrow.Duration(3), col.Value(2))
+ })
+ }
+}
+
func TestGetOriginSchemaUnregisteredExtension(t *testing.T) {
uuidType := extensions.NewUUIDType()
md := arrow.NewMetadata([]string{"PARQUET:field_id"}, []string{"-1"})