Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <[email protected]>
  • Loading branch information
hawkingrei committed Nov 15, 2024
1 parent e4c6ffe commit 258267b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
6 changes: 2 additions & 4 deletions pkg/parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
57 changes: 56 additions & 1 deletion pkg/planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 258267b

Please sign in to comment.