Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Mar 7, 2021
1 parent e8791ec commit 9339181
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
8 changes: 6 additions & 2 deletions planner/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func getCandidateFromfilterNode(f *stream.FilterOperator, tableName string, info
// we'll start with checking if the path is the primary key of the table
if pk := info.GetPrimaryKey(); pk != nil && pk.Path.IsEqual(path) {
cd.isPk = true
cd.priority = 2
cd.priority = 3

ranges, err := getRangesFromOp(op, e)
if err != nil {
Expand All @@ -514,7 +514,11 @@ func getCandidateFromfilterNode(f *stream.FilterOperator, tableName string, info
// if not, check if an index exists for that path
if idx := indexes.GetIndexByPath(document.Path(path)); idx != nil {
cd.isIndex = true
cd.priority = 1
if idx.Info.Unique {
cd.priority = 2
} else {
cd.priority = 1
}

ranges, err := getRangesFromOp(op, e)
if err != nil {
Expand Down
35 changes: 32 additions & 3 deletions planner/optimizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,38 @@ func TestUseIndexBasedOnSelectionNodeRule(t *testing.T) {
stream.New(stream.SeqScan("foo")).Pipe(stream.Filter(parser.MustParseExpr("1 IN a"))),
},
{
"FROM foo WHERE a > 10",
stream.New(stream.SeqScan("foo")).Pipe(stream.Filter(parser.MustParseExpr("a > 10"))),
stream.New(stream.IndexScan("idx_foo_a", st.Range{Min: parser.MustParseExpr("10"), Exclusive: true})),
"FROM foo WHERE a >= 10",
stream.New(stream.SeqScan("foo")).Pipe(stream.Filter(parser.MustParseExpr("a >= 10"))),
stream.New(stream.IndexScan("idx_foo_a", st.Range{Min: parser.MustParseExpr("10")})),
},
{
"FROM foo WHERE k = 1",
stream.New(stream.SeqScan("foo")).Pipe(stream.Filter(parser.MustParseExpr("k = 1"))),
stream.New(stream.PkScan("foo", st.Range{Min: parser.MustParseExpr("1"), Exact: true})),
},
{
"FROM foo WHERE k = 1 AND b = 2",
stream.New(stream.SeqScan("foo")).
Pipe(stream.Filter(parser.MustParseExpr("k = 1"))).
Pipe(stream.Filter(parser.MustParseExpr("b = 2"))),
stream.New(stream.PkScan("foo", st.Range{Min: parser.MustParseExpr("1"), Exact: true})).
Pipe(stream.Filter(parser.MustParseExpr("b = 2"))),
},
{
"FROM foo WHERE a = 1 AND k = 2",
stream.New(stream.SeqScan("foo")).
Pipe(stream.Filter(parser.MustParseExpr("a = 1"))).
Pipe(stream.Filter(parser.MustParseExpr("2 = k"))),
stream.New(stream.PkScan("foo", st.Range{Min: parser.MustParseExpr("2"), Exact: true})).
Pipe(stream.Filter(parser.MustParseExpr("a = 1"))),
},
{
"FROM foo WHERE a = 1 AND k < 2",
stream.New(stream.SeqScan("foo")).
Pipe(stream.Filter(parser.MustParseExpr("a = 1"))).
Pipe(stream.Filter(parser.MustParseExpr("k < 2"))),
stream.New(stream.IndexScan("idx_foo_a", st.Range{Min: parser.MustParseExpr("1"), Exact: true})).
Pipe(stream.Filter(parser.MustParseExpr("k < 2"))),
},
}

Expand Down

0 comments on commit 9339181

Please sign in to comment.