@@ -1244,7 +1244,7 @@ func (g *gbyResolver) Leave(inNode ast.Node) (ast.Node, bool) {
12441244
12451245func tblInfoFromCol (from ast.ResultSetNode , col * expression.Column ) * model.TableInfo {
12461246 var tableList []* ast.TableName
1247- tableList = extractTableList (from , tableList )
1247+ tableList = extractTableList (from , tableList , true )
12481248 for _ , field := range tableList {
12491249 if field .Name .L == col .TblName .L {
12501250 return field .TableInfo
@@ -2144,7 +2144,7 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {
21442144 }
21452145
21462146 var tableList []* ast.TableName
2147- tableList = extractTableList (sel .From .TableRefs , tableList )
2147+ tableList = extractTableList (sel .From .TableRefs , tableList , false )
21482148 for _ , t := range tableList {
21492149 dbName := t .Schema .L
21502150 if dbName == "" {
@@ -2262,6 +2262,15 @@ func (b *planBuilder) buildUpdateLists(tableList []*ast.TableName, list []*ast.A
22622262 p = np
22632263 newList = append (newList , & expression.Assignment {Col : col , Expr : newExpr })
22642264 }
2265+ for _ , assign := range newList {
2266+ col := assign .Col
2267+
2268+ dbName := col .DBName .L
2269+ if dbName == "" {
2270+ dbName = b .ctx .GetSessionVars ().CurrentDB
2271+ }
2272+ b .visitInfo = appendVisitInfo (b .visitInfo , mysql .UpdatePriv , dbName , col .OrigTblName .L , "" )
2273+ }
22652274 return newList , p , nil
22662275}
22672276
@@ -2363,7 +2372,7 @@ func (b *planBuilder) buildDelete(delete *ast.DeleteStmt) (Plan, error) {
23632372 del .SetSchema (expression .NewSchema ())
23642373
23652374 var tableList []* ast.TableName
2366- tableList = extractTableList (delete .TableRefs .TableRefs , tableList )
2375+ tableList = extractTableList (delete .TableRefs .TableRefs , tableList , true )
23672376
23682377 // Collect visitInfo.
23692378 if delete .Tables != nil {
@@ -2416,14 +2425,16 @@ func (b *planBuilder) buildDelete(delete *ast.DeleteStmt) (Plan, error) {
24162425}
24172426
24182427// extractTableList extracts all the TableNames from node.
2419- func extractTableList (node ast.ResultSetNode , input []* ast.TableName ) []* ast.TableName {
2428+ // If asName is true, extract AsName prior to OrigName.
2429+ // Privilege check should use OrigName, while expression may use AsName.
2430+ func extractTableList (node ast.ResultSetNode , input []* ast.TableName , asName bool ) []* ast.TableName {
24202431 switch x := node .(type ) {
24212432 case * ast.Join :
2422- input = extractTableList (x .Left , input )
2423- input = extractTableList (x .Right , input )
2433+ input = extractTableList (x .Left , input , asName )
2434+ input = extractTableList (x .Right , input , asName )
24242435 case * ast.TableSource :
24252436 if s , ok := x .Source .(* ast.TableName ); ok {
2426- if x .AsName .L != "" {
2437+ if x .AsName .L != "" && asName {
24272438 newTableName := * s
24282439 newTableName .Name = x .AsName
24292440 input = append (input , & newTableName )
0 commit comments