Skip to content

Commit

Permalink
expression: label more expressions as thread-safe and add more test c…
Browse files Browse the repository at this point in the history
…ases (#57171)

ref #54057
  • Loading branch information
qw4990 authored Nov 6, 2024
1 parent 0e8c8da commit 7ef4f02
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 61 deletions.
24 changes: 24 additions & 0 deletions pkg/expression/builtin_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ func (c *isTrueOrFalseFunctionClass) getFunction(ctx BuildContext, args []Expres
type builtinRealIsTrueSig struct {
baseBuiltinFunc
keepNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinRealIsTrueSig) Clone() builtinFunc {
Expand All @@ -593,6 +597,10 @@ func (b *builtinRealIsTrueSig) evalInt(ctx EvalContext, row chunk.Row) (int64, b
type builtinDecimalIsTrueSig struct {
baseBuiltinFunc
keepNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinDecimalIsTrueSig) Clone() builtinFunc {
Expand All @@ -618,6 +626,10 @@ func (b *builtinDecimalIsTrueSig) evalInt(ctx EvalContext, row chunk.Row) (int64
type builtinIntIsTrueSig struct {
baseBuiltinFunc
keepNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinIntIsTrueSig) Clone() builtinFunc {
Expand Down Expand Up @@ -668,6 +680,10 @@ func (b *builtinVectorFloat32IsTrueSig) evalInt(ctx EvalContext, row chunk.Row)
type builtinRealIsFalseSig struct {
baseBuiltinFunc
keepNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinRealIsFalseSig) Clone() builtinFunc {
Expand All @@ -693,6 +709,10 @@ func (b *builtinRealIsFalseSig) evalInt(ctx EvalContext, row chunk.Row) (int64,
type builtinDecimalIsFalseSig struct {
baseBuiltinFunc
keepNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinDecimalIsFalseSig) Clone() builtinFunc {
Expand All @@ -718,6 +738,10 @@ func (b *builtinDecimalIsFalseSig) evalInt(ctx EvalContext, row chunk.Row) (int6
type builtinIntIsFalseSig struct {
baseBuiltinFunc
keepNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinIntIsFalseSig) Clone() builtinFunc {
Expand Down
28 changes: 28 additions & 0 deletions pkg/expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,21 @@ type baseInSig struct {
// It works with builtinInXXXSig.hashset to accelerate 'eval'.
nonConstArgsIdx []int
hasNull bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

// builtinInIntSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInIntSig struct {
baseInSig
// the bool value in the map is used to identify whether the constant stored in key is signed or unsigned
hashSet map[int64]bool

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinInIntSig) buildHashMapForConstArgs(ctx BuildContext) error {
Expand Down Expand Up @@ -290,6 +298,10 @@ func (b *builtinInIntSig) evalInt(ctx EvalContext, row chunk.Row) (int64, bool,
type builtinInStringSig struct {
baseInSig
hashSet set.StringSet

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinInStringSig) buildHashMapForConstArgs(ctx BuildContext) error {
Expand Down Expand Up @@ -364,6 +376,10 @@ func (b *builtinInStringSig) evalInt(ctx EvalContext, row chunk.Row) (int64, boo
type builtinInRealSig struct {
baseInSig
hashSet set.Float64Set

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinInRealSig) buildHashMapForConstArgs(ctx BuildContext) error {
Expand Down Expand Up @@ -435,6 +451,10 @@ func (b *builtinInRealSig) evalInt(ctx EvalContext, row chunk.Row) (int64, bool,
type builtinInDecimalSig struct {
baseInSig
hashSet set.StringSet

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinInDecimalSig) buildHashMapForConstArgs(ctx BuildContext) error {
Expand Down Expand Up @@ -515,6 +535,10 @@ func (b *builtinInDecimalSig) evalInt(ctx EvalContext, row chunk.Row) (int64, bo
type builtinInTimeSig struct {
baseInSig
hashSet map[types.CoreTime]struct{}

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinInTimeSig) buildHashMapForConstArgs(ctx BuildContext) error {
Expand Down Expand Up @@ -586,6 +610,10 @@ func (b *builtinInTimeSig) evalInt(ctx EvalContext, row chunk.Row) (int64, bool,
type builtinInDurationSig struct {
baseInSig
hashSet map[time.Duration]struct{}

// NOTE: Any new fields added here must be thread-safe or immutable during execution,
// as this expression may be shared across sessions.
// If a field does not meet these requirements, set SafeToShareAcrossSession to false.
}

func (b *builtinInDurationSig) buildHashMapForConstArgs(ctx BuildContext) error {
Expand Down
60 changes: 60 additions & 0 deletions pkg/expression/builtin_threadsafe_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 0 additions & 60 deletions pkg/expression/builtin_threadunsafe_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pkg/expression/generator/builtin_threadsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ import (
"strings"
)

var (
specialSafeFuncs = map[string]struct{}{
"builtinInIntSig": {},
"builtinInStringSig": {},
"builtinInRealSig": {},
"builtinInDecimalSig": {},
"builtinInTimeSig": {},
"builtinInDurationSig": {},
"builtinRealIsTrueSig": {},
"builtinDecimalIsTrueSig": {},
"builtinIntIsTrueSig": {},
"builtinRealIsFalseSig": {},
"builtinDecimalIsFalseSig": {},
"builtinIntIsFalseSig": {},
// NOTE: please make sure there are test cases for all functions here.
}
)

func collectThreadSafeBuiltinFuncs(file string) (safeFuncNames, unsafeFuncNames []string) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, 0)
Expand All @@ -54,6 +72,10 @@ func collectThreadSafeBuiltinFuncs(file string) (safeFuncNames, unsafeFuncNames
return true
}
allFuncNames = append(allFuncNames, typeName)
if _, ok := specialSafeFuncs[typeName]; ok {
safeFuncNames = append(safeFuncNames, typeName)
return true
}
if len(structType.Fields.List) != 1 { // this structure only has 1 field
return true
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/core/casetest/instanceplancache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ go_test(
name = "instanceplancache_test",
timeout = "short",
srcs = [
"builtin_func_test.go",
"concurrency_test.go",
"concurrency_tpcc_test.go",
"dml_test.go",
"main_test.go",
"others_test.go",
],
flaky = True,
shard_count = 20,
shard_count = 28,
deps = [
"//pkg/parser/auth",
"//pkg/testkit",
Expand Down
Loading

0 comments on commit 7ef4f02

Please sign in to comment.