Skip to content

Commit 00620ac

Browse files
author
Justin Jaffray
committed
opt: support view and sequence numeric refs
I ended up complicating this much more than necessary - it turns out the heuristic planner doesn't even support specifying columns for view numeric references, so this feature is actually really straightforward. I added a separate test file that is run under the heuristic planner as well (the existing numeric reference test file panics when run under the HP, concerningly...). Release note: None
1 parent 69fc2b9 commit 00620ac

File tree

4 files changed

+144
-70
lines changed

4 files changed

+144
-70
lines changed
Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,52 @@
1-
# LogicTest: local-opt
2-
# ------------------------------------------------------------------------------
3-
# Numeric References Tests.
4-
# These are put at the beginning of the file to ensure the numeric table
5-
# reference is 53 (the numeric reference of the first table).
6-
# If the numbering scheme in cockroach changes, this test will break.
7-
# TODO(madhavsuresh): get the numeric reference ID in a less brittle fashion
8-
# ------------------------------------------------------------------------------
1+
# LogicTest: local-opt local
92

103
statement ok
11-
CREATE TABLE num_ref (a INT PRIMARY KEY, xx INT, b INT, c INT, INDEX bc (b,c))
4+
CREATE TABLE x (a INT PRIMARY KEY, xx INT, b INT, c INT, INDEX bc (b,c))
125

136
statement ok
14-
CREATE TABLE num_ref_hidden (a INT, b INT)
15-
16-
17-
statement ok
18-
ALTER TABLE num_ref RENAME COLUMN b TO d
7+
INSERT INTO x VALUES (1), (2), (3)
198

209
statement ok
21-
ALTER TABLE num_ref RENAME COLUMN a TO p
10+
CREATE VIEW view_ref AS SELECT a, 1 FROM x
2211

23-
statement ok
24-
ALTER TABLE num_ref DROP COLUMN xx
12+
let $v_id
13+
SELECT id FROM system.namespace WHERE name='view_ref'
2514

26-
statement ok
27-
INSERT INTO num_ref VALUES (1, 10, 101), (2, 20, 200), (3, 30, 300)
28-
29-
statement ok
30-
INSERT INTO num_ref_hidden VALUES (1, 10), (2, 20), (3, 30)
15+
statement error cannot specify an explicit column list when accessing a view by reference
16+
SELECT * FROM [$v_id(1) AS _]
3117

32-
query III
33-
SELECT * FROM num_ref
18+
query II
19+
SELECT * FROM [$v_id AS _]
3420
----
35-
1 10 101
36-
2 20 200
37-
3 30 300
38-
39-
query III
40-
SELECT * FROM [53 as num_ref_alias]
41-
----
42-
1 10 101
43-
2 20 200
44-
3 30 300
45-
46-
query III
47-
SELECT * FROM [53(4,3,1) AS num_ref_alias]
48-
----
49-
101 10 1
50-
200 20 2
51-
300 30 3
21+
1 1
22+
2 1
23+
3 1
5224

5325
query I
54-
SELECT * FROM [53(4) AS num_ref_alias]@[2]
55-
----
56-
101
57-
200
58-
300
59-
60-
query I
61-
SELECT * FROM [53(1) AS num_ref_alias]@[1]
26+
SELECT foo.a FROM [$v_id AS foo]
6227
----
6328
1
6429
2
6530
3
6631

67-
query III colnames
68-
SELECT * FROM [53(1,3,4) AS num_ref_alias(col1,col2,col3)]
69-
----
70-
col1 col2 col3
71-
1 10 101
72-
2 20 200
73-
3 30 300
32+
statement ok
33+
CREATE SEQUENCE seq
7434

75-
query I
76-
SELECT * FROM [54(1,3) AS num_ref_hidden]
77-
----
78-
1
79-
2
80-
3
35+
let $seq_id
36+
SELECT id FROM system.namespace WHERE name='seq'
8137

82-
query I
83-
SELECT count(rowid) FROM [54(3) AS num_ref_hidden]
38+
query IIB
39+
SELECT * FROM [$seq_id AS _]
8440
----
85-
3
41+
0 0 true
8642

87-
# Ensure that privileges are checked when using numeric references.
88-
user testuser
43+
# Col refs in sequences are ignored.
44+
query IIB
45+
SELECT * FROM [$seq_id(1) AS _]
46+
----
47+
0 0 true
8948

