Skip to content

Commit

Permalink
Redis State Store query: numeric operators do not work correctly on l…
Browse files Browse the repository at this point in the history
…arge numbers (dapr#3334)

Signed-off-by: Guido Spadotto <guido.spadotto@profesia.it>
Signed-off-by: Guido Spadotto <guido.spad8@gmail.com>
Co-authored-by: Guido Spadotto <guido.spadotto@profesia.it>
  • Loading branch information
2 people authored and berndverst committed Feb 7, 2024
1 parent 6ef6281 commit 70c23bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 18 additions & 6 deletions state/redis/redis_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ func (q *Query) VisitEQ(f *query.EQ) (string, error) {
switch v := f.Val.(type) {
case string:
return fmt.Sprintf("@%s:(%s)", alias, v), nil
case float64, float32:
return fmt.Sprintf("@%s:[%f %f]", alias, v, v), nil
default:
return fmt.Sprintf("@%s:[%v %v]", alias, v, v), nil
return fmt.Sprintf("@%s:[%d %d]", alias, v, v), nil
}
}

Expand All @@ -77,8 +79,10 @@ func (q *Query) VisitNEQ(f *query.NEQ) (string, error) {
switch v := f.Val.(type) {
case string:
return fmt.Sprintf("@%s:(%s)", alias, v), nil
case float64, float32:
return fmt.Sprintf("@%s:[%f %f]", alias, v, v), nil
default:
return fmt.Sprintf("@%s:[%v %v]", alias, v, v), nil
return fmt.Sprintf("@%s:[%d %d]", alias, v, v), nil
}
}

Expand All @@ -92,8 +96,10 @@ func (q *Query) VisitGT(f *query.GT) (string, error) {
switch v := f.Val.(type) {
case string:
return "", fmt.Errorf("unsupported type of value %s; string type not permitted", f.Val)
case float64, float32:
return fmt.Sprintf("@%s:[(%f +inf]", alias, v), nil
default:
return fmt.Sprintf("@%s:[(%v +inf]", alias, v), nil
return fmt.Sprintf("@%s:[(%d +inf]", alias, v), nil
}
}

Expand All @@ -107,8 +113,10 @@ func (q *Query) VisitGTE(f *query.GTE) (string, error) {
switch v := f.Val.(type) {
case string:
return "", fmt.Errorf("unsupported type of value %s; string type not permitted", f.Val)
case float64, float32:
return fmt.Sprintf("@%s:[%f +inf]", alias, v), nil
default:
return fmt.Sprintf("@%s:[%v +inf]", alias, v), nil
return fmt.Sprintf("@%s:[%d +inf]", alias, v), nil
}
}

Expand All @@ -122,8 +130,10 @@ func (q *Query) VisitLT(f *query.LT) (string, error) {
switch v := f.Val.(type) {
case string:
return "", fmt.Errorf("unsupported type of value %s; string type not permitted", f.Val)
case float64, float32:
return fmt.Sprintf("@%s:[-inf (%f]", alias, v), nil
default:
return fmt.Sprintf("@%s:[-inf (%v]", alias, v), nil
return fmt.Sprintf("@%s:[-inf (%d]", alias, v), nil
}
}

Expand All @@ -136,8 +146,10 @@ func (q *Query) VisitLTE(f *query.LTE) (string, error) {
switch v := f.Val.(type) {
case string:
return "", fmt.Errorf("unsupported type of value %s; string type not permitted", f.Val)
case float64, float32:
return fmt.Sprintf("@%s:[-inf %f]", alias, v), nil
default:
return fmt.Sprintf("@%s:[-inf %v]", alias, v), nil
return fmt.Sprintf("@%s:[-inf %d]", alias, v), nil
}
}

Expand Down
8 changes: 4 additions & 4 deletions state/redis/redis_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/dapr/components-contrib/state/query"
)

func TestMongoQuery(t *testing.T) {
func TestRedisQuery(t *testing.T) {
tests := []struct {
input string
query []interface{}
Expand All @@ -44,15 +44,15 @@ func TestMongoQuery(t *testing.T) {
},
{
input: "../../tests/state/query/q6.json",
query: []interface{}{"((@id:[123 123])|((@org:(B)) (((@id:[567 567])|(@id:[890 890])))))", "SORTBY", "id", "LIMIT", "0", "2"},
query: []interface{}{"((@id:[123.000000 123.000000])|((@org:(B)) (((@id:[567.000000 567.000000])|(@id:[890.000000 890.000000])))))", "SORTBY", "id", "LIMIT", "0", "2"},
},
{
input: "../../tests/state/query/q6-notequal.json",
query: []interface{}{"((@id:[123 123])|(-(@org:(B)) (((@id:[567 567])|(@id:[890 890])))))", "SORTBY", "id", "LIMIT", "0", "2"},
query: []interface{}{"((@id:[123.000000 123.000000])|(-(@org:(B)) (((@id:[567.000000 567.000000])|(@id:[890.000000 890.000000])))))", "SORTBY", "id", "LIMIT", "0", "2"},
},
{
input: "../../tests/state/query/q7.json",
query: []interface{}{"((@id:[-inf (123])|((@org:[2 +inf]) (((@id:[567 567])|(@id:[890 890])))))", "SORTBY", "id", "LIMIT", "0", "2"},
query: []interface{}{"((@id:[-inf (123.000000])|((@org:[2.000000 +inf]) (((@id:[567.000000 567.000000])|(@id:[890.000000 890.000000])))))", "SORTBY", "id", "LIMIT", "0", "2"},
},
}
for _, test := range tests {
Expand Down

0 comments on commit 70c23bb

Please sign in to comment.