todo merge with code in load_data.go
(parserData []types.Datum, rowID int64)
| 97 | |
| 98 | // todo merge with code in load_data.go |
| 99 | func (en *TableKVEncoder) parserData2TableData(parserData []types.Datum, rowID int64) ([]types.Datum, error) { |
| 100 | row := make([]types.Datum, 0, len(en.insertColumns)) |
| 101 | setVar := func(name string, col *types.Datum) { |
| 102 | // User variable names are not case-sensitive |
| 103 | // https://dev.mysql.com/doc/refman/8.0/en/user-variables.html |
| 104 | name = strings.ToLower(name) |
| 105 | if col == nil || col.IsNull() { |
| 106 | en.SessionCtx.UnsetUserVar(name) |
| 107 | } else { |
| 108 | en.SessionCtx.SetUserVarVal(name, *col) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | hasValue := make([]bool, len(en.Columns)) |
| 113 | for i := range en.insertColumns { |
| 114 | offset := en.insertColumns[i].Offset |
| 115 | hasValue[offset] = true |
| 116 | } |
| 117 | |
| 118 | for i := range en.fieldMappings { |
| 119 | col := en.fieldMappings[i].Column |
| 120 | if i >= len(parserData) { |
| 121 | if col == nil { |
| 122 | setVar(en.fieldMappings[i].UserVar.Name, nil) |
| 123 | continue |
| 124 | } |
| 125 | |
| 126 | // If some columns is missing and their type is time and has not null flag, they should be set as current time. |
| 127 | if types.IsTypeTime(col.GetType()) && mysql.HasNotNullFlag(col.GetFlag()) { |
| 128 | row = append(row, types.NewTimeDatum(types.CurrentTime(col.GetType()))) |
| 129 | continue |
| 130 | } |
| 131 | |
| 132 | row = append(row, types.NewDatum(nil)) |
| 133 | hasValue[col.Offset] = false |
| 134 | continue |
| 135 | } |
| 136 | |
| 137 | if col == nil { |
| 138 | setVar(en.fieldMappings[i].UserVar.Name, &parserData[i]) |
| 139 | continue |
| 140 | } |
| 141 | |
| 142 | row = append(row, parserData[i]) |
| 143 | } |
| 144 | for i := 0; i < len(en.columnAssignments); i++ { |
| 145 | // eval expression of `SET` clause |
| 146 | d, err := en.columnAssignments[i].Eval(en.SessionCtx.GetExprCtx().GetEvalCtx(), chunk.Row{}) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | row = append(row, d) |
| 151 | } |
| 152 | |
| 153 | // a new row buffer will be allocated in getRow |
| 154 | newRow, err := en.getRow(row, hasValue, rowID) |
| 155 | if err != nil { |
| 156 | return nil, err |
no test coverage detected