Skip to content

Commit

Permalink
opt: create library that determines how joins affect input rows
Browse files Browse the repository at this point in the history
Previously, there was no simple way to determine whether all rows from
a join input will be included in its output, nor whether input rows will
be duplicated by the join.

This patch adds a library that constructs a Multiplicity struct for join
operators. The Multiplicity can be queried for information about how a
join will affect its input rows (e.g. duplicated, filtered and/or
null-extended). The existing SimplifyLeftJoinWithFilters rule has been
refactored to use this library. The Multiplicity library will also be
useful for future join elimination and limit pushdown rules.

Release note: None
  • Loading branch information
DrewKimball committed May 28, 2020
1 parent 163affa commit e039beb
Show file tree
Hide file tree
Showing 24 changed files with 1,475 additions and 294 deletions.
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/fk
Original file line number Diff line number Diff line change
Expand Up @@ -3198,3 +3198,24 @@ ALTER TABLE t2 DROP CONSTRAINT fk1; ALTER TABLE t2 DROP CONSTRAINT fk2; TRUNCATE

statement ok
DROP TABLE t2 CASCADE; DROP TABLE t1 CASCADE

# Regression test for #49628.
statement ok
CREATE TABLE xyz (x INT, y INT, z INT, PRIMARY KEY (x, y, z));
CREATE TABLE fk_ref
(
a INT NOT NULL,
b INT,
c INT NOT NULL,
FOREIGN KEY (a, b, c) REFERENCES xyz (x, y, z)
);
INSERT INTO fk_ref (VALUES (1, NULL, 1));

query IIIIII
SELECT * FROM fk_ref LEFT JOIN xyz ON a = x
----
1 NULL 1 NULL NULL NULL

statement ok
DROP TABLE fk_ref;
DROP TABLE xyz;
19 changes: 19 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/join
Original file line number Diff line number Diff line change
Expand Up @@ -1093,3 +1093,22 @@ CREATE TABLE t44746_1(c1 INT)
# Note: an "error parsing regexp" would also be acceptable here.
statement ok
SELECT * FROM t44746_0 FULL JOIN t44746_1 ON (SUBSTRING('', ')') = '') = (c1 > 0)

# Regression test for #49630.
statement ok
DROP TABLE empty;
CREATE TABLE xy (x INT PRIMARY KEY, y INT);
CREATE TABLE fk_ref (r INT NOT NULL REFERENCES xy (x));
CREATE TABLE empty (v INT);
INSERT INTO xy (VALUES (1, 1));
INSERT INTO fk_ref (VALUES (1));

query IIII
SELECT * FROM fk_ref LEFT JOIN (SELECT * FROM xy INNER JOIN empty ON True) ON r = x
----
1 NULL NULL NULL

statement ok
DROP TABLE empty;
DROP TABLE fk_ref;
DROP TABLE xy;
1 change: 0 additions & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/enums
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ scan t
└── constraint: /1
├── [/'hello' - /'hello']
└── [/'hi' - /'hi']

6 changes: 6 additions & 0 deletions pkg/sql/opt/memo/expr_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,12 @@ func (f *ExprFmtCtx) formatRelational(e RelExpr, tp treeprinter.Node) {
if r.JoinSize > 1 {
tp.Childf("join-size: %d", r.JoinSize)
}
switch e.Op() {
case opt.InnerJoinOp, opt.LeftJoinOp, opt.FullJoinOp:
if s := r.MultiplicityProps.String(); (r.Available&props.MultiplicityProps) != 0 && s != "" {
tp.Childf("multiplicity: %s", s)
}
}
if withUses := relational.Shared.Rule.WithUses; len(withUses) > 0 {
n := tp.Childf("cte-uses")
ids := make([]opt.WithID, 0, len(withUses))
Expand Down
Loading

0 comments on commit e039beb

Please sign in to comment.