Skip to content

Use sysvar Type correctly for automatic validation / docs #35048

@morgo

Description

@morgo

Enhancement

Some system variables either have no type (default: TypeString) or use TypeString when they could use a different builtin type (TypeInt, TypeEnum, TypeBool) that is more suited. Here is what I found from manual inspection:

Variable Name Notes
block_encryption_mode The type is not defined, but if you look in the code it should be TypeEnum. The possible values are "aes-128-ecb","aes-192-ecb","aes-256-ecb","aes-128-cbc","aes-192-cbc","aes-256-cbc","aes-128-ofb","aes-192-ofb","aes-256-ofb","aes-128-cfb","aes-192-cfb","aes-256-cfb"
ddl_slow_threshold The type should be TypeInt with a MinValue of 0, MaxValue needs to be determined.
have_openssl, have_ssl The type should be TypeBool
last_insert_id/identity The type should be TypeInt, but it might also require AllowEmpty: true set.
tidb_build_stats_concurrency Needs to be TypeInt. For MaxValue use MaxConfigurableConcurrency constant
tidb_checksum_table_concurrency Needs to be TypeInt. For MaxValue use MaxConfigurableConcurrency constant
tidb_current_ts Needs to be TypeInt. might also require AllowEmpty: true set.
tidb_ddl_reorg_priority Should be TypeEnum. PossibleValues: PRIORITY_LOW,PRIORITY_NORMAL,PRIORITY_HIGH.
tidb_force_priority Should be TypeEnum. PossibleValues: NO_PRIORITY, LOW_PRIORITY, HIGH_PRIORITY, DELAYED
tidb_opt_write_row_id Should be a TypeBool
tidb_partition_prune_mode Should be TypeEnum. Possible values: static, dynamic, static-only, dynamic-only.

What does "Type" do?

The name is a bit misleading, since Type is mainly used for validation purposes only (see exception). Internally, all values are stored in mysql.global_variables as strings, and the API uses string for values.

(Exception) Type information is also used in SELECT @@varname context, since booleans are expected to return "0" here versus "OFF" in SHOW VARIABLES LIKE 'varname' context. See relevant code here:

// GetNativeValType attempts to convert the val to the approx MySQL non-string type
func (sv *SysVar) GetNativeValType(val string) (types.Datum, byte, uint) {
switch sv.Type {
case TypeUnsigned:
u, err := strconv.ParseUint(val, 10, 64)
if err != nil {
u = 0
}
return types.NewUintDatum(u), mysql.TypeLonglong, mysql.UnsignedFlag
case TypeBool:
optVal := int64(0) // OFF
if TiDBOptOn(val) {
optVal = 1
}
return types.NewIntDatum(optVal), mysql.TypeLong, 0
}
return types.NewStringDatum(val), mysql.TypeVarString, 0
}

Why do this?

The advantage is that the documentation is auto-generated to show Possible Values for enums. Another advantage is that the validation becomes more consistent. For example, tidb_partition_prune_mode appears to be case-sensitive, but normally for ENUM values MySQL is insensitive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueDenotes an issue ready for a new contributor, according to the "help wanted" guidelines.help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.type/enhancementThe issue or PR belongs to an enhancement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions