Skip to content

Commit

Permalink
Merge pull request #7535 from dolthub/nicktobey/import-escape
Browse files Browse the repository at this point in the history
Allow importing tables when the columns contain characters that require escaping.
  • Loading branch information
nicktobey authored Feb 23, 2024
2 parents 2e6b391 + fec4bb4 commit 41d7441
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions go/libraries/doltcore/mvdata/engine_table_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (s *SqlEngineTableWriter) createTable() error {
var pks string
var sep string
for _, i := range s.tableSchema.PkOrdinals {
pks += sep + s.tableSchema.Schema[i].Name
pks += sep + sql.QuoteIdentifier(s.tableSchema.Schema[i].Name)
sep = ", "
}
if len(sep) > 0 {
Expand Down Expand Up @@ -303,7 +303,7 @@ func (s *SqlEngineTableWriter) getInsertNode(inputChannel chan sql.Row, replace
}
sep := ""
for _, col := range s.rowOperationSchema.Schema {
colNames += fmt.Sprintf("%s`%s`", sep, col.Name)
colNames += fmt.Sprintf("%s%s", sep, sql.QuoteIdentifier(col.Name))
values += fmt.Sprintf("%s1", sep)
if update {
duplicate += fmt.Sprintf("%s`%s` = VALUES(`%s`)", sep, col.Name, col.Name)
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/bats/helper/escaped-characters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--Key1,"""Key2",'Key3,`Key4,/Key5
0, 1, 2, 3, 4
27 changes: 26 additions & 1 deletion integration-tests/bats/import-tables.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,29 @@ teardown() {
run dolt table import -c -u -r t test.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "Must specify exactly one of -c, -u, -a, or -r." ]] || false
}
}

@test "import-tables: import tables where field names need to be escaped" {
dolt table import -c t `batshelper escaped-characters.csv`

run dolt sql -q "show create table t;"
[ "$status" -eq 0 ]
[[ "$output" =~ '`--Key1` int' ]] || false
[[ "$output" =~ '`"Key2` int' ]] || false
[[ "$output" =~ '`'"'"'Key3` int' ]] || false
[[ "$output" =~ '```Key4` int' ]] || false
[[ "$output" =~ '`/Key5` int' ]] || false

run dolt sql -q "select * from t;"
[ "$status" -eq 0 ]
[[ "$output" =~ '| --Key1 | "Key2 | '"'"'Key3 | `Key4 | /Key5 |' ]] || false
[[ "$output" =~ '| 0 | 1 | 2 | 3 | 4 |' ]] || false
}

@test "import-tables: import tables where primary key names need to be escaped" {
dolt table import -c -pk "--Key1" t `batshelper escaped-characters.csv`
dolt table import -r -pk '"Key2' t `batshelper escaped-characters.csv`
dolt table import -r -pk "'Key3" t `batshelper escaped-characters.csv`
dolt table import -r -pk '`Key4' t `batshelper escaped-characters.csv`
dolt table import -r -pk "/Key5" t `batshelper escaped-characters.csv`
}

0 comments on commit 41d7441

Please sign in to comment.