From 258267be4a80f9bfca24e51972a6c4bcbc762aa2 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 15 Nov 2024 11:54:53 +0800 Subject: [PATCH] update dependencies Signed-off-by: Weizhen Wang --- pkg/parser/ast/dml.go | 6 ++-- pkg/planner/core/preprocess.go | 57 +++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/pkg/parser/ast/dml.go b/pkg/parser/ast/dml.go index a52cfd526a5f6..a609313d4fe07 100644 --- a/pkg/parser/ast/dml.go +++ b/pkg/parser/ast/dml.go @@ -294,7 +294,7 @@ func (n *TableName) restoreName(ctx *format.RestoreCtx) { if n.Schema.String() != "" { ctx.WriteName(n.Schema.String()) ctx.WritePlain(".") - } else if ctx.DefaultDB != "" { + } else if ctx.DefaultDB != "" && !n.IsAlias { // Try CTE, for a CTE table name, we shouldn't write the database name. if !ctx.IsCTETableName(n.Name.L) { ctx.WriteName(ctx.DefaultDB) @@ -331,9 +331,7 @@ func (n *TableName) restoreIndexHints(ctx *format.RestoreCtx) error { } func (n *TableName) Restore(ctx *format.RestoreCtx) error { - if !n.IsAlias { - n.restoreName(ctx) - } + n.restoreName(ctx) n.restorePartitions(ctx) if err := n.restoreIndexHints(ctx); err != nil { return err diff --git a/pkg/planner/core/preprocess.go b/pkg/planner/core/preprocess.go index 209f540a7b25f..0d345ba559bba 100644 --- a/pkg/planner/core/preprocess.go +++ b/pkg/planner/core/preprocess.go @@ -571,7 +571,9 @@ func (p *preprocessor) checkBindGrammar(originNode, hintedNode ast.StmtNode, def TableInfo: tableInfo, }) } - + aliasChecker := &aliasChecker{} + originNode.Accept(aliasChecker) + hintedNode.Accept(aliasChecker) originSQL := parser.NormalizeForBinding(utilparser.RestoreWithDefaultDB(originNode, defaultDB, originNode.Text()), false) hintedSQL := parser.NormalizeForBinding(utilparser.RestoreWithDefaultDB(hintedNode, defaultDB, hintedNode.Text()), false) if originSQL != hintedSQL { @@ -1991,3 +1993,56 @@ func (p *preprocessor) skipLockMDL() bool { // skip lock mdl for ANALYZE statement. return p.flag&inImportInto > 0 || p.flag&inAnalyze > 0 } + +type aliasChecker struct { +} + +func (e *aliasChecker) Enter(in ast.Node) (ast.Node, bool) { + if deleteStmt, ok := in.(*ast.DeleteStmt); ok { + // delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id + // you should set tt1 as alias. + + // 1. check the tableRefs of deleteStmt + var aliases []*pmodel.CIStr + if deleteStmt.TableRefs != nil && deleteStmt.TableRefs.TableRefs != nil { + tableRefs := deleteStmt.TableRefs.TableRefs + if val := getTableRefsAlias(tableRefs.Left); val != nil { + aliases = append(aliases, val) + } + if val := getTableRefsAlias(tableRefs.Right); val != nil { + aliases = append(aliases, val) + } + } + // 2. check the Tables to tag the alias + if deleteStmt.Tables != nil && deleteStmt.Tables.Tables != nil { + for _, table := range deleteStmt.Tables.Tables { + if table.Schema.String() != "" { + continue + } + for _, alias := range aliases { + if table.Name.L == alias.L { + table.IsAlias = true + } + } + } + } + return in, true + } + return in, false +} + +func getTableRefsAlias(tableRefs ast.ResultSetNode) *pmodel.CIStr { + switch v := tableRefs.(type) { + case *ast.Join: + if v.Left != nil { + return getTableRefsAlias(v.Left) + } + case *ast.TableSource: + return &v.AsName + } + return nil +} + +func (*aliasChecker) Leave(in ast.Node) (ast.Node, bool) { + return in, true +}