Added support for 'SELECT VALUE' statement

This commit is contained in:
Pijus Kamandulis 2024-02-13 21:22:55 +02:00
parent 9c56d01d27
commit e89f2e5611
7 changed files with 431 additions and 310 deletions

View File

@ -14,7 +14,10 @@ func RequestLogger() gin.HandlerFunc {
rdr1 := io.NopCloser(bytes.NewBuffer(buf)) rdr1 := io.NopCloser(bytes.NewBuffer(buf))
rdr2 := io.NopCloser(bytes.NewBuffer(buf)) rdr2 := io.NopCloser(bytes.NewBuffer(buf))
fmt.Println(readBody(rdr1)) bodyStr := readBody(rdr1)
if bodyStr != "" {
fmt.Println(bodyStr)
}
c.Request.Body = rdr2 c.Request.Body = rdr2
c.Next() c.Next()

View File

@ -16,19 +16,30 @@ const (
ConstantTypeBoolean ConstantTypeBoolean
) )
type SelectItemType int
const (
SelectItemTypeField SelectItemType = iota
SelectItemTypeObject
SelectItemTypeArray
)
type SelectStmt struct { type SelectStmt struct {
Columns []FieldPath SelectItems []SelectItem
Table Table Table Table
Filters interface{} Filters interface{}
} }
type Table struct { type Table struct {
Value string Value string
} }
type FieldPath struct { type SelectItem struct {
Alias string Alias string
Path []string Path []string
SelectItems []SelectItem
Type SelectItemType
IsTopLevel bool
} }
type LogicalExpression struct { type LogicalExpression struct {

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ import "github.com/pikami/cosmium/parsers"
func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt, error) { func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt, error) {
selectStmt := parsers.SelectStmt{ selectStmt := parsers.SelectStmt{
Columns: columns.([]parsers.FieldPath), SelectItems: columns.([]parsers.SelectItem),
Table: table.(parsers.Table), Table: table.(parsers.Table),
} }
@ -18,7 +18,7 @@ func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt
return selectStmt, nil return selectStmt, nil
} }
func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parsers.FieldPath, error) { func makeSelectItem(name interface{}, path interface{}, alias interface{}, selectItemType parsers.SelectItemType) (parsers.SelectItem, error) {
ps := path.([]interface{}) ps := path.([]interface{})
paths := make([]string, 1) paths := make([]string, 1)
@ -27,21 +27,21 @@ func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parse
paths = append(paths, p.(string)) paths = append(paths, p.(string))
} }
fieldPath := parsers.FieldPath{Path: paths} selectItem := parsers.SelectItem{Path: paths, Type: selectItemType}
if aliasValue, ok := alias.(string); ok { if aliasValue, ok := alias.(string); ok {
fieldPath.Alias = aliasValue selectItem.Alias = aliasValue
} }
return fieldPath, nil return selectItem, nil
} }
func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.FieldPath, error) { func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.SelectItem, error) {
collsAsArray := other_columns.([]interface{}) collsAsArray := other_columns.([]interface{})
columnList := make([]parsers.FieldPath, len(collsAsArray) + 1) columnList := make([]parsers.SelectItem, len(collsAsArray) + 1)
columnList[0] = column.(parsers.FieldPath) columnList[0] = column.(parsers.SelectItem)
for i, v := range collsAsArray { for i, v := range collsAsArray {
if col, ok := v.(parsers.FieldPath); ok { if col, ok := v.(parsers.SelectItem); ok {
columnList[i+1] = col columnList[i+1] = col
} }
} }
@ -79,23 +79,31 @@ Input <- selectStmt:SelectStmt {
return selectStmt, nil return selectStmt, nil
} }
SelectStmt <- Select ws columns:ColumnList ws SelectStmt <- Select ws columns:Selection ws
From ws table:TableName ws From ws table:TableName ws
whereClause:(ws Where ws condition:Condition { return condition, nil })? { whereClause:(ws Where ws condition:Condition { return condition, nil })? {
return makeSelectStmt(columns, table, whereClause) return makeSelectStmt(columns, table, whereClause)
} }
ColumnList <- column:FieldPath other_columns:(ws "," ws coll:FieldPath {return coll, nil })* { Selection <- SelectValueSpec / ColumnList
ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return coll, nil })* {
return makeColumnList(column, other_columns) return makeColumnList(column, other_columns)
} }
SelectValueSpec <- "VALUE" ws column:SelectItem {
selectItem := column.(parsers.SelectItem)
selectItem.IsTopLevel = true
return makeColumnList(selectItem, make([]interface{}, 0))
}
TableName <- key:Identifier { TableName <- key:Identifier {
return parsers.Table{Value: key.(string)}, nil return parsers.Table{Value: key.(string)}, nil
} }
FieldPath <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* SelectItem <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)*
asClause:(ws "AS" ws alias:Identifier { return alias, nil })? { asClause:(ws "AS" ws alias:Identifier { return alias, nil })? {
return makeFieldPath(name, path, asClause) return makeSelectItem(name, path, asClause, parsers.SelectItemTypeField)
} }
DotFieldAccess <- "." id:Identifier { DotFieldAccess <- "." id:Identifier {
@ -122,7 +130,7 @@ AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpressi
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd) return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd)
} }
ComparisonExpression <- left:(Literal / FieldPath) ws op:ComparisonOperator ws right:(Literal / FieldPath) { ComparisonExpression <- left:(Literal / SelectItem) ws op:ComparisonOperator ws right:(Literal / SelectItem) {
return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil
} }

