Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: add NULL DEFINED BY and FORMAT to LOAD DATA #41332

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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