-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Parquet Support for Reads (#62)
- Loading branch information
Showing
13 changed files
with
668 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package block | ||
|
||
import ( | ||
"github.com/kelindar/talaria/internal/column" | ||
"github.com/kelindar/talaria/internal/encoding/parquet" | ||
"github.com/kelindar/talaria/internal/encoding/typeof" | ||
) | ||
|
||
// FromParquetBy decodes a set of blocks from a Parquet file and repartitions | ||
// it by the specified partition key. | ||
func FromParquetBy(payload []byte, partitionBy string, filter *typeof.Schema, apply applyFunc) ([]Block, error) { | ||
const max = 10000000 // 10MB | ||
|
||
iter, err := parquet.FromBuffer(payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Find the partition index | ||
schema := iter.Schema() | ||
cols := schema.Columns() | ||
partitionIdx, ok := findString(cols, partitionBy) | ||
if !ok { | ||
return nil, nil // Skip the file if it has no partition column | ||
} | ||
|
||
// The resulting set of blocks, repartitioned and chunked | ||
blocks := make([]Block, 0, 128) | ||
|
||
// Create presto columns and iterate | ||
result, size := make(map[string]column.Columns, 16), 0 | ||
_, _ = iter.Range(func(rowIdx int, r []interface{}) bool { | ||
if size >= max { | ||
pending, err := makeBlocks(result) | ||
if err != nil { | ||
return true | ||
} | ||
|
||
size = 0 // Reset the size | ||
blocks = append(blocks, pending...) | ||
result = make(map[string]column.Columns, 16) | ||
} | ||
|
||
// Get the partition value, must be a string | ||
partition, ok := convertToString(r[partitionIdx]) | ||
if !ok { | ||
return true | ||
} | ||
|
||
// Skip the record if the partition is actually empty | ||
if partition == "" { | ||
return false | ||
} | ||
|
||
// Get the block for that partition | ||
columns, exists := result[partition] | ||
if !exists { | ||
columns = column.MakeColumns(filter) | ||
result[partition] = columns | ||
} | ||
|
||
// Prepare a row for transformation | ||
row := NewRow(schema, len(r)) | ||
for i, v := range r { | ||
columnName := cols[i] | ||
columnType := schema[columnName] | ||
|
||
// Encode to JSON | ||
if columnType == typeof.JSON { | ||
if encoded, ok := convertToJSON(v); ok { | ||
v = encoded | ||
} | ||
} | ||
|
||
row.Set(columnName, v) | ||
} | ||
|
||
// Append computed columns and fill nulls for the row | ||
out, _ := apply(row) | ||
|
||
size += out.AppendTo(columns) | ||
size += columns.FillNulls() | ||
return false | ||
}, cols...) | ||
|
||
// Write the last chunk | ||
last, err := makeBlocks(result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
blocks = append(blocks, last...) | ||
return blocks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package block | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
const testFileForParquet = "../../../test/test2.parquet" | ||
|
||
func TestFromParquet_Nested(t *testing.T) { | ||
o, err := ioutil.ReadFile(testFileForParquet) | ||
assert.NotEmpty(t, o) | ||
assert.NoError(t, err) | ||
|
||
apply := Transform(nil) | ||
b, err := FromParquetBy(o, "foo", nil, apply) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 10000, len(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package parquet | ||
|
||
import ( | ||
"bytes" | ||
goparquet "github.com/fraugster/parquet-go" | ||
"github.com/kelindar/talaria/internal/encoding/typeof" | ||
"github.com/kelindar/talaria/internal/monitor/errors" | ||
"io" | ||
"os" | ||
"sort" | ||
) | ||
|
||
var errNoWriter = errors.New("unable to create Parquet writer") | ||
|
||
// Iterator represents parquet data frame. | ||
type Iterator interface { | ||
io.Closer | ||
Range(f func(int, []interface{}) bool, columns ...string) (int, bool) | ||
Schema() typeof.Schema | ||
} | ||
|
||
// FromFile creates an iterator from a file. | ||
func FromFile(filename string) (Iterator, error) { | ||
rf, err := os.Open(filename) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := goparquet.NewFileReader(rf) | ||
return &iterator{reader: r}, nil | ||
} | ||
|
||
// FromBuffer creates an iterator from a buffer. | ||
func FromBuffer(b []byte) (Iterator, error) { | ||
r, err := goparquet.NewFileReader(bytes.NewReader(b)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &iterator{reader: r}, nil | ||
} | ||
|
||
// Range is a helper function that ranges over a set of columns in a Parquet buffer | ||
func Range(payload []byte, f func(int, []interface{}) bool, columns ...string) error { | ||
i, err := FromBuffer(payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, _ = i.Range(f, columns...) | ||
return nil | ||
} | ||
|
||
// First selects a first row only, then stops. | ||
func First(payload []byte, columns ...string) (result []interface{}, err error) { | ||
err = Range(payload, func(_ int, v []interface{}) bool { | ||
result = v | ||
return true // No need to iterate further, we just take 1st element | ||
}, columns...) | ||
return | ||
} | ||
|
||
// Iterator represents parquet data frame. | ||
type iterator struct { | ||
reader *goparquet.FileReader | ||
} | ||
|
||
// Range iterates through the reader. | ||
func (i *iterator) Range(f func(int, []interface{}) bool, columns ...string) (index int, stop bool) { | ||
//TODO: Do this once the release is done | ||
//c := i.reader.SchemaReader.setSelectedColumns | ||
r := i.reader | ||
for { | ||
row, err := r.NextRow() | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
var arr []interface{} | ||
|
||
// We need to ensure that the row has columns ordered by name since that is how columns are generated | ||
// in the upstream schema | ||
keys := make([]string, len(row)) | ||
i := 0 | ||
for k := range row { | ||
keys[i] = k | ||
i++ | ||
} | ||
sort.Strings(keys) | ||
|
||
for k := range keys { | ||
k := keys[k] | ||
v := row[k] | ||
|
||
arr = append(arr, v) | ||
} | ||
|
||
if stop = f(index-1, arr); stop { | ||
return index, false | ||
} | ||
} | ||
|
||
return index, true | ||
} | ||
|
||
// Schema gets the SQL schema for the iterator. | ||
func (i *iterator) Schema() typeof.Schema { | ||
schema := i.reader.SchemaReader | ||
result := make(typeof.Schema, len(schema.Columns())) | ||
for _, c := range schema.Columns() { | ||
t := c.Type() | ||
|
||
if t, supported := typeof.FromParquet(t); supported { | ||
result[c.Name()] = t | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// Close closes the iterator. | ||
func (i *iterator) Close() error { | ||
// No Op | ||
return nil | ||
} |
Oops, something went wrong.