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: change SET_VAR behavior for non-verified vars and add some var #47258

Merged
merged 3 commits into from
Sep 25, 2023
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
2 changes: 1 addition & 1 deletion errno/errname.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ var MySQLErrName = map[uint16]*mysql.ErrMessage{
ErrRoleNotGranted: mysql.Message("%s is not granted to %s", nil),
ErrMaxExecTimeExceeded: mysql.Message("Query execution was interrupted, maximum statement execution time exceeded", nil),
ErrLockAcquireFailAndNoWaitSet: mysql.Message("Statement aborted because lock(s) could not be acquired immediately and NOWAIT is set.", nil),
ErrNotHintUpdatable: mysql.Message("Variable '%s' cannot be set using SET_VAR hint.", nil),
ErrNotHintUpdatable: mysql.Message("Variable '%s' might not be affected by SET_VAR hint.", nil),
ErrExistsInHistoryPassword: mysql.Message("Cannot use these credentials for '%s@%s' because they contradict the password history policy.", nil),
ErrInvalidDefaultUTF8MB4Collation: mysql.Message("Invalid default collation %s: utf8mb4_0900_ai_ci or utf8mb4_general_ci or utf8mb4_bin expected", nil),
ErrForeignKeyCannotDropParent: mysql.Message("Cannot drop table '%s' referenced by a foreign key constraint '%s' on table '%s'.", nil),
Expand Down
2 changes: 1 addition & 1 deletion errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,7 @@ Argument %s of GROUPING function is not in GROUP BY

["planner:3637"]
error = '''
Variable '%s' cannot be set using SET_VAR hint.
Variable '%s' might not be affected by SET_VAR hint.
'''

["planner:8006"]
Expand Down
3 changes: 1 addition & 2 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,8 @@ func handleStmtHints(hints []*ast.TableOptimizerHint) (stmtHints stmtctx.StmtHin
warns = append(warns, core.ErrUnresolvedHintName.GenWithStackByArgs(setVarHint.VarName, hint.HintName.String()))
continue
}
if !sysVar.IsHintUpdatable {
if !sysVar.IsHintUpdatableVerfied {
warns = append(warns, core.ErrNotHintUpdatable.GenWithStackByArgs(setVarHint.VarName))
continue
}
// If several hints with the same variable name appear in the same statement, the first one is applied and the others are ignored with a warning
if _, ok := setVars[setVarHint.VarName]; ok {
Expand Down
5 changes: 3 additions & 2 deletions session/test/vars/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ func TestSetVarHint(t *testing.T) {
require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 0)

tk.MustExec("SELECT /*+ SET_VAR(collation_server = 'utf8') */ 1;")
require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 1)
require.EqualError(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings()[0].Err, "[planner:3637]Variable 'collation_server' cannot be set using SET_VAR hint.")
require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 2)
require.EqualError(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings()[0].Err, "[planner:3637]Variable 'collation_server' might not be affected by SET_VAR hint.")
require.EqualError(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings()[1].Err, "[ddl:1273]Unknown collation: 'utf8'")

tk.MustExec("SELECT /*+ SET_VAR(max_size = 1G) */ 1;")
require.Len(t, tk.Session().GetSessionVars().StmtCtx.GetWarnings(), 1)
Expand Down
54 changes: 27 additions & 27 deletions sessionctx/variable/noop.go

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions sessionctx/variable/setvar_affect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package variable

var isHintUpdatable = map[string]struct{}{
var isHintUpdatableVerified = map[string]struct{}{
"tidb_opt_agg_push_down": {},
"tidb_opt_derive_topn": {},
"tidb_opt_broadcast_cartesian_join": {},
Expand Down Expand Up @@ -96,6 +96,13 @@ var isHintUpdatable = map[string]struct{}{
"tidb_opt_fix_control": {},
"tidb_runtime_filter_type": {},
"tidb_runtime_filter_mode": {},
"tidb_session_alias": {},
"tidb_opt_objective": {},
"mpp_exchange_compression_mode": {},
"tidb_allow_fallback_to_tikv": {},
"tiflash_fastscan": {},
"tiflash_fine_grained_shuffle_batch_size": {},
"tiflash_find_grained_shuffle_stream_count": {},
// Variables that is compatible with MySQL.
"cte_max_recursion_depth": {},
"sql_mode": {},
Expand All @@ -104,8 +111,8 @@ var isHintUpdatable = map[string]struct{}{

func setHintUpdatable(vars []*SysVar) {
for _, v := range vars {
if _, ok := isHintUpdatable[v.Name]; ok {
v.IsHintUpdatable = true
if _, ok := isHintUpdatableVerified[v.Name]; ok {
v.IsHintUpdatableVerfied = true
}
}
}
16 changes: 8 additions & 8 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ var defaultSysVars = []*SysVar{
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: DefaultWeekFormat, Value: "0", Type: TypeUnsigned, MinValue: 0, MaxValue: 7},
{Scope: ScopeGlobal | ScopeSession, Name: SQLModeVar, Value: mysql.DefaultSQLMode, IsHintUpdatable: true, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
{Scope: ScopeGlobal | ScopeSession, Name: SQLModeVar, Value: mysql.DefaultSQLMode, IsHintUpdatableVerfied: true, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
// Ensure the SQL mode parses
normalizedValue = mysql.FormatSQLModeStr(normalizedValue)
if _, err := mysql.GetSQLMode(normalizedValue); err != nil {
Expand All @@ -1427,12 +1427,12 @@ var defaultSysVars = []*SysVar{
s.SetStatusFlag(mysql.ServerStatusNoBackslashEscaped, sqlMode.HasNoBackslashEscapesMode())
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: MaxExecutionTime, Value: "0", Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxInt32, IsHintUpdatable: true, SetSession: func(s *SessionVars, val string) error {
{Scope: ScopeGlobal | ScopeSession, Name: MaxExecutionTime, Value: "0", Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxInt32, IsHintUpdatableVerfied: true, SetSession: func(s *SessionVars, val string) error {
timeoutMS := tidbOptPositiveInt32(val, 0)
s.MaxExecutionTime = uint64(timeoutMS)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiKVClientReadTimeout, Value: "0", Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxInt32, IsHintUpdatable: true, SetSession: func(s *SessionVars, val string) error {
{Scope: ScopeGlobal | ScopeSession, Name: TiKVClientReadTimeout, Value: "0", Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxInt32, IsHintUpdatableVerfied: true, SetSession: func(s *SessionVars, val string) error {
timeoutMS := tidbOptPositiveInt32(val, 0)
s.TiKVClientReadTimeout = uint64(timeoutMS)
return nil
Expand All @@ -1456,7 +1456,7 @@ var defaultSysVars = []*SysVar{
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: SQLLogBin, Value: On, Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TimeZone, Value: "SYSTEM", IsHintUpdatable: true, Validation: func(varErrFunctionsNoopImpls *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
{Scope: ScopeGlobal | ScopeSession, Name: TimeZone, Value: "SYSTEM", IsHintUpdatableVerfied: true, Validation: func(varErrFunctionsNoopImpls *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
if strings.EqualFold(normalizedValue, "SYSTEM") {
return "SYSTEM", nil
}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ var defaultSysVars = []*SysVar{
s.LockWaitTimeout = lockWaitSec * 1000
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: GroupConcatMaxLen, Value: "1024", IsHintUpdatable: true, Type: TypeUnsigned, MinValue: 4, MaxValue: math.MaxUint64, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
{Scope: ScopeGlobal | ScopeSession, Name: GroupConcatMaxLen, Value: "1024", IsHintUpdatableVerfied: true, Type: TypeUnsigned, MinValue: 4, MaxValue: math.MaxUint64, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
// https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_group_concat_max_len
// Minimum Value 4
// Maximum Value (64-bit platforms) 18446744073709551615
Expand Down Expand Up @@ -1615,7 +1615,7 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: WindowingUseHighPrecision, Value: On, Type: TypeBool, IsHintUpdatable: true, SetSession: func(s *SessionVars, val string) error {
{Scope: ScopeGlobal | ScopeSession, Name: WindowingUseHighPrecision, Value: On, Type: TypeBool, IsHintUpdatableVerfied: true, SetSession: func(s *SessionVars, val string) error {
s.WindowingUseHighPrecision = TiDBOptOn(val)
return nil
}},
Expand Down Expand Up @@ -1674,7 +1674,7 @@ var defaultSysVars = []*SysVar{
s.SetAllowInSubqToJoinAndAgg(TiDBOptOn(val))
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptPreferRangeScan, Value: BoolToOnOff(DefOptPreferRangeScan), Type: TypeBool, IsHintUpdatable: true, SetSession: func(s *SessionVars, val string) error {
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptPreferRangeScan, Value: BoolToOnOff(DefOptPreferRangeScan), Type: TypeBool, IsHintUpdatableVerfied: true, SetSession: func(s *SessionVars, val string) error {
s.SetAllowPreferRangeScan(TiDBOptOn(val))
return nil
}},
Expand Down Expand Up @@ -2596,7 +2596,7 @@ var defaultSysVars = []*SysVar{
s.EnableMPPSharedCTEExecution = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptFixControl, Value: "", Type: TypeStr, IsHintUpdatable: true,
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptFixControl, Value: "", Type: TypeStr, IsHintUpdatableVerfied: true,
SetGlobal: func(ctx context.Context, vars *SessionVars, val string) error {
// validation logic for setting global
// we don't put this in Validation to avoid repeating the checking logic for setting session.
Expand Down
6 changes: 3 additions & 3 deletions sessionctx/variable/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ type SysVar struct {
SetSession func(*SessionVars, string) error
// SetGlobal is called after validation
SetGlobal func(context.Context, *SessionVars, string) error
// IsHintUpdatable indicate whether it's updatable via SET_VAR() hint (optional)
IsHintUpdatable bool
// IsHintUpdatableVerfied indicate whether we've confirmed that SET_VAR() hint is worked for this hint.
IsHintUpdatableVerfied bool
// Deprecated: Hidden previously meant that the variable still responds to SET but doesn't show up in SHOW VARIABLES
// However, this feature is no longer used. All variables are visble.
Hidden bool
Expand Down Expand Up @@ -639,7 +639,7 @@ func init() {
sysVars = make(map[string]*SysVar)
setHintUpdatable(defaultSysVars)
// Destroy the map after init.
maps.Clear(isHintUpdatable)
maps.Clear(isHintUpdatableVerified)
for _, v := range defaultSysVars {
RegisterSysVar(v)
}
Expand Down