Skip to content

Commit

Permalink
importer: support {} format for arrays in CSV
Browse files Browse the repository at this point in the history
Release note (sql change): Arrays can now be imported in a CSV file
using the {} format, similar to COPY FROM. Importing array expressions
(e.g. ARRAY[1, 2, 3]) is still supported as well.
  • Loading branch information
rafiss committed Aug 11, 2022
1 parent 0ad97e4 commit bad5944
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
19 changes: 19 additions & 0 deletions pkg/sql/importer/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,25 @@ ORDER BY table_name
},
},
},
{
name: "array",
create: `a string, b string[]`,
typ: "CSV",
data: `cat,"{somevalue,anothervalue,anothervalue123}"`,
query: map[string][][]string{
`SELECT * from t`: {
{"cat", "{somevalue,anothervalue,anothervalue123}"},
},
},
},
{
name: "array",
create: `a string, b string[]`,
typ: "CSV",
data: `dog,{some,thing}`,
err: "error parsing row 1: expected 2 fields, got 3",
rejected: "dog,{some,thing}\n",
},

// PG COPY
{
Expand Down
17 changes: 12 additions & 5 deletions pkg/sql/importer/read_import_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,18 @@ func (c *csvRowConsumer) FillDatums(
var err error
conv.Datums[datumIdx], err = rowenc.ParseDatumStringAs(conv.VisibleColTypes[i], field.Val, conv.EvalCtx)
if err != nil {
col := conv.VisibleCols[i]
return newImportRowError(
errors.Wrapf(err, "parse %q as %s", col.GetName(), col.GetType().SQLString()),
strRecord(record, c.opts.Comma),
rowNum)
// Fallback to parsing as a string literal. This allows us to support
// both array expressions (like `ARRAY[1, 2, 3]`) and literals (like
// `{1, 2, 3}`).
var err2 error
conv.Datums[datumIdx], _, err2 = tree.ParseAndRequireString(conv.VisibleColTypes[i], field.Val, conv.EvalCtx)
if err2 != nil {
col := conv.VisibleCols[i]
return newImportRowError(
errors.Wrapf(errors.CombineErrors(err, err2), "parse %q as %s", col.GetName(), col.GetType().SQLString()),
strRecord(record, c.opts.Comma),
rowNum)
}
}
}
datumIdx++
Expand Down

0 comments on commit bad5944

Please sign in to comment.