-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/project leaf operator #8
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bec1b3b
feat(ProjectExecCSV) Implement csv reader that transforms csv file in…
334c0c5
feat:InmemoryProject Operator.this provide a base for easy mock testi…
7159b86
removed json as datasource
a9de3db
feature:ParquetSource operator is implemented
98b55f3
feat: Implement s3 file reader & file downloader
6ee5178
feat:Implement Project operator | tested on top source operators
e7e9d82
chore: add next steps. Looking at Expr and Filter operators
f97e73b
chore: add next steps. Looking at Expr and Filter operators
86b1e3c
fix:revised PR
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1,38 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/apache/arrow/go/v17/arrow" | ||
| ) | ||
|
|
||
| // handle keeping only the request columsn but make sure the schema and columns are also aligned | ||
|
Rich-T-kid marked this conversation as resolved.
Outdated
|
||
| // returns error if a column doesnt exist | ||
| func ProjectSchemaFilterDown(schema *arrow.Schema, cols []arrow.Array, keepCols ...string) (*arrow.Schema, []arrow.Array, error) { | ||
| if len(keepCols) == 0 { | ||
| return arrow.NewSchema([]arrow.Field{}, nil), nil, errors.New("no columns passed in") | ||
| } | ||
|
|
||
| // Build map: columnName -> original index | ||
| fieldIndex := make(map[string]int) | ||
| for i, f := range schema.Fields() { | ||
| fieldIndex[f.Name] = i | ||
| } | ||
|
|
||
| newFields := make([]arrow.Field, 0, len(keepCols)) | ||
| newCols := make([]arrow.Array, 0, len(keepCols)) | ||
|
|
||
| // Preserve order from keepCols, not schema order | ||
| for _, name := range keepCols { | ||
| idx, exists := fieldIndex[name] | ||
| if !exists { | ||
| return arrow.NewSchema([]arrow.Field{}, nil), []arrow.Array{}, errors.New("invalid column passed in to be pruned") | ||
| } | ||
|
|
||
| newFields = append(newFields, schema.Field(idx)) | ||
| newCols = append(newCols, cols[idx]) | ||
| } | ||
|
|
||
| newSchema := arrow.NewSchema(newFields, nil) | ||
| return newSchema, newCols, nil | ||
| } | ||
212 changes: 212 additions & 0 deletions
212
src/Backend/opti-sql-go/operators/project/source/csv.go
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,213 @@ | ||
| package source | ||
|
|
||
| import ( | ||
| "encoding/csv" | ||
| "fmt" | ||
| "io" | ||
| "opti-sql-go/operators" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/apache/arrow/go/v15/arrow/memory" | ||
| "github.com/apache/arrow/go/v17/arrow" | ||
| "github.com/apache/arrow/go/v17/arrow/array" | ||
| ) | ||
|
|
||
| type ProjectCSVLeaf struct { | ||
| r *csv.Reader | ||
| schema *arrow.Schema // columns to project as well as types to cast to | ||
| colPosition map[string]int | ||
| firstDataRow []string | ||
| done bool // if this is set in Next, we have reached EOF | ||
| } | ||
|
|
||
| // assume everything is on disk for now | ||
| func NewProjectCSVLeaf(source io.Reader) (*ProjectCSVLeaf, error) { | ||
| r := csv.NewReader(source) | ||
| proj := &ProjectCSVLeaf{ | ||
| r: r, | ||
| colPosition: make(map[string]int), | ||
| } | ||
| var err error | ||
| // construct the schema from the header | ||
| proj.schema, err = proj.parseHeader() | ||
| return proj, err | ||
| } | ||
|
|
||
| func (pcsv *ProjectCSVLeaf) Next(n uint64) (*operators.RecordBatch, error) { | ||
| if pcsv.done { | ||
| return nil, io.EOF | ||
| } | ||
|
|
||
| // 1. Create builders | ||
| builders := pcsv.initBuilders() | ||
|
|
||
| rowsRead := uint64(0) | ||
|
|
||
| // Process stored first row (from parseHeader) --- | ||
| if pcsv.firstDataRow != nil && rowsRead < n { | ||
| if err := pcsv.processRow(pcsv.firstDataRow, builders); err != nil { | ||
| return nil, err | ||
| } | ||
| pcsv.firstDataRow = nil // consume it once | ||
| rowsRead++ | ||
| } | ||
|
|
||
| // Stream remaining rows from CSV reader --- | ||
| for rowsRead < n { | ||
| row, err := pcsv.r.Read() | ||
| if err == io.EOF { | ||
| if rowsRead == 0 { | ||
| pcsv.done = true | ||
| return nil, io.EOF | ||
| } | ||
| break | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // append to builders | ||
| if err := pcsv.processRow(row, builders); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| rowsRead++ | ||
| } | ||
|
|
||
| // Freeze into Arrow arrays | ||
| columns := pcsv.finalizeBuilders(builders) | ||
|
|
||
| return &operators.RecordBatch{ | ||
| Schema: pcsv.schema, | ||
| Columns: columns, | ||
| }, nil | ||
| } | ||
|
|
||
| func (pcsv *ProjectCSVLeaf) initBuilders() []array.Builder { | ||
| fields := pcsv.schema.Fields() | ||
| builders := make([]array.Builder, len(fields)) | ||
|
|
||
| for i, f := range fields { | ||
| builders[i] = array.NewBuilder(memory.DefaultAllocator, f.Type) | ||
| } | ||
|
|
||
| return builders | ||
| } | ||
| func (pcsv *ProjectCSVLeaf) processRow( | ||
| content []string, | ||
| builders []array.Builder, | ||
| ) error { | ||
| fields := pcsv.schema.Fields() | ||
|
|
||
| for i, f := range fields { | ||
| colIdx := pcsv.colPosition[f.Name] | ||
| cell := content[colIdx] | ||
|
|
||
| switch b := builders[i].(type) { | ||
|
|
||
| case *array.Int64Builder: | ||
| if cell == "" || cell == "NULL" { | ||
| b.AppendNull() | ||
| } else { | ||
| v, _ := strconv.ParseInt(cell, 10, 64) | ||
| b.Append(v) | ||
| } | ||
|
|
||
| case *array.Float64Builder: | ||
| if cell == "" || cell == "NULL" { | ||
| b.AppendNull() | ||
| } else { | ||
| v, _ := strconv.ParseFloat(cell, 64) | ||
| b.Append(v) | ||
| } | ||
|
|
||
| case *array.StringBuilder: | ||
| if cell == "" || cell == "NULL" { | ||
| b.AppendNull() | ||
| } else { | ||
| b.Append(cell) | ||
| } | ||
|
|
||
| case *array.BooleanBuilder: | ||
| if cell == "" || cell == "NULL" { | ||
| b.AppendNull() | ||
| } else { | ||
| b.Append(cell == "true") | ||
| } | ||
|
|
||
| default: | ||
| return fmt.Errorf("unsupported Arrow type: %s", f.Type) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| func (pcsv *ProjectCSVLeaf) finalizeBuilders(builders []array.Builder) []arrow.Array { | ||
| columns := make([]arrow.Array, len(builders)) | ||
|
|
||
| for i, b := range builders { | ||
| columns[i] = b.NewArray() | ||
| b.Release() | ||
| } | ||
|
|
||
| return columns | ||
| } | ||
|
|
||
| // first call to csv.Reader | ||
| func (pscv *ProjectCSVLeaf) parseHeader() (*arrow.Schema, error) { | ||
| header, err := pscv.r.Read() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| firstDataRow, err := pscv.r.Read() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| pscv.firstDataRow = firstDataRow | ||
| newFields := make([]arrow.Field, 0, len(header)) | ||
| for i, colName := range header { | ||
| sampleValue := firstDataRow[i] | ||
| newFields = append(newFields, arrow.Field{ | ||
| Name: colName, | ||
| Type: parseDataType(sampleValue), | ||
| Nullable: true, | ||
| }) | ||
| pscv.colPosition[colName] = i | ||
| } | ||
| return arrow.NewSchema(newFields, nil), nil | ||
| } | ||
| func parseDataType(sample string) arrow.DataType { | ||
| sample = strings.TrimSpace(sample) | ||
|
|
||
| // Nulls or empty fields → treat as nullable string in inference | ||
| if sample == "" || strings.EqualFold(sample, "NULL") { | ||
| return arrow.BinaryTypes.String | ||
| } | ||
|
|
||
| // Boolean | ||
| if sample == "true" || sample == "false" { | ||
| return arrow.FixedWidthTypes.Boolean | ||
| } | ||
|
|
||
| // Try int | ||
| if _, err := strconv.Atoi(sample); err == nil { | ||
| return arrow.PrimitiveTypes.Int64 | ||
| } | ||
|
|
||
| // Try float | ||
| if _, err := strconv.ParseFloat(sample, 64); err == nil { | ||
| return arrow.PrimitiveTypes.Float64 | ||
| } | ||
|
|
||
| // Fallback to string | ||
| return arrow.BinaryTypes.String | ||
| } | ||
|
|
||
| /* | ||
| Integers (int8, int16, int32, int64) - whole numbers like 42, -100 | ||
| Floating point (float32, float64) - decimal numbers like 3.14, -0.5 | ||
| Booleans - true/false values (often represented as "true"/"false", "1"/"0", or "yes"/"no") | ||
| Strings (text) - any text like "hello", "John Doe" | ||
| Nulls | ||
| */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.