90-
statement error pq: user testuser does not have SELECT privilege on relation num_ref
91-
SELECT * FROM [53 AS t]
49+
query IIB
50+
SELECT * FROM [$seq_id(1, 2) AS _]
51+
----
52+
0 0 true
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# LogicTest: local-opt
2+
# ------------------------------------------------------------------------------
3+
# Numeric References Tests.
4+
# These are put at the beginning of the file to ensure the numeric table
5+
# reference is 53 (the numeric reference of the first table).
6+
# If the numbering scheme in cockroach changes, this test will break.
7+
# TODO(madhavsuresh): get the numeric reference ID in a less brittle fashion
8+
# ------------------------------------------------------------------------------
9+
10+
statement ok
11+
CREATE TABLE num_ref (a INT PRIMARY KEY, xx INT, b INT, c INT, INDEX bc (b,c))
12+
13+
statement ok
14+
CREATE TABLE num_ref_hidden (a INT, b INT)
15+
16+
statement ok
17+
ALTER TABLE num_ref RENAME COLUMN b TO d
18+
19+
statement ok
20+
ALTER TABLE num_ref RENAME COLUMN a TO p
21+
22+
statement ok
23+
ALTER TABLE num_ref DROP COLUMN xx
24+
25+
statement ok
26+
INSERT INTO num_ref VALUES (1, 10, 101), (2, 20, 200), (3, 30, 300)
27+
28+
statement ok
29+
INSERT INTO num_ref_hidden VALUES (1, 10), (2, 20), (3, 30)
30+
31+
query III
32+
SELECT * FROM num_ref
33+
----
34+
1 10 101
35+
2 20 200
36+
3 30 300
37+
38+
let $num_ref_id
39+
SELECT id FROM system.namespace WHERE name='num_ref'
40+
41+
query III
42+
SELECT * FROM [$num_ref_id as num_ref_alias]
43+
----
44+
1 10 101
45+
2 20 200
46+
3 30 300
47+
48+
query III
49+
SELECT * FROM [$num_ref_id(4,3,1) AS num_ref_alias]
50+
----
51+
101 10 1
52+
200 20 2
53+
300 30 3
54+
55+
query I
56+
SELECT * FROM [$num_ref_id(4) AS num_ref_alias]@[2]
57+
----
58+
101
59+
200
60+
300
61+
62+
query I
63+
SELECT * FROM [$num_ref_id(1) AS num_ref_alias]@[1]
64+
----
65+
1
66+
2
67+
3
68+
69+
query III colnames
70+
SELECT * FROM [$num_ref_id(1,3,4) AS num_ref_alias(col1,col2,col3)]
71+
----
72+
col1 col2 col3
73+
1 10 101
74+
2 20 200
75+
3 30 300
76+
77+
let $num_ref_hidden_id
78+
SELECT id FROM system.namespace WHERE name='num_ref_hidden'
79+
80+
query I
81+
SELECT * FROM [$num_ref_hidden_id(1,3) AS num_ref_hidden]
82+
----
83+
1
84+
2
85+
3
86+
87+
query I
88+
SELECT count(rowid) FROM [$num_ref_hidden_id(3) AS num_ref_hidden]
89+
----
90+
3
91+
92+
# Ensure that privileges are checked when using numeric references.
93+
user testuser
94+
95+
statement error pq: user testuser does not have SELECT privilege on relation num_ref
96+
SELECT * FROM [$num_ref_id AS t]

pkg/sql/opt/optbuilder/select.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,19 @@ func (b *Builder) buildDataSource(
130130
switch t := ds.(type) {
131131
case cat.Table:
132132
outScope = b.buildScanFromTableRef(t, source, indexFlags, inScope)
133+
case cat.View:
134+
if source.Columns != nil {
135+
panic(builderError{
136+
fmt.Errorf("cannot specify an explicit column list when accessing a view by reference"),
137+
})
138+
}
139+
140+
outScope = b.buildView(t, inScope)
141+
case cat.Sequence:
142+
// Any explicitly listed columns are ignored.
143+
outScope = b.buildSequenceSelect(t, inScope)
133144
default:
134-
panic(unimplementedWithIssueDetailf(35708, fmt.Sprintf("%T", t), "view and sequence numeric refs are not supported"))
145+
panic(errors.AssertionFailedf("unsupported catalog object"))
135146
}
136147
b.renameSource(source.As, outScope)
137148
return outScope

pkg/sql/opt/testutils/testcat/test_catalog.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ func (tc *Catalog) ResolveDataSourceByID(
156156
if tab, ok := ds.(*Table); ok && tab.TabID == id {
157157
return ds, nil
158158
}
159+
if v, ok := ds.(*View); ok && v.ViewID == id {
160+
return ds, nil
161+
}
162+
if v, ok := ds.(*Sequence); ok && v.SeqID == id {
163+
return ds, nil
164+
}
159165
}
160166
return nil, pgerror.Newf(pgcode.UndefinedTable,
161167
"relation [%d] does not exist", id)

0 commit comments

Comments
 (0)