Kuba Podgórski 2020-08-24 15:32:06 +02:00
parent bc3f329e8f
commit 397d37a129
2 changed files with 54 additions and 1 deletions

View file

@ -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()

View file

@ -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")
}