Skip to content

Commit 0147ad3

Browse files
committed
Preserve lateralness when replanning join.
1 parent 8a5f582 commit 0147ad3

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

sql/analyzer/unnest_exists_subqueries.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ func unnestExistSubqueries(ctx *sql.Context, scope *plan.Scope, a *Analyzer, fil
193193
ret = plan.NewAntiJoinIncludingNulls(ret, s.inner, cond).WithComment(comment)
194194
qFlags.Set(sql.QFlagInnerJoin)
195195
case plan.JoinTypeSemi:
196-
ret = plan.NewCrossJoin(ret, s.inner).WithComment(comment)
196+
if sq.Correlated().Empty() {
197+
ret = plan.NewCrossJoin(ret, s.inner).WithComment(comment)
198+
} else {
199+
ret = plan.NewLateralCrossJoin(ret, s.inner).WithComment(comment)
200+
}
197201
qFlags.Set(sql.QFlagCrossJoin)
198202
default:
199203
return filter, transform.SameTree, fmt.Errorf("hoistSelectExists failed on unexpected join type")

sql/memo/exec_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func (b *ExecBuilder) buildMergeJoin(j *MergeJoin, children ...sql.Node) (sql.No
299299

300300
func (b *ExecBuilder) buildLateralJoin(j *LateralJoin, children ...sql.Node) (sql.Node, error) {
301301
if len(j.Filter) == 0 {
302-
return plan.NewCrossJoin(children[0], children[1]), nil
302+
return plan.NewLateralCrossJoin(children[0], children[1]), nil
303303
}
304304
filters := b.buildFilterConjunction(j.Filter...)
305305
return plan.NewJoin(children[0], children[1], j.Op.AsLateral(), filters), nil

sql/plan/join.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ func NewCrossJoin(left, right sql.Node) *JoinNode {
532532
return NewJoin(left, right, JoinTypeCross, nil)
533533
}
534534

535+
func NewLateralCrossJoin(left, right sql.Node) *JoinNode {
536+
return NewJoin(left, right, JoinTypeLateralCross, nil)
537+
}
538+
535539
// NaturalJoin is a join that automatically joins by all the columns with the
536540
// same name.
537541
// NaturalJoin is a placeholder node, it should be transformed into an INNER

0 commit comments

Comments
 (0)