View File

@ -12,7 +12,7 @@ import (
// For Parser Debugging // For Parser Debugging
// func Test_ParseTest(t *testing.T) { // func Test_ParseTest(t *testing.T) {
// // select c.id, c._self, c._rid, c._ts, [c[\"pk\"]] as _partitionKeyValue from c // // select c.id, c._self, c._rid, c._ts, [c[\"pk\"]] as _partitionKeyValue from c
// res, err := nosql.Parse("", []byte("select c.id, c._self AS self, c._rid, c._ts FROM c where c.id=\"12345\" AND c.pk=123")) // res, err := nosql.Parse("", []byte("SELECT VALUE c.id FROM c"))
// if err != nil { // if err != nil {
// log.Fatal(err) // log.Fatal(err)
// } // }
@ -38,12 +38,12 @@ func testQueryParse(t *testing.T, query string, expectedQuery parsers.SelectStmt
} }
func Test_Parse(t *testing.T) { func Test_Parse(t *testing.T) {
t.Run("Shoul parse simple SELECT", func(t *testing.T) { t.Run("Should parse simple SELECT", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,
`SELECT c.id, c["pk"] FROM c`, `SELECT c.id, c["pk"] FROM c`,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},
{Path: []string{"c", "pk"}}, {Path: []string{"c", "pk"}},
}, },
@ -52,6 +52,19 @@ func Test_Parse(t *testing.T) {
) )
}) })
t.Run("Should parse SELECT VALUE", func(t *testing.T) {
testQueryParse(
t,
`SELECT VALUE c.id FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}, IsTopLevel: true},
},
Table: parsers.Table{Value: "c"},
},
)
})
t.Run("Should parse SELECT with single WHERE condition", func(t *testing.T) { t.Run("Should parse SELECT with single WHERE condition", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,
@ -59,13 +72,13 @@ func Test_Parse(t *testing.T) {
FROM c FROM c
WHERE c.isCool=true`, WHERE c.isCool=true`,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},
}, },
Table: parsers.Table{Value: "c"}, Table: parsers.Table{Value: "c"},
Filters: parsers.ComparisonExpression{ Filters: parsers.ComparisonExpression{
Operation: "=", Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "isCool"}}, Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true}, Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
}, },
}, },
@ -79,7 +92,7 @@ func Test_Parse(t *testing.T) {
FROM c FROM c
WHERE c.id="12345" OR c.pk=123`, WHERE c.id="12345" OR c.pk=123`,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},
{Path: []string{"c", "_self"}, Alias: "self"}, {Path: []string{"c", "_self"}, Alias: "self"},
{Path: []string{"c", "_rid"}}, {Path: []string{"c", "_rid"}},
@ -91,12 +104,12 @@ func Test_Parse(t *testing.T) {
Expressions: []interface{}{ Expressions: []interface{}{
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Operation: "=", Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "id"}}, Left: parsers.SelectItem{Path: []string{"c", "id"}},
Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "12345"}, Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "12345"},
}, },
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Operation: "=", Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "pk"}}, Left: parsers.SelectItem{Path: []string{"c", "pk"}},
Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 123}, Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 123},
}, },
}, },
@ -105,7 +118,7 @@ func Test_Parse(t *testing.T) {
) )
}) })
t.Run("Shoul correctly parse literals in conditions", func(t *testing.T) { t.Run("Should correctly parse literals in conditions", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,
`select c.id `select c.id
@ -115,27 +128,27 @@ func Test_Parse(t *testing.T) {
AND c.float=6.9 AND c.float=6.9
AND c.string="hello"`, AND c.string="hello"`,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{{Path: []string{"c", "id"}, Alias: ""}}, SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
Table: parsers.Table{Value: "c"}, Table: parsers.Table{Value: "c"},
Filters: parsers.LogicalExpression{ Filters: parsers.LogicalExpression{
Expressions: []interface{}{ Expressions: []interface{}{
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "boolean"}}, Left: parsers.SelectItem{Path: []string{"c", "boolean"}},
Right: parsers.Constant{Type: 3, Value: true}, Right: parsers.Constant{Type: 3, Value: true},
Operation: "=", Operation: "=",
}, },
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "integer"}}, Left: parsers.SelectItem{Path: []string{"c", "integer"}},
Right: parsers.Constant{Type: 1, Value: 1}, Right: parsers.Constant{Type: 1, Value: 1},
Operation: "=", Operation: "=",
}, },
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "float"}}, Left: parsers.SelectItem{Path: []string{"c", "float"}},
Right: parsers.Constant{Type: 2, Value: 6.9}, Right: parsers.Constant{Type: 2, Value: 6.9},
Operation: "=", Operation: "=",
}, },
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "string"}}, Left: parsers.SelectItem{Path: []string{"c", "string"}},
Right: parsers.Constant{Type: 0, Value: "hello"}, Right: parsers.Constant{Type: 0, Value: "hello"},
Operation: "=", Operation: "=",
}, },

View File

@ -14,24 +14,33 @@ func Execute(query parsers.SelectStmt, data []RowType) []RowType {
for _, row := range data { for _, row := range data {
// Check if the row satisfies the filter conditions // Check if the row satisfies the filter conditions
if evaluateFilters(query.Filters, row) { if evaluateFilters(query.Filters, row) {
// Construct a new row based on the selected columns result = append(result, selectRow(query.SelectItems, row))
newRow := make(map[string]interface{})
for _, column := range query.Columns {
destinationName := column.Alias
if destinationName == "" {
destinationName = column.Path[len(column.Path)-1]
}
newRow[destinationName] = getFieldValue(column, row)
}
// Add the new row to the result
result = append(result, newRow)
} }
} }
return result return result
} }
func selectRow(selectItems []parsers.SelectItem, row RowType) interface{} {
// When the first value is top level, select it instead
if len(selectItems) > 0 && selectItems[0].IsTopLevel {
return getFieldValue(selectItems[0], row)
}
// Construct a new row based on the selected columns
newRow := make(map[string]interface{})
for _, column := range selectItems {
destinationName := column.Alias
if destinationName == "" {
destinationName = column.Path[len(column.Path)-1]
}
newRow[destinationName] = getFieldValue(column, row)
}
return newRow
}
// Helper function to evaluate filter conditions recursively // Helper function to evaluate filter conditions recursively
func evaluateFilters(expr ExpressionType, row RowType) bool { func evaluateFilters(expr ExpressionType, row RowType) bool {
if expr == nil { if expr == nil {
@ -74,7 +83,7 @@ func evaluateFilters(expr ExpressionType, row RowType) bool {
return false return false
} }
func getFieldValue(field parsers.FieldPath, row RowType) interface{} { func getFieldValue(field parsers.SelectItem, row RowType) interface{} {
value := row value := row
for _, pathSegment := range field.Path[1:] { for _, pathSegment := range field.Path[1:] {
if nestedValue, ok := value.(map[string]interface{}); ok { if nestedValue, ok := value.(map[string]interface{}); ok {
@ -88,7 +97,7 @@ func getFieldValue(field parsers.FieldPath, row RowType) interface{} {
func getExpressionParameterValue(parameter interface{}, row RowType) interface{} { func getExpressionParameterValue(parameter interface{}, row RowType) interface{} {
switch typedParameter := parameter.(type) { switch typedParameter := parameter.(type) {
case parsers.FieldPath: case parsers.SelectItem:
return getFieldValue(typedParameter, row) return getFieldValue(typedParameter, row)
case parsers.Constant: case parsers.Constant:
return typedParameter.Value return typedParameter.Value

View File

@ -27,11 +27,11 @@ func Test_Execute(t *testing.T) {
map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true}, map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
} }
t.Run("Shoul execute simple SELECT", func(t *testing.T) { t.Run("Should execute simple SELECT", func(t *testing.T) {
testQueryExecute( testQueryExecute(
t, t,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},
{Path: []string{"c", "pk"}}, {Path: []string{"c", "pk"}},
}, },
@ -45,17 +45,34 @@ func Test_Execute(t *testing.T) {
) )
}) })
t.Run("Shoul execute SELECT with single WHERE condition", func(t *testing.T) { t.Run("Should execute SELECT VALUE", func(t *testing.T) {
testQueryExecute( testQueryExecute(
t, t,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}, IsTopLevel: true},
},
Table: parsers.Table{Value: "c"},
},
mockData,
[]memoryexecutor.RowType{
"12345",
"67890",
},
)
})
t.Run("Should execute SELECT with single WHERE condition", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},
}, },
Table: parsers.Table{Value: "c"}, Table: parsers.Table{Value: "c"},
Filters: parsers.ComparisonExpression{ Filters: parsers.ComparisonExpression{
Operation: "=", Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "isCool"}}, Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true}, Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
}, },
}, },
@ -70,7 +87,7 @@ func Test_Execute(t *testing.T) {
testQueryExecute( testQueryExecute(
t, t,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},
{Path: []string{"c", "_self"}, Alias: "self"}, {Path: []string{"c", "_self"}, Alias: "self"},
}, },
@ -80,12 +97,12 @@ func Test_Execute(t *testing.T) {
Expressions: []interface{}{ Expressions: []interface{}{
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Operation: "=", Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "id"}}, Left: parsers.SelectItem{Path: []string{"c", "id"}},
Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "67890"}, Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "67890"},
}, },
parsers.ComparisonExpression{ parsers.ComparisonExpression{
Operation: "=", Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "pk"}}, Left: parsers.SelectItem{Path: []string{"c", "pk"}},
Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 456}, Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 456},
}, },
}, },