Skip to content

Commit

Permalink
parser: add NULL DEFINED BY and FORMAT to LOAD DATA (pingcap#41332)
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 authored and ghazalfamilyusa committed Feb 15, 2023
1 parent c2a7146 commit f7b036d
Show file tree
Hide file tree
Showing 5 changed files with 9,831 additions and 9,659 deletions.
35 changes: 32 additions & 3 deletions parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1814,17 +1814,20 @@ const (

// LoadDataStmt is a statement to load data from a specified file, then insert this rows into an existing table.
// See https://dev.mysql.com/doc/refman/5.7/en/load-data.html
// in TiDB we extend the syntax to use LOAD DATA as a more general way to import data.
// in TiDB we extend the syntax to use LOAD DATA as a more general way to import data, see
// https://github.com/pingcap/tidb/issues/40499
type LoadDataStmt struct {
dmlNode

FileLocRef FileLocRefTp
Path string
Format string // empty only when it's CSV-like
OnDuplicate OnDuplicateKeyHandlingType
Table *TableName
Columns []*ColumnName
FieldsInfo *FieldsClause
LinesInfo *LinesClause
NullInfo *NullDefinedBy
IgnoreLines uint64
ColumnAssignments []*Assignment

Expand All @@ -1841,6 +1844,10 @@ func (n *LoadDataStmt) Restore(ctx *format.RestoreCtx) error {
}
ctx.WriteKeyWord("INFILE ")
ctx.WriteString(n.Path)
if n.Format != "" {
ctx.WriteKeyWord(" FORMAT ")
ctx.WriteString(n.Format)
}
if n.OnDuplicate == OnDuplicateKeyHandlingReplace {
ctx.WriteKeyWord(" REPLACE")
} else if n.OnDuplicate == OnDuplicateKeyHandlingIgnore {
Expand All @@ -1850,8 +1857,15 @@ func (n *LoadDataStmt) Restore(ctx *format.RestoreCtx) error {
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore LoadDataStmt.Table")
}
n.FieldsInfo.Restore(ctx)
n.LinesInfo.Restore(ctx)
if n.FieldsInfo != nil {
n.FieldsInfo.Restore(ctx)
}
if n.LinesInfo != nil {
n.LinesInfo.Restore(ctx)
}
if n.NullInfo != nil {
n.NullInfo.Restore(ctx)
}
if n.IgnoreLines != 0 {
ctx.WriteKeyWord(" IGNORE ")
ctx.WritePlainf("%d", n.IgnoreLines)
Expand Down Expand Up @@ -1993,6 +2007,21 @@ func (n *LinesClause) Restore(ctx *format.RestoreCtx) error {
return nil
}

// NullDefinedBy represent a syntax that extends MySQL's standard
type NullDefinedBy struct {
NullDef string
OptEnclosed bool
}

// Restore for NullDefinedBy
func (n *NullDefinedBy) Restore(ctx *format.RestoreCtx) {
ctx.WriteKeyWord(" NULL DEFINED BY ")
ctx.WriteString(n.NullDef)
if n.OptEnclosed {
ctx.WriteKeyWord(" OPTIONALLY ENCLOSED")
}
}

// CallStmt represents a call procedure query node.
// See https://dev.mysql.com/doc/refman/5.7/en/call.html
type CallStmt struct {
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ var tokenMap = map[string]int{
"DEC": decimalType,
"DECIMAL": decimalType,
"DEFAULT": defaultKwd,
"DEFINED": defined,
"DEFINER": definer,
"DELAY_KEY_WRITE": delayKeyWrite,
"DELAYED": delayed,
Expand Down
Loading

0 comments on commit f7b036d

Please sign in to comment.