Skip to content

Commit

Permalink
Merge #60591
Browse files Browse the repository at this point in the history
60591: opt: create key for UNIQUE WITHOUT INDEX constraints r=rytaft a=rytaft

**opt: don't add unique constraints with an index to test catalog**

This commit updates the test catalog so that it is consistent with the
real catalog and doesn't add unique constraints with an index to the
catalog table. This also prevents some tests from failing unnecessarily.

Release note: None

**opt: create key for UNIQUE WITHOUT INDEX constraints**

This commit teaches the optimizer that columns with a valid `UNIQUE WITHOUT INDEX`
constraint form a key, and the functional dependencies should reflect that. This will be
necessary to support locality optimized search.

Fixes #58944
Informs #55185

Release note (performance improvement): The optimizer now knows that
the unique columns in an implicitly partitioned unique index form a
key. This can be used to enable certain optimizations and may result
in better plans.

Co-authored-by: Rebecca Taft <becca@cockroachlabs.com>
  • Loading branch information
craig[bot] and rytaft committed Feb 17, 2021
2 parents bededc2 + 99c3af7 commit 06486c9
Show file tree
Hide file tree
Showing 23 changed files with 490 additions and 267 deletions.
24 changes: 12 additions & 12 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ TABLE ok1
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
└── partition by list prefixes
Expand Down Expand Up @@ -469,7 +469,7 @@ TABLE ok2
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
└── partition by list prefixes
Expand Down Expand Up @@ -513,7 +513,7 @@ TABLE ok3
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
└── partition by list prefixes
Expand Down Expand Up @@ -560,7 +560,7 @@ TABLE ok4
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
└── partition by list prefixes
Expand Down Expand Up @@ -600,7 +600,7 @@ TABLE ok5
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
└── partition by list prefixes
Expand Down Expand Up @@ -665,7 +665,7 @@ TABLE ok6
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
└── b int not null
scan ok6
Expand Down Expand Up @@ -702,7 +702,7 @@ TABLE ok7
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
└── b int not null
scan ok7
Expand Down Expand Up @@ -745,7 +745,7 @@ TABLE ok8
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
└── b int not null
scan ok8
Expand Down Expand Up @@ -790,7 +790,7 @@ TABLE ok9
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
└── b int not null
scan ok9
Expand Down Expand Up @@ -837,7 +837,7 @@ TABLE ok10
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
└── b int not null
scan ok10
Expand Down Expand Up @@ -893,7 +893,7 @@ TABLE ok11
├── c int not null
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
├── c int not null
Expand Down Expand Up @@ -940,7 +940,7 @@ TABLE ok12
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
└── PRIMARY INDEX primary
├── a int not null
├── b int not null
└── partition by list prefixes
Expand Down
23 changes: 13 additions & 10 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning_implicit
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ vectorized: true
│ │
│ └── • hash join (right anti)
│ │ equality: (pk) = (column2)
│ │ left cols are key
│ │ right cols are key
│ │
│ ├── • scan
Expand Down Expand Up @@ -631,25 +632,26 @@ vectorized: true
│ │
│ └── • render
│ │
│ └── • cross join (right outer)
│ └── • cross join (left outer)
│ │
│ ├── • filter
│ │ │ filter: pk = 3
│ │ │
│ │ └── • scan
│ │ missing stats
│ │ table: t@primary
│ │ spans: FULL SCAN
│ ├── • values
│ │ size: 7 columns, 1 row
│ │
│ └── • values
│ size: 7 columns, 1 row
│ └── • filter
│ │ filter: pk = 3
│ │
│ └── • scan
│ missing stats
│ table: t@primary
│ spans: FULL SCAN
├── • constraint-check
│ │
│ └── • error if rows
│ │
│ └── • hash join (right semi)
│ │ equality: (pk) = (upsert_pk)
│ │ right cols are key
│ │ pred: column3 != partition_by
│ │
│ ├── • scan
Expand All @@ -666,6 +668,7 @@ vectorized: true
└── • hash join (right semi)
│ equality: (b) = (column5)
│ right cols are key
│ pred: (upsert_pk != pk) OR (column3 != partition_by)
├── • scan
Expand Down
64 changes: 33 additions & 31 deletions pkg/ccl/logictestccl/testdata/logic_test/regional_by_row
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,16 @@ vectorized: true
│ │
│ └── • render
│ │
│ └── • cross join (right outer)
│ └── • cross join (left outer)
│ │
│ ├── • scan
│ │ missing stats
│ │ table: regional_by_row_table@primary
│ │ spans: [/'ap-southeast-2'/2 - /'ap-southeast-2'/2] [/'ca-central-1'/2 - /'ca-central-1'/2] [/'us-east-1'/2 - /'us-east-1'/2]
│ ├── • values
│ │ size: 6 columns, 1 row
│ │
│ └── • values
│ size: 6 columns, 1 row
│ └── • scan
│ missing stats
│ table: regional_by_row_table@primary
│ spans: [/'ap-southeast-2'/2 - /'ap-southeast-2'/2] [/'ca-central-1'/2 - /'ca-central-1'/2] [/'us-east-1'/2 - /'us-east-1'/2]
│ locking strength: for update
├── • constraint-check
│ │
Expand All @@ -389,11 +390,11 @@ vectorized: true
│ │
│ └── • cross join
│ │
│ ├── • scan buffer
│ │ label: buffer 1
│ ├── • values
│ │ size: 1 column, 3 rows
│ │
│ └── • values
size: 1 column, 3 rows
│ └── • scan buffer
label: buffer 1
├── • constraint-check
│ │
Expand All @@ -407,11 +408,11 @@ vectorized: true
│ │
│ └── • cross join
│ │
│ ├── • scan buffer
│ │ label: buffer 1
│ ├── • values
│ │ size: 1 column, 3 rows
│ │
│ └── • values
size: 1 column, 3 rows
│ └── • scan buffer
label: buffer 1
└── • constraint-check
Expand All @@ -425,11 +426,11 @@ vectorized: true
└── • cross join
├── • scan buffer
label: buffer 1
├── • values
size: 1 column, 3 rows
└── • values
size: 1 column, 3 rows
└── • scan buffer
label: buffer 1

