Skip to content

Commit

Permalink
Merge pull request #42791 from RaduBerinde/backport19.2-42760
Browse files Browse the repository at this point in the history
release-19.2: opt: fix incorrect lax key calculation
  • Loading branch information
RaduBerinde authored Nov 26, 2019
2 parents b509b19 + e457987 commit 5dc63ff
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 0 additions & 1 deletion pkg/sql/opt/memo/testdata/logprops/index-join
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ SELECT y FROM a WHERE s = 'foo' AND x + y = 10
----
project
├── columns: y:2(int)
├── lax-key: (2)
├── prune: (2)
└── select
├── columns: x:1(int!null) y:2(int) s:3(string!null)
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/opt/memo/testdata/logprops/scan
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,32 @@ select
└── lt [type=bool]
├── variable: d [type=date]
└── const: '2017-01-05' [type=date]


# Regression test for #42731: we were incorrectly setting cardinality [0 - 1].
exec-ddl
CREATE TABLE t42731 (id INT PRIMARY KEY, unique_value INT UNIQUE, notnull_value INT NOT NULL)
----

norm
SELECT * FROM t42731 WHERE unique_value IS NULL AND notnull_value = 2000
----
select
├── columns: id:1(int!null) unique_value:2(int) notnull_value:3(int!null)
├── key: (1)
├── fd: ()-->(2,3), (2)~~>(1)
├── prune: (1)
├── interesting orderings: (+1) (+2,+1)
├── scan t42731
│ ├── columns: id:1(int!null) unique_value:2(int) notnull_value:3(int!null)
│ ├── key: (1)
│ ├── fd: (1)-->(2,3), (2)~~>(1,3)
│ ├── prune: (1-3)
│ └── interesting orderings: (+1) (+2,+1)
└── filters
├── is [type=bool, outer=(2), constraints=(/2: [/NULL - /NULL]; tight), fd=()-->(2)]
│ ├── variable: unique_value [type=int]
│ └── null [type=unknown]
└── eq [type=bool, outer=(3), constraints=(/3: [/2000 - /2000]; tight), fd=()-->(3)]
├── variable: notnull_value [type=int]
└── const: 2000 [type=int]
4 changes: 2 additions & 2 deletions pkg/sql/opt/props/func_dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,8 @@ func (f *FuncDepSet) inClosureOf(cols, in opt.ColSet, strict bool) bool {
if fd.from.SubsetOf(in) && !fd.to.SubsetOf(in) {
laxIn.UnionWith(fd.to)

// Equivalencies and constants are always transitive.
if fd.equiv || fd.from.Empty() {
// Equivalencies are always transitive.
if fd.equiv {
in.UnionWith(fd.to)

// Restart iteration to get transitive closure.
Expand Down
8 changes: 7 additions & 1 deletion pkg/sql/opt/props/func_dep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ func TestFuncDeps_ColsAreKey(t *testing.T) {
{cols: c(10), strict: false, lax: false},
{cols: c(11), strict: false, lax: false},
{cols: c(), strict: false, lax: false},
{cols: c(2, 11), strict: false, lax: true},

// This case is interesting: if we take into account that 3 is a constant,
// we could put 2 and 3 together and use (2,3)~~>(1,4,5) and (1)==(10) to
// prove that (2,3) is a lax key. But this is only true when that constant
// value for 3 is not NULL. We would have to pass non-null information to
// the check. See #42731.
{cols: c(2, 11), strict: false, lax: false},
}

for _, tc := range testcases {
Expand Down

0 comments on commit 5dc63ff

Please sign in to comment.