Skip to content

Commit dbb6a69

Browse files
authored
Merge pull request #3206 from dolthub/james/join
fix equality check in `buildSingleLookupPlan()`
2 parents c8d2027 + 23a6eb7 commit dbb6a69

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

enginetest/join_planning_tests.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,34 @@ join uv d on d.u = c.x`,
17881788
},
17891789
},
17901790
},
1791+
{
1792+
name: "single look up plan does not drop complex equality filters",
1793+
setup: []string{
1794+
"create table t1 (i int primary key);",
1795+
"create table t2 (j int);",
1796+
"create table t3 (k int);",
1797+
"insert into t1 values (1), (2);",
1798+
"insert into t2 values (1), (2);",
1799+
"insert into t3 values (3);",
1800+
},
1801+
tests: []JoinPlanTest{
1802+
{
1803+
q: "select * from t1 cross join t2 join (select * from t3) v3 on v3.k = t2.j;",
1804+
types: []plan.JoinType{plan.JoinTypeHash, plan.JoinTypeCross},
1805+
exp: []sql.Row{},
1806+
},
1807+
{
1808+
q: "select * from t1 cross join t2 join (select * from t3) v3 on v3.k >= t2.j order by i, j, k;",
1809+
types: []plan.JoinType{plan.JoinTypeInner, plan.JoinTypeCross},
1810+
exp: []sql.Row{
1811+
{1, 1, 3},
1812+
{1, 2, 3},
1813+
{2, 1, 3},
1814+
{2, 2, 3},
1815+
},
1816+
},
1817+
},
1818+
},
17911819
}
17921820

17931821
func TestJoinPlanning(t *testing.T, harness Harness) {

sql/memo/join_order_builder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (j *joinOrderBuilder) buildSingleLookupPlan() bool {
285285
}
286286
filter := edge.filters[0]
287287
_, tables, _ := getExprScalarProps(filter)
288-
if tables.Len() != 2 {
288+
if tables.Len() != 2 || !isSimpleEquality(filter) {
289289
// We have encountered a filter condition more complicated than a simple equality check.
290290
// We probably can't optimize this, so bail out.
291291
return false
@@ -319,7 +319,6 @@ func (j *joinOrderBuilder) buildSingleLookupPlan() bool {
319319
for idx, ok := remainingVertexes.next(0); ok; idx, ok = remainingVertexes.next(idx + 1) {
320320
nextVertex := newBitSet(idx)
321321
j.addJoin(plan.JoinTypeCross, currentlyJoinedVertexes, nextVertex, nil, nil, false)
322-
323322
currentlyJoinedVertexes = currentlyJoinedVertexes.union(nextVertex)
324323
}
325324
return false

sql/memo/rel_props.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,21 @@ func getExprScalarProps(e sql.Expression) (sql.ColSet, sql.FastIntSet, bool) {
538538
return cols, tables, nullRej
539539
}
540540

541+
func isSimpleEquality(expr sql.Expression) bool {
542+
hasOnlyEquals := true
543+
transform.InspectExpr(expr, func(e sql.Expression) bool {
544+
switch e.(type) {
545+
case *expression.GetField:
546+
case *expression.Equals, *expression.NullSafeEquals:
547+
default:
548+
hasOnlyEquals = false
549+
return true
550+
}
551+
return false
552+
})
553+
return hasOnlyEquals
554+
}
555+
541556
// allTableCols returns the full schema of a table ignoring
542557
// declared projections.
543558
func allTableCols(rel SourceRel) sql.Schema {

0 commit comments

Comments
 (0)