A single *Thing in a subquery is the same as LIMIT 1

This commit is contained in:
Tobie Morgan Hitchcock 2017-11-26 13:45:48 +00:00
parent 687c3238b0
commit 1ac78aa950
3 changed files with 63 additions and 2 deletions

View file

@ -275,6 +275,23 @@ func (e *executor) fetchVersion(ctx context.Context, val sql.Expr) (int64, error
}
func (e *executor) fetchOutputs(ctx context.Context, stm *sql.SelectStatement) (int, error) {
l, err := e.fetchLimit(ctx, stm.Limit)
if err != nil {
return -1, err
}
if len(stm.What) == 1 {
if _, ok := stm.What[0].(*sql.Thing); ok {
l = 1
}
}
return l, nil
}
func calcAsBool(i interface{}) bool {
switch v := i.(type) {

View file

@ -95,12 +95,12 @@ func (e *executor) fetchSelect(ctx context.Context, stm *sql.SelectStatement, do
return nil, err
}
lim, err := e.fetchLimit(ctx, stm.Limit)
cnt, err := e.fetchOutputs(ctx, stm)
if err != nil {
return nil, err
}
switch lim {
switch cnt {
case 1:
switch len(stm.Expr) {
case 1:

View file

@ -159,6 +159,28 @@ func TestSelect(t *testing.T) {
})
Convey("Select records using an * subquery, specifying a single record", t, func() {
setupDB()
txt := `
USE NS test DB test;
CREATE person:1 SET name="Tobias";
CREATE person:2 SET name="Silvana";
CREATE person:3 SET name="Jonathan";
CREATE person:4 SET name="Benjamin";
CREATE person:5 SET name="Alexander";
SELECT * FROM (SELECT * FROM person:5);
`
res, err := Execute(setupKV(), txt, nil)
So(err, ShouldBeNil)
So(res, ShouldHaveLength, 7)
So(res[6].Result, ShouldHaveLength, 1)
So(data.Consume(res[6].Result[0]).Get("name").Data(), ShouldEqual, "Alexander")
})
Convey("Select records using an id subquery", t, func() {
setupDB()
@ -204,6 +226,28 @@ func TestSelect(t *testing.T) {
})
Convey("Select records using an id subquery, specifying a single record", t, func() {
setupDB()
txt := `
USE NS test DB test;
CREATE person:1 SET name="Tobias";
CREATE person:2 SET name="Silvana";
CREATE person:3 SET name="Jonathan";
CREATE person:4 SET name="Benjamin";
CREATE person:5 SET name="Alexander";
SELECT * FROM (SELECT id FROM (SELECT * FROM person:5));
`
res, err := Execute(setupKV(), txt, nil)
So(err, ShouldBeNil)
So(res, ShouldHaveLength, 7)
So(res[6].Result, ShouldHaveLength, 1)
So(data.Consume(res[6].Result[0]).Get("name").Data(), ShouldEqual, "Alexander")
})
Convey("Select records using a single field subquery", t, func() {
setupDB()