mirror of
https://github.com/pikami/cosmium.git
synced 2026-06-14 16:27:17 +01:00
Compare commits
4 Commits
e20a6ca7cd
..
0.1.24
| Author | SHA1 | Date | |
|---|---|---|---|
| fba9b3df5f | |||
| b743e23ff9 | |||
| 11851297f5 | |||
| 560ea5296d |
@@ -1,12 +1,15 @@
|
||||
package badgerdatastore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
type BadgerDataStore struct {
|
||||
db *badger.DB
|
||||
gcTicker *time.Ticker
|
||||
}
|
||||
|
||||
type BadgerDataStoreOptions struct {
|
||||
@@ -25,12 +28,24 @@ func NewBadgerDataStore(options BadgerDataStoreOptions) *BadgerDataStore {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &BadgerDataStore{
|
||||
gcTicker := time.NewTicker(5 * time.Minute)
|
||||
|
||||
ds := &BadgerDataStore{
|
||||
db: db,
|
||||
gcTicker: gcTicker,
|
||||
}
|
||||
|
||||
go ds.runGarbageCollector()
|
||||
|
||||
return ds
|
||||
}
|
||||
|
||||
func (r *BadgerDataStore) Close() {
|
||||
if r.gcTicker != nil {
|
||||
r.gcTicker.Stop()
|
||||
r.gcTicker = nil
|
||||
}
|
||||
|
||||
r.db.Close()
|
||||
r.db = nil
|
||||
}
|
||||
@@ -39,3 +54,13 @@ func (r *BadgerDataStore) DumpToJson() (string, error) {
|
||||
logger.ErrorLn("Badger datastore does not support state export currently.")
|
||||
return "{}", nil
|
||||
}
|
||||
|
||||
func (r *BadgerDataStore) runGarbageCollector() {
|
||||
for range r.gcTicker.C {
|
||||
again:
|
||||
err := r.db.RunValueLogGC(0.7)
|
||||
if err == nil {
|
||||
goto again
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ const (
|
||||
SelectItemTypeConstant
|
||||
SelectItemTypeFunctionCall
|
||||
SelectItemTypeSubQuery
|
||||
SelectItemTypeExpression
|
||||
SelectItemTypeBinaryExpression
|
||||
)
|
||||
|
||||
type SelectItem struct {
|
||||
@@ -64,6 +66,12 @@ type ComparisonExpression struct {
|
||||
Operation string
|
||||
}
|
||||
|
||||
type BinaryExpression struct {
|
||||
Left interface{}
|
||||
Right interface{}
|
||||
Operation string
|
||||
}
|
||||
|
||||
type ConstantType int
|
||||
|
||||
const (
|
||||
|
||||
@@ -0,0 +1,366 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_Arithmetics(t *testing.T) {
|
||||
t.Run("Should parse multiplication before addition", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.a + c.b * c.c FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "b"),
|
||||
Right: testutils.SelectItem_Path("c", "c"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse division before subtraction", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.x - c.y / c.z FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "-",
|
||||
Left: testutils.SelectItem_Path("c", "x"),
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "/",
|
||||
Left: testutils.SelectItem_Path("c", "y"),
|
||||
Right: testutils.SelectItem_Path("c", "z"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle complex mixed operations", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.a + c.b * c.c - c.d / c.e FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "-",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "b"),
|
||||
Right: testutils.SelectItem_Path("c", "c"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "/",
|
||||
Left: testutils.SelectItem_Path("c", "d"),
|
||||
Right: testutils.SelectItem_Path("c", "e"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should respect parentheses overriding precedence", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT (c.a + c.b) * c.c FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Path("c", "b"),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Path("c", "c"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle nested parentheses", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT ((c.a + c.b) * c.c) - c.d FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "-",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Path("c", "b"),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Path("c", "c"),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Path("c", "d"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should be left associative for same precedence operators", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.a - c.b - c.c FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "-",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "-",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Path("c", "b"),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Path("c", "c"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should be left associative with multiplication and division", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.a * c.b / c.c FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "/",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Path("c", "b"),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Path("c", "c"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle math with constants", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT 10 + 20 * 5 FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Constant_Int(10),
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Constant_Int(20),
|
||||
Right: testutils.SelectItem_Constant_Int(5),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle math with floating point numbers", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.price * 1.08 FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "price"),
|
||||
Right: testutils.SelectItem_Constant_Float(1.08),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle parentheses around single value", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT (c.value) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("c", "value"),
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle function calls in math expressions", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT LENGTH(c.name) * 2 + 10 FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallLength,
|
||||
Arguments: []interface{}{testutils.SelectItem_Path("c", "name")},
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Int(2),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Int(10),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle multiple select items with math", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.a + c.b, c.x * c.y FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Path("c", "b"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "x"),
|
||||
Right: testutils.SelectItem_Path("c", "y"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle math in WHERE clause", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM c WHERE c.price * 1.08 > 100`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: ">",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "price"),
|
||||
Right: testutils.SelectItem_Constant_Float(1.08),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Int(100),
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
+2244
-1841
File diff suppressed because it is too large
Load Diff
+80
-13
@@ -178,6 +178,32 @@ func combineExpressions(ex1 interface{}, exs interface{}, operation parsers.Logi
|
||||
}, nil
|
||||
}
|
||||
|
||||
func makeMathExpression(left interface{}, operations interface{}) (interface{}, error) {
|
||||
if operations == nil || len(operations.([]interface{})) == 0 {
|
||||
return left, nil
|
||||
}
|
||||
|
||||
result := left.(parsers.SelectItem)
|
||||
ops := operations.([]interface{})
|
||||
|
||||
for _, op := range ops {
|
||||
opData := op.([]interface{})
|
||||
operation := opData[0].(string)
|
||||
right := opData[1].(parsers.SelectItem)
|
||||
|
||||
result = parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Left: result,
|
||||
Right: right,
|
||||
Operation: operation,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Input <- selectStmt:SelectStmt {
|
||||
@@ -204,7 +230,7 @@ TopClause <- Top ws count:Integer {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItem { return column, nil }) {
|
||||
FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItemWithAlias { return column, nil }) {
|
||||
tableTyped := table.(parsers.Table)
|
||||
|
||||
if selectItem != nil {
|
||||
@@ -213,7 +239,7 @@ FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItem { r
|
||||
}
|
||||
|
||||
return tableTyped, nil
|
||||
} / From ws column:SelectItem {
|
||||
} / From ws column:SelectItemWithAlias {
|
||||
tableSelectItem := column.(parsers.SelectItem)
|
||||
table := parsers.Table{
|
||||
Value: tableSelectItem.Alias,
|
||||
@@ -251,7 +277,7 @@ SubQuerySelectItem <- subQuery:SubQuery asClause:(ws alias:AsClause { return ali
|
||||
return selectItem, nil
|
||||
}
|
||||
|
||||
JoinClause <- Join ws table:TableName ws In ws column:SelectItem {
|
||||
JoinClause <- Join ws table:TableName ws In ws column:SelectItemWithAlias {
|
||||
return makeJoin(table, column)
|
||||
} / Join ws subQuery:SubQuerySelectItem {
|
||||
return makeJoin(nil, subQuery)
|
||||
@@ -269,11 +295,34 @@ SelectAsterisk <- "*" {
|
||||
return makeColumnList(selectItem, make([]interface{}, 0))
|
||||
}
|
||||
|
||||
ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return coll, nil })* {
|
||||
ColumnList <- column:ExpressionOrSelectItem other_columns:(ws "," ws coll:ExpressionOrSelectItem {return coll, nil })* {
|
||||
return makeColumnList(column, other_columns)
|
||||
}
|
||||
|
||||
SelectValueSpec <- "VALUE"i ws column:SelectItem {
|
||||
ExpressionOrSelectItem <- expression:OrExpression asClause:AsClause? {
|
||||
switch typedValue := expression.(type) {
|
||||
case parsers.ComparisonExpression, parsers.LogicalExpression:
|
||||
selectItem := parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: typedValue,
|
||||
}
|
||||
|
||||
if aliasValue, ok := asClause.(string); ok {
|
||||
selectItem.Alias = aliasValue
|
||||
}
|
||||
|
||||
return selectItem, nil
|
||||
case parsers.SelectItem:
|
||||
if aliasValue, ok := asClause.(string); ok {
|
||||
typedValue.Alias = aliasValue
|
||||
}
|
||||
return typedValue, nil
|
||||
default:
|
||||
return typedValue, nil
|
||||
}
|
||||
} / item:SelectItemWithAlias { return item, nil }
|
||||
|
||||
SelectValueSpec <- "VALUE"i ws column:SelectItemWithAlias {
|
||||
selectItem := column.(parsers.SelectItem)
|
||||
selectItem.IsTopLevel = true
|
||||
return makeColumnList(selectItem, make([]interface{}, 0))
|
||||
@@ -306,7 +355,15 @@ SelectProperty <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* {
|
||||
return makeSelectItem(name, path, parsers.SelectItemTypeField)
|
||||
}
|
||||
|
||||
SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
SelectItemWithAlias <- selectItem:SelectItem asClause:AsClause? {
|
||||
item := selectItem.(parsers.SelectItem)
|
||||
if aliasValue, ok := asClause.(string); ok {
|
||||
item.Alias = aliasValue
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) {
|
||||
var itemResult parsers.SelectItem
|
||||
switch typedValue := selectItem.(type) {
|
||||
case parsers.SelectItem:
|
||||
@@ -323,10 +380,6 @@ SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectAr
|
||||
}
|
||||
}
|
||||
|
||||
if aliasValue, ok := asClause.(string); ok {
|
||||
itemResult.Alias = aliasValue
|
||||
}
|
||||
|
||||
return itemResult, nil
|
||||
}
|
||||
|
||||
@@ -360,10 +413,20 @@ AndExpression <- ex1:ComparisonExpression ex2:(ws And ws ex:ComparisonExpression
|
||||
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd)
|
||||
}
|
||||
|
||||
ComparisonExpression <- "(" ws ex:OrExpression ws ")" { return ex, nil }
|
||||
/ left:SelectItem ws op:ComparisonOperator ws right:SelectItem {
|
||||
ComparisonExpression <- left:AddSubExpression ws op:ComparisonOperator ws right:AddSubExpression {
|
||||
return parsers.ComparisonExpression{Left:left,Right:right,Operation:op.(string)}, nil
|
||||
} / inv:(Not ws)? ex:SelectItem {
|
||||
} / ex:AddSubExpression { return ex, nil }
|
||||
|
||||
AddSubExpression <- left:MulDivExpression operations:(ws op:AddOrSubtractOperation ws right:MulDivExpression { return []interface{}{op, right}, nil })* {
|
||||
return makeMathExpression(left, operations)
|
||||
}
|
||||
|
||||
MulDivExpression <- left:SelectItemWithParentheses operations:(ws op:MultiplyOrDivideOperation ws right:SelectItemWithParentheses { return []interface{}{op, right}, nil })* {
|
||||
return makeMathExpression(left, operations)
|
||||
}
|
||||
|
||||
SelectItemWithParentheses <- "(" ws ex:OrExpression ws ")" { return ex, nil }
|
||||
/ inv:(Not ws)? ex:SelectItem {
|
||||
if inv != nil {
|
||||
ex1 := ex.(parsers.SelectItem)
|
||||
ex1.Invert = true
|
||||
@@ -420,6 +483,10 @@ ComparisonOperator <- ("<=" / ">=" / "=" / "!=" / "<" / ">") {
|
||||
return string(c.text), nil
|
||||
}
|
||||
|
||||
AddOrSubtractOperation <- ("+" / "-") { return string(c.text), nil }
|
||||
|
||||
MultiplyOrDivideOperation <- ("*" / "/") { return string(c.text), nil }
|
||||
|
||||
Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral / ParameterConstant / NullConstant
|
||||
|
||||
ParameterConstant <- "@" Identifier {
|
||||
|
||||
@@ -195,4 +195,73 @@ func Test_Parse_Select(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse comparison expressions in SELECT", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c["id"] = "123", c["pk"] > 456 FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: testutils.SelectItem_Path("c", "id"),
|
||||
Right: testutils.SelectItem_Constant_String("123"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: parsers.ComparisonExpression{
|
||||
Operation: ">",
|
||||
Left: testutils.SelectItem_Path("c", "pk"),
|
||||
Right: testutils.SelectItem_Constant_Int(456),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse logical expressions in SELECT", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c["id"] = "123" OR c["pk"] > 456, c["isCool"] AND c["hasRizz"] AS isRizzler FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeOr,
|
||||
Expressions: []interface{}{
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: testutils.SelectItem_Path("c", "id"),
|
||||
Right: testutils.SelectItem_Constant_String("123"),
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Operation: ">",
|
||||
Left: testutils.SelectItem_Path("c", "pk"),
|
||||
Right: testutils.SelectItem_Constant_Int(456),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Alias: "isRizzler",
|
||||
Value: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeAnd,
|
||||
Expressions: []interface{}{
|
||||
testutils.SelectItem_Path("c", "isCool"),
|
||||
testutils.SelectItem_Path("c", "hasRizz"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_Arithmetics(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "a": 420},
|
||||
map[string]interface{}{"id": 2, "a": 6.9},
|
||||
map[string]interface{}{"id": 3},
|
||||
}
|
||||
|
||||
t.Run("Should execute simple arithmetics", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Alias: "result",
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "+",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Constant_Float(2.0),
|
||||
Right: testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "result": 426.0},
|
||||
map[string]interface{}{"id": 2, "result": 12.9},
|
||||
map[string]interface{}{"id": 3, "result": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute arithmetics in WHERE clause", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
{
|
||||
Alias: "result",
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Constant_Int(2),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: ">",
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeBinaryExpression,
|
||||
Value: parsers.BinaryExpression{
|
||||
Operation: "*",
|
||||
Left: testutils.SelectItem_Path("c", "a"),
|
||||
Right: testutils.SelectItem_Constant_Int(2),
|
||||
},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Int(500),
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "result": 840.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -69,6 +69,28 @@ func (r rowContext) resolveSelectItem(selectItem parsers.SelectItem) interface{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeExpression {
|
||||
if typedExpression, ok := selectItem.Value.(parsers.ComparisonExpression); ok {
|
||||
return r.filters_ComparisonExpression(typedExpression)
|
||||
}
|
||||
|
||||
if typedExpression, ok := selectItem.Value.(parsers.LogicalExpression); ok {
|
||||
return r.filters_LogicalExpression(typedExpression)
|
||||
}
|
||||
|
||||
logger.ErrorLn("parsers.SelectItem has incorrect Value type (expected parsers.ComparisonExpression)")
|
||||
return nil
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeBinaryExpression {
|
||||
if typedSelectItem, ok := selectItem.Value.(parsers.BinaryExpression); ok {
|
||||
return r.selectItem_SelectItemTypeBinaryExpression(typedSelectItem)
|
||||
}
|
||||
|
||||
logger.ErrorLn("parsers.SelectItem has incorrect Value type (expected parsers.BinaryExpression)")
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.selectItem_SelectItemTypeField(selectItem)
|
||||
}
|
||||
|
||||
@@ -304,6 +326,45 @@ func (r rowContext) selectItem_SelectItemTypeFunctionCall(functionCall parsers.F
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeBinaryExpression(binaryExpression parsers.BinaryExpression) interface{} {
|
||||
if binaryExpression.Left == nil || binaryExpression.Right == nil {
|
||||
logger.Debug("parsers.BinaryExpression has nil Left or Right value")
|
||||
return nil
|
||||
}
|
||||
|
||||
leftValue := r.resolveSelectItem(binaryExpression.Left.(parsers.SelectItem))
|
||||
rightValue := r.resolveSelectItem(binaryExpression.Right.(parsers.SelectItem))
|
||||
|
||||
if leftValue == nil || rightValue == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
leftNumber, leftIsNumber := numToFloat64(leftValue)
|
||||
rightNumber, rightIsNumber := numToFloat64(rightValue)
|
||||
|
||||
if !leftIsNumber || !rightIsNumber {
|
||||
logger.Debug("Binary expression operands are not numbers, returning nil")
|
||||
return nil
|
||||
}
|
||||
|
||||
switch binaryExpression.Operation {
|
||||
case "+":
|
||||
return leftNumber + rightNumber
|
||||
case "-":
|
||||
return leftNumber - rightNumber
|
||||
case "*":
|
||||
return leftNumber * rightNumber
|
||||
case "/":
|
||||
if rightNumber == 0 {
|
||||
logger.Debug("Division by zero in binary expression")
|
||||
return nil
|
||||
}
|
||||
return leftNumber / rightNumber
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem) interface{} {
|
||||
value := r.tables[selectItem.Path[0]]
|
||||
|
||||
@@ -339,6 +400,7 @@ func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem
|
||||
}
|
||||
|
||||
func compareValues(val1, val2 interface{}) int {
|
||||
// Handle nil values
|
||||
if val1 == nil && val2 == nil {
|
||||
return 0
|
||||
} else if val1 == nil {
|
||||
@@ -347,27 +409,24 @@ func compareValues(val1, val2 interface{}) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Handle number values
|
||||
val1Number, val1IsNumber := numToFloat64(val1)
|
||||
val2Number, val2IsNumber := numToFloat64(val2)
|
||||
if val1IsNumber && val2IsNumber {
|
||||
if val1Number < val2Number {
|
||||
return -1
|
||||
} else if val1Number > val2Number {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Handle different types
|
||||
if reflect.TypeOf(val1) != reflect.TypeOf(val2) {
|
||||
return 1
|
||||
}
|
||||
|
||||
switch val1 := val1.(type) {
|
||||
case int:
|
||||
val2 := val2.(int)
|
||||
if val1 < val2 {
|
||||
return -1
|
||||
} else if val1 > val2 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case float64:
|
||||
val2 := val2.(float64)
|
||||
if val1 < val2 {
|
||||
return -1
|
||||
} else if val1 > val2 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case string:
|
||||
val2 := val2.(string)
|
||||
return strings.Compare(val1, val2)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_Expressions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "age": 10, "isCool": true},
|
||||
map[string]interface{}{"id": "456", "age": 20, "isCool": false},
|
||||
map[string]interface{}{"id": "789", "age": 30, "isCool": true},
|
||||
}
|
||||
|
||||
t.Run("Should execute comparison expressions in SELECT", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "isAdult",
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: parsers.ComparisonExpression{
|
||||
Operation: ">=",
|
||||
Left: testutils.SelectItem_Path("c", "age"),
|
||||
Right: testutils.SelectItem_Constant_Int(18),
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "isNotCool",
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: parsers.ComparisonExpression{
|
||||
Operation: "!=",
|
||||
Left: testutils.SelectItem_Path("c", "isCool"),
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "isAdult": false, "isNotCool": false},
|
||||
map[string]interface{}{"id": "456", "isAdult": true, "isNotCool": true},
|
||||
map[string]interface{}{"id": "789", "isAdult": true, "isNotCool": false},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute logical expressions in SELECT", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "isCoolAndAdult",
|
||||
Type: parsers.SelectItemTypeExpression,
|
||||
Value: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeAnd,
|
||||
Expressions: []interface{}{
|
||||
testutils.SelectItem_Path("c", "isCool"),
|
||||
parsers.ComparisonExpression{
|
||||
Operation: ">=",
|
||||
Left: testutils.SelectItem_Path("c", "age"),
|
||||
Right: testutils.SelectItem_Constant_Int(18),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "isCoolAndAdult": false},
|
||||
map[string]interface{}{"id": "456", "isCoolAndAdult": false},
|
||||
map[string]interface{}{"id": "789", "isCoolAndAdult": true},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -605,10 +605,30 @@ func numToInt(ex interface{}) (int, bool) {
|
||||
|
||||
func numToFloat64(num interface{}) (float64, bool) {
|
||||
switch val := num.(type) {
|
||||
case float64:
|
||||
return val, true
|
||||
case int:
|
||||
return float64(val), true
|
||||
case int8:
|
||||
return float64(val), true
|
||||
case int16:
|
||||
return float64(val), true
|
||||
case int32:
|
||||
return float64(val), true
|
||||
case int64:
|
||||
return float64(val), true
|
||||
case uint:
|
||||
return float64(val), true
|
||||
case uint8:
|
||||
return float64(val), true
|
||||
case uint16:
|
||||
return float64(val), true
|
||||
case uint32:
|
||||
return float64(val), true
|
||||
case uint64:
|
||||
return float64(val), true
|
||||
case float32:
|
||||
return float64(val), true
|
||||
case float64:
|
||||
return val, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user