Skip to content

Commit

Permalink
sql: Added SELECT query and CTAS timestamp fields to TableDescriptor
Browse files Browse the repository at this point in the history
CTAS was initially planned and executed in the same user transaction.
As a first step in improving its performance and scalability
(issue cockroachdb#25828) we needed to split the responsibility of creating a new
TableDescriptor, and executing the AS query.

While the user txn continues to create and store the new desc, the
execution has been made async by moving the logic to the
SchemaChanger. This requires the SchemaChanger to be able to
bootstrap its state from the desc it reads, thereby requiring the
addition of these fields.

Release note: None
  • Loading branch information
adityamaru27 committed Jun 20, 2019
1 parent 8dc92a6 commit 87a618f
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 60 deletions.
4 changes: 2 additions & 2 deletions pkg/acceptance/testdata/psql/test-psql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ echo 'Testing copy error'
output="$(psql -d testdb -c 'copy missing from stdin' 2>&1 || true)"
echo $output | grep 'relation "missing" does not exist'

# Test that CREATE TABLE AS returns tag SELECT, not CREATE (#20227).
psql -d testdb -c "CREATE TABLE ctas AS SELECT 1" | grep "SELECT"
# Test that CREATE TABLE AS returns tag CREATE TABLE AS, not CREATE (#20227).
psql -d testdb -c "CREATE TABLE ctas AS SELECT 1" | grep "CREATE TABLE AS"
2 changes: 1 addition & 1 deletion pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ func Example_sql() {
// sql -e create table t.g1 (x int)
// CREATE TABLE
// sql -e create table t.g2 as select * from generate_series(1,10)
// SELECT 10
// CREATE TABLE AS
// sql -d nonexistent -e select count(*) from "".information_schema.tables limit 0
// count
// sql -d nonexistent -e create database nonexistent; create table foo(x int); select * from foo
Expand Down
34 changes: 26 additions & 8 deletions pkg/sql/logictest/testdata/logic_test/create_as
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
# LogicTest: local local-opt

statement count 3
statement ok
CREATE TABLE stock (item, quantity) AS VALUES ('cups', 10), ('plates', 15), ('forks', 30)

statement count 1
statement count 3
SELECT * FROM stock

statement ok
CREATE TABLE runningOut AS SELECT * FROM stock WHERE quantity < 12

statement count 1
SELECT * FROM runningOut

query TI
SELECT * FROM runningOut
----
cups 10

statement count 3
statement ok
CREATE TABLE itemColors (color) AS VALUES ('blue'), ('red'), ('green')

statement count 9
statement count 3
SELECT * FROM itemColors

statement ok
CREATE TABLE itemTypes AS (SELECT item, color FROM stock, itemColors)

statement count 9
SELECT * FROM itemTypes

query TT rowsort
SELECT * FROM itemTypes
----
Expand All @@ -39,9 +51,12 @@ CREATE TABLE t2 (col1, col2, col3) AS SELECT * FROM stock
statement error pgcode 42601 CREATE TABLE specifies 1 column name, but data source has 2 columns
CREATE TABLE t2 (col1) AS SELECT * FROM stock

statement count 5
statement ok
CREATE TABLE unionstock AS SELECT * FROM stock UNION VALUES ('spoons', 25), ('knives', 50)

statement count 5
SELECT * FROM unionstock

query TI
SELECT * FROM unionstock ORDER BY quantity
----
Expand All @@ -51,7 +66,7 @@ spoons 25
forks 30
knives 50

statement count 0
statement ok
CREATE TABLE IF NOT EXISTS unionstock AS VALUES ('foo', 'bar')

query TI
Expand All @@ -62,10 +77,13 @@ cups 10
statement ok
CREATE DATABASE smtng

statement count 3
statement ok
CREATE TABLE smtng.something AS SELECT * FROM stock

statement count 0
statement count 3
SELECT * FROM smtng.something;

statement ok
CREATE TABLE IF NOT EXISTS smtng.something AS SELECT * FROM stock

query TI
Expand Down
50 changes: 40 additions & 10 deletions pkg/sql/logictest/testdata/logic_test/schema_change_in_txn
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,12 @@ COMMIT

subtest create_with_other_commands_in_txn

statement count 3
statement ok
CREATE TABLE kv (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 15)

statement count 3
SELECT * FROM kv

statement ok
BEGIN

Expand Down Expand Up @@ -313,9 +316,12 @@ subtest create_as_with_add_column_index_in_txn
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE stock (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 15)

statement count 3
SELECT * FROM stock

# index is only over data added in the transaction so the backfill occurs
# within the trasaction.
statement ok
Expand Down Expand Up @@ -395,15 +401,21 @@ subtest create_as_drop_and_create_in_txn
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE hood (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 15)

statement count 3
SELECT * FROM hood

statement ok
DROP TABLE hood

statement count 3
statement ok
CREATE TABLE hood (item, quantity) AS VALUES ('plates', 10), ('knives', 30), ('spoons', 12)

statement count 3
SELECT * FROM hood

query TI rowsort
SELECT * FROM hood
----
Expand All @@ -419,15 +431,21 @@ subtest create_as_rename_and_create_in_txn
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE shop (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 15)

statement count 3
SELECT * FROM shop

statement ok
ALTER TABLE shop RENAME TO ship

statement count 3
statement ok
CREATE TABLE shop (item, quantity) AS VALUES ('spoons', 11), ('plates', 34), ('knives', 22)

statement count 3
SELECT * FROM shop

query TI rowsort
SELECT * FROM shop
----
Expand All @@ -450,9 +468,12 @@ subtest create_as_fail_unique_index
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE shopping (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 10)

statement count 3
SELECT * FROM shopping

statement error pgcode 23505 violates unique constraint "bar"
CREATE UNIQUE INDEX bar ON shopping (quantity)

Expand All @@ -468,9 +489,12 @@ subtest add_column_not_null_violation
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE shopping (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 10)

statement count 3
SELECT * FROM shopping

statement error pgcode 23502 null value in column \"q\" violates not-null constraint
ALTER TABLE shopping ADD COLUMN q DECIMAL NOT NULL

Expand All @@ -486,9 +510,12 @@ subtest add_column_computed_column_failure
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE shopping (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 10)

statement count 3
SELECT * FROM shopping

statement error pgcode 42P15 division by zero
ALTER TABLE shopping ADD COLUMN c int AS (quantity::int // 0) STORED

Expand All @@ -500,9 +527,12 @@ subtest create_as_add_multiple_columns
statement ok
BEGIN

statement count 3
statement ok
CREATE TABLE cutlery (item, quantity) AS VALUES ('cups', 10), ('plates', 30), ('forks', 15)

statement count 3
SELECT * FROM cutlery

# Add two columns, one with a computed and the other without any default.
statement ok
ALTER TABLE cutlery ADD COLUMN c INT AS (quantity + 4) STORED, ADD COLUMN d INT
Expand Down
Loading

0 comments on commit 87a618f

Please sign in to comment.