Skip to content

Commit

Permalink
opt: Add execbuilder support for the Update operator
Browse files Browse the repository at this point in the history
Build an updateNode from the optimizer's new Update operator. For now,
always fetch all columns from the table. In the future, do "needed
column" analysis to select only those columns that are needed for the
update.

Release note: None
  • Loading branch information
andy-kimball committed Dec 3, 2018
1 parent 382b1dc commit ebc3720
Show file tree
Hide file tree
Showing 21 changed files with 454 additions and 219 deletions.
15 changes: 10 additions & 5 deletions pkg/sql/descriptor_mutation_test.go
Expand Up @@ -286,7 +286,8 @@ CREATE INDEX allidx ON t.test (k, v);
} else {
_, err = sqlDB.Exec(`INSERT INTO t.test (k, v, i) VALUES ('b', 'y', 'i')`)
}
if !testutils.IsError(err, `column "i" does not exist`) {
if !testutils.IsError(err, `column "i" does not exist`) &&
!testutils.IsError(err, `column "i" is being backfilled`) {
t.Fatal(err)
}
if useUpsert {
Expand All @@ -304,7 +305,8 @@ CREATE INDEX allidx ON t.test (k, v);
} else {
_, err = sqlDB.Exec(`INSERT INTO t.test VALUES ('b', 'y', 'i')`)
}
if !testutils.IsError(err, "(IN|UP)SERT has more expressions than target columns, 3 expressions for 2 targets") {
if !testutils.IsError(err, "(IN|UP)SERT has more expressions than target columns, 3 expressions for 2 targets") &&
!testutils.IsError(err, `column "i" is being backfilled`) {
t.Fatal(err)
}

Expand Down Expand Up @@ -390,7 +392,8 @@ CREATE INDEX allidx ON t.test (k, v);
}
} else {
_, err := sqlDB.Exec(`UPDATE t.test SET (v, i) = ('u', 'u') WHERE k = 'a'`)
if !testutils.IsError(err, `column "i" does not exist`) {
if !testutils.IsError(err, `column "i" does not exist`) &&
!testutils.IsError(err, `column "i" is being backfilled`) {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -726,7 +729,8 @@ CREATE INDEX allidx ON t.test (k, v);
}
} else {
_, err := sqlDB.Exec(`UPDATE t.test SET (v, i) = ('u', 'u') WHERE k = 'a'`)
if !testutils.IsError(err, `column "i" does not exist`) {
if !testutils.IsError(err, `column "i" does not exist`) &&
!testutils.IsError(err, `column "i" is being backfilled`) {
t.Error(err)
}
}
Expand All @@ -739,7 +743,8 @@ CREATE INDEX allidx ON t.test (k, v);
// TODO(vivek): Fix this error to return the same is being
// backfilled error.
_, err = sqlDB.Exec(`UPDATE t.test SET i = 'u' WHERE k = 'a'`)
if !testutils.IsError(err, `column "i" does not exist`) {
if !testutils.IsError(err, `column "i" does not exist`) &&
!testutils.IsError(err, `column "i" is being backfilled`) {
t.Error(err)
}
_, err = sqlDB.Exec(`DELETE FROM t.test WHERE i < 'a'`)
Expand Down
4 changes: 0 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Expand Up @@ -1987,12 +1987,8 @@ SELECT pg_catalog.pg_encoding_to_char(6), pg_catalog.pg_encoding_to_char(7)
----
UTF8 NULL

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query TITI
SELECT pg_catalog.inet_client_addr(), pg_catalog.inet_client_port(), pg_catalog.inet_server_addr(), pg_catalog.inet_server_port()
FROM pg_class
WHERE relname = 'pg_constraint'
----
::/0 0 ::/0 0

Expand Down
39 changes: 0 additions & 39 deletions pkg/sql/logictest/testdata/logic_test/check_constraints
Expand Up @@ -303,47 +303,8 @@ UPDATE t9 SET b = 4 WHERE a = 5
statement error pgcode 23514 failed to satisfy CHECK constraint \(a > b\)
UPDATE t9 SET b = 6 WHERE a = 5

# Only column families that are needed to validate check constraints are fetched.
query TTTTT
EXPLAIN (VERBOSE) UPDATE t9 SET b = 2 WHERE a = 5
----
count · · () ·
└── update · · () ·
│ table t9 · ·
│ set b · ·
│ check 0 t9.a > t9.b · ·
│ check 1 t9.d IS NULL · ·
└── render · · (a, b, d, "?column?") a=CONST; "?column?"=CONST; key()
│ render 0 test.public.t9.a · ·
│ render 1 test.public.t9.b · ·
│ render 2 test.public.t9.d · ·
│ render 3 2 · ·
└── scan · · (a, b, c[omitted], d, e[omitted]) a=CONST; key()
· table t9@primary · ·
· spans /5/0-/5/1/2 /5/3/1-/5/3/2 · ·

statement ok
UPDATE t9 SET a = 7 WHERE a = 4

statement error pgcode 23514 failed to satisfy CHECK constraint \(a > b\)
UPDATE t9 SET a = 2 WHERE a = 5

query TTTTT
EXPLAIN (VERBOSE) UPDATE t9 SET a = 2 WHERE a = 5
----
count · · () ·
└── update · · () ·
│ table t9 · ·
│ set a · ·
│ check 0 t9.a > t9.b · ·
│ check 1 t9.d IS NULL · ·
└── render · · (a, b, c, d, e, "?column?") a=CONST; "?column?"=CONST; key()
│ render 0 test.public.t9.a · ·
│ render 1 test.public.t9.b · ·
│ render 2 test.public.t9.c · ·
│ render 3 test.public.t9.d · ·
│ render 4 test.public.t9.e · ·
│ render 5 2 · ·
└── scan · · (a, b, c, d, e) a=CONST; key()
· table t9@primary · ·
· spans /5-/5/# · ·
4 changes: 3 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/computed
Expand Up @@ -725,7 +725,9 @@ INSERT INTO error_check VALUES(1, '1')
statement error could not parse "foo" as type int: strconv.ParseInt
INSERT INTO error_check VALUES(2, 'foo')

statement error computed column i:
# NOTE: The CBO cannot show the name of the computed column in the error message
# because the computation is part of an overall SQL statement.
statement error could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax
UPDATE error_check SET s = 'foo' WHERE k = 1

# Upsert -> update
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/default
Expand Up @@ -108,10 +108,10 @@ CREATE TABLE v (
)

statement ok
UPDATE v SET a = DEFAULT
UPDATE v SET b = DEFAULT

statement ok
UPDATE v SET (a, c) = (DEFAULT, DEFAULT)
UPDATE v SET (b, c) = (DEFAULT, DEFAULT)

query TTBTTTB colnames
SHOW COLUMNS FROM v
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/insert
Expand Up @@ -640,7 +640,9 @@ CREATE TABLE mutation (
)

# Check that values cannot be inserted into the dropped column.
statement error column "y" does not exist
# Error is different between HP and CBO, but is reasonable in both cases. Just
# verify there's an error that references column y.
statement error column "y"
INSERT INTO mutation(x, y) VALUES (2, 2)

statement ok
Expand Down
60 changes: 0 additions & 60 deletions pkg/sql/logictest/testdata/logic_test/pgoidtype
Expand Up @@ -40,42 +40,26 @@ SELECT pg_typeof('upper'::REGPROC), pg_typeof('upper'::REGPROCEDURE), pg_typeof(
----
regproc regprocedure regtype

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT 'pg_constraint'::REGCLASS, 'pg_catalog.pg_constraint'::REGCLASS
FROM pg_class
WHERE relname = 'pg_constraint'
----
pg_constraint pg_constraint

query error pgcode 42P01 relation "foo.pg_constraint" does not exist
SELECT 'foo.pg_constraint'::REGCLASS

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT '"pg_constraint"'::REGCLASS, ' "pg_constraint" '::REGCLASS
FROM pg_class
WHERE relname = 'pg_constraint'
----
pg_constraint pg_constraint

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT 'pg_constraint '::REGCLASS, ' pg_constraint '::REGCLASS
FROM pg_class
WHERE relname = 'pg_constraint'
----
pg_constraint pg_constraint

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT 'pg_constraint '::REGCLASS, '"pg_constraint"'::REGCLASS::OID
FROM pg_class
WHERE relname = 'pg_constraint'
----
pg_constraint 139623798

Expand All @@ -91,24 +75,16 @@ WHERE relname = 'pg_constraint'
----
139623798 pg_constraint 139623798 pg_constraint pg_constraint

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OOOO
SELECT 'upper'::REGPROC, 'upper'::REGPROCEDURE, 'pg_catalog.upper'::REGPROCEDURE, 'upper'::REGPROC::OID
FROM pg_class
WHERE relname = 'pg_constraint'
----
upper upper upper 2896429946

query error invalid function name
SELECT 'invalid.more.pg_catalog.upper'::REGPROCEDURE

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OOO
SELECT 'upper(int)'::REGPROC, 'upper(int)'::REGPROCEDURE, 'upper(int)'::REGPROC::OID
FROM pg_class
WHERE relname = 'pg_constraint'
----
upper upper 2896429946

Expand All @@ -130,48 +106,28 @@ SELECT 'blah(, )'::REGPROC
query error more than one function named 'sqrt'
SELECT 'sqrt'::REGPROC

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OOOO
SELECT 'array_in'::REGPROC, 'array_in(a,b,c)'::REGPROC, 'pg_catalog.array_in'::REGPROC, 'pg_catalog.array_in( a ,b, c )'::REGPROC
FROM pg_class
WHERE relname = 'pg_constraint'
----
array_in array_in array_in array_in

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OOOO
SELECT 'array_in'::REGPROCEDURE, 'array_in(a,b,c)'::REGPROCEDURE, 'pg_catalog.array_in'::REGPROCEDURE, 'pg_catalog.array_in( a ,b, c )'::REGPROCEDURE
FROM pg_class
WHERE relname = 'pg_constraint'
----
array_in array_in array_in array_in

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT 'public'::REGNAMESPACE, 'public'::REGNAMESPACE::OID
FROM pg_class
WHERE relname = 'pg_constraint'
----
public 2397796629

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT 'bool'::REGTYPE, 'bool'::REGTYPE::OID
FROM pg_class
WHERE relname = 'pg_constraint'
----
boolean 16

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OO
SELECT 'numeric(10,3)'::REGTYPE, 'numeric( 10, 3 )'::REGTYPE
FROM pg_class
WHERE relname = 'pg_constraint'
----
numeric numeric

Expand All @@ -195,22 +151,14 @@ SELECT 'blah'::REGTYPE

## Test other cast syntaxes

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query O
SELECT CAST ('pg_constraint' AS REGCLASS)
FROM pg_class
WHERE relname = 'pg_constraint'
----
pg_constraint

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
# This forces the b_expr form of the cast syntax.
query OO
SELECT ('pg_constraint')::REGCLASS, ('pg_constraint')::REGCLASS::OID
FROM pg_class
WHERE relname = 'pg_constraint'
----
pg_constraint 139623798

Expand Down Expand Up @@ -341,21 +289,13 @@ SELECT NOT (prorettype::regtype::text = 'foo') AND proretset FROM pg_proc WHERE
----
false

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query TTTTT
SELECT crdb_internal.create_regtype(10, 'foo'), crdb_internal.create_regclass(10, 'foo'), crdb_internal.create_regproc(10, 'foo'), crdb_internal.create_regprocedure(10, 'foo'), crdb_internal.create_regnamespace(10, 'foo')
FROM pg_class
WHERE relname = 'pg_constraint'
----
foo foo foo foo foo

# TODO(jordan): Restore this to original form by removing FROM
# clause once issue 32422 is fixed.
query OOOOO
SELECT crdb_internal.create_regtype(10, 'foo')::oid, crdb_internal.create_regclass(10, 'foo')::oid, crdb_internal.create_regproc(10, 'foo')::oid, crdb_internal.create_regprocedure(10, 'foo')::oid, crdb_internal.create_regnamespace(10, 'foo')::oid
FROM pg_class
WHERE relname = 'pg_constraint'
----
10 10 10 10 10

Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/subquery
Expand Up @@ -237,7 +237,9 @@ SELECT * FROM xyz
statement error value type tuple{int, int} doesn't match type INT of column "z"
UPDATE xyz SET z = (SELECT (10, 11)) WHERE x = 7

statement error pgcode 42601 subquery must return 2 columns, found 1
# Error is different between HP and CBO, but is reasonable in both cases. Just
# verify there's an error that contains the word "columns".
statement error columns
UPDATE xyz SET (y, z) = (SELECT (11, 12)) WHERE x = 7

#regression, see #6852
Expand Down
26 changes: 25 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/update
Expand Up @@ -469,7 +469,9 @@ UPDATE t29494 SET x = 124 WHERE x = 12 RETURNING *
----
124

statement error column "y" does not exist
# The heuristic planner and cost-based optimizer show different errors for this
# case (but both are reasonable and contain the word "column").
statement error column
UPDATE t29494 SET y = 123

# Check the new column is not usable in assignments. We need to
Expand All @@ -483,6 +485,28 @@ UPDATE t29494 SET x = y
statement ok
COMMIT

# Use delete-only mutation columns with default and computed expressions.
statement ok
CREATE TABLE mutation (m INT PRIMARY KEY, n INT)

statement ok
INSERT INTO mutation VALUES (1, 1)

statement ok
BEGIN; ALTER TABLE mutation add COLUMN o INT DEFAULT(10), ADD COLUMN p INT AS (o + n) STORED

statement ok
UPDATE mutation SET m=2 WHERE n=1

statement ok
COMMIT TRANSACTION

query IIII colnames
SELECT * FROM mutation
----
m n o p
2 1 10 11

#regression test for #32477
subtest reject_special_funcs_inset

Expand Down

0 comments on commit ebc3720

Please sign in to comment.