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))
rdr2 := io.NopCloser(bytes.NewBuffer(buf))
fmt.Println(readBody(rdr1))
bodyStr := readBody(rdr1)
if bodyStr != "" {
fmt.Println(bodyStr)
}
c.Request.Body = rdr2
c.Next()

View File

@ -16,8 +16,16 @@ const (
ConstantTypeBoolean
)
type SelectItemType int
const (
SelectItemTypeField SelectItemType = iota
SelectItemTypeObject
SelectItemTypeArray
)
type SelectStmt struct {
Columns []FieldPath
SelectItems []SelectItem
Table Table
Filters interface{}
}
@ -26,9 +34,12 @@ type Table struct {
Value string
}
type FieldPath struct {
type SelectItem struct {
Alias string
Path []string
SelectItems []SelectItem
Type SelectItemType
IsTopLevel bool
}
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) {
selectStmt := parsers.SelectStmt{
Columns: columns.([]parsers.FieldPath),
SelectItems: columns.([]parsers.SelectItem),
Table: table.(parsers.Table),
}
@ -18,7 +18,7 @@ func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt
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{})
paths := make([]string, 1)
@ -27,21 +27,21 @@ func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parse
paths = append(paths, p.(string))
}
fieldPath := parsers.FieldPath{Path: paths}
selectItem := parsers.SelectItem{Path: paths, Type: selectItemType}
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{})
columnList := make([]parsers.FieldPath, len(collsAsArray) + 1)
columnList[0] = column.(parsers.FieldPath)
columnList := make([]parsers.SelectItem, len(collsAsArray) + 1)
columnList[0] = column.(parsers.SelectItem)
for i, v := range collsAsArray {
if col, ok := v.(parsers.FieldPath); ok {
if col, ok := v.(parsers.SelectItem); ok {
columnList[i+1] = col
}
}
@ -79,23 +79,31 @@ Input <- selectStmt:SelectStmt {
return selectStmt, nil
}
SelectStmt <- Select ws columns:ColumnList ws
SelectStmt <- Select ws columns:Selection ws
From ws table:TableName ws
whereClause:(ws Where ws condition:Condition { return condition, nil })? {
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)
}
SelectValueSpec <- "VALUE" ws column:SelectItem {
selectItem := column.(parsers.SelectItem)
selectItem.IsTopLevel = true
return makeColumnList(selectItem, make([]interface{}, 0))
}
TableName <- key:Identifier {
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 })? {
return makeFieldPath(name, path, asClause)
return makeSelectItem(name, path, asClause, parsers.SelectItemTypeField)
}
DotFieldAccess <- "." id:Identifier {
@ -122,7 +130,7 @@ AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpressi
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
}

View File

