Skip to content

Commit 3693a55

Browse files
authored
Merge pull request #3155 from dolthub/tim/load-data-null-default
Made LOAD DATA handle NULL values and defaults correctly
2 parents f6c0a7b + fb4b16d commit 3693a55

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

enginetest/queries/load_queries.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ import (
2222
)
2323

2424
var LoadDataScripts = []ScriptTest{
25+
{
26+
Name: "LOAD DATA applies column defaults when \\N provided",
27+
SetUpScript: []string{
28+
"create table t (pk int primary key, c1 int default 1, c2 int)",
29+
// Explicitly use Windows-style line endings to be robust on Windows CI
30+
"LOAD DATA INFILE './testdata/load_defaults_null.csv' INTO TABLE t FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n'",
31+
},
32+
Assertions: []ScriptTestAssertion{
33+
{
34+
Query: "select * from t",
35+
Expected: []sql.Row{{1, 1, 1}},
36+
},
37+
},
38+
},
2539
{
2640
Name: "Basic load data with enclosed values.",
2741
SetUpScript: []string{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,\N,1

sql/rowexec/ddl_iters.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,19 @@ func (l *loadDataIter) parseFields(ctx *sql.Context, line string) ([]sql.Express
207207
}
208208
}
209209
case "NULL":
210-
exprs[exprIdx] = expression.NewLiteral(nil, types.Null)
210+
// For MySQL LOAD DATA semantics, \N (mapped to NULL here) should use the column default
211+
// if one exists; otherwise insert NULL.
212+
destIdx := l.fieldToColMap[fieldIdx]
213+
if destIdx >= 0 {
214+
destCol := l.destSch[destIdx]
215+
if destCol.Default != nil {
216+
exprs[exprIdx] = destCol.Default
217+
} else {
218+
exprs[exprIdx] = expression.NewLiteral(nil, types.Null)
219+
}
220+
} else {
221+
exprs[exprIdx] = expression.NewLiteral(nil, types.Null)
222+
}
211223
default:
212224
exprs[exprIdx] = expression.NewLiteral(field, types.LongText)
213225
}

0 commit comments

Comments
 (0)