Skip to content

Commit 6af1c9c

Browse files
authored
Merge pull request #154290 from cockroachdb/blathers/backport-release-25.2-154162
release-25.2: sql: fix `COPY` with hidden and inaccessible columns
2 parents 16b967b + 3c8e96e commit 6af1c9c

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pkg/sql/copy_from.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,15 @@ func newCopyMachine(
343343
// to have field data then we have to populate the expectedHiddenColumnIdxs
344344
// field with the columns indexes we expect to be hidden.
345345
if c.p.SessionData().ExpectAndIgnoreNotVisibleColumnsInCopy && len(n.Columns) == 0 {
346+
numInaccessibleCols := 0
346347
for i, col := range tableDesc.PublicColumns() {
347348
if col.IsHidden() {
348-
c.expectedHiddenColumnIdxs = append(c.expectedHiddenColumnIdxs, i)
349+
// Offset the index by the number of preceding inaccessible
350+
// columns, which are never expected in the input.
351+
c.expectedHiddenColumnIdxs = append(c.expectedHiddenColumnIdxs, i-numInaccessibleCols)
352+
}
353+
if col.IsInaccessible() {
354+
numInaccessibleCols++
349355
}
350356
}
351357
}

pkg/sql/pgwire/testdata/pgtest/copy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,52 @@ ReadyForQuery
199199
{"Type":"CommandComplete","CommandTag":"SELECT 3"}
200200
{"Type":"ReadyForQuery","TxStatus":"I"}
201201

202+
# Regression test for #119524. expect_and_ignore_not_visible_columns_in_copy
203+
# should behave correctly with inaccessible columns.
204+
send
205+
Query {"String": "CREATE TABLE t119524 (x INT PRIMARY KEY, y INT, INDEX ((x + y)))"}
206+
----
207+
208+
until
209+
ReadyForQuery
210+
----
211+
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
212+
{"Type":"ReadyForQuery","TxStatus":"I"}
213+
214+
send
215+
Query {"String": "ALTER TABLE t119524 ADD COLUMN z INT NOT VISIBLE"}
216+
----
217+
218+
until
219+
ReadyForQuery
220+
----
221+
{"Type":"CommandComplete","CommandTag":"ALTER TABLE"}
222+
{"Type":"ReadyForQuery","TxStatus":"I"}
223+
224+
send crdb_only
225+
Query {"String": "COPY t119524 FROM STDIN"}
226+
CopyData {"Data": "1\t2\t3\n"}
227+
CopyDone
228+
----
229+
230+
until crdb_only
231+
ReadyForQuery
232+
----
233+
{"Type":"CopyInResponse","ColumnFormatCodes":[0,0]}
234+
{"Type":"CommandComplete","CommandTag":"COPY 1"}
235+
{"Type":"ReadyForQuery","TxStatus":"I"}
236+
237+
send crdb_only
238+
Query {"String": "SELECT *, z FROM t119524"}
239+
----
240+
241+
until ignore=RowDescription crdb_only
242+
ReadyForQuery
243+
----
244+
{"Type":"DataRow","Values":[{"text":"1"},{"text":"2"},null]}
245+
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
246+
{"Type":"ReadyForQuery","TxStatus":"I"}
247+
202248
send crdb_only
203249
Query {"String": "SET expect_and_ignore_not_visible_columns_in_copy = false"}
204250
----

0 commit comments

Comments
 (0)