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

evalEngine: Implement ELT and FIELD #15249

Merged
merged 9 commits into from
Apr 1, 2024
Merged
24 changes: 24 additions & 0 deletions go/vt/vtgate/evalengine/cached_size.go

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

161 changes: 161 additions & 0 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,167 @@
}, "FN BIT_LENGTH VARCHAR(SP-1)")
}

func (asm *assembler) Fn_FIELD_i(args int) {
asm.adjustStack(-args + 1)
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-args] == nil {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}

Check warning on line 2356 in go/vt/vtgate/evalengine/compiler_asm.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L2354-L2356

Added lines #L2354 - L2356 were not covered by tests
tar := env.vm.stack[env.vm.sp-args].(*evalInt64)

for i := range args - 1 {
if env.vm.stack[env.vm.sp-args+i+1] == nil {
continue
}

Check warning on line 2363 in go/vt/vtgate/evalengine/compiler_asm.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L2363

Added line #L2363 was not covered by tests
arg := env.vm.stack[env.vm.sp-args+i+1].(*evalInt64)

if tar.i == arg.i {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(int64(i + 1))
env.vm.sp -= args - 1
return 1
}
}

env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}, "FN FIELD INT64(SP-%d)...INT64(SP-1)", args)
}

func (asm *assembler) Fn_FIELD_b(args int, col colldata.Collation) {
asm.adjustStack(-args + 1)
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-args] == nil {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}

Check warning on line 2387 in go/vt/vtgate/evalengine/compiler_asm.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L2385-L2387

Added lines #L2385 - L2387 were not covered by tests
tar := env.vm.stack[env.vm.sp-args].(*evalBytes)

for i := range args - 1 {
if env.vm.stack[env.vm.sp-args+i+1] == nil {
continue
}

str := env.vm.stack[env.vm.sp-args+i+1].(*evalBytes)

// We cannot do these comparison earlier in the compilation,
// because if we convert everything first, we error on cases
// where there is a match. MySQL will do an element for element
// comparison where if there's a match already, it doesn't matter
// if there was an invalid conversion later on.
//
// This means we also must convert here in this compiler function
// and can't eagerly do the conversion.
toCharset := col.Charset()
fromCharset := colldata.Lookup(str.col.Collation).Charset()
if fromCharset != toCharset && !toCharset.IsSuperset(fromCharset) {
str, env.vm.err = evalToVarchar(str, col.ID(), true)
if env.vm.err != nil {
env.vm.stack[env.vm.sp-args] = nil
env.vm.sp -= args - 1
return 1
}
}

// Compare target and current string
if col.Collate(tar.bytes, str.bytes, false) == 0 {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(int64(i + 1))
env.vm.sp -= args - 1
return 1
}
}

env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}, "FN FIELD VARCHAR(SP-%d)...VARCHAR(SP-1)", args)
dbussink marked this conversation as resolved.
Show resolved Hide resolved
}

func (asm *assembler) Fn_FIELD_d(args int) {
asm.adjustStack(-args + 1)
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-args] == nil {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}

Check warning on line 2438 in go/vt/vtgate/evalengine/compiler_asm.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L2436-L2438

Added lines #L2436 - L2438 were not covered by tests
tar := env.vm.stack[env.vm.sp-args].(*evalDecimal)

for i := range args - 1 {
if env.vm.stack[env.vm.sp-args+i+1] == nil {
continue
}

Check warning on line 2445 in go/vt/vtgate/evalengine/compiler_asm.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vtgate/evalengine/compiler_asm.go#L2445

Added line #L2445 was not covered by tests
arg := env.vm.stack[env.vm.sp-args+i+1].(*evalDecimal)

if tar.dec.Equal(arg.dec) {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(int64(i + 1))
env.vm.sp -= args - 1
return 1
}
}

env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}, "FN FIELD DECIMAL(SP-%d)...DECIMAL(SP-1)", args)
}

func (asm *assembler) Fn_FIELD_f(args int) {
asm.adjustStack(-args + 1)
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-args] == nil {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}

tar := env.vm.stack[env.vm.sp-args].(*evalFloat)

for i := range args - 1 {
if env.vm.stack[env.vm.sp-args+i+1] == nil {
continue
}

arg := env.vm.stack[env.vm.sp-args+i+1].(*evalFloat)

if tar.f == arg.f {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(int64(i + 1))
env.vm.sp -= args - 1
return 1
}
}

env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(0)
env.vm.sp -= args - 1
return 1
}, "FN FIELD FLOAT64(SP-%d)...FLOAT64(SP-1)", args)
}

func (asm *assembler) Fn_ELT(args int, tt sqltypes.Type, tc collations.TypedCollation) {
asm.adjustStack(-args + 1)
asm.emit(func(env *ExpressionEnv) int {
i := env.vm.stack[env.vm.sp-args].(*evalInt64)

if i.i < 1 || int(i.i) >= args || env.vm.stack[env.vm.sp-args+int(i.i)] == nil {
env.vm.stack[env.vm.sp-args] = nil
} else {
b := env.vm.stack[env.vm.sp-args+int(i.i)].(*evalBytes)
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalRaw(b.bytes, tt, tc)
}

env.vm.sp -= args - 1
return 1
}, "FN ELT INT64(SP-%d) VARCHAR(SP-%d)...VARCHAR(SP-1)", args, args-1)
}

func (asm *assembler) Fn_INSERT(col collations.TypedCollation) {
asm.adjustStack(-3)

Expand Down
Loading
Loading