Skip to content

Commit

Permalink
Merge pull request #74435 from adityamaru/import-pgdump-more-cols-tha…
Browse files Browse the repository at this point in the history
…n-data

release-21.2: importccl: fix import pgdump target column bug
  • Loading branch information
adityamaru authored Jan 5, 2022
2 parents fb88f95 + 23547e4 commit a3ea612
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/ccl/importccl/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5611,6 +5611,33 @@ func TestImportPgDump(t *testing.T) {
sqlDB.Exec(t, "IMPORT PGDUMP ($1)", srv.URL)
sqlDB.CheckQueryResults(t, `SELECT * from t`, [][]string{{"2", "42", "1"}, {"4", "42", "3"}})
})

t.Run("more-target-cols-than-data", func(t *testing.T) {
data := `
COPY public.t (a, b, c) FROM stdin;
a b c
\.
INSERT INTO public.t (a, b, c) VALUES ('a', 'b', 'c');
`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
_, _ = w.Write([]byte(data))
}
}))
defer srv.Close()
defer sqlDB.Exec(t, "DROP TABLE t")
sqlDB.Exec(t, `
IMPORT TABLE t (
c STRING,
a STRING,
b STRING,
d STRING
) PGDUMP DATA ($1) WITH ignore_unsupported_statements`, srv.URL)
sqlDB.CheckQueryResults(t, `SELECT * from t`,
[][]string{{"c", "a", "b", "NULL"}, {"c", "a", "b", "NULL"}})
})

t.Run("import-into-not-supported", func(t *testing.T) {
data := `INSERT INTO t VALUES (1, 2), (3, 4)`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/ccl/importccl/read_import_pgdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,14 @@ func (m *pgDumpReader) readFile(
conv.TargetColOrds.Add(idx)
targetColMapIdx[j] = idx
}
// For any missing columns, fill those to NULL.
// These will get filled in with the correct default / computed
// expression if there are any for these columns.
for idx := range conv.VisibleCols {
if !conv.TargetColOrds.Contains(idx) {
conv.Datums[idx] = tree.DNull
}
}
}
for {
row, err := ps.Next()
Expand Down

0 comments on commit a3ea612

Please sign in to comment.