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 e589462a fix(parquet/schema): ignore logical types this version does
not know (#1649)
e589462a is described below
commit e589462ab594533760f24c7d18ade2b1a28382db
Author: Sankalp Thakur <[email protected]>
AuthorDate: Tue Nov 24 05:01:50 2026 +0400
fix(parquet/schema): ignore logical types this version does not know (#1649)
### Rationale for this change
A Parquet file that uses a logical type added after this version of
arrow-go makes `file.OpenParquetFile` panic with `invalid logical type`.
The thrift decoder skips the union member it does not know and leaves an
empty `LogicalType` union behind. `getLogicalType`
(`parquet/schema/logical_types.go`) has a case for every member, and
nothing for the empty one, so it reaches `panic("invalid logical
type")`. The old `case l == nil` could never run, because
`l.IsSetSTRING()` dereferences the nil union first.
The format says what a reader should do (parquet-format
`LogicalTypes.md`, "Unsupported Logical Types"): "readers should ignore
both the logical type annotation and column order for that column. Only
the physical type information should be used to process the column's
data." Arrow C++ does this since apache/arrow#41765. arrow-go is one of
the implementations listed without a fix in apache/parquet-format#599.
### What changes are included in this PR?
- `getLogicalType` returns `NoLogicalType` for a nil union and for an
empty one. The dead `case l == nil` branch is removed.
- Logical type: a primitive column with an unknown annotation reads as
having no logical type, so it is processed by its physical type.
- Column order: `PrimitiveNodeFromThrift` marks such a node, and
`Column.SortOrder()` returns `SortUNKNOWN` for it. Without this the
column became `NoLogicalType`, which maps to the default order of the
physical type (unsigned for BYTE_ARRAY), and the file's min/max
statistics were trusted even though the unknown type may define another
order. Arrow C++ makes the sort order UNKNOWN as well.
- A group node with an unknown annotation is built with no logical type.
- `converted_type`: behaviour is unchanged. If the element also has a
`converted_type` next to the unknown logical type (for example UTF8 or
LIST), it is dropped with the annotation, and the node reads as plain
physical type or plain group. A newer logical type has no converted
equivalent, so the case is rare. I kept the PR to the logical type path,
like #41765, and can fall back to the converted type in a follow-up if
you prefer that.
- Out of scope: `getLogicalType` still panics for a TIME or TIMESTAMP
whose `TimeUnit` union is empty, and an unrecognized logical/physical
type combination still returns an error from `NewPrimitiveNodeLogical`.
Writing such a file back drops the unknown annotation, because
`NoLogicalType` is not serialized.
### Are these changes tested?
Yes.
- `TestUnknownThriftLogicalType`
(`parquet/schema/unknown_logical_type_test.go`) covers a primitive and a
group element with and without a `converted_type`, and asserts
`SortUNKNOWN` for the primitive column. It fails without the sort order
change (expected 2, actual 1).
- `TestReadUnknownLogicalType`
(`parquet/pqarrow/unknown_logical_type_test.go`) reads
`unknown-logical-type.parquet` from parquet-testing (a string column and
a binary column) with a checked allocator.
- `TestInvalidConvertedTypeInDeserialize` used to expect the panic and
now expects the node to read as having no logical type.
Run:
- `PARQUET_TEST_DATA=$PWD/parquet-testing/data go test ./parquet/schema
./parquet/pqarrow -run 'Unknown|Invalid' -count=1`: ok
- `PARQUET_TEST_DATA=$PWD/parquet-testing/data go test ./parquet/schema
-count=1`: ok
- `go vet ./parquet/schema ./parquet/pqarrow` and `gofmt -l`: clean
### Are there any user-facing changes?
Yes. Files that use a logical type from a newer writer can be opened and
read by their physical type instead of panicking. Min/max statistics of
such a column are not used for pruning. There is no API change.
### 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/unknown_logical_type_test.go | 76 ++++++++++++++++++++
parquet/schema/column.go | 4 ++
parquet/schema/logical_types.go | 11 ++-
parquet/schema/node.go | 27 +++++--
parquet/schema/schema_flatten_test.go | 10 +--
parquet/schema/unknown_logical_type_test.go | 101 +++++++++++++++++++++++++++
6 files changed, 218 insertions(+), 11 deletions(-)
diff --git a/parquet/pqarrow/unknown_logical_type_test.go
b/parquet/pqarrow/unknown_logical_type_test.go
new file mode 100644
index 00000000..990ae004
--- /dev/null
+++ b/parquet/pqarrow/unknown_logical_type_test.go
@@ -0,0 +1,76 @@
+// 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 pqarrow_test
+
+import (
+ "context"
+ "os"
+ "path"
+ "testing"
+
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ "github.com/apache/arrow-go/v18/parquet"
+ "github.com/apache/arrow-go/v18/parquet/file"
+ "github.com/apache/arrow-go/v18/parquet/pqarrow"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// unknown-logical-type.parquet was written with a logical type id no reader
knows yet.
+// An older reader has to ignore it and read the column as its physical type.
+func TestReadUnknownLogicalType(t *testing.T) {
+ dir := os.Getenv("PARQUET_TEST_DATA")
+ if dir == "" {
+ dir = "../../parquet-testing/data"
+ t.Log("PARQUET_TEST_DATA not set, using
../../parquet-testing/data")
+ }
+ require.DirExists(t, dir)
+
+ rdr, err := file.OpenParquetFile(path.Join(dir,
"unknown-logical-type.parquet"), false)
+ require.NoError(t, err)
+ defer rdr.Close()
+
+ sc := rdr.MetaData().Schema
+ require.Equal(t, 2, sc.NumColumns())
+ assert.Equal(t, parquet.Types.ByteArray, sc.Column(1).PhysicalType())
+ assert.True(t, sc.Column(1).LogicalType().IsNone())
+
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+ fr, err := pqarrow.NewFileReader(rdr, pqarrow.ArrowReadProperties{},
mem)
+ require.NoError(t, err)
+ tbl, err := fr.ReadTable(context.Background())
+ require.NoError(t, err)
+ defer tbl.Release()
+
+ require.EqualValues(t, 3, tbl.NumRows())
+ require.EqualValues(t, 2, tbl.NumCols())
+
+ known := tbl.Column(0).Data().Chunk(0)
+ assert.Equal(t, arrow.STRING, known.DataType().ID())
+ for i, want := range []string{"known string 1", "known string 2",
"known string 3"} {
+ assert.Equal(t, want, known.(*array.String).Value(i))
+ }
+
+ unknown := tbl.Column(1).Data().Chunk(0)
+ assert.Equal(t, arrow.BINARY, unknown.DataType().ID())
+ for i, want := range []string{"unknown string 1", "unknown string 2",
"unknown string 3"} {
+ assert.Equal(t, want, string(unknown.(*array.Binary).Value(i)))
+ }
+}
diff --git a/parquet/schema/column.go b/parquet/schema/column.go
index c7e02e7e..5221b289 100644
--- a/parquet/schema/column.go
+++ b/parquet/schema/column.go
@@ -108,6 +108,10 @@ func (c *Column) SchemaNode() Node {
// SortOrder returns the sort order of this column's statistics based on the
// Logical and Converted types.
func (c *Column) SortOrder() SortOrder {
+ if c.pnode.unknownLogical {
+ // the unknown logical type may define its own order, so
min/max must not be trusted
+ return SortUNKNOWN
+ }
if c.LogicalType() != nil {
return GetLogicalSortOrder(c.LogicalType(),
format.Type(c.pnode.PhysicalType()))
}
diff --git a/parquet/schema/logical_types.go b/parquet/schema/logical_types.go
index f1e85bd0..cc04884a 100644
--- a/parquet/schema/logical_types.go
+++ b/parquet/schema/logical_types.go
@@ -35,7 +35,14 @@ type DecimalMetadata struct {
Precision int32
}
+// getLogicalType converts the thrift union. A nil union, or one that holds a
member
+// this version does not know (a logical type added after it), reads as
NoLogicalType:
+// readers are meant to ignore annotations they do not recognize.
func getLogicalType(l *format.LogicalType) LogicalType {
+ if l == nil {
+ return NoLogicalType{}
+ }
+
switch {
case l.IsSetSTRING():
return StringLogicalType{}
@@ -77,10 +84,8 @@ func getLogicalType(l *format.LogicalType) LogicalType {
return geographyLogicalTypeFromThrift(l.GEOGRAPHY)
case l.IsSetGEOMETRY():
return geometryLogicalTypeFromThrift(l.GEOMETRY)
- case l == nil:
- return NoLogicalType{}
default:
- panic("invalid logical type")
+ return NoLogicalType{}
}
}
diff --git a/parquet/schema/node.go b/parquet/schema/node.go
index 75ae1c5e..c6fa024e 100644
--- a/parquet/schema/node.go
+++ b/parquet/schema/node.go
@@ -146,6 +146,10 @@ type PrimitiveNode struct {
physicalType parquet.Type
typeLen int
decimalMetaData DecimalMetadata
+ // the thrift schema element carried a logical type this version does
not know, or one
+ // its physical type does not support, so readers must ignore the
column order too
+ // (LogicalTypes.md, "Unsupported Logical Types")
+ unknownLogical bool
}
// NewPrimitiveNodeLogical constructs a Primitive node using the provided
logical type for a given
@@ -269,9 +273,19 @@ func PrimitiveNodeFromThrift(elem *format.SchemaElement)
(*PrimitiveNode, error)
}
if elem.IsSetLogicalType() {
- return NewPrimitiveNodeLogical(elem.GetName(),
parquet.Repetition(elem.GetRepetitionType()),
- getLogicalType(elem.GetLogicalType()),
parquet.Type(elem.GetType()), int(elem.GetTypeLength()),
- fieldID)
+ logical := getLogicalType(elem.GetLogicalType())
+ physical := parquet.Type(elem.GetType())
+ // a known logical type on a physical type it does not apply to
is ignored the same way
+ unknown := logical.IsNone() || !logical.IsApplicable(physical,
elem.GetTypeLength())
+ if unknown {
+ logical = NoLogicalType{}
+ }
+ n, err := NewPrimitiveNodeLogical(elem.GetName(),
parquet.Repetition(elem.GetRepetitionType()),
+ logical, physical, int(elem.GetTypeLength()), fieldID)
+ if err == nil && unknown {
+ n.unknownLogical = true
+ }
+ return n, err
} else if elem.IsSetConvertedType() {
return NewPrimitiveNodeConverted(elem.GetName(),
parquet.Repetition(elem.GetRepetitionType()),
parquet.Type(elem.GetType()),
ConvertedType(elem.GetConvertedType()),
@@ -477,7 +491,12 @@ func GroupNodeFromThrift(elem *format.SchemaElement,
fields FieldList) (*GroupNo
}
if elem.IsSetLogicalType() {
- return NewGroupNodeLogical(elem.GetName(),
parquet.Repetition(elem.GetRepetitionType()), fields,
getLogicalType(elem.GetLogicalType()), id)
+ logical := getLogicalType(elem.GetLogicalType())
+ if logical.IsNone() {
+ // a logical type this version does not know is
ignored, as is the converted type
+ return NewGroupNodeConverted(elem.GetName(),
parquet.Repetition(elem.GetRepetitionType()), fields, ConvertedTypes.None, id)
+ }
+ return NewGroupNodeLogical(elem.GetName(),
parquet.Repetition(elem.GetRepetitionType()), fields, logical, id)
}
converted := ConvertedTypes.None
diff --git a/parquet/schema/schema_flatten_test.go
b/parquet/schema/schema_flatten_test.go
index a3939161..ddcafd01 100644
--- a/parquet/schema/schema_flatten_test.go
+++ b/parquet/schema/schema_flatten_test.go
@@ -22,6 +22,7 @@ import (
"github.com/apache/arrow-go/v18/parquet"
format "github.com/apache/arrow-go/v18/parquet/internal/gen-go/parquet"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
@@ -146,11 +147,12 @@ func TestInvalidConvertedTypeInDeserialize(t *testing.T) {
assert.True(t, n.LogicalType().IsValid())
assert.True(t, n.LogicalType().IsSerialized())
intermediary := n.toThrift()
- // corrupt it
+ // an empty union is what the thrift decoder leaves for a logical type
it does not
+ // know, which readers ignore
intermediary.LogicalType.STRING = nil
- assert.Panics(t, func() {
- PrimitiveNodeFromThrift(intermediary)
- })
+ node, err := PrimitiveNodeFromThrift(intermediary)
+ require.NoError(t, err)
+ assert.True(t, node.LogicalType().IsNone())
}
func TestInvalidTimeUnitInTimeLogical(t *testing.T) {
diff --git a/parquet/schema/unknown_logical_type_test.go
b/parquet/schema/unknown_logical_type_test.go
new file mode 100644
index 00000000..fdddeada
--- /dev/null
+++ b/parquet/schema/unknown_logical_type_test.go
@@ -0,0 +1,101 @@
+// 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 schema
+
+import (
+ "testing"
+
+ "github.com/apache/arrow-go/v18/parquet"
+ format "github.com/apache/arrow-go/v18/parquet/internal/gen-go/parquet"
+ "github.com/apache/thrift/lib/go/thrift"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// A logical type added after this version is skipped by the thrift decoder,
which leaves
+// an empty union behind. The format tells readers to ignore the annotation
(and the column
+// order) of such a column and to use its physical type only.
+func TestUnknownThriftLogicalType(t *testing.T) {
+ unknown := &format.LogicalType{}
+
+ assert.IsType(t, NoLogicalType{}, getLogicalType(unknown))
+ assert.IsType(t, NoLogicalType{}, getLogicalType(nil))
+
+ t.Run("primitive", func(t *testing.T) {
+ for _, converted := range []*format.ConvertedType{nil,
format.ConvertedTypePtr(format.ConvertedType_UTF8)} {
+ node, err :=
PrimitiveNodeFromThrift(&format.SchemaElement{
+ Name: "col",
+ Type:
format.TypePtr(format.Type_BYTE_ARRAY),
+ RepetitionType:
format.FieldRepetitionTypePtr(format.FieldRepetitionType_OPTIONAL),
+ ConvertedType: converted,
+ LogicalType: unknown,
+ })
+ require.NoError(t, err)
+ assert.Equal(t, parquet.Types.ByteArray,
node.PhysicalType())
+ assert.True(t, node.LogicalType().IsNone())
+ assert.Equal(t, ConvertedTypes.None,
node.ConvertedType())
+
+ root, err := NewGroupNode("schema",
parquet.Repetitions.Required, FieldList{node}, -1)
+ require.NoError(t, err)
+ assert.Equal(t, SortUNKNOWN,
NewSchema(root).Column(0).SortOrder())
+ }
+ })
+
+ t.Run("group", func(t *testing.T) {
+ for _, converted := range []*format.ConvertedType{nil,
format.ConvertedTypePtr(format.ConvertedType_LIST)} {
+ node, err := GroupNodeFromThrift(&format.SchemaElement{
+ Name: "col",
+ NumChildren: thrift.Int32Ptr(0),
+ RepetitionType:
format.FieldRepetitionTypePtr(format.FieldRepetitionType_OPTIONAL),
+ ConvertedType: converted,
+ LogicalType: unknown,
+ }, FieldList{})
+ require.NoError(t, err)
+ assert.True(t, node.LogicalType().IsNone())
+ assert.Equal(t, ConvertedTypes.None,
node.ConvertedType())
+ }
+ })
+}
+
+// The same section of the format covers a known logical type on a physical
type it does not
+// apply to, such as TIMESTAMP on FIXED_LEN_BYTE_ARRAY(12), which newer
writers may produce.
+func TestInapplicableThriftLogicalType(t *testing.T) {
+ elems := []*format.SchemaElement{
+ {
+ Name: "schema",
+ NumChildren: thrift.Int32Ptr(1),
+ RepetitionType:
format.FieldRepetitionTypePtr(format.FieldRepetitionType_REQUIRED),
+ },
+ {
+ Name: "ts",
+ Type:
format.TypePtr(format.Type_FIXED_LEN_BYTE_ARRAY),
+ TypeLength: thrift.Int32Ptr(12),
+ RepetitionType:
format.FieldRepetitionTypePtr(format.FieldRepetitionType_OPTIONAL),
+ LogicalType: NewTimestampLogicalType(true,
TimeUnitNanos).toThrift(),
+ },
+ }
+
+ root, err := FromParquet(elems)
+ require.NoError(t, err)
+
+ col := NewSchema(root.(*GroupNode)).Column(0)
+ assert.Equal(t, parquet.Types.FixedLenByteArray, col.PhysicalType())
+ assert.Equal(t, 12, col.TypeLength())
+ assert.True(t, col.LogicalType().IsNone())
+ assert.Equal(t, ConvertedTypes.None, col.ConvertedType())
+ assert.Equal(t, SortUNKNOWN, col.SortOrder())
+}