Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading maps with map vs key_value #96

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion floor/interfaces/unmarshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,16 @@ func (e *unmarshElem) Map() (UnmarshalMap, error) {
return nil, fmt.Errorf("data is not a map, found %T instead", e.data)
}

if len(data) == 0 {
return &unmarshMap{data: []map[string]interface{}{}, idx: -1}, ErrFieldNotPresent
}

kvData, ok := data["key_value"]
if !ok {
return nil, errors.New("sub-group key_value not found")
kvData, ok = data["map"]
if !ok {
return nil, errors.New("sub-group key_value not found")
}
}

kvList, ok := kvData.([]map[string]interface{})
Expand Down
9 changes: 8 additions & 1 deletion floor/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,20 @@ func (um *reflectUnmarshaller) fillMap(value reflect.Value, data interfaces.Unma
}

keyValueList, err := data.Map()
if err != nil {
if err != nil && !errors.Is(err, interfaces.ErrFieldNotPresent) {
return err
}

if *schemaDef.RootColumn.SchemaElement.RepetitionType == parquet.FieldRepetitionType_REQUIRED && errors.Is(err, interfaces.ErrFieldNotPresent) {
return fmt.Errorf("field %s is required", schemaDef.RootColumn.SchemaElement.GetName())
}

value.Set(reflect.MakeMap(value.Type()))

keyValueSchemaDef := schemaDef.SubSchema("key_value")
if keyValueSchemaDef == nil {
keyValueSchemaDef = schemaDef.SubSchema("map")
}
keySchemaDef := keyValueSchemaDef.SubSchema("key")
valueSchemaDef := keyValueSchemaDef.SubSchema("value")

Expand Down
36 changes: 36 additions & 0 deletions floor/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,39 @@ func BenchmarkReadFile(b *testing.B) {
_ = hlReader.Scan(&msg)
}
}

func TestCanReadMap(t *testing.T) {
filename := os.Getenv("CDC_PARQUET_FILE")
if filename == "" {
//filename = "/Users/mparsons/tmp/gen/part_1.parquet"
t.Skip("missing CDC_PARQUET_FILE, skipping")
}
type AV struct {
B string `parquet:"b"`
N string `parquet:"n"`
}
type CDC struct {
TenantID string `parquet:"tenantid"`
RowID string `parquet:"rowid"`
TransactionID string `parquet:"transactionid"`
Begin int64 `parquet:"begin"`
End int64 `parquet:"end"`
RecordType uint8 `parquet:"recordtype"`
EventID string `parquet:"eventid"`
EventSource string `parquet:"eventsource"`
Operation uint8 `parquet:"operation"`
SequenceNumber string `parquet:"sequencenumber"`
ApproximateCreationDateTime [12]byte `parquet:"approximatecreationdatetime"`
Keys map[string]AV `parquet:"keys"`
Old map[string]AV `parquet:"old"`
New map[string]AV `parquet:"new"`
}

fr, err := NewFileReader(filename)
require.NoError(t, err)

for fr.Next() {
var rec CDC
require.NoError(t, fr.Scan(&rec))
}
}