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

Add union all clause #408

Merged
merged 2 commits into from
Jun 5, 2021
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
1 change: 1 addition & 0 deletions cmd/genji/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRn
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
1 change: 1 addition & 0 deletions engine/badgerengine/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRn
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ go 1.16

require (
github.com/buger/jsonparser v1.1.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/btree v1.0.1
github.com/google/go-cmp v0.5.5
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/vmihailenco/msgpack/v5 v5.3.2
go.etcd.io/bbolt v1.3.5
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/tools v0.1.1
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
11 changes: 9 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -48,7 +54,8 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 15 additions & 0 deletions internal/planner/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ var optimizerRules = []func(s *stream.Stream, tx *database.Transaction) (*stream
func Optimize(s *stream.Stream, tx *database.Transaction) (*stream.Stream, error) {
var err error

if firstNode, ok := s.First().(*stream.ConcatOperator); ok {
// If the first operation is a concat, optimize both streams individually.
s1, err := Optimize(firstNode.S1, tx)
if err != nil {
return nil, err
}
s2, err := Optimize(firstNode.S2, tx)
if err != nil {
return nil, err
}

firstNode.S1, firstNode.S2 = s1, s2
return s, nil
}

for _, rule := range optimizerRules {
s, err = rule(s, tx)
if err != nil {
Expand Down
118 changes: 118 additions & 0 deletions internal/planner/optimizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,121 @@ func TestUseIndexBasedOnSelectionNodeRule_Composite(t *testing.T) {
}
})
}

func TestOptimize(t *testing.T) {
t.Run("concat operator operands are optimized", func(t *testing.T) {
t.Run("PrecalculateExprRule", func(t *testing.T) {
_, tx, cleanup := testutil.NewTestTx(t)
defer cleanup()
testutil.MustExec(t, tx, `
CREATE TABLE foo;
CREATE TABLE bar;
`)

got, err := planner.Optimize(
st.New(st.Concat(
st.New(st.SeqScan("foo")).Pipe(st.Filter(parser.MustParseExpr("a = 1 + 2"))),
st.New(st.SeqScan("bar")).Pipe(st.Filter(parser.MustParseExpr("b = 1 + 2"))),
)),
tx)

want := st.New(st.Concat(
st.New(st.SeqScan("foo")).Pipe(st.Filter(parser.MustParseExpr("a = 3"))),
st.New(st.SeqScan("bar")).Pipe(st.Filter(parser.MustParseExpr("b = 3"))),
))

require.NoError(t, err)
require.Equal(t, want.String(), got.String())
})

t.Run("RemoveUnnecessarySelectionNodesRule", func(t *testing.T) {
_, tx, cleanup := testutil.NewTestTx(t)
defer cleanup()
testutil.MustExec(t, tx, `
CREATE TABLE foo;
CREATE TABLE bar;
`)

got, err := planner.Optimize(
st.New(st.Concat(
st.New(st.SeqScan("foo")).Pipe(st.Filter(parser.MustParseExpr("10"))),
st.New(st.Concat(
st.New(st.SeqScan("bar")).Pipe(st.Filter(parser.MustParseExpr("11"))),
st.New(st.SeqScan("bar")).Pipe(st.Filter(parser.MustParseExpr("12"))),
)),
)),
tx)

want := st.New(st.Concat(
st.New(st.SeqScan("foo")),
st.New(st.Concat(
st.New(st.SeqScan("bar")),
st.New(st.SeqScan("bar")),
)),
))

require.NoError(t, err)
require.Equal(t, want.String(), got.String())
})

t.Run("RemoveUnnecessaryDedupNodeRule", func(t *testing.T) {
_, tx, cleanup := testutil.NewTestTx(t)
defer cleanup()
testutil.MustExec(t, tx, `
CREATE TABLE foo(a integer PRIMARY KEY);
CREATE TABLE bar(a integer PRIMARY KEY);
`)

got, err := planner.Optimize(
st.New(st.Concat(
st.New(st.SeqScan("foo")).
Pipe(st.Project(parser.MustParseExpr("a"))).
Pipe(st.Distinct()),
st.New(st.SeqScan("bar")).
Pipe(st.Project(parser.MustParseExpr("a"))).
Pipe(st.Distinct()),
)),
tx)

want := st.New(st.Concat(
st.New(st.SeqScan("foo")).
Pipe(st.Project(parser.MustParseExpr("a"))),
st.New(st.SeqScan("bar")).
Pipe(st.Project(parser.MustParseExpr("a"))),
))

require.NoError(t, err)
require.Equal(t, want.String(), got.String())
})
})

t.Run("UseIndexBasedOnSelectionNodeRule", func(t *testing.T) {
_, tx, cleanup := testutil.NewTestTx(t)
defer cleanup()
testutil.MustExec(t, tx, `
CREATE TABLE foo;
CREATE TABLE bar;
CREATE INDEX idx_foo_a_d ON foo(a, d);
CREATE INDEX idx_bar_a_d ON bar(a, d);
`)

got, err := planner.Optimize(
st.New(st.Concat(
st.New(st.SeqScan("foo")).
Pipe(st.Filter(parser.MustParseExpr("a = 1"))).
Pipe(st.Filter(parser.MustParseExpr("d = 2"))),
st.New(st.SeqScan("bar")).
Pipe(st.Filter(parser.MustParseExpr("a = 1"))).
Pipe(st.Filter(parser.MustParseExpr("d = 2"))),
)),
tx)

want := st.New(st.Concat(
st.New(st.IndexScan("idx_foo_a_d", st.IndexRange{Min: testutil.ExprList(t, `[1, 2]`), Exact: true})),
st.New(st.IndexScan("idx_bar_a_d", st.IndexRange{Min: testutil.ExprList(t, `[1, 2]`), Exact: true})),
))

require.NoError(t, err)
require.Equal(t, want.String(), got.String())
})
}
190 changes: 190 additions & 0 deletions internal/query/gen_select_with_union_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* CODE GENERATED AUTOMATICALLY WITH github.com/genjidb/genji/dev/gensqltest
* THIS FILE SHOULD NOT BE EDITED BY HAND
*/
package query_test

import (
"testing"

"github.com/genjidb/genji"
"github.com/genjidb/genji/internal/testutil"
"github.com/stretchr/testify/require"
)

func TestGenSelectWithUnion(t *testing.T) {
setup := func(t *testing.T, db *genji.DB) {
t.Helper()

q := `
CREATE TABLE foo;
CREATE TABLE bar;
CREATE TABLE baz;
INSERT INTO foo (a,b) VALUES (1.0, 1.0), (2.0, 2.0);
INSERT INTO bar (a,b) VALUES (3.0, 3.0), (4.0, 4.0);
INSERT INTO baz (x,y) VALUES ("a", "a"), ("b", "b");
`
err := db.Exec(q)
require.NoError(t, err)
}

// --------------------------------------------------------------------------
t.Run("basic union all", func(t *testing.T) {
db, err := genji.Open(":memory:")
require.NoError(t, err)
defer db.Close()

setup(t, db)

t.Run(`SELECT * FROM foo`, func(t *testing.T) {
q := `
SELECT * FROM foo
UNION ALL
SELECT * FROM bar;
`
res, err := db.Query(q)
require.NoError(t, err)
defer res.Close()
raw := `
{ "a": 1.0, "b": 1.0}
{ "a": 2.0, "b": 2.0}
{ "a": 3.0, "b": 3.0}
{ "a": 4.0, "b": 4.0}
`
testutil.RequireStreamEq(t, raw, res)
})

})

// --------------------------------------------------------------------------
t.Run("basic union all with diff fields", func(t *testing.T) {
db, err := genji.Open(":memory:")
require.NoError(t, err)
defer db.Close()

setup(t, db)

t.Run(`SELECT * FROM foo`, func(t *testing.T) {
q := `
SELECT * FROM foo
UNION ALL
SELECT * FROM baz;
`
res, err := db.Query(q)
require.NoError(t, err)
defer res.Close()
raw := `
{ "a": 1.0, "b": 1.0}
{ "a": 2.0, "b": 2.0}
{ "x": "a", "y": "a"}
{ "x": "b", "y": "b"}
`
testutil.RequireStreamEq(t, raw, res)
})

})

// --------------------------------------------------------------------------
t.Run("union all with conditions", func(t *testing.T) {
db, err := genji.Open(":memory:")
require.NoError(t, err)
defer db.Close()

setup(t, db)

t.Run(`SELECT * FROM foo WHERE a > 1`, func(t *testing.T) {
q := `
SELECT * FROM foo WHERE a > 1
UNION ALL
SELECT * FROM baz WHERE x != "b";
`
res, err := db.Query(q)
require.NoError(t, err)
defer res.Close()
raw := `
{ "a": 2.0, "b": 2.0}
{ "x": "a", "y": "a"}
`
testutil.RequireStreamEq(t, raw, res)
})

})

// --------------------------------------------------------------------------
t.Run("self union all", func(t *testing.T) {
db, err := genji.Open(":memory:")
require.NoError(t, err)
defer db.Close()

setup(t, db)

t.Run(`SELECT * FROM foo WHERE a > 1`, func(t *testing.T) {
q := `
SELECT * FROM foo WHERE a > 1
UNION ALL
SELECT * FROM foo WHERE a <= 1;
`
res, err := db.Query(q)
require.NoError(t, err)
defer res.Close()
raw := `
{ "a": 2.0, "b": 2.0}
{ "a": 1.0, "b": 1.0}
`
testutil.RequireStreamEq(t, raw, res)
})

})

// --------------------------------------------------------------------------
t.Run("multiple unions all", func(t *testing.T) {
db, err := genji.Open(":memory:")
require.NoError(t, err)
defer db.Close()

setup(t, db)

t.Run(`SELECT * FROM foo`, func(t *testing.T) {
q := `
SELECT * FROM foo
UNION ALL
SELECT * FROM bar
UNION ALL SELECT * FROM baz;
`
res, err := db.Query(q)
require.NoError(t, err)
defer res.Close()
raw := `
{ "a": 1.0, "b": 1.0}
{ "a": 2.0, "b": 2.0}
{ "a": 3.0, "b": 3.0}
{ "a": 4.0, "b": 4.0}
{ "x": "a", "y": "a"}
{ "x": "b", "y": "b"}
`
testutil.RequireStreamEq(t, raw, res)
})

})

// --------------------------------------------------------------------------
t.Run("basic union", func(t *testing.T) {
db, err := genji.Open(":memory:")
require.NoError(t, err)
defer db.Close()

setup(t, db)

t.Run(`SELECT * FROM foo`, func(t *testing.T) {
q := `
SELECT * FROM foo
UNION
SELECT * FROM bar;
`
err := db.Exec(q)
require.Errorf(t, err, "expected\n%s\nto raise an error but got none", q)
})

})

}
Loading