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

opt: add support for numeric references #28159

Merged
merged 1 commit into from
Aug 8, 2018
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
5 changes: 5 additions & 0 deletions pkg/sql/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ func (p *planner) getTableScanByRef(
return planDataSource{}, errors.Wrapf(err, "%s", tree.ErrString(tref))
}

if tref.Columns != nil && len(tref.Columns) == 0 {
return planDataSource{}, pgerror.NewErrorf(pgerror.CodeSyntaxError,
"an explicit list of column IDs must include at least one column")
}

// Ideally, we'd like to populate DatabaseName here, however that
// would require a reverse-lookup from DB ID to database name, and
// we do not provide an API to do this without a KV lookup. The
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/drop_table
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ b
statement error pgcode 42P01 relation "a" does not exist
SELECT * FROM a

statement error pq: \[53 AS a\]: table is being dropped
SELECT * FROM [53 AS a]

statement error pgcode 42P01 relation "a" does not exist
DROP TABLE a

Expand Down
85 changes: 85 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/numeric_references
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# LogicTest: local-opt
# ------------------------------------------------------------------------------
# Numeric References Tests.
# These are put at the beginning of the file to ensure the numeric table
# reference is 53 (the numeric reference of the first table).
# If the numbering scheme in cockroach changes, this test will break.
# TODO(madhavsuresh): get the numeric reference ID in a less brittle fashion
# ------------------------------------------------------------------------------

statement ok
CREATE TABLE num_ref (a INT PRIMARY KEY, xx INT, b INT, c INT, INDEX bc (b,c))

statement ok
CREATE TABLE num_ref_hidden (a INT, b INT)


statement ok
ALTER TABLE num_ref RENAME COLUMN b TO d

statement ok
ALTER TABLE num_ref RENAME COLUMN a TO p

statement ok
ALTER TABLE num_ref DROP COLUMN xx

statement ok
INSERT INTO num_ref VALUES (1, 10, 101), (2, 20, 200), (3, 30, 300)

statement ok
INSERT INTO num_ref_hidden VALUES (1, 10), (2, 20), (3, 30)

query III
SELECT * FROM num_ref
----
1 10 101
2 20 200
3 30 300

query III
SELECT * FROM [53 as num_ref_alias]
----
1 10 101
2 20 200
3 30 300

query III
SELECT * FROM [53(4,3,1) AS num_ref_alias]
----
101 10 1
200 20 2
300 30 3

query I
SELECT * FROM [53(4) AS num_ref_alias]@[2]
----
101
200
300

query I
SELECT * FROM [53(1) AS num_ref_alias]@[1]
----
1
2
3

query III colnames
SELECT * FROM [53(1,3,4) AS num_ref_alias(col1,col2,col3)]
----
col1 col2 col3
1 10 101
2 20 200
3 30 300

query I
SELECT * FROM [54(1,3) AS num_ref_hidden]
----
1
2
3

query I
SELECT count(rowid) FROM [54(3) AS num_ref_hidden]
----
3
6 changes: 6 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/privileges_table
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ SET DATABASE = a
statement ok
CREATE TABLE t (k INT PRIMARY KEY, v int)

statement ok
SELECT * from [54 as num_ref]

statement ok
SHOW GRANTS ON t

Expand Down Expand Up @@ -66,6 +69,9 @@ SHOW COLUMNS FROM t
statement error pq: user testuser does not have SELECT privilege on relation t
SELECT r FROM t

statement error pq: user testuser does not have SELECT privilege on relation t
SELECT * from [56 as num_ref]

