@@ -553,6 +553,9 @@ func (p *preprocessor) checkBindGrammar(originNode, hintedNode ast.StmtNode, def
553
553
tn .TableInfo = tableInfo
554
554
tn .DBInfo = dbInfo
555
555
}
556
+ aliasChecker := & aliasChecker {}
557
+ originNode .Accept (aliasChecker )
558
+ hintedNode .Accept (aliasChecker )
556
559
557
560
originSQL := parser .NormalizeForBinding (utilparser .RestoreWithDefaultDB (originNode , defaultDB , originNode .Text ()))
558
561
hintedSQL := parser .NormalizeForBinding (utilparser .RestoreWithDefaultDB (hintedNode , defaultDB , hintedNode .Text ()))
@@ -1928,3 +1931,59 @@ func (p *preprocessor) skipLockMDL() bool {
1928
1931
// skip lock mdl for ANALYZE statement.
1929
1932
return p .flag & inImportInto > 0 || p .flag & inAnalyze > 0
1930
1933
}
1934
+
1935
+ // aliasChecker is used to check the alias of the table in delete statement.
1936
+ //
1937
+ // for example: delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id
1938
+ // `delete tt1` will be transformed to `delete current_database.t1` by default.
1939
+ // because `tt1` cannot be used as alias in delete statement.
1940
+ // so we have to set `tt1` as alias by aliasChecker.
1941
+ type aliasChecker struct {}
1942
+
1943
+ func (* aliasChecker ) Enter (in ast.Node ) (ast.Node , bool ) {
1944
+ if deleteStmt , ok := in .(* ast.DeleteStmt ); ok {
1945
+ // 1. check the tableRefs of deleteStmt to find the alias
1946
+ var aliases []* model.CIStr
1947
+ if deleteStmt .TableRefs != nil && deleteStmt .TableRefs .TableRefs != nil {
1948
+ tableRefs := deleteStmt .TableRefs .TableRefs
1949
+ if val := getTableRefsAlias (tableRefs .Left ); val != nil {
1950
+ aliases = append (aliases , val )
1951
+ }
1952
+ if val := getTableRefsAlias (tableRefs .Right ); val != nil {
1953
+ aliases = append (aliases , val )
1954
+ }
1955
+ }
1956
+ // 2. check the Tables to tag the alias
1957
+ if deleteStmt .Tables != nil && deleteStmt .Tables .Tables != nil {
1958
+ for _ , table := range deleteStmt .Tables .Tables {
1959
+ if table .Schema .String () != "" {
1960
+ continue
1961
+ }
1962
+ for _ , alias := range aliases {
1963
+ if table .Name .L == alias .L {
1964
+ table .IsAlias = true
1965
+ break
1966
+ }
1967
+ }
1968
+ }
1969
+ }
1970
+ return in , true
1971
+ }
1972
+ return in , false
1973
+ }
1974
+
1975
+ func getTableRefsAlias (tableRefs ast.ResultSetNode ) * model.CIStr {
1976
+ switch v := tableRefs .(type ) {
1977
+ case * ast.Join :
1978
+ if v .Left != nil {
1979
+ return getTableRefsAlias (v .Left )
1980
+ }
1981
+ case * ast.TableSource :
1982
+ return & v .AsName
1983
+ }
1984
+ return nil
1985
+ }
1986
+
1987
+ func (* aliasChecker ) Leave (in ast.Node ) (ast.Node , bool ) {
1988
+ return in , true
1989
+ }
0 commit comments