Skip to content

Commit a17d15a

Browse files
author
James Cor
committed
fix equality check in buildSingleLookupPlan
1 parent c8d2027 commit a17d15a

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
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) {

enginetest/memory_engine_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,30 @@ func TestSingleQueryPrepared(t *testing.T) {
200200

201201
// Convenience test for debugging a single query. Unskip and set to the desired query.
202202
func TestSingleScript(t *testing.T) {
203-
t.Skip()
203+
//t.Skip()
204204
var scripts = []queries.ScriptTest{
205205
{
206-
Name: "AS OF propagates to nested CALLs",
207-
SetUpScript: []string{},
206+
Name: "test",
207+
SetUpScript: []string{
208+
"create table t1 (i int primary key);",
209+
"create table t2 (j int);",
210+
"create table t3 (k int);",
211+
"insert into t1 values (1), (2);",
212+
"insert into t2 values (1), (2);",
213+
"insert into t3 values (3);",
214+
},
208215
Assertions: []queries.ScriptTestAssertion{
209216
{
210-
Query: "create procedure create_proc() create table t (i int primary key, j int);",
211-
Expected: []sql.Row{
212-
{types.NewOkResult(0)},
213-
},
217+
Query: "select * from t1 cross join t2 join (select * from t3) v3 on v3.k = t2.j;",
218+
Expected: []sql.Row{},
214219
},
215220
{
216-
Query: "call create_proc()",
221+
Query: "select * from t1 cross join t2 join (select * from t3) v3 on v3.k >= t2.j order by i, j, k;",
217222
Expected: []sql.Row{
218-
{types.NewOkResult(0)},
223+
{1, 1, 3},
224+
{1, 2, 3},
225+
{2, 1, 3},
226+
{2, 2, 3},
219227
},
220228
},
221229
},
@@ -230,8 +238,8 @@ func TestSingleScript(t *testing.T) {
230238
panic(err)
231239
}
232240

233-
//engine.EngineAnalyzer().Debug = true
234-
//engine.EngineAnalyzer().Verbose = true
241+
engine.EngineAnalyzer().Debug = true
242+
engine.EngineAnalyzer().Verbose = true
235243

236244
enginetest.TestScriptWithEngine(t, engine, harness, test)
237245
}

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,18 @@ 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+
if _, isEq := e.(*expression.Equals); !isEq {
545+
hasOnlyEquals = false
546+
return true
547+
}
548+
return false
549+
})
550+
return hasOnlyEquals
551+
}
552+
541553
// allTableCols returns the full schema of a table ignoring
542554
// declared projections.
543555
func allTableCols(rel SourceRel) sql.Schema {

0 commit comments

Comments
 (0)