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

implement REPLACE stmt in new DML plan #21260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
39 changes: 16 additions & 23 deletions pkg/sql/plan/bind_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ func (builder *QueryBuilder) bindInsert(stmt *tree.Insert, bindCtx *BindContext)
return 0, err
}

// clusterTable, err := getAccountInfoOfClusterTable(ctx, stmt.Accounts, tableDef, tblInfo.isClusterTable[0])
// if err != nil {
// return 0, err
// }
// if len(stmt.OnDuplicateUpdate) > 0 && clusterTable.IsClusterTable {
// return 0, moerr.NewNotSupported(builder.compCtx.GetContext(), "INSERT ... ON DUPLICATE KEY UPDATE ... for cluster table")
// }

if stmt.IsRestore {
builder.isRestore = true
if stmt.IsRestoreByTs {
Expand All @@ -58,7 +50,7 @@ func (builder *QueryBuilder) bindInsert(stmt *tree.Insert, bindCtx *BindContext)
}()
}

lastNodeID, colName2Idx, skipUniqueIdx, err := builder.initInsertStmt(bindCtx, stmt, dmlCtx.objRefs[0], dmlCtx.tableDefs[0])
lastNodeID, colName2Idx, skipUniqueIdx, err := builder.initInsertStmt(bindCtx, stmt.Rows, stmt.Columns, dmlCtx.objRefs[0], dmlCtx.tableDefs[0])
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -90,11 +82,6 @@ func (builder *QueryBuilder) appendDedupAndMultiUpdateNodesForBindInsert(
skipUniqueIdx []bool,
astUpdateExprs tree.UpdateExprs,
) (int32, error) {
var err error

selectNode := builder.qry.Nodes[lastNodeID]
selectTag := selectNode.BindingTags[0]

tableDef := dmlCtx.tableDefs[0]
pkName := tableDef.Pkey.PkeyColName

Expand All @@ -109,7 +96,13 @@ func (builder *QueryBuilder) appendDedupAndMultiUpdateNodesForBindInsert(
}
}

var onDupAction plan.Node_OnDuplicateAction
var (
err error
onDupAction plan.Node_OnDuplicateAction
)

selectNode := builder.qry.Nodes[lastNodeID]
selectTag := selectNode.BindingTags[0]
scanTag := builder.genNewTag()
updateExprs := make(map[string]*plan.Expr)

Expand Down Expand Up @@ -676,20 +669,20 @@ func (builder *QueryBuilder) appendDedupAndMultiUpdateNodesForBindInsert(
// If the INSERT statement specifies the columns, it validates the column names against the table definition
// and returns an error if any of the column names are invalid.
// The function returns the list of insert columns and an error, if any.
func (builder *QueryBuilder) getInsertColsFromStmt(stmt *tree.Insert, tableDef *TableDef) ([]string, error) {
func (builder *QueryBuilder) getInsertColsFromStmt(astCols tree.IdentifierList, tableDef *TableDef) ([]string, error) {
var insertColNames []string
colToIdx := make(map[string]int)
for i, col := range tableDef.Cols {
colToIdx[strings.ToLower(col.Name)] = i
}
if stmt.Columns == nil {
if astCols == nil {
for _, col := range tableDef.Cols {
if !col.Hidden {
insertColNames = append(insertColNames, col.Name)
}
}
} else {
for _, column := range stmt.Columns {
for _, column := range astCols {
colName := strings.ToLower(string(column))
idx, ok := colToIdx[colName]
if !ok {
Expand All @@ -701,7 +694,7 @@ func (builder *QueryBuilder) getInsertColsFromStmt(stmt *tree.Insert, tableDef *
return insertColNames, nil
}

func (builder *QueryBuilder) initInsertStmt(bindCtx *BindContext, stmt *tree.Insert, objRef *plan.ObjectRef, tableDef *plan.TableDef) (int32, map[string]int32, []bool, error) {
func (builder *QueryBuilder) initInsertStmt(bindCtx *BindContext, astRows *tree.Select, astCols tree.IdentifierList, objRef *plan.ObjectRef, tableDef *plan.TableDef) (int32, map[string]int32, []bool, error) {
var (
lastNodeID int32
err error
Expand All @@ -711,12 +704,12 @@ func (builder *QueryBuilder) initInsertStmt(bindCtx *BindContext, stmt *tree.Ins
var insertColumns []string

//var ifInsertFromUniqueColMap map[string]bool
if insertColumns, err = builder.getInsertColsFromStmt(stmt, tableDef); err != nil {
if insertColumns, err = builder.getInsertColsFromStmt(astCols, tableDef); err != nil {
return 0, nil, nil, err
}

var astSelect *tree.Select
switch selectImpl := stmt.Rows.Select.(type) {
switch selectImpl := astRows.Select.(type) {
// rewrite 'insert into tbl values (1,1)' to 'insert into tbl select * from (values row(1,1))'
case *tree.ValuesClause:
isAllDefault := false
Expand All @@ -741,7 +734,7 @@ func (builder *QueryBuilder) initInsertStmt(bindCtx *BindContext, stmt *tree.Ins
// example1:insert into a values ();
// but it does not work at the case:
// insert into a(a) values (); insert into a values (0),();
if isAllDefault && stmt.Columns != nil {
if isAllDefault && astCols != nil {
return 0, nil, nil, moerr.NewInvalidInput(builder.GetContext(), "insert values does not match the number of columns")
}
lastNodeID, err = builder.buildValueScan(isAllDefault, bindCtx, tableDef, selectImpl, insertColumns)
Expand All @@ -750,7 +743,7 @@ func (builder *QueryBuilder) initInsertStmt(bindCtx *BindContext, stmt *tree.Ins
}

case *tree.SelectClause:
astSelect = stmt.Rows
astSelect = astRows

subCtx := NewBindContext(builder, bindCtx)
lastNodeID, err = builder.bindSelect(astSelect, subCtx, false)
Expand Down
Loading
Loading