2024-02-11 20:15:08 +00:00
|
|
|
package nosql_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
"github.com/pikami/cosmium/parsers"
|
2024-02-11 20:15:08 +00:00
|
|
|
"github.com/pikami/cosmium/parsers/nosql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// For Parser Debugging
|
|
|
|
// func Test_ParseTest(t *testing.T) {
|
|
|
|
// // select c.id, c._self, c._rid, c._ts, [c[\"pk\"]] as _partitionKeyValue from c
|
2024-02-13 19:22:55 +00:00
|
|
|
// res, err := nosql.Parse("", []byte("SELECT VALUE c.id FROM c"))
|
2024-02-11 20:15:08 +00:00
|
|
|
// if err != nil {
|
|
|
|
// log.Fatal(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// result, err := json.MarshalIndent(res, "", " ")
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fmt.Printf("output:\n%v\n", string(result))
|
|
|
|
// }
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
func testQueryParse(t *testing.T, query string, expectedQuery parsers.SelectStmt) {
|
2024-02-11 20:15:08 +00:00
|
|
|
parsedQuery, err := nosql.Parse("", []byte(query))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(parsedQuery, expectedQuery) {
|
|
|
|
t.Errorf("parsed query does not match expected structure.\nExpected: %+v\nGot: %+v", expectedQuery, parsedQuery)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Parse(t *testing.T) {
|
2024-02-13 19:22:55 +00:00
|
|
|
t.Run("Should parse simple SELECT", func(t *testing.T) {
|
2024-02-11 20:15:08 +00:00
|
|
|
testQueryParse(
|
|
|
|
t,
|
2024-02-11 22:55:07 +00:00
|
|
|
`SELECT c.id, c["pk"] FROM c`,
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.SelectStmt{
|
2024-02-13 19:22:55 +00:00
|
|
|
SelectItems: []parsers.SelectItem{
|
2024-02-11 20:15:08 +00:00
|
|
|
{Path: []string{"c", "id"}},
|
|
|
|
{Path: []string{"c", "pk"}},
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
Table: parsers.Table{Value: "c"},
|
2024-02-11 20:15:08 +00:00
|
|
|
},
|
|
|
|
)
|
2024-02-14 19:03:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should parse SELECT TOP", func(t *testing.T) {
|
|
|
|
testQueryParse(
|
|
|
|
t,
|
|
|
|
`SELECT TOP 1 c.id FROM c`,
|
|
|
|
parsers.SelectStmt{
|
|
|
|
SelectItems: []parsers.SelectItem{
|
|
|
|
{Path: []string{"c", "id"}},
|
|
|
|
},
|
|
|
|
Table: parsers.Table{Value: "c"},
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
)
|
2024-02-11 20:15:08 +00:00
|
|
|
})
|
|
|
|
|
2024-02-13 19:22:55 +00:00
|
|
|
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"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-13 19:57:33 +00:00
|
|
|
t.Run("Should parse SELECT array", func(t *testing.T) {
|
|
|
|
testQueryParse(
|
|
|
|
t,
|
|
|
|
`SELECT [c.id, c.pk] as arr FROM c`,
|
|
|
|
parsers.SelectStmt{
|
|
|
|
SelectItems: []parsers.SelectItem{
|
|
|
|
{
|
|
|
|
Alias: "arr",
|
|
|
|
Type: parsers.SelectItemTypeArray,
|
|
|
|
SelectItems: []parsers.SelectItem{
|
|
|
|
{Path: []string{"c", "id"}},
|
|
|
|
{Path: []string{"c", "pk"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Table: parsers.Table{Value: "c"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-13 20:42:18 +00:00
|
|
|
t.Run("Should parse SELECT object", func(t *testing.T) {
|
|
|
|
testQueryParse(
|
|
|
|
t,
|
|
|
|
`SELECT { id: c.id, _pk: c.pk } AS obj FROM c`,
|
|
|
|
parsers.SelectStmt{
|
|
|
|
SelectItems: []parsers.SelectItem{
|
|
|
|
{
|
|
|
|
Alias: "obj",
|
|
|
|
Type: parsers.SelectItemTypeObject,
|
|
|
|
SelectItems: []parsers.SelectItem{
|
|
|
|
{Alias: "id", Path: []string{"c", "id"}},
|
|
|
|
{Alias: "_pk", Path: []string{"c", "pk"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Table: parsers.Table{Value: "c"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-11 23:54:12 +00:00
|
|
|
t.Run("Should parse SELECT with single WHERE condition", func(t *testing.T) {
|
2024-02-11 20:15:08 +00:00
|
|
|
testQueryParse(
|
|
|
|
t,
|
|
|
|
`select c.id
|
|
|
|
FROM c
|
2024-02-11 22:17:21 +00:00
|
|
|
WHERE c.isCool=true`,
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.SelectStmt{
|
2024-02-13 19:22:55 +00:00
|
|
|
SelectItems: []parsers.SelectItem{
|
2024-02-11 20:15:08 +00:00
|
|
|
{Path: []string{"c", "id"}},
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
Table: parsers.Table{Value: "c"},
|
|
|
|
Filters: parsers.ComparisonExpression{
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
2024-02-11 20:15:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should parse SELECT with multiple WHERE conditions", func(t *testing.T) {
|
|
|
|
testQueryParse(
|
|
|
|
t,
|
|
|
|
`select c.id, c._self AS self, c._rid, c._ts
|
|
|
|
FROM c
|
|
|
|
WHERE c.id="12345" OR c.pk=123`,
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.SelectStmt{
|
2024-02-13 19:22:55 +00:00
|
|
|
SelectItems: []parsers.SelectItem{
|
2024-02-11 20:15:08 +00:00
|
|
|
{Path: []string{"c", "id"}},
|
|
|
|
{Path: []string{"c", "_self"}, Alias: "self"},
|
|
|
|
{Path: []string{"c", "_rid"}},
|
|
|
|
{Path: []string{"c", "_ts"}},
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
Table: parsers.Table{Value: "c"},
|
|
|
|
Filters: parsers.LogicalExpression{
|
|
|
|
Operation: parsers.LogicalExpressionTypeOr,
|
2024-02-11 20:15:08 +00:00
|
|
|
Expressions: []interface{}{
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.ComparisonExpression{
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "12345"},
|
2024-02-11 20:15:08 +00:00
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.ComparisonExpression{
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "pk"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 123},
|
2024-02-11 20:15:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-13 19:22:55 +00:00
|
|
|
t.Run("Should correctly parse literals in conditions", func(t *testing.T) {
|
2024-02-11 20:15:08 +00:00
|
|
|
testQueryParse(
|
|
|
|
t,
|
|
|
|
`select c.id
|
|
|
|
FROM c
|
|
|
|
WHERE c.boolean=true
|
|
|
|
AND c.integer=1
|
|
|
|
AND c.float=6.9
|
|
|
|
AND c.string="hello"`,
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.SelectStmt{
|
2024-02-13 19:22:55 +00:00
|
|
|
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
|
|
|
|
Table: parsers.Table{Value: "c"},
|
2024-02-11 21:14:30 +00:00
|
|
|
Filters: parsers.LogicalExpression{
|
2024-02-11 20:15:08 +00:00
|
|
|
Expressions: []interface{}{
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.ComparisonExpression{
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "boolean"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: 3, Value: true},
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.ComparisonExpression{
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "integer"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: 1, Value: 1},
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.ComparisonExpression{
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "float"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: 2, Value: 6.9},
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
parsers.ComparisonExpression{
|
2024-02-13 19:22:55 +00:00
|
|
|
Left: parsers.SelectItem{Path: []string{"c", "string"}},
|
2024-02-11 21:14:30 +00:00
|
|
|
Right: parsers.Constant{Type: 0, Value: "hello"},
|
2024-02-11 20:15:08 +00:00
|
|
|
Operation: "=",
|
|
|
|
},
|
|
|
|
},
|
2024-02-11 21:14:30 +00:00
|
|
|
Operation: parsers.LogicalExpressionTypeAnd,
|
2024-02-11 20:15:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|