diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 21b358eb82700..d62a47c0f60cd 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -64,19 +64,19 @@ "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "general": { "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", - "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=", + "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, "generatedRepoSpecs": { - "local_config_apple_cc": { + "local_config_apple_cc_toolchains": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf", + "ruleClassName": "_apple_cc_autoconf_toolchains", "attributes": {} }, - "local_config_apple_cc_toolchains": { + "local_config_apple_cc": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf_toolchains", + "ruleClassName": "_apple_cc_autoconf", "attributes": {} } }, @@ -92,7 +92,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", + "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/parser/ast/dml.go b/parser/ast/dml.go index a573aa5546346..99ab3dd296e51 100644 --- a/parser/ast/dml.go +++ b/parser/ast/dml.go @@ -282,6 +282,11 @@ type TableName struct { TableSample *TableSample // AS OF is used to see the data as it was at a specific point in time. AsOf *AsOfClause + // IsAlias is true if this table name is an alias. + // sometime, we need to distinguish the table name is an alias or not. + // for example ```delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id``` + // ```tt1``` is a alias name. so we need to set IsAlias to true and restore the table name without database name. + IsAlias bool } func (*TableName) resultSet() {} @@ -293,7 +298,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) diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index e33e1a518e186..785ff61a615f3 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -543,7 +543,9 @@ func (p *preprocessor) checkBindGrammar(originNode, hintedNode ast.StmtNode, def tn.TableInfo = tableInfo tn.DBInfo = dbInfo } - + aliasChecker := &aliasChecker{} + originNode.Accept(aliasChecker) + hintedNode.Accept(aliasChecker) originSQL := parser.Normalize(utilparser.RestoreWithDefaultDB(originNode, defaultDB, originNode.Text())) hintedSQL := parser.Normalize(utilparser.RestoreWithDefaultDB(hintedNode, defaultDB, hintedNode.Text())) if originSQL != hintedSQL { @@ -1937,3 +1939,59 @@ func (p *preprocessor) skipLockMDL() bool { // skip lock mdl for ANALYZE statement. return p.flag&inAnalyze > 0 } + +// aliasChecker is used to check the alias of the table in delete statement. +// +// for example: delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id +// `delete tt1` will be transformed to `delete current_database.t1` by default. +// because `tt1` cannot be used as alias in delete statement. +// so we have to set `tt1` as alias by aliasChecker. +type aliasChecker struct{} + +func (*aliasChecker) Enter(in ast.Node) (ast.Node, bool) { + if deleteStmt, ok := in.(*ast.DeleteStmt); ok { + // 1. check the tableRefs of deleteStmt to find the alias + var aliases []*model.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 + break + } + } + } + } + return in, true + } + return in, false +} + +func getTableRefsAlias(tableRefs ast.ResultSetNode) *model.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 +} diff --git a/planner/core/preprocess_test.go b/planner/core/preprocess_test.go index 6d6ebb1b8bf49..eecac4b957f7f 100644 --- a/planner/core/preprocess_test.go +++ b/planner/core/preprocess_test.go @@ -425,3 +425,14 @@ func TestPreprocessCTE(t *testing.T) { require.Equal(t, tc.after, rs.String()) } } + +func TestPreprocessDeleteFromWithAlias(t *testing.T) { + // https://github.com/pingcap/tidb/issues/56726 + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t1(id int);") + tk.MustExec(" create table t2(id int);") + tk.MustExec("delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id;") + tk.MustExec("create global binding for delete tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id using delete /*+ MAX_EXECUTION_TIME(10)*/ tt1 from t1 tt1,(select max(id) id from t2)tt2 where tt1.id<=tt2.id;") +} diff --git a/tools/check/check-bazel-prepare.sh b/tools/check/check-bazel-prepare.sh index 2a8d97ebdc767..21e44e1352cd5 100755 --- a/tools/check/check-bazel-prepare.sh +++ b/tools/check/check-bazel-prepare.sh @@ -19,7 +19,7 @@ # -o pipefail: sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, # or to zero if all commands of the pipeline exit successfully. set -euo pipefail - +rm -rf /home/jenkins/.cache/bazel/_bazel_jenkins/install/a09dbb90c658248f08f9aa0eba11997d before_checksum=`find . -type f \( -name '*.bazel' -o -name '*.bzl' \) -exec md5sum {} \;| sort -k 2` make bazel_prepare after_checksum=`find . -type f \( -name '*.bazel' -o -name '*.bzl' \) -exec md5sum {} \;| sort -k 2`