Skip to content

Commit b4981db

Browse files
committed
added and updated tests
1 parent d3b442a commit b4981db

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

enginetest/enginetests.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestSpatialQueriesPrepared(t *testing.T, harness Harness) {
148148

149149
// TestJoinQueries tests join queries against a provided harness.
150150
func TestJoinQueries(t *testing.T, harness Harness) {
151-
harness.Setup(setup.MydbData, setup.MytableData, setup.Pk_tablesData, setup.OthertableData, setup.NiltableData, setup.XyData, setup.FooData)
151+
harness.Setup(setup.MydbData, setup.MytableData, setup.Pk_tablesData, setup.OthertableData, setup.NiltableData, setup.XyData, setup.FooData, setup.Comp_index_tablesData)
152152
e, err := harness.NewEngine(t)
153153
require.NoError(t, err)
154154

@@ -263,7 +263,7 @@ func TestQueriesPrepared(t *testing.T, harness Harness) {
263263

264264
// TestJoinQueriesPrepared tests join queries as prepared statements against a provided harness.
265265
func TestJoinQueriesPrepared(t *testing.T, harness Harness) {
266-
harness.Setup(setup.MydbData, setup.MytableData, setup.Pk_tablesData, setup.OthertableData, setup.NiltableData, setup.XyData, setup.FooData)
266+
harness.Setup(setup.MydbData, setup.MytableData, setup.Pk_tablesData, setup.OthertableData, setup.NiltableData, setup.XyData, setup.FooData, setup.Comp_index_tablesData)
267267
for _, tt := range queries.JoinQueryTests {
268268
if tt.SkipPrepared {
269269
continue

enginetest/join_planning_tests.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ var JoinPlanningTests = []joinPlanScript{
425425
exp: []sql.Row{{0, 2}, {2, 1}, {3, 3}},
426426
},
427427
{
428-
// anti join will be cross-join-right, be passed non-nil parent row
428+
// anti join will be cross-join-right, then converted to inner join when filters are pushed down, be passed non-nil parent row
429429
q: "select x,a from ab, (select * from xy where x != (select r from rs where r = 1) order by 1) sq where x = 2 and b = 2 order by 1,2;",
430-
types: []plan.JoinType{plan.JoinTypeCrossHash, plan.JoinTypeLeftOuter},
430+
types: []plan.JoinType{plan.JoinTypeInner, plan.JoinTypeLeftOuter},
431431
exp: []sql.Row{{2, 0}, {2, 1}, {2, 2}},
432432
},
433433
{

enginetest/queries/join_queries.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,15 @@ on w = 0;`,
789789
Query: "select * from foo.othertable join mydb.othertable on foo.othertable.text = 'a'",
790790
Expected: []sql.Row{{"a", 4, "third", 1}, {"a", 4, "second", 2}, {"a", 4, "first", 3}},
791791
},
792+
{
793+
794+
Query: `select * from comp_index_t0 c join comp_index_t0 b join comp_index_t0 a on a.v2 = b.pk and b.v2 = c.pk and c.v2 = 1`,
795+
Expected: []sql.Row{},
796+
},
797+
{
798+
Query: "select * from comp_index_t0 a join comp_index_t0 b join comp_index_t0 c on a.v2 = b.pk and b.v2 = c.pk and c.v2 = 5",
799+
Expected: []sql.Row{},
800+
},
792801
}
793802

794803
var JoinScriptTests = []ScriptTest{

enginetest/spatial_index_tests.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,14 @@ func evalSpatialIndexPlanTest(t *testing.T, harness Harness, e QueryEngine, quer
391391
if n == nil {
392392
return false
393393
}
394-
if _, ok := n.(*plan.Filter); ok {
394+
switch n := n.(type) {
395+
case *plan.Filter:
395396
hasFilter = true
396-
}
397-
if _, ok := n.(*plan.IndexedTableAccess); ok {
397+
case *plan.JoinNode:
398+
if n.Filter != nil {
399+
hasFilter = true
400+
}
401+
case *plan.IndexedTableAccess:
398402
hasRightOrder = hasFilter
399403
hasIndex = true
400404
}

sql/analyzer/pushdown.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func pushFilters(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, s
8787

8888
// move filter predicates directly above their respective tables in joins
8989
ret, same, err := pushdownAboveTables(n, filters)
90-
if same || err != nil {
90+
if err != nil {
9191
return n, transform.SameTree, err
9292
}
9393

@@ -314,19 +314,32 @@ func pushdownFiltersUnderSubqueryAlias(ctx *sql.Context, a *Analyzer, sa *plan.S
314314
// removePushedDownPredicates removes all handled filter predicates from the filter given and returns. If all
315315
// predicates have been handled, it replaces the filter with its child.
316316
func removePushedDownPredicates(ctx *sql.Context, a *Analyzer, node *plan.Filter, filters *filterSet) sql.Node {
317-
if filters.handledCount() == 0 {
318-
a.Log("no handled filters, leaving filter untouched")
319-
return nil
320-
}
321-
322-
// figure out if the filter's filters were all handled
323317
filterExpressions := expression.SplitConjunction(node.Expression)
324318
unhandled := subtractExprSet(filterExpressions, filters.handledFilters)
319+
325320
if len(unhandled) == 0 {
326321
a.Log("filter node has no unhandled filters, so it will be removed")
327322
return node.Child
328323
}
329324

325+
// push filters into joinChild
326+
if joinChild, ok := node.Child.(*plan.JoinNode); ok && !joinChild.Op.IsOuter() {
327+
a.Log("pushing filters into join node")
328+
if joinChild.Op.IsCross() {
329+
return plan.NewInnerJoin(joinChild.Left(), joinChild.Right(), expression.JoinAnd(unhandled...))
330+
}
331+
if joinChild.Filter != nil {
332+
unhandled = append(unhandled, joinChild.Filter)
333+
}
334+
joinChild.Filter = expression.JoinAnd(unhandled...)
335+
return joinChild
336+
}
337+
338+
if filters.handledCount() == 0 {
339+
a.Log("no handled filters, leaving filter untouched")
340+
return nil
341+
}
342+
330343
if len(unhandled) == len(filterExpressions) {
331344
a.Log("no filters removed from filter node")
332345
return nil
@@ -339,14 +352,5 @@ func removePushedDownPredicates(ctx *sql.Context, a *Analyzer, node *plan.Filter
339352
unhandled,
340353
)
341354

342-
if joinChild, ok := node.Child.(*plan.JoinNode); ok && !joinChild.Op.IsOuter() {
343-
if joinChild.Op.IsCross() {
344-
return plan.NewInnerJoin(joinChild.Left(), joinChild.Right(), expression.JoinAnd(unhandled...))
345-
}
346-
unhandled = append(unhandled, joinChild.Filter)
347-
joinChild.Filter = expression.JoinAnd(unhandled...)
348-
return joinChild
349-
}
350-
351355
return plan.NewFilter(expression.JoinAnd(unhandled...), node.Child)
352356
}

0 commit comments

Comments
 (0)