Skip to content

Commit 1c18555

Browse files
authored
planner: do not convert update to point get if the expr has sub-query (#47454)
close #47445
1 parent 0c9d228 commit 1c18555

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

pkg/parser/ast/expressions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ type ParamMarkerExpr interface {
981981
SetOrder(int)
982982
}
983983

984-
// ParenthesesExpr is the parentheses expression.
984+
// ParenthesesExpr is the parentheses' expression.
985985
type ParenthesesExpr struct {
986986
exprNode
987987
// Expr is the expression in parentheses.

pkg/planner/core/plan_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,22 @@ func TestIssue40535(t *testing.T) {
755755
require.Empty(t, tk.Session().LastMessage())
756756
}
757757

758+
func TestIssue47445(t *testing.T) {
759+
store, _ := testkit.CreateMockStoreAndDomain(t)
760+
tk := testkit.NewTestKit(t, store)
761+
tk.MustExec("use test;")
762+
tk.MustExec("CREATE TABLE golang1 ( `fcbpdt` CHAR (8) COLLATE utf8_general_ci NOT NULL, `fcbpsq` VARCHAR (20) COLLATE utf8_general_ci NOT NULL, `procst` char (4) COLLATE utf8_general_ci DEFAULT NULL,`cipstx` VARCHAR (105) COLLATE utf8_general_ci DEFAULT NULL, `cipsst` CHAR (4) COLLATE utf8_general_ci DEFAULT NULL, `dyngtg` VARCHAR(4) COLLATE utf8_general_ci DEFAULT NULL, `blncdt` VARCHAR (8) COLLATE utf8_general_ci DEFAULT NULL, PRIMARY KEY ( fcbpdt, fcbpsq ))")
763+
tk.MustExec("insert into golang1 values('20230925','12023092502158016','abc','','','','')")
764+
tk.MustExec("create table golang2 (`sysgrp` varchar(20) NOT NULL,`procst` varchar(8) NOT NULL,`levlid` int(11) NOT NULL,PRIMARY key (procst));")
765+
tk.MustExec("insert into golang2 VALUES('COMMON','ACSC',90)")
766+
tk.MustExec("insert into golang2 VALUES('COMMON','abc',8)")
767+
tk.MustExec("insert into golang2 VALUES('COMMON','CH02',6)")
768+
tk.MustExec("UPDATE golang1 a SET procst =(CASE WHEN ( SELECT levlid FROM golang2 b WHERE b.sysgrp = 'COMMON' AND b.procst = 'ACSC' ) > ( SELECT levlid FROM golang2 c WHERE c.sysgrp = 'COMMON' AND c.procst = a.procst ) THEN 'ACSC' ELSE a.procst END ), cipstx = 'CI010000', cipsst = 'ACSC', dyngtg = 'EAYT', blncdt= '20230925' WHERE fcbpdt = '20230925' AND fcbpsq = '12023092502158016'")
769+
tk.MustQuery("select * from golang1").Check(testkit.Rows("20230925 12023092502158016 ACSC CI010000 ACSC EAYT 20230925"))
770+
tk.MustExec("UPDATE golang1 a SET procst= (SELECT 1 FROM golang2 c WHERE c.procst = a.procst) WHERE fcbpdt = '20230925' AND fcbpsq = '12023092502158016'")
771+
tk.MustQuery("select * from golang1").Check(testkit.Rows("20230925 12023092502158016 1 CI010000 ACSC EAYT 20230925"))
772+
}
773+
758774
func TestExplainValuesStatement(t *testing.T) {
759775
store, _ := testkit.CreateMockStoreAndDomain(t)
760776
tk := testkit.NewTestKit(t, store)

pkg/planner/core/point_get_plan.go

+50-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"sort"
2020
"strconv"
2121
"strings"
22+
"sync"
2223
"unsafe"
2324

2425
"github.com/pingcap/errors"
@@ -1552,13 +1553,57 @@ func findInPairs(colName string, pairs []nameValuePair) int {
15521553
return -1
15531554
}
15541555

1555-
func tryUpdatePointPlan(ctx sessionctx.Context, updateStmt *ast.UpdateStmt) Plan {
1556-
// avoid using the point_get when assignment_list contains the subquery in the UPDATE.
1557-
for _, list := range updateStmt.List {
1558-
if _, ok := list.Expr.(*ast.SubqueryExpr); ok {
1559-
return nil
1556+
// Use cache to avoid allocating memory every time.
1557+
var subQueryCheckerPool = &sync.Pool{New: func() any { return &subQueryChecker{} }}
1558+
1559+
type subQueryChecker struct {
1560+
hasSubQuery bool
1561+
}
1562+
1563+
func (s *subQueryChecker) Enter(in ast.Node) (node ast.Node, skipChildren bool) {
1564+
if s.hasSubQuery {
1565+
return in, true
1566+
}
1567+
1568+
if _, ok := in.(*ast.SubqueryExpr); ok {
1569+
s.hasSubQuery = true
1570+
return in, true
1571+
}
1572+
1573+
return in, false
1574+
}
1575+
1576+
func (s *subQueryChecker) Leave(in ast.Node) (ast.Node, bool) {
1577+
// Before we enter the sub-query, we should keep visiting its children.
1578+
return in, !s.hasSubQuery
1579+
}
1580+
1581+
func isExprHasSubQuery(expr ast.Node) bool {
1582+
checker := subQueryCheckerPool.Get().(*subQueryChecker)
1583+
defer func() {
1584+
// Do not forget to reset the flag.
1585+
checker.hasSubQuery = false
1586+
subQueryCheckerPool.Put(checker)
1587+
}()
1588+
expr.Accept(checker)
1589+
return checker.hasSubQuery
1590+
}
1591+
1592+
func checkIfAssignmentListHasSubQuery(list []*ast.Assignment) bool {
1593+
for _, a := range list {
1594+
if isExprHasSubQuery(a) {
1595+
return true
15601596
}
15611597
}
1598+
return false
1599+
}
1600+
1601+
func tryUpdatePointPlan(ctx sessionctx.Context, updateStmt *ast.UpdateStmt) Plan {
1602+
// Avoid using the point_get when assignment_list contains the sub-query in the UPDATE.
1603+
if checkIfAssignmentListHasSubQuery(updateStmt.List) {
1604+
return nil
1605+
}
1606+
15621607
selStmt := &ast.SelectStmt{
15631608
Fields: &ast.FieldList{},
15641609
From: updateStmt.TableRefs,

0 commit comments

Comments
 (0)