Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
statestore query API: remove 'value' prefix from key names in query J…
…SON object

Signed-off-by: Dmitry Shmulevich <[email protected]>
  • Loading branch information
dmitsh committed Feb 10, 2022
commit 716f9d2ffe641dba974057de269c9660cc6e5b82
8 changes: 4 additions & 4 deletions state/azure/cosmosdb/cosmosdb_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (q *Query) VisitEQ(f *query.EQ) (string, error) {
}
name := q.setNextParameter(val)

return fmt.Sprintf("%s = %s", replaceKeywords("c."+f.Key), name), nil
return fmt.Sprintf("%s = %s", replaceKeywords("c.value"+f.Key), name), nil
}

func (q *Query) VisitIN(f *query.IN) (string, error) {
Expand All @@ -57,7 +57,7 @@ func (q *Query) VisitIN(f *query.IN) (string, error) {
names[i] = q.setNextParameter(val)
}

return fmt.Sprintf("%s IN (%s)", replaceKeywords("c."+f.Key), strings.Join(names, ", ")), nil
return fmt.Sprintf("%s IN (%s)", replaceKeywords("c.value"+f.Key), strings.Join(names, ", ")), nil
}

func (q *Query) visitFilters(op string, filters []query.Filter) (string, error) {
Expand Down Expand Up @@ -115,9 +115,9 @@ func (q *Query) Finalize(filters string, qq *query.Query) error {
order := make([]string, sz)
for i, item := range qq.Sort {
if item.Order == query.DESC {
order[i] = fmt.Sprintf("%s DESC", replaceKeywords("c."+item.Key))
order[i] = fmt.Sprintf("%s DESC", replaceKeywords("c.value"+item.Key))
} else {
order[i] = fmt.Sprintf("%s ASC", replaceKeywords("c."+item.Key))
order[i] = fmt.Sprintf("%s ASC", replaceKeywords("c.value"+item.Key))
}
}
orderBy = fmt.Sprintf(" ORDER BY %s", strings.Join(order, ", "))
Expand Down
6 changes: 3 additions & 3 deletions state/azure/cosmosdb/cosmosdb_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestCosmosDbQuery(t *testing.T) {
{
input: "../../../tests/state/query/q2.json",
query: documentdb.Query{
Query: "SELECT * FROM c WHERE c.state = @__param__0__",
Query: "SELECT * FROM c WHERE c['value'].state = @__param__0__",
Parameters: []documentdb.Parameter{
{
Name: "@__param__0__",
Expand All @@ -84,7 +84,7 @@ func TestCosmosDbQuery(t *testing.T) {
{
input: "../../../tests/state/query/q3.json",
query: documentdb.Query{
Query: "SELECT * FROM c WHERE c.person.org = @__param__0__ AND c.state IN (@__param__1__, @__param__2__) ORDER BY c.state DESC, c.person.name ASC",
Query: "SELECT * FROM c WHERE c['value'].person.org = @__param__0__ AND c['value'].state IN (@__param__1__, @__param__2__) ORDER BY c['value'].state DESC, c['value'].person.name ASC",
Parameters: []documentdb.Parameter{
{
Name: "@__param__0__",
Expand All @@ -104,7 +104,7 @@ func TestCosmosDbQuery(t *testing.T) {
{
input: "../../../tests/state/query/q4.json",
query: documentdb.Query{
Query: "SELECT * FROM c WHERE c.person.org = @__param__0__ OR (c.person.org = @__param__1__ AND c.state IN (@__param__2__, @__param__3__)) ORDER BY c.state DESC, c.person.name ASC",
Query: "SELECT * FROM c WHERE c['value'].person.org = @__param__0__ OR (c['value'].person.org = @__param__1__ AND c['value'].state IN (@__param__2__, @__param__3__)) ORDER BY c['value'].state DESC, c['value'].person.name ASC",
Parameters: []documentdb.Parameter{
{
Name: "@__param__0__",
Expand Down
6 changes: 3 additions & 3 deletions state/mongodb/mongodb_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ type Query struct {

func (q *Query) VisitEQ(f *query.EQ) (string, error) {
// { <key>: <val> }
return fmt.Sprintf("{ %q: %q }", f.Key, f.Val), nil
return fmt.Sprintf(`{ "value%s": %q }`, f.Key, f.Val), nil
}

func (q *Query) VisitIN(f *query.IN) (string, error) {
// { $in: [ <val1>, <val2>, ... , <valN> ] }
if len(f.Vals) == 0 {
return "", fmt.Errorf("empty IN operator for key %q", f.Key)
}
str := fmt.Sprintf(`{ %q: { "$in": [ %q`, f.Key, f.Vals[0])
str := fmt.Sprintf(`{ "value%s": { "$in": [ %q`, f.Key, f.Vals[0])
for _, v := range f.Vals[1:] {
str += fmt.Sprintf(", %q", v)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (q *Query) Finalize(filters string, qq *query.Query) error {
if s.Order == query.DESC {
order = -1
}
sort = append(sort, bson.E{Key: s.Key, Value: order})
sort = append(sort, bson.E{Key: "value" + s.Key, Value: order})
}
q.opts.SetSort(sort)
}
Expand Down
6 changes: 3 additions & 3 deletions state/mongodb/mongodb_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func TestMongoQuery(t *testing.T) {
},
{
input: "../../tests/state/query/q2.json",
query: `{ "state": "CA" }`,
query: `{ "value.state": "CA" }`,
},
{
input: "../../tests/state/query/q3.json",
query: `{ "$and": [ { "person.org": "A" }, { "state": { "$in": [ "CA", "WA" ] } } ] }`,
query: `{ "$and": [ { "value.person.org": "A" }, { "value.state": { "$in": [ "CA", "WA" ] } } ] }`,
},
{
input: "../../tests/state/query/q4.json",
query: `{ "$or": [ { "person.org": "A" }, { "$and": [ { "person.org": "B" }, { "state": { "$in": [ "CA", "WA" ] } } ] } ] }`,
query: `{ "$or": [ { "value.person.org": "A" }, { "$and": [ { "value.person.org": "B" }, { "value.state": { "$in": [ "CA", "WA" ] } } ] } ] }`,
},
}
for _, test := range tests {
Expand Down
3 changes: 3 additions & 0 deletions state/postgresql/postgresql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func (q *Query) addParamValueAndReturnPosition(value interface{}) int {
}

func translateFieldToFilter(key string) string {
// remove preceding dot
key = strings.TrimPrefix(key, ".")

fieldParts := strings.Split(key, ".")
filterField := fieldParts[0]
fieldParts = fieldParts[1:]
Expand Down
10 changes: 5 additions & 5 deletions state/postgresql/postgresql_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestPostgresqlQueryBuildQuery(t *testing.T) {
input: "../../tests/state/query/q2.json",
query: "SELECT key, value, xmin as etag FROM state WHERE state=$1 LIMIT 2",
},
{
input: "../../tests/state/query/q2-token.json",
query: "SELECT key, value, xmin as etag FROM state WHERE state=$1 LIMIT 2 OFFSET 2",
},
{
input: "../../tests/state/query/q3.json",
query: "SELECT key, value, xmin as etag FROM state WHERE (person->>'org'=$1 AND (state=$2 OR state=$3)) ORDER BY state DESC, person->>'name'",
Expand All @@ -46,11 +50,7 @@ func TestPostgresqlQueryBuildQuery(t *testing.T) {
},
{
input: "../../tests/state/query/q5.json",
query: "SELECT key, value, xmin as etag FROM state WHERE (value->'person'->>'org'=$1 AND (value->'person'->>'name'=$2 OR (value->>'state'=$3 OR value->>'state'=$4))) ORDER BY value->>'state' DESC, value->'person'->>'name' LIMIT 2",
},
{
input: "../../tests/state/query/q6.json",
query: "SELECT key, value, xmin as etag FROM state WHERE value->>'state'=$1 LIMIT 2 OFFSET 2",
query: "SELECT key, value, xmin as etag FROM state WHERE (person->>'org'=$1 AND (person->>'name'=$2 OR (state=$3 OR state=$4))) ORDER BY state DESC, person->>'name' LIMIT 2",
},
}
for _, test := range tests {
Expand Down
20 changes: 10 additions & 10 deletions state/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ func TestQuery(t *testing.T) {
Filters: nil,
Sort: nil,
Page: Pagination{Limit: 2, Token: ""},
Filter: &EQ{Key: "state", Val: "CA"},
Filter: &EQ{Key: ".state", Val: "CA"},
},
},
{
input: "../../tests/state/query/q3.json",
query: Query{
Filters: nil,
Sort: []Sorting{
{Key: "state", Order: "DESC"},
{Key: "person.name", Order: ""},
{Key: ".state", Order: "DESC"},
{Key: ".person.name", Order: ""},
},
Page: Pagination{Limit: 0, Token: ""},
Filter: &AND{
Filters: []Filter{
&EQ{Key: "person.org", Val: "A"},
&IN{Key: "state", Vals: []interface{}{"CA", "WA"}},
&EQ{Key: ".person.org", Val: "A"},
&IN{Key: ".state", Vals: []interface{}{"CA", "WA"}},
},
},
},
Expand All @@ -66,17 +66,17 @@ func TestQuery(t *testing.T) {
query: Query{
Filters: nil,
Sort: []Sorting{
{Key: "state", Order: "DESC"},
{Key: "person.name", Order: ""},
{Key: ".state", Order: "DESC"},
{Key: ".person.name", Order: ""},
},
Page: Pagination{Limit: 2, Token: ""},
Filter: &OR{
Filters: []Filter{
&EQ{Key: "person.org", Val: "A"},
&EQ{Key: ".person.org", Val: "A"},
&AND{
Filters: []Filter{
&EQ{Key: "person.org", Val: "B"},
&IN{Key: "state", Vals: []interface{}{"CA", "WA"}},
&EQ{Key: ".person.org", Val: "B"},
&IN{Key: ".state", Vals: []interface{}{"CA", "WA"}},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"filter": {
"EQ": {
"value.state": "CA"
".state": "CA"
}
},
"page": {
Expand Down
2 changes: 1 addition & 1 deletion tests/state/query/q2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"filter": {
"EQ": {
"state": "CA"
".state": "CA"
}
},
"page": {
Expand Down
8 changes: 4 additions & 4 deletions tests/state/query/q3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
"AND": [
{
"EQ": {
"person.org": "A"
".person.org": "A"
}
},
{
"IN": {
"state":["CA", "WA"]
".state":["CA", "WA"]
}
}
]
},
"sort": [
{
"key": "state",
"key": ".state",
"order": "DESC"
},
{
"key": "person.name"
"key": ".person.name"
}
]
}
10 changes: 5 additions & 5 deletions tests/state/query/q4.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
"OR": [
{
"EQ": {
"person.org": "A"
".person.org": "A"
}
},
{
"AND": [
{
"EQ": {
"person.org": "B"
".person.org": "B"
}
},
{
"IN": {
"state": ["CA", "WA"]
".state": ["CA", "WA"]
}
}
]
Expand All @@ -24,11 +24,11 @@
},
"sort": [
{
"key": "state",
"key": ".state",
"order": "DESC"
},
{
"key": "person.name"
"key": ".person.name"
}
],
"page": {
Expand Down
10 changes: 5 additions & 5 deletions tests/state/query/q5.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
"AND": [
{
"EQ": {
"value.person.org": "A"
".person.org": "A"
}
},
{
"OR": [
{
"EQ": {
"value.person.name": "B"
".person.name": "B"
}
},
{
"IN": {
"value.state": ["CA", "WA"]
".state": ["CA", "WA"]
}
}
]
Expand All @@ -24,11 +24,11 @@
},
"sort": [
{
"key": "value.state",
"key": ".state",
"order": "DESC"
},
{
"key": "value.person.name"
"key": ".person.name"
}
],
"page": {
Expand Down