Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: move checks for noop functions in select statements to preprocess #53279

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4039,16 +4039,7 @@ func (b *PlanBuilder) buildSelect(ctx context.Context, sel *ast.SelectStmt) (p b
return nil, plannererrors.ErrCTERecursiveForbidsAggregation.FastGenByArgs(b.genCTETableNameForError())
}
}
noopFuncsMode := b.ctx.GetSessionVars().NoopFuncsMode
if sel.SelectStmtOpts != nil {
if sel.SelectStmtOpts.CalcFoundRows && noopFuncsMode != variable.OnInt {
err = expression.ErrFunctionsNoopImpl.GenWithStackByArgs("SQL_CALC_FOUND_ROWS")
if noopFuncsMode == variable.OffInt {
return nil, err
}
// NoopFuncsMode is Warn, append an error
b.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
}
origin := b.inStraightJoin
b.inStraightJoin = sel.SelectStmtOpts.StraightJoin
defer func() { b.inStraightJoin = origin }()
Expand Down Expand Up @@ -4188,14 +4179,6 @@ func (b *PlanBuilder) buildSelect(ctx context.Context, sel *ast.SelectStmt) (p b
}
l := sel.LockInfo
if l != nil && l.LockType != ast.SelectLockNone {
if l.LockType == ast.SelectLockForShare && noopFuncsMode != variable.OnInt {
err = expression.ErrFunctionsNoopImpl.GenWithStackByArgs("LOCK IN SHARE MODE")
if noopFuncsMode == variable.OffInt {
return nil, err
}
// NoopFuncsMode is Warn, append an error
b.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
}
for _, tName := range l.Tables {
// CTE has no *model.HintedTable, we need to skip it.
if tName.TableInfo == nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
if node.With != nil {
p.preprocessWith.cteStack = append(p.preprocessWith.cteStack, node.With.CTEs)
}
p.checkSelectNoopFuncs(node)
case *ast.SetOprStmt:
if node.With != nil {
p.preprocessWith.cteStack = append(p.preprocessWith.cteStack, node.With.CTEs)
Expand Down Expand Up @@ -1151,6 +1152,31 @@ func (p *preprocessor) checkCreateIndexGrammar(stmt *ast.CreateIndexStmt) {
p.err = checkIndexInfo(stmt.IndexName, stmt.IndexPartSpecifications)
}

func (p *preprocessor) checkSelectNoopFuncs(stmt *ast.SelectStmt) {
noopFuncsMode := p.sctx.GetSessionVars().NoopFuncsMode
if noopFuncsMode == variable.OnInt {
return
}
if stmt.SelectStmtOpts != nil && stmt.SelectStmtOpts.CalcFoundRows {
err := expression.ErrFunctionsNoopImpl.GenWithStackByArgs("SQL_CALC_FOUND_ROWS")
if noopFuncsMode == variable.OffInt {
p.err = err
return
}
// NoopFuncsMode is Warn, append an error
p.sctx.GetSessionVars().StmtCtx.AppendWarning(err)
}
if stmt.LockInfo != nil && stmt.LockInfo.LockType == ast.SelectLockForShare {
err := expression.ErrFunctionsNoopImpl.GenWithStackByArgs("LOCK IN SHARE MODE")
if noopFuncsMode == variable.OffInt {
p.err = err
return
}
// NoopFuncsMode is Warn, append an error
p.sctx.GetSessionVars().StmtCtx.AppendWarning(err)
}
}

func (p *preprocessor) checkGroupBy(stmt *ast.GroupByClause) {
noopFuncsMode := p.sctx.GetSessionVars().NoopFuncsMode
for _, item := range stmt.Items {
Expand Down
41 changes: 41 additions & 0 deletions tests/integrationtest/r/expression/noop_functions.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
set tidb_enable_non_prepared_plan_cache=0;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY);
INSERT INTO t1 VALUES (1),(2),(3);
SET @@tidb_enable_noop_functions='ON';
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1;
a
1
SELECT * FROM t1 LOCK IN SHARE MODE;
a
1
2
3
SELECT * FROM t1 WHERE a=1 LOCK IN SHARE MODE;
a
1
SET @@tidb_enable_noop_functions='WARN';
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1;
a
1
Level Code Message
Warning 1235 function SQL_CALC_FOUND_ROWS has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
SELECT * FROM t1 LOCK IN SHARE MODE;
a
1
2
3
Level Code Message
Warning 1235 function LOCK IN SHARE MODE has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
SELECT * FROM t1 WHERE a=1 LOCK IN SHARE MODE;
a
1
Level Code Message
Warning 1235 function LOCK IN SHARE MODE has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
SET @@tidb_enable_noop_functions='OFF';
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1;
Error 1235 (42000): function SQL_CALC_FOUND_ROWS has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
SELECT * FROM t1 LOCK IN SHARE MODE;
Error 1235 (42000): function LOCK IN SHARE MODE has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
SELECT * FROM t1 WHERE a=1 LOCK IN SHARE MODE;
Error 1235 (42000): function LOCK IN SHARE MODE has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
26 changes: 26 additions & 0 deletions tests/integrationtest/t/expression/noop_functions.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# variable changes in the test will not affect the plan cache
set tidb_enable_non_prepared_plan_cache=0;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY);
INSERT INTO t1 VALUES (1),(2),(3);

SET @@tidb_enable_noop_functions='ON';
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1;
SELECT * FROM t1 LOCK IN SHARE MODE;
# test the fast path for point-get queries
SELECT * FROM t1 WHERE a=1 LOCK IN SHARE MODE;

SET @@tidb_enable_noop_functions='WARN';
--enable_warnings
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1;
SELECT * FROM t1 LOCK IN SHARE MODE;
SELECT * FROM t1 WHERE a=1 LOCK IN SHARE MODE;
--disable_warnings

SET @@tidb_enable_noop_functions='OFF';
--error 1235
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1;
--error 1235
SELECT * FROM t1 LOCK IN SHARE MODE;
--error 1235
SELECT * FROM t1 WHERE a=1 LOCK IN SHARE MODE;