Skip to content

Commit 0f151f0

Browse files
authored
Merge pull request cockroachdb#32591 from vivekmenezes/backport2.1-32188
backport-2.1: sql: fix panic on UPDATE RETURNING * in middle of schema change
2 parents 20cf978 + bbbfc1f commit 0f151f0

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

pkg/sql/logictest/testdata/logic_test/delete

+21-3
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,35 @@ SELECT count(*) FROM [DELETE FROM unindexed LIMIT 5 RETURNING v]
187187
subtest regression_29494
188188

189189
statement ok
190-
CREATE TABLE t29494(x INT); BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
190+
CREATE TABLE t29494(x INT PRIMARY KEY); INSERT INTO t29494 VALUES (12)
191+
192+
statement ok
193+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
191194

192195
# Check that the new column is not visible
193196
query T
194197
SELECT create_statement FROM [SHOW CREATE t29494]
195198
----
196199
CREATE TABLE t29494 (
197-
x INT NULL,
198-
FAMILY "primary" (x, rowid)
200+
x INT NOT NULL,
201+
CONSTRAINT "primary" PRIMARY KEY (x ASC),
202+
FAMILY "primary" (x)
199203
)
200204

201205
# Check that the new column is not usable in RETURNING
202206
statement error column "y" does not exist
203207
DELETE FROM t29494 RETURNING y
208+
209+
statement ok
210+
ROLLBACK
211+
212+
statement ok
213+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
214+
215+
query I
216+
DELETE FROM t29494 RETURNING *
217+
----
218+
12
219+
220+
statement ok
221+
COMMIT

pkg/sql/logictest/testdata/logic_test/insert

+18-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,10 @@ INSERT INTO derived(y) VALUES('abcd')
580580
subtest regression_29494
581581

582582
statement ok
583-
CREATE TABLE t29494(x INT); BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
583+
CREATE TABLE t29494(x INT); INSERT INTO t29494 VALUES (12)
584+
585+
statement ok
586+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
584587

585588
# Check that the new column is not visible
586589
query T
@@ -594,3 +597,17 @@ CREATE TABLE t29494 (
594597
# Check that the new column is not usable in RETURNING
595598
statement error column "y" does not exist
596599
INSERT INTO t29494(x) VALUES (123) RETURNING y
600+
601+
statement ok
602+
ROLLBACK
603+
604+
statement ok
605+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
606+
607+
query I
608+
INSERT INTO t29494(x) VALUES (123) RETURNING *
609+
----
610+
123
611+
612+
statement ok
613+
COMMIT

pkg/sql/logictest/testdata/logic_test/update

+13-3
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,19 @@ UPDATE derived SET y = 'abcd'
437437
subtest regression_29494
438438

439439
statement ok
440-
CREATE TABLE t29494(x INT); BEGIN; ALTER TABLE t29494 ADD COLUMN y INT
440+
CREATE TABLE t29494(x INT PRIMARY KEY); INSERT INTO t29494 VALUES (12)
441+
442+
statement ok
443+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
441444

442445
# Check that the new column is not visible
443446
query T
444447
SELECT create_statement FROM [SHOW CREATE t29494]
445448
----
446449
CREATE TABLE t29494 (
447-
x INT NULL,
448-
FAMILY "primary" (x, rowid)
450+
x INT NOT NULL,
451+
CONSTRAINT "primary" PRIMARY KEY (x ASC),
452+
FAMILY "primary" (x)
449453
)
450454

451455
# Check that the new column is not usable in RETURNING
@@ -457,6 +461,12 @@ UPDATE t29494 SET x = 123 RETURNING y
457461
statement ok
458462
ROLLBACK; BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
459463

464+
# Returning * doesn't return y
465+
query I
466+
UPDATE t29494 SET x = 124 WHERE x = 12 RETURNING *
467+
----
468+
124
469+
460470
statement error column "y" does not exist
461471
UPDATE t29494 SET y = 123
462472

pkg/sql/logictest/testdata/logic_test/upsert

+25-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,10 @@ UPSERT INTO tn2(x, y) VALUES (123, 'abcd')
622622
subtest regression_29494
623623

624624
statement ok
625-
CREATE TABLE t29494(x INT); BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
625+
CREATE TABLE t29494(x INT); INSERT INTO t29494 VALUES (12)
626+
627+
statement ok
628+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
626629

627630
# Check that the new column is not visible
628631
query T
@@ -647,6 +650,27 @@ INSERT INTO t29494(x) VALUES (123) ON CONFLICT(rowid) DO UPDATE SET x = 400 RETU
647650
statement ok
648651
ROLLBACK
649652

653+
statement ok
654+
BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
655+
656+
query I
657+
UPSERT INTO t29494(x) VALUES (12) RETURNING *
658+
----
659+
12
660+
661+
query I
662+
UPSERT INTO t29494(x) VALUES (123) RETURNING *
663+
----
664+
123
665+
666+
query I
667+
INSERT INTO t29494(x) VALUES (123) ON CONFLICT(rowid) DO UPDATE SET x = 400 RETURNING *
668+
----
669+
123
670+
671+
statement ok
672+
COMMIT
673+
650674
subtest regression_31255
651675

652676
statement ok

pkg/sql/update.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func (p *planner) Update(
275275
// If rowsNeeded is set, we have requested from the source above
276276
// all the columns from the descriptor. However, to ensure that
277277
// modified rows include all columns, the construction of the
278-
// source has used publicAndNonVivible columns so the source may
278+
// source has used publicAndNonPublicColumns so the source may
279279
// contain additional columns for every newly added column not yet
280280
// visible.
281281
// We do not want these to be available for RETURNING below.
@@ -675,7 +675,15 @@ func (u *updateNode) processSourceRow(params runParams, sourceVals tree.Datums)
675675

676676
// If result rows need to be accumulated, do it.
677677
if u.run.rows != nil {
678-
if _, err := u.run.rows.AddRow(params.ctx, newValues); err != nil {
678+
// The new values can include all columns, the construction of the
679+
// values has used publicAndNonPublicColumns so the values may
680+
// contain additional columns for every newly added column not yet
681+
// visible. We do not want them to be available for RETURNING.
682+
//
683+
// MakeUpdater guarantees that the first columns of the new values
684+
// are those specified u.columns.
685+
resultValues := newValues[:len(u.columns)]
686+
if _, err := u.run.rows.AddRow(params.ctx, resultValues); err != nil {
679687
return err
680688
}
681689
}

0 commit comments

Comments
 (0)