@@ -571,7 +571,9 @@ func (p *preprocessor) checkBindGrammar(originNode, hintedNode ast.StmtNode, def
571
571
TableInfo : tableInfo ,
572
572
})
573
573
}
574
-
574
+ aliasChecker := & aliasChecker {}
575
+ originNode .Accept (aliasChecker )
576
+ hintedNode .Accept (aliasChecker )
575
577
originSQL := parser .NormalizeForBinding (utilparser .RestoreWithDefaultDB (originNode , defaultDB , originNode .Text ()), false )
576
578
hintedSQL := parser .NormalizeForBinding (utilparser .RestoreWithDefaultDB (hintedNode , defaultDB , hintedNode .Text ()), false )
577
579
if originSQL != hintedSQL {
@@ -1991,3 +1993,59 @@ func (p *preprocessor) skipLockMDL() bool {
1991
1993
// skip lock mdl for ANALYZE statement.
1992
1994
return p .flag & inImportInto > 0 || p .flag & inAnalyze > 0
1993
1995
}
1996
+
1997
+ // aliasChecker is used to check the alias of the table in delete statement.
1998
+ //
1999
+ // for example: delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id
2000
+ // `delete tt1` will be transformed to `delete current_database.t1` by default.
2001
+ // because `tt1` cannot be used as alias in delete statement.
2002
+ // so we have to set `tt1` as alias by aliasChecker.
2003
+ type aliasChecker struct {}
2004
+
2005
+ func (* aliasChecker ) Enter (in ast.Node ) (ast.Node , bool ) {
2006
+ if deleteStmt , ok := in .(* ast.DeleteStmt ); ok {
2007
+ // 1. check the tableRefs of deleteStmt to find the alias
2008
+ var aliases []* pmodel.CIStr
2009
+ if deleteStmt .TableRefs != nil && deleteStmt .TableRefs .TableRefs != nil {
2010
+ tableRefs := deleteStmt .TableRefs .TableRefs
2011
+ if val := getTableRefsAlias (tableRefs .Left ); val != nil {
2012
+ aliases = append (aliases , val )
2013
+ }
2014
+ if val := getTableRefsAlias (tableRefs .Right ); val != nil {
2015
+ aliases = append (aliases , val )
2016
+ }
2017
+ }
2018
+ // 2. check the Tables to tag the alias
2019
+ if deleteStmt .Tables != nil && deleteStmt .Tables .Tables != nil {
2020
+ for _ , table := range deleteStmt .Tables .Tables {
2021
+ if table .Schema .String () != "" {
2022
+ continue
2023
+ }
2024
+ for _ , alias := range aliases {
2025
+ if table .Name .L == alias .L {
2026
+ table .IsAlias = true
2027
+ break
2028
+ }
2029
+ }
2030
+ }
2031
+ }
2032
+ return in , true
2033
+ }
2034
+ return in , false
2035
+ }
2036
+
2037
+ func getTableRefsAlias (tableRefs ast.ResultSetNode ) * pmodel.CIStr {
2038
+ switch v := tableRefs .(type ) {
2039
+ case * ast.Join :
2040
+ if v .Left != nil {
2041
+ return getTableRefsAlias (v .Left )
2042
+ }
2043
+ case * ast.TableSource :
2044
+ return & v .AsName
2045
+ }
2046
+ return nil
2047
+ }
2048
+
2049
+ func (* aliasChecker ) Leave (in ast.Node ) (ast.Node , bool ) {
2050
+ return in , true
2051
+ }
0 commit comments