Skip to content
Open
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
3 changes: 1 addition & 2 deletions arrow/avro/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func (r *OCFReader) decodeOCFToChan() {
defer close(r.avroChan)
for r.r.HasNext() {
for {
select {
case <-r.readerCtx.Done():
r.err = fmt.Errorf("avro decoding cancelled, %d records read", r.avroDatumCount)
Expand All @@ -34,7 +34,6 @@ func (r *OCFReader) decodeOCFToChan() {
err := r.r.Decode(&datum)
if err != nil {
if errors.Is(err, io.EOF) {
r.err = nil
return
}
r.err = err
Expand Down
27 changes: 12 additions & 15 deletions arrow/avro/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import (
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/hamba/avro/v2/ocf"
"github.com/tidwall/sjson"

avro "github.com/hamba/avro/v2"
"github.com/twmb/avro"
"github.com/twmb/avro/ocf"
)

var ErrMismatchFields = errors.New("arrow/avro: number of records mismatch")
Expand All @@ -47,9 +46,9 @@ type schemaEdit struct {
value any
}

// Reader wraps goavro/OCFReader and creates array.RecordBatches from a schema.
// OCFReader reads Avro OCF files and exposes them as array.RecordBatches.
type OCFReader struct {
r *ocf.Decoder
r *ocf.Reader
avroSchema string
avroSchemaEdits []schemaEdit
schema *arrow.Schema
Expand Down Expand Up @@ -82,7 +81,7 @@ type OCFReader struct {
// NewReader returns a reader that reads from an Avro OCF file and creates
// arrow.RecordBatches from the converted avro data.
func NewOCFReader(r io.Reader, opts ...Option) (*OCFReader, error) {
ocfr, err := ocf.NewDecoder(r)
ocfr, err := ocf.NewReader(r)
if err != nil {
return nil, fmt.Errorf("%w: could not create avro ocfreader", arrow.ErrInvalid)
}
Expand All @@ -108,22 +107,20 @@ func NewOCFReader(r io.Reader, opts ...Option) (*OCFReader, error) {
}
rr.avroSchema = schema.String()
if len(rr.avroSchemaEdits) > 0 {
// execute schema edits
for _, e := range rr.avroSchemaEdits {
err := rr.editAvroSchema(e)
if err != nil {
return nil, fmt.Errorf("%w: could not edit avro schema", arrow.ErrInvalid)
}
}
// validate edited schema
schema, err = avro.Parse(rr.avroSchema)
if err != nil {
return nil, fmt.Errorf("%w: could not parse modified avro schema", arrow.ErrInvalid)
}
}
rr.schema, err = ArrowSchemaFromAvro(schema)
rr.schema, err = ArrowSchemaFromAvroJSON(rr.avroSchema)
if err != nil {
return nil, fmt.Errorf("%w: could not convert avro schema", arrow.ErrInvalid)
msg := "could not convert avro schema"
if len(rr.avroSchemaEdits) > 0 {
msg = "could not parse modified avro schema"
}
return nil, fmt.Errorf("%w: %s: %w", arrow.ErrInvalid, msg, err)
}
if rr.mem == nil {
rr.mem = memory.DefaultAllocator
Expand All @@ -147,7 +144,7 @@ func NewOCFReader(r io.Reader, opts ...Option) (*OCFReader, error) {
func (rr *OCFReader) Reuse(r io.Reader, opts ...Option) error {
rr.Close()
rr.err = nil
ocfr, err := ocf.NewDecoder(r)
ocfr, err := ocf.NewReader(r)
if err != nil {
return fmt.Errorf("%w: could not create avro ocfreader", arrow.ErrInvalid)
}
Expand Down
20 changes: 8 additions & 12 deletions arrow/avro/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package avro
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/avro/testdata"
hamba "github.com/hamba/avro/v2"
"github.com/apache/arrow-go/v18/arrow/extensions"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -127,6 +126,10 @@ func TestReader(t *testing.T) {
Name: "uuidField",
Type: arrow.BinaryTypes.String,
},
{
Name: "fixedUuidField",
Type: extensions.NewUUIDType(),
},
{
Name: "timemillis",
Type: arrow.FixedWidthTypes.Time32ms,
Expand Down Expand Up @@ -167,20 +170,13 @@ func TestReader(t *testing.T) {
t.Fatal(err)
}
r := new(OCFReader)
r.avroSchema = schema.String()
r.avroSchema = schema
r.editAvroSchema(schemaEdit{method: "delete", path: "fields.0"})
schema, err = hamba.Parse(r.avroSchema)
got, err := ArrowSchemaFromAvroJSON(r.avroSchema)
if err != nil {
t.Fatalf("%v: could not parse modified avro schema", arrow.ErrInvalid)
}
got, err := ArrowSchemaFromAvro(schema)
if err != nil {
t.Fatalf("%v", err)
}
assert.Equal(t, want.String(), got.String())
if fmt.Sprintf("%+v", want.String()) != fmt.Sprintf("%+v", got.String()) {
t.Fatalf("got=%v,\n want=%v", got.String(), want.String())
}
})

t.Run("ShouldLoadExpectedRecords", func(t *testing.T) {
Expand All @@ -200,7 +196,7 @@ func TestReader(t *testing.T) {
exists := ar.Next()

if ar.Err() != nil {
t.Error("failed to read next record: %w", ar.Err())
t.Errorf("failed to read next record: %v", ar.Err())
}
if !exists {
t.Error("no record exists")
Expand Down
Loading