fix parens around where clause bug (#2121)

Bug: if a sql query had a where clause within parens, the entire
clause would be ignored; and instead of it translating to a pql
intersection, it would become an All().

This occured b/c the parser library mapped such an expresstion to
a sqlparser.ParenExpr, and we did not have this as a condition in
a type switch.

So instead of treating a ParenExpr as nothing, we now recurse into
it.
This commit is contained in:
Samir Patel 2022-06-16 22:21:24 -05:00 • committed by GitHub
parent c0b26d5b69
commit f08d3e2834
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View file

@ -499,9 +499,10 @@ func TestQuerySQL(t *testing.T) {
defer tearDownFunc()
tests := []struct {
sql string
exp tableResponse
eq func(tableResponse, tableResponse) error
sql string
exp tableResponse
eq func(tableResponse, tableResponse) error
wantErr string
}{
{
// Extract(Limit(All(), limit=100, offset=0),Rows(age))
@ -885,6 +886,26 @@ func TestQuerySQL(t *testing.T) {
},
eq: equalUnordered,
},
{
//Extract(Intersect(Row(timestamp>"2017-09-02T12:32:00Z"),Row(timestamp<"2019-09-02T12:32:00Z")),Rows(age), Rows(height))
//Testing the parenthesis around where clause
sql: "select age, height from grouper where (timestamp > '2017-09-02T12:32:00Z' and timestamp < '2019-09-02T12:32:00Z')",
exp: tableResponse{
headers: []columnInfo{
{"age", "int64"},
{"height", "int64"},
},
rows: []row{
{[]columnResponse{int64(31), int64(110)}},
},
},
eq: equalUnordered,
},
{
//Testing empty parenthesis around where clause
sql: "select age, height from grouper where ()",
wantErr: "parsing sql",
},
{
//Distinct(Row(timestamp>"2019-09-02T12:32:00Z"), index='grouper',field='age')
sql: "select distinct age from grouper where timestamp > '2019-09-02T12:32:00Z'",
@ -997,6 +1018,14 @@ func TestQuerySQL(t *testing.T) {
for i, test := range tests {
t.Run("test-"+strconv.Itoa(i), func(t *testing.T) {
resp, err := gh.QuerySQLUnary(ctx, &pb.QuerySQLRequest{Sql: test.sql})
if test.wantErr != "" {
if err == nil {
t.Errorf("expected error %q, got nil", test.wantErr)
} else if !strings.Contains(err.Error(), test.wantErr) {
t.Errorf("expected error %q, got %q", test.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("sql: %s, error: %v", test.sql, err)
} else {
@ -1020,6 +1049,14 @@ func TestQuerySQL(t *testing.T) {
}
mock := &mockPilosa_QuerySQLServer{ctx: context.Background()}
err := gh.QuerySQL(&pb.QuerySQLRequest{Sql: test.sql}, mock)
if test.wantErr != "" {
if err == nil {
t.Errorf("expected error %q, got nil", test.wantErr)
} else if !strings.Contains(err.Error(), test.wantErr) {
t.Errorf("expected error %q, got %q", test.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("sql: %s, error: %v", test.sql, err)
} else {

View file

@ -380,6 +380,8 @@ func generateWhereMask(e sqlparser.Expr) []wherePart {
wp = append(wp, generateWhereMask(expr.Expr)...)
case *sqlparser.IsExpr:
wp = append(wp, WherePartFieldCondition)
case *sqlparser.ParenExpr:
wp = append(wp, generateWhereMask(expr.Expr)...)
}
return wp