Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis State Store query: numeric operators do not work correctly on large numbers #3334

Merged
merged 4 commits into from
Feb 5, 2024
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
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
Loading