Skip to content

Commit 5b202fe

Browse files
committed
Made LOAD DATA handle NULL values and defaults correctly
1 parent a7e14d8 commit 5b202fe

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

enginetest/queries/load_queries.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ 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+
"LOAD DATA INFILE './testdata/load_defaults_null.csv' INTO TABLE t FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'",
30+
},
31+
Assertions: []ScriptTestAssertion{
32+
{
33+
Query: "select * from t",
34+
Expected: []sql.Row{{1, 1, 1}},
35+
},
36+
},
37+
},
2538
{
2639
Name: "Basic load data with enclosed values.",
2740
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,20 @@ func (l *loadDataIter) parseFields(ctx *sql.Context, line string) ([]sql.Express
206206
exprs[exprIdx] = expression.NewLiteral(nil, types.Null)
207207
}
208208
}
209-
case "NULL":
210-
exprs[exprIdx] = expression.NewLiteral(nil, types.Null)
209+
case "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)