diff --git a/server/grpc_test.go b/server/grpc_test.go index 77c030e23..f3521f1e9 100644 --- a/server/grpc_test.go +++ b/server/grpc_test.go @@ -25,7 +25,9 @@ import ( "github.com/pilosa/pilosa/v2/pql" pb "github.com/pilosa/pilosa/v2/proto" "github.com/pilosa/pilosa/v2/server" + "github.com/pilosa/pilosa/v2/sql" "github.com/pilosa/pilosa/v2/test" + "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -770,6 +772,50 @@ func TestQuerySQLUnary(t *testing.T) { } } +func TestQuerySQLUnaryWithError(t *testing.T) { + + ctx := context.Background() + gh, tearDownFunc := setUpTestQuerySQLUnary(ctx, t) + defer tearDownFunc() + + tests := []struct { + sql string + err error + }{ + { + + sql: "select * from index_not_found", + err: pilosa.ErrIndexNotFound, + }, + { + + sql: "select field_not_found from grouper", + err: pilosa.ErrFieldNotFound, + }, + { + + sql: "select * from grouper, index_not_found", + err: sql.ErrUnsupportedQuery, + }, + { + + sql: "select _id, age, field_not_found from grouper", + err: pilosa.ErrFieldNotFound, + }, + } + + for i, test := range tests { + t.Run("test-"+strconv.Itoa(i), func(t *testing.T) { + _, err := gh.QuerySQLUnary(ctx, &pb.QuerySQLRequest{Sql: test.sql}) + if err == nil { + t.Fatalf("sql: %s, expected error: %v", test.sql, test.err) + } else if errors.Cause(err) != test.err { + t.Fatalf("sql: %s, expected error: %v, got: %v", test.sql, test.err, err) + } + }) + } +} + func setUpTestQuerySQLUnary(ctx context.Context, t *testing.T) (gh *server.GRPCHandler, tearDownFunc func()) { t.Helper() diff --git a/sql/extract.go b/sql/extract.go index 8ea1c00cf..500df9012 100644 --- a/sql/extract.go +++ b/sql/extract.go @@ -145,7 +145,11 @@ func extractSelectFields(index *pilosa.Index, stmt *sqlparser.Select) ([]Column, column = NewIDIndexColumn(index, alias) } } else { - column = NewFieldColumn(index.Field(fieldName), alias) + field := index.Field(fieldName) + if field == nil { + return nil, features, errors.Wrapf(pilosa.ErrFieldNotFound, "field %s", fieldName) + } + column = NewFieldColumn(field, alias) } case *sqlparser.FuncExpr: funcName := FuncName(strings.ToLower(colExpr.Name.String())) @@ -160,6 +164,9 @@ func extractSelectFields(index *pilosa.Index, stmt *sqlparser.Select) ([]Column, if colExpr, ok := expr.Expr.(*sqlparser.ColName); ok { fieldName := colExpr.Name.String() field = index.Field(fieldName) + if field == nil { + return nil, features, errors.Wrapf(pilosa.ErrFieldNotFound, "field %s", fieldName) + } } else { return nil, features, errors.New("table name is required") }