statement error user testuser does not have GRANT privilege on relation t
GRANT ALL ON t TO bar

Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/opt/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ type Table interface {
// position within the table, where i < ColumnCount.
Column(i int) Column

// LookupColumnOrdinal returns the ordinal of the column with the given ID.
// Note that this takes the internal column ID, and has no relation to
// ColumnIDs in the optimizer.
LookupColumnOrdinal(colID uint32) (int, error)

// IndexCount returns the number of indexes defined on this table. This
// includes the primary index, so the count is always >= 1.
IndexCount() int
Expand All @@ -219,6 +224,10 @@ type Catalog interface {
// FindTable returns a Table interface for the database table matching the
// given table name. Returns an error if the table does not exist.
FindTable(ctx context.Context, name *tree.TableName) (Table, error)

// FindTableByID returns a Table interface for the database table
// matching the given table ID. Returns an error if the table does not exist.
FindTableByID(ctx context.Context, tableID int64) (Table, error)
}

// FormatCatalogTable nicely formats a catalog table using a treeprinter for
Expand Down
206 changes: 206 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/select
Original file line number Diff line number Diff line change
@@ -1,5 +1,211 @@
# LogicTest: local-opt


# ------------------------------------------------------------------------------
# Numeric References Tests.
# These are put at the beginning of the file to ensure the numeric table
# reference is 53 (the numeric reference of the first table).
# If the numbering scheme in cockroach changes, this test will break.
# These tests replicate the tests at sql/table_ref_test.go. The reason
# for duplication is to include tests within the opt testing framework
# TODO(madhavsuresh): get the numeric reference ID in a less brittle fashion
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE num_ref (a INT PRIMARY KEY, xx INT, b INT, c INT, INDEX bc (b,c))

statement ok
CREATE TABLE num_ref_hidden (a INT, b INT)

statement ok
ALTER TABLE num_ref RENAME COLUMN b TO d

statement ok
ALTER TABLE num_ref RENAME COLUMN a TO p

statement ok
ALTER TABLE num_ref DROP COLUMN xx

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53 AS num_ref_alias]
----
scan · · (p, d, c) ·
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4) AS num_ref_alias]
----
scan · · (c) ·
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(1,4) AS num_ref_alias]
----
scan · · (p, c) ·
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(1,3,4) AS num_ref_alias]
----
scan · · (p, d, c) ·
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4,3,1) AS num_ref_alias]
----
render · · (c, d, p) ·
│ render 0 c · ·
│ render 1 d · ·
│ render 2 p · ·
└── scan · · (p, d, c) ·
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4,3,1) AS num_ref_alias(col1,col2,col3)]
----
render · · (col1, col2, col3) ·
│ render 0 c · ·
│ render 1 d · ·
│ render 2 p · ·
└── scan · · (p, d, c) ·
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4,3,1) AS num_ref_alias]@bc
----
scan · · (c, d, p) p!=NULL; weak-key(c,d,p)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4) AS num_ref_alias]@bc
----
render · · (c) ·
│ render 0 num_ref_alias.c · ·
└── scan · · (c, p[hidden,omitted], d[hidden,omitted]) p!=NULL; weak-key(c,p,d)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(3) AS num_ref_alias]@bc
----
render · · (d) ·
│ render 0 num_ref_alias.d · ·
└── scan · · (d, p[hidden,omitted], c[hidden,omitted]) p!=NULL; weak-key(d,p,c)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(1) AS num_ref_alias]@bc
----
render · · (p) p!=NULL
│ render 0 num_ref_alias.p · ·
└── scan · · (p, d[hidden,omitted], c[hidden,omitted]) p!=NULL; weak-key(p,d,c)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(1) AS num_ref_alias]@[1]
----
render · · (p) p!=NULL; key(p)
│ render 0 num_ref_alias.p · ·
└── scan · · (p, d[hidden,omitted], c[hidden,omitted]) p!=NULL; key(p)
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(1) AS num_ref_alias]@[2]
----
render · · (p) p!=NULL
│ render 0 num_ref_alias.p · ·
└── scan · · (p, d[hidden,omitted], c[hidden,omitted]) p!=NULL; weak-key(p,d,c)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(3) AS num_ref_alias]@[1]
----
render · · (d) ·
│ render 0 num_ref_alias.d · ·
└── scan · · (d, p[hidden,omitted], c[hidden,omitted]) p!=NULL; key(p)
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(3) AS num_ref_alias]@[2]
----
render · · (d) ·
│ render 0 num_ref_alias.d · ·
└── scan · · (d, p[hidden,omitted], c[hidden,omitted]) p!=NULL; weak-key(d,p,c)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4) AS num_ref_alias]@[1]
----
render · · (c) ·
│ render 0 num_ref_alias.c · ·
└── scan · · (c, p[hidden,omitted], d[hidden,omitted]) p!=NULL; key(p)
· table num_ref@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [53(4) AS num_ref_alias]@[2]
----
render · · (c) ·
│ render 0 num_ref_alias.c · ·
└── scan · · (c, p[hidden,omitted], d[hidden,omitted]) p!=NULL; weak-key(c,p,d)
· table num_ref@bc · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [54(1,3) AS num_ref_alias]
----
scan · · (a) ·
· table num_ref_hidden@primary · ·
· spans ALL · ·


query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM [54(3) AS num_ref_alias]
----
scan · · () ·
· table num_ref_hidden@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT rowid FROM [54(3) AS num_ref_alias]
----
scan · · (rowid[hidden]) ·
· table num_ref_hidden@primary · ·
· spans ALL · ·

query error pq: \[666\(1\) AS num_ref_alias\]: relation \"\[666\]\" does not exist
EXPLAIN (VERBOSE) SELECT * FROM [666(1) AS num_ref_alias]

query error pq: column \[666\] does not exist
EXPLAIN (VERBOSE) SELECT * FROM [53(666) AS num_ref_alias]

query error pq: column \[2\] does not exist
EXPLAIN (VERBOSE) SELECT * FROM [53(2) AS num_ref_alias]

query error pq: an explicit list of column IDs must include at least one column
EXPLAIN (VERBOSE) SELECT * FROM [53() AS num_ref_alias]

query error pq: an explicit list of column IDs must include at least one column
EXPLAIN (VERBOSE) SELECT 1 FROM [53() as num_ref_alias]

statement ok
DROP TABLE num_ref

query error pq: \[53\(1\) AS num_ref_alias\]: table is being dropped
EXPLAIN (VERBOSE) SELECT * FROM [53(1) AS num_ref_alias]

# ------------------------------------------------------------------------------
# Basic filter combinations.
# ------------------------------------------------------------------------------
Expand Down
Loading