Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-22.2.0: opt: fix WithScan errors in apply-joins #89856

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/apply_join
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,23 @@ LEFT JOIN LATERAL (
) ON true;
----
NULL

subtest regression_89601

statement ok
CREATE TABLE t89601 (i INT4);
INSERT INTO t89601 VALUES (0)

# Regression test for #89601. All with bindings should be added to the new
# metadata when planning the RHS of an apply-join.
statement ok
SELECT NULL
FROM t89601 t1, t89601 t2
WHERE EXISTS(
SELECT NULL
FROM t89601 t3, t89601 t4
WHERE t3.i IN (
WITH w AS (SELECT NULL)
SELECT t4.i::INT8 FROM w
)
)
7 changes: 3 additions & 4 deletions pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,10 +1079,9 @@ func (b *Builder) buildApplyJoin(join memo.RelExpr) (execPlan, error) {
// because the call to Factory.CopyAndReplace below clears With
// expressions in the metadata.
if !addedWithBindings {
for i, n := opt.WithID(1), b.mem.MaxWithID(); i <= n; i++ {
memoExpr := b.mem.Metadata().WithBinding(i)
f.Metadata().AddWithBinding(i, memoExpr)
}
b.mem.Metadata().ForEachWithBinding(func(id opt.WithID, expr opt.Expr) {
f.Metadata().AddWithBinding(id, expr)
})
addedWithBindings = true
}
// Fall through.
Expand Down
6 changes: 0 additions & 6 deletions pkg/sql/opt/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,6 @@ func (m *Memo) NextWithID() opt.WithID {
return m.curWithID
}

// MaxWithID returns the current maximum assigned identifier for a WITH
// expression.
func (m *Memo) MaxWithID() opt.WithID {
return m.curWithID
}

// Detach is used when we detach a memo that is to be reused later (either for
// execbuilding or with AssignPlaceholders). New expressions should no longer be
// constructed in this memo.
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/opt/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,11 @@ func (md *Metadata) WithBinding(id WithID) Expr {
}
return res
}

// ForEachWithBinding calls fn with each bound (WithID, Expr) pair in the
// metadata.
func (md *Metadata) ForEachWithBinding(fn func(WithID, Expr)) {
for id, expr := range md.withBindings {
fn(id, expr)
}
}