Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fef2a1b
New implementation of addMergeJoins.
nicktobey Aug 9, 2023
30c15a3
Allow comparison expresions that aren't column literals, since we alr…
nicktobey Aug 9, 2023
37574b1
Refactor `combineIntoTuple`
nicktobey Aug 9, 2023
1a4f4ae
Add tests for new merge join planner.
nicktobey Aug 9, 2023
6458e64
Give helper functions more clear names and add docstrings.
nicktobey Aug 9, 2023
61f55a9
Allow the same index column to match multiple filters.
nicktobey Aug 21, 2023
194376f
Implmement new coster for MergeJoin.
nicktobey Aug 10, 2023
12fef8d
Merge branch 'main' into nicktobey/mergejoin
nicktobey Aug 25, 2023
f83c611
Allow index scan in join to put a range on the target column for case…
nicktobey Aug 30, 2023
8d5204b
Make the IntSequence test function consistently use int64 for its gen…
nicktobey Aug 30, 2023
65657ad
Update join planning tests for new merge coster.
nicktobey Aug 30, 2023
845a880
[ga-format-pr] Run ./format_repo.sh to fix formatting
nicktobey Aug 31, 2023
02bed5f
Remove inaccurate comment from coster: every key expression in a merg…
nicktobey Aug 31, 2023
907f5a2
Add regression test to query_plans.go
nicktobey Aug 31, 2023
8838561
Add additional planning tests.
nicktobey Aug 31, 2023
0f4c82f
Merge branch 'nicktobey/mergejoin' of github.com:dolthub/go-mysql-ser…
nicktobey Aug 31, 2023
3dd2523
Rename `isUniquqLookup` and `isLinearMerge` to `isInjectiveLookup` an…
nicktobey Sep 1, 2023
f802586
Move comments describing main loop of addMergeJoins to be right befor…
nicktobey Sep 1, 2023
f3a8935
Update typo in query_plans.go
nicktobey Sep 1, 2023
5dd2366
Remove getColumnRefFromScalar in favor of checking the columns in Sca…
nicktobey Sep 1, 2023
7ffa17a
When checking to see if the secondary index matches a set of filters …
nicktobey Sep 2, 2023
71dcf54
Add additional planning tests for the new merge join.
nicktobey Sep 2, 2023
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
12 changes: 12 additions & 0 deletions enginetest/join_op_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,18 @@ SELECT SUM(x) FROM xy WHERE x IN (
},
},
},
{
name: "multi-column merge join",
setup: [][]string{
setup.Pk_tablesData[0],
},
tests: []JoinOpTests{
{
Query: `SELECT l.pk1, l.pk2, l.c1, r.pk1, r.pk2, r.c1 FROM two_pk l JOIN two_pk r ON l.pk1=r.pk1 AND l.pk2=r.pk2`,
Expected: []sql.Row{sql.Row{0, 0, 0, 0, 0, 0}, sql.Row{0, 1, 10, 0, 1, 10}, sql.Row{1, 0, 20, 1, 0, 20}, sql.Row{1, 1, 30, 1, 1, 30}},
},
},
},
}

var rangeJoinOpTests = []JoinOpTests{
Expand Down
95 changes: 89 additions & 6 deletions enginetest/join_planning_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package enginetest

import (
"fmt"
"github.com/dolthub/go-mysql-server/sql/expression"
"strings"
"testing"

Expand All @@ -29,12 +30,13 @@ import (
)

type JoinPlanTest struct {
q string
types []plan.JoinType
indexes []string
exp []sql.Row
order []string
skipOld bool
q string
types []plan.JoinType
indexes []string
mergeCompares []string
exp []sql.Row
order []string
skipOld bool
}

var JoinPlanningTests = []struct {
Expand Down Expand Up @@ -182,6 +184,19 @@ var JoinPlanningTests = []struct {
},
},
},
{
name: "multi-column merge join",
setup: setup.Pk_tablesData[0],

tests: []JoinPlanTest{
{
q: `SELECT /*+ MERGE_JOIN(l,r) */ l.pk1, l.pk2, l.c1, r.pk1, r.pk2, r.c1 FROM two_pk l JOIN two_pk r ON l.pk1=r.pk1 AND l.pk2=r.pk2`,
types: []plan.JoinType{plan.JoinTypeMerge},
mergeCompares: []string{"((r.pk1, r.pk2) = (l.pk1, l.pk2))"},
exp: []sql.Row{sql.Row{0, 0, 0, 0, 0, 0}, sql.Row{0, 1, 10, 0, 1, 10}, sql.Row{1, 0, 20, 1, 0, 20}, sql.Row{1, 1, 30, 1, 1, 30}},
},
},
},
{
name: "merge join keyless index",
setup: []string{
Expand Down Expand Up @@ -1411,6 +1426,9 @@ func TestJoinPlanning(t *testing.T, harness Harness) {
if tt.indexes != nil {
evalIndexTest(t, harness, e, tt)
}
if tt.mergeCompares != nil {
evalMergeCmpTest(t, harness, e, tt)
}
if tt.exp != nil {
evalJoinCorrectness(t, harness, e, tt.q, tt.q, tt.exp, tt.skipOld)
}
Expand Down Expand Up @@ -1447,6 +1465,37 @@ func evalJoinTypeTest(t *testing.T, harness Harness, e *sqle.Engine, tt JoinPlan
})
}

func evalMergeCmpTest(t *testing.T, harness Harness, e *sqle.Engine, tt JoinPlanTest) {
hasMergeJoin := false
for _, joinType := range tt.types {
if joinType.IsMerge() {
hasMergeJoin = true
}
}
if !hasMergeJoin {
return
}
t.Run(tt.q+"merge join compare", func(t *testing.T) {
if tt.skipOld {
t.Skip()
}

ctx := NewContext(harness)
ctx = ctx.WithQuery(tt.q)

a, err := e.AnalyzeQuery(ctx, tt.q)
require.NoError(t, err)

// consider making this a string too
compares := collectMergeCompares(a)
var cmp []string
for _, i := range compares {
cmp = append(cmp, i.String())
}
require.Equal(t, tt.mergeCompares, cmp, fmt.Sprintf("unexpected plan:\n%s", sql.DebugString(a)))
})
}

func evalIndexTest(t *testing.T, harness Harness, e *sqle.Engine, tt JoinPlanTest) {
t.Run(tt.q+" join indexes", func(t *testing.T) {
if tt.skipOld {
Expand Down Expand Up @@ -1524,6 +1573,40 @@ func collectJoinTypes(n sql.Node) []plan.JoinType {
return types
}

func collectMergeCompares(n sql.Node) []sql.Expression {
var compares []sql.Expression
transform.Inspect(n, func(n sql.Node) bool {
if n == nil {
return true
}

if ex, ok := n.(sql.Expressioner); ok {
for _, e := range ex.Expressions() {
transform.InspectExpr(e, func(e sql.Expression) bool {
sq, ok := e.(*plan.Subquery)
if !ok {
return false
}
compares = append(compares, collectMergeCompares(sq.Query)...)
return false
})
}
}

join, ok := n.(*plan.JoinNode)
if !ok {
return true
}
if !join.Op.IsMerge() {
return true
}

compares = append(compares, expression.SplitConjunction(join.JoinCond())[0])
return true
})
return compares
}

func collectIndexes(n sql.Node) []sql.Index {
var indexes []sql.Index
transform.Inspect(n, func(n sql.Node) bool {
Expand Down
Loading