forked from cube2222/octosql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
244 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module github.com/cube2222/octosql | ||
|
||
require github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 | ||
require ( | ||
github.com/pkg/errors v0.8.1 | ||
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 | ||
) |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ= | ||
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY= |
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,73 @@ | ||
package octosql | ||
|
||
type RelationType string | ||
|
||
const ( | ||
Equal RelationType = "equal" | ||
NotEqual RelationType = "not_equal" | ||
MoreThan RelationType = "more_than" | ||
LessThan RelationType = "less_than" | ||
Like RelationType = "like" | ||
In RelationType = "in" | ||
) | ||
|
||
type Formula interface { | ||
Evaluate(record Record, primitives map[string]interface{}) bool | ||
Fields() map[string]struct{} | ||
Primitives() map[string]struct{} | ||
} | ||
|
||
// TODO: Implement: | ||
|
||
type And struct { | ||
Left, Right Formula | ||
} | ||
|
||
type Or struct { | ||
Left, Right Formula | ||
} | ||
|
||
type Not struct { | ||
Child Formula | ||
} | ||
|
||
type Predicate struct { | ||
Left Expression | ||
Filter RelationType | ||
Right Expression | ||
} | ||
|
||
func (p *Predicate) Evaluate(record Record, primitives map[string]interface{}) bool { | ||
panic("implement me") | ||
return true | ||
} | ||
|
||
type Expression interface { | ||
ExpressionValue(record Record, primitives map[string]interface{}) interface{} | ||
} | ||
|
||
type Primitive struct { | ||
id string | ||
} | ||
|
||
func (p *Primitive) ExpressionValue(record Record, primitives map[string]interface{}) interface{} { | ||
return primitives[p.id] | ||
} | ||
|
||
type FieldReference struct { | ||
id FieldIdentifier | ||
} | ||
|
||
func (f *FieldReference) ExpressionValue(record Record, primitives map[string]interface{}) interface{} { | ||
return record.Value(f.id) | ||
} | ||
|
||
var expression = Predicate{ | ||
Left: &FieldReference{ | ||
id: "City", | ||
}, | ||
Filter: Equal, | ||
Right: &Primitive{ | ||
id: "city_x", | ||
}, | ||
} |
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,4 @@ | ||
{"name": "jan", "surname": "chomiak", "age": 3, "city": "warsaw"} | ||
{"name": "wojtek", "surname": "kuzminski", "age": 4, "city": "warsaw"} | ||
{"name": "adam", "surname": "cz", "age": 5, "city": "ciechanowo"} | ||
{"name": "Kuba", "surname": "M", "age": 2, "city": "ciechanowo", "pochodzenie": ["polska", "niemcy"]} |
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,53 @@ | ||
package json | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/cube2222/octosql" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type JSONDataSource struct { | ||
path string | ||
} | ||
|
||
func (ds *JSONDataSource) Get(primitiveValues map[string]interface{}) (octosql.RecordStream, error) { | ||
file, err := os.Open(ds.path) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't open file") | ||
} | ||
sc := bufio.NewScanner(file) | ||
return &JSONRecordStream{ | ||
file: file, | ||
sc: sc, | ||
isDone: false, | ||
}, nil | ||
} | ||
|
||
type JSONRecordStream struct { | ||
file *os.File | ||
sc *bufio.Scanner | ||
isDone bool | ||
} | ||
|
||
func (rs *JSONRecordStream) Next() (*octosql.Record, error) { | ||
if rs.isDone { | ||
return nil, octosql.ErrEndOfStream | ||
} | ||
|
||
if !rs.sc.Scan() { | ||
rs.isDone = true | ||
rs.file.Close() | ||
return nil, octosql.ErrEndOfStream | ||
} | ||
|
||
var record map[string]interface{} | ||
err := json.Unmarshal(rs.sc.Bytes(), &record) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't unmarshal json record") | ||
} | ||
|
||
return octosql.NewRecord(record), 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,41 @@ | ||
package json | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/cube2222/octosql" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type JSONDataSourceDescription struct { | ||
path string | ||
} | ||
|
||
func NewJSONDataSourceDescription(path string) *JSONDataSourceDescription { | ||
return &JSONDataSourceDescription{ | ||
path: path, | ||
} | ||
} | ||
|
||
func (dsd *JSONDataSourceDescription) Initialize(ctx context.Context, predicates octosql.Formula) (octosql.DataSource, error) { | ||
_, err := os.Stat(dsd.path) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "couldn't stat file") | ||
} | ||
|
||
return &JSONDataSource{ | ||
path: dsd.path, | ||
}, nil | ||
} | ||
|
||
func (dsd *JSONDataSourceDescription) PrimaryKeys() map[string]struct{} { | ||
return make(map[string]struct{}) | ||
} | ||
|
||
func (dsd *JSONDataSourceDescription) AvailableFilters() map[octosql.FieldType]map[octosql.RelationType]struct{} { | ||
return map[octosql.FieldType]map[octosql.RelationType]struct{}{ | ||
octosql.Primary: make(map[octosql.RelationType]struct{}), | ||
octosql.Secondary: make(map[octosql.RelationType]struct{}), | ||
} | ||
} |
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