query T
EXPLAIN UPSERT INTO regional_by_row_table (crdb_region, pk, pk2, a, b)
Expand All @@ -451,6 +452,7 @@ vectorized: true
│ │
│ └── • lookup join (left outer)
│ │ table: regional_by_row_table@primary
│ │ equality cols are key
│ │ lookup condition: (column2 = pk) AND (crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1'))
│ │ locking strength: for update
│ │
Expand All @@ -471,11 +473,11 @@ vectorized: true
│ │
│ └── • cross join
│ │
│ ├── • scan buffer
│ │ label: buffer 1
│ ├── • values
│ │ size: 1 column, 3 rows
│ │
│ └── • values
size: 1 column, 3 rows
│ └── • scan buffer
label: buffer 1
├── • constraint-check
│ │
Expand All @@ -489,11 +491,11 @@ vectorized: true
│ │
│ └── • cross join
│ │
│ ├── • scan buffer
│ │ label: buffer 1
│ ├── • values
│ │ size: 1 column, 3 rows
│ │
│ └── • values
size: 1 column, 3 rows
│ └── • scan buffer
label: buffer 1
└── • constraint-check
Expand All @@ -507,11 +509,11 @@ vectorized: true
└── • cross join
├── • scan buffer
label: buffer 1
├── • values
size: 1 column, 3 rows
└── • values
size: 1 column, 3 rows
└── • scan buffer
label: buffer 1

query TIIIIIIIIT colnames
SELECT * FROM (VALUES ('us-east-1', 23, 24, 25, 26), ('ca-central-1', 30, 30, 31, 32)) AS v(crdb_region, pk, pk2, a, b)
Expand Down
12 changes: 6 additions & 6 deletions pkg/ccl/logictestccl/testdata/logic_test/zone
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TABLE t
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
├── PRIMARY INDEX primary
│ ├── k int not null
│ └── ZONE
│ └── constraints: [+region=test,+dc=dc2]
Expand Down Expand Up @@ -91,7 +91,7 @@ TABLE t
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
├── PRIMARY INDEX primary
│ ├── k int not null
│ └── ZONE
│ └── constraints: [+region=test,+dc=dc2]
Expand Down Expand Up @@ -160,7 +160,7 @@ TABLE t
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
├── PRIMARY INDEX primary
│ ├── k int not null
│ └── ZONE
│ └── constraints: [+region=test,+dc=dc1]
Expand Down Expand Up @@ -272,7 +272,7 @@ TABLE t
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
├── PRIMARY INDEX primary
│ ├── k int not null
│ └── ZONE
│ ├── constraints: [+region=test]
Expand Down Expand Up @@ -329,7 +329,7 @@ TABLE t
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
├── PRIMARY INDEX primary
│ ├── k int not null
│ └── ZONE
│ ├── constraints: [+region=test]
Expand Down Expand Up @@ -440,7 +440,7 @@ TABLE t
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
├── PRIMARY INDEX primary
│ ├── k int not null
│ └── ZONE
│ ├── constraints: [+region=test]
Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/opt/cat/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,19 @@ func FormatTable(cat Catalog, tab Table, tp treeprinter.Node) {
// debugging and testing.
func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node) {
idx := tab.Index(ord)
inverted := ""
if idx.IsInverted() {
inverted = "INVERTED "
idxType := ""
if idx.Ordinal() == PrimaryIndex {
idxType = "PRIMARY "
} else if idx.IsUnique() {
idxType = "UNIQUE "
} else if idx.IsInverted() {
idxType = "INVERTED "
}
mutation := ""
if IsMutationIndex(tab, ord) {
mutation = " (mutation)"
}
child := tp.Childf("%sINDEX %s%s", inverted, idx.Name(), mutation)
child := tp.Childf("%sINDEX %s%s", idxType, idx.Name(), mutation)

var buf bytes.Buffer
colCount := idx.ColumnCount()
Expand Down
Loading

0 comments on commit 06486c9

Please sign in to comment.