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.

93 changes: 93 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,99 @@ func (asm *assembler) Fn_BIT_LENGTH() {
}, "FN BIT_LENGTH VARCHAR(SP-1)")
}

func (asm *assembler) Fn_FIELD(args int, containsOnlyString, containsOnlyInt64 bool) {
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
}

if containsOnlyInt64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind the compiler is that we can emit efficient code once types are known. It looks like these are compile time known values, so we should split this up into separate Fn_FIELD functions. Like maybe Fn_FIELD_i, Fn_FIELD_s etc.

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
}

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
}
}
} else if containsOnlyString {
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)

// Compare target and current string
if len(tar.bytes) == len(str.bytes) {
eq := true
for i, b := range tar.bytes {
if str.bytes[i] != b {
eq = false
break
}
}

if eq {
env.vm.stack[env.vm.sp-args] = env.vm.arena.newEvalInt64(int64(i + 1))
env.vm.sp -= args - 1
return 1
}
}
}
} else {
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 VARCHAR(SP-%d)...VARCHAR(SP-1)", args)
dbussink marked this conversation as resolved.
Show resolved Hide resolved
}

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