-
Notifications
You must be signed in to change notification settings - Fork 8
Implement stream-based input record decoder's instead of batch-based #52
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
Changes from 1 commit
e730345
5de7542
c19f8b0
75d4a0e
baf29c0
d2d8591
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| package columnifier | ||
|
|
||
| import ( | ||
| "io" | ||
| "io/ioutil" | ||
| "os" | ||
|
|
||
| "github.com/reproio/columnify/record" | ||
|
|
||
| "github.com/reproio/columnify/parquet" | ||
| "github.com/reproio/columnify/schema" | ||
| "github.com/xitongsys/parquet-go-source/local" | ||
| "github.com/xitongsys/parquet-go/marshal" | ||
| parquetSource "github.com/xitongsys/parquet-go/source" | ||
| "github.com/xitongsys/parquet-go/writer" | ||
| ) | ||
|
|
@@ -65,17 +68,27 @@ func NewParquetColumnifier(st string, sf string, rt string, output string, confi | |
| } | ||
|
|
||
| // Write reads, converts input binary data and write it to buffer. | ||
| func (c *parquetColumnifier) Write(data []byte) (int, error) { | ||
| func (c *parquetColumnifier) WriteFromReader(reader io.Reader) (int, error) { | ||
| // Intermediate record type is map[string]interface{} | ||
| c.w.MarshalFunc = parquet.MarshalMap | ||
| records, err := record.FormatToMap(data, c.schema, c.rt) | ||
| c.w.MarshalFunc = marshal.MarshalJSON | ||
syucream marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| decoder, err := record.NewJsonDecoder(reader, c.schema, c.rt) | ||
| if err != nil { | ||
| return -1, err | ||
| } | ||
|
|
||
| beforeSize := c.w.Size | ||
| for _, r := range records { | ||
| if err := c.w.Write(r); err != nil { | ||
| for { | ||
| var v string | ||
| err = decoder.Decode(&v) | ||
abicky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| if err == io.EOF { | ||
| break | ||
| } else { | ||
| return -1, err | ||
| } | ||
| } | ||
|
|
||
| if err := c.w.Write(v); err != nil { | ||
| return -1, err | ||
| } | ||
| } | ||
|
|
@@ -103,11 +116,12 @@ func (c *parquetColumnifier) WriteFromFiles(paths []string) (int, error) { | |
| var n int | ||
|
|
||
| for _, p := range paths { | ||
| data, err := ioutil.ReadFile(p) | ||
| f, err := os.Open(p) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this PR makes iterating processes reading some blocks of the file instead of reading the whole file. That's good. |
||
| if err != nil { | ||
| return -1, err | ||
| } | ||
| if n, err = c.Write(data); err != nil { | ||
|
|
||
| if n, err = c.WriteFromReader(f); err != nil { | ||
| return -1, err | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,71 @@ const ( | |
| TsvDelimiter delimiter = '\t' | ||
| ) | ||
|
|
||
| type csvInnerDecoder struct { | ||
| r *csv.Reader | ||
| names []string | ||
| } | ||
|
|
||
| func newCsvInnerDecoder(r io.Reader, s *schema.IntermediateSchema, delimiter delimiter) (*csvInnerDecoder, error) { | ||
| names, err := getFieldNamesFromSchema(s) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| reader := csv.NewReader(r) | ||
| reader.Comma = rune(delimiter) | ||
|
|
||
| return &csvInnerDecoder{ | ||
| r: reader, | ||
| names: names, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *csvInnerDecoder) Decode(r *map[string]interface{}) error { | ||
| values, err := d.r.Read() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(d.names) != len(values) { | ||
syucream marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return fmt.Errorf("incompleted value %v: %w", values, ErrUnconvertibleRecord) | ||
| } | ||
|
|
||
| *r = make(map[string]interface{}) | ||
syucream marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for i, v := range values { | ||
| n := d.names[i] | ||
|
|
||
| // bool | ||
| if v != "0" && v != "1" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you use the schema information? I'm concerned that the string
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer not to touch this place now and will aim it as more common and separated issue from this. |
||
| if vv, err := strconv.ParseBool(v); err == nil { | ||
| (*r)[n] = vv | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // int | ||
| if vv, err := strconv.ParseInt(v, 10, 64); err == nil { | ||
| (*r)[n] = vv | ||
| continue | ||
| } | ||
|
|
||
| // float | ||
| if vv, err := strconv.ParseFloat(v, 64); err == nil { | ||
| (*r)[n] = vv | ||
| continue | ||
| } | ||
|
|
||
| // others; to string | ||
| (*r)[n] = v | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func getFieldNamesFromSchema(s *schema.IntermediateSchema) ([]string, error) { | ||
| elems := s.ArrowSchema.Fields() | ||
|
|
||
| if len(elems) < 2 { | ||
| if len(elems) == 0 { | ||
| return nil, fmt.Errorf("no element is available: %w", ErrUnconvertibleRecord) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.