Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions arrow/avro/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,16 @@ func arrowSchemafromAvro(n *schemaNode) {
arrowSchemafromAvro(c)
n.arrowField = buildArrowField(n, arrow.MapOf(arrow.BinaryTypes.String, c.arrowField.Type), c.arrowField.Metadata)
case "union":
if n.schema.(*avro.UnionSchema).Nullable() {
if len(n.schema.(*avro.UnionSchema).Types()) > 1 {
n.schema = n.schema.(*avro.UnionSchema).Types()[1]
us := n.schema.(*avro.UnionSchema)
if us.Nullable() {
if len(us.Types()) > 1 {
n.schema = us.Types()[1]
n.union = true
n.nullable = true
arrowSchemafromAvro(n)
}
} else {
panic(fmt.Errorf("complex (non-nullable) avro union at '%v' is not supported", n.schemaPath()))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add support and use the arrow union type? We could also do this as a follow up instead.

}
// Avro "fixed" field type = Arrow FixedSize Primitive BinaryType
case "fixed":
Expand Down Expand Up @@ -245,6 +248,8 @@ func iterateFields(n *schemaNode) {
c.nullable = true
arrowSchemafromAvro(c)
}
} else {
panic(fmt.Errorf("complex (non-nullable) avro union in field '%v' is not supported", f.Name()))
}
default:
n.schemaCache.Add(f.Name(), f.Type())
Expand Down
25 changes: 25 additions & 0 deletions arrow/avro/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package avro

import (
"fmt"
"strings"
"testing"

"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/avro/testdata"
avropkg "github.com/hamba/avro/v2"
)

func TestSchemaStringEqual(t *testing.T) {
Expand Down Expand Up @@ -174,3 +176,26 @@ func TestSchemaStringEqual(t *testing.T) {
})
}
}

func TestComplexUnionReportsError(t *testing.T) {
// Non-nullable union (e.g. [int, string]) is not supported and should
// produce a clear error rather than being silently dropped.
const avroSchemaJSON = `{
"type": "record",
"name": "WithComplexUnion",
"fields": [
{"name": "value", "type": ["int", "string"]}
]
}`
schema, err := avropkg.Parse(avroSchemaJSON)
if err != nil {
t.Fatalf("avro parse: %v", err)
}
got, err := ArrowSchemaFromAvro(schema)
if err == nil {
t.Fatalf("expected error for complex union, got schema=%v", got)
}
if !strings.Contains(err.Error(), "union") {
t.Fatalf("expected error to mention union, got: %v", err)
}
}