@ -12,7 +12,7 @@ import (
// For Parser Debugging
// func Test_ParseTest(t *testing.T) {
// // 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 {
// log.Fatal(err)
// }
@ -38,12 +38,12 @@ func testQueryParse(t *testing.T, query string, expectedQuery parsers.SelectStmt
}
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(
t,
`SELECT c.id, c["pk"] FROM c`,
parsers.SelectStmt{
Columns: []parsers.FieldPath{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{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) {
testQueryParse(
t,
@ -59,13 +72,13 @@ func Test_Parse(t *testing.T) {
FROM c
WHERE c.isCool=true`,
parsers.SelectStmt{
Columns: []parsers.FieldPath{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
},
Table: parsers.Table{Value: "c"},
Filters: parsers.ComparisonExpression{
Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "isCool"}},
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
},
},
@ -79,7 +92,7 @@ func Test_Parse(t *testing.T) {
FROM c
WHERE c.id="12345" OR c.pk=123`,
parsers.SelectStmt{
Columns: []parsers.FieldPath{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{Path: []string{"c", "_self"}, Alias: "self"},
{Path: []string{"c", "_rid"}},
@ -91,12 +104,12 @@ func Test_Parse(t *testing.T) {
Expressions: []interface{}{
parsers.ComparisonExpression{
Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "id"}},
Left: parsers.SelectItem{Path: []string{"c", "id"}},
Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "12345"},
},
parsers.ComparisonExpression{
Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "pk"}},
Left: parsers.SelectItem{Path: []string{"c", "pk"}},
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(
t,
`select c.id
@ -115,27 +128,27 @@ func Test_Parse(t *testing.T) {
AND c.float=6.9
AND c.string="hello"`,
parsers.SelectStmt{
Columns: []parsers.FieldPath{{Path: []string{"c", "id"}, Alias: ""}},
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
Table: parsers.Table{Value: "c"},
Filters: parsers.LogicalExpression{
Expressions: []interface{}{
parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "boolean"}},
Left: parsers.SelectItem{Path: []string{"c", "boolean"}},
Right: parsers.Constant{Type: 3, Value: true},
Operation: "=",
},
parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "integer"}},
Left: parsers.SelectItem{Path: []string{"c", "integer"}},
Right: parsers.Constant{Type: 1, Value: 1},
Operation: "=",
},
parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "float"}},
Left: parsers.SelectItem{Path: []string{"c", "float"}},
Right: parsers.Constant{Type: 2, Value: 6.9},
Operation: "=",
},
parsers.ComparisonExpression{
Left: parsers.FieldPath{Path: []string{"c", "string"}},
Left: parsers.SelectItem{Path: []string{"c", "string"}},
Right: parsers.Constant{Type: 0, Value: "hello"},
Operation: "=",
},

View File

@ -14,9 +14,22 @@ func Execute(query parsers.SelectStmt, data []RowType) []RowType {
for _, row := range data {
// Check if the row satisfies the filter conditions
if evaluateFilters(query.Filters, row) {
result = append(result, selectRow(query.SelectItems, row))
}
}
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 query.Columns {
for _, column := range selectItems {
destinationName := column.Alias
if destinationName == "" {
destinationName = column.Path[len(column.Path)-1]
@ -24,12 +37,8 @@ func Execute(query parsers.SelectStmt, data []RowType) []RowType {
newRow[destinationName] = getFieldValue(column, row)
}
// Add the new row to the result
result = append(result, newRow)
}
}
return result
return newRow
}
// Helper function to evaluate filter conditions recursively
@ -74,7 +83,7 @@ func evaluateFilters(expr ExpressionType, row RowType) bool {
return false
}
func getFieldValue(field parsers.FieldPath, row RowType) interface{} {
func getFieldValue(field parsers.SelectItem, row RowType) interface{} {
value := row
for _, pathSegment := range field.Path[1:] {
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{} {
switch typedParameter := parameter.(type) {
case parsers.FieldPath:
case parsers.SelectItem:
return getFieldValue(typedParameter, row)
case parsers.Constant:
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},
}
t.Run("Shoul execute simple SELECT", func(t *testing.T) {
t.Run("Should execute simple SELECT", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
Columns: []parsers.FieldPath{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{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(
t,
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"}},
},
Table: parsers.Table{Value: "c"},
Filters: parsers.ComparisonExpression{
Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "isCool"}},
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
},
},
@ -70,7 +87,7 @@ func Test_Execute(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
Columns: []parsers.FieldPath{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{Path: []string{"c", "_self"}, Alias: "self"},
},
@ -80,12 +97,12 @@ func Test_Execute(t *testing.T) {
Expressions: []interface{}{
parsers.ComparisonExpression{
Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "id"}},
Left: parsers.SelectItem{Path: []string{"c", "id"}},
Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "67890"},
},
parsers.ComparisonExpression{
Operation: "=",
Left: parsers.FieldPath{Path: []string{"c", "pk"}},
Left: parsers.SelectItem{Path: []string{"c", "pk"}},
Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 456},
},
},