Skip to content

Commit

Permalink
evalengine: Mark UUID() function as non-constant (#14051)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink authored Sep 21, 2023
1 parent 408c687 commit 5d9ee02
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
62 changes: 62 additions & 0 deletions go/vt/vtgate/evalengine/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,65 @@ func TestBindVarLiteral(t *testing.T) {
})
}
}

func TestCompilerNonConstant(t *testing.T) {
var testCases = []struct {
expression string
}{
{
expression: "RANDOM_BYTES(4)",
},
{
expression: "UUID()",
},
}

for _, tc := range testCases {
t.Run(tc.expression, func(t *testing.T) {
expr, err := sqlparser.ParseExpr(tc.expression)
if err != nil {
t.Fatal(err)
}

cfg := &evalengine.Config{
Collation: collations.CollationUtf8mb4ID,
Optimization: evalengine.OptimizationLevelCompile,
}

converted, err := evalengine.Translate(expr, cfg)
if err != nil {
t.Fatal(err)
}

env := evalengine.EmptyExpressionEnv()
var prev string
for i := 0; i < 1000; i++ {
expected, err := env.Evaluate(evalengine.Deoptimize(converted))
if err != nil {
t.Fatal(err)
}
if expected.String() == prev {
t.Fatalf("constant evaluation from eval engine: got %s multiple times", expected.String())
}
prev = expected.String()
}

if cfg.CompilerErr != nil {
t.Fatalf("bad compilation: %v", cfg.CompilerErr)
}

// re-run the same evaluation multiple times to ensure results are always consistent
for i := 0; i < 1000; i++ {
res, err := env.EvaluateVM(converted.(*evalengine.CompiledExpr))
if err != nil {
t.Fatal(err)
}

if res.String() == prev {
t.Fatalf("constant evaluation from eval engine: got %s multiple times", res.String())
}
prev = res.String()
}
})
}
}
4 changes: 4 additions & 0 deletions go/vt/vtgate/evalengine/fn_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ func (call *builtinUUID) compile(c *compiler) (ctype, error) {
return ctype{Type: sqltypes.VarChar, Flag: 0, Col: collationUtf8mb3}, nil
}

func (call *builtinUUID) constant() bool {
return false
}

func (call *builtinUUIDToBin) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if arg == nil || err != nil {
Expand Down

0 comments on commit 5d9ee02

Please sign in to comment.