-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 string INSERT
#15201
Changes from all commits
f6d2e9d
fb1cae3
39a6729
372a9b4
ebc5eb4
0a54117
bde399e
04490f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import ( | ||
"bytes" | ||
"math" | ||
|
||
"vitess.io/vitess/go/mysql/collations" | ||
"vitess.io/vitess/go/mysql/collations/charset" | ||
|
@@ -29,6 +30,11 @@ | |
) | ||
|
||
type ( | ||
builtinInsert struct { | ||
CallExpr | ||
collate collations.ID | ||
} | ||
|
||
builtinChangeCase struct { | ||
CallExpr | ||
upcase bool | ||
|
@@ -106,6 +112,7 @@ | |
} | ||
) | ||
|
||
var _ IR = (*builtinInsert)(nil) | ||
var _ IR = (*builtinChangeCase)(nil) | ||
var _ IR = (*builtinCharLength)(nil) | ||
var _ IR = (*builtinLength)(nil) | ||
|
@@ -120,6 +127,122 @@ | |
var _ IR = (*builtinPad)(nil) | ||
var _ IR = (*builtinTrim)(nil) | ||
|
||
func insert(str, newstr *evalBytes, pos, l int) []byte { | ||
pos-- | ||
|
||
cs := colldata.Lookup(str.col.Collation).Charset() | ||
strLen := charset.Length(cs, str.bytes) | ||
|
||
if pos < 0 || strLen <= pos { | ||
return str.bytes | ||
} | ||
if l < 0 { | ||
l = strLen | ||
} | ||
|
||
front := charset.Slice(cs, str.bytes, 0, pos) | ||
var back []byte | ||
if pos <= math.MaxInt-l && pos+l < strLen { | ||
back = charset.Slice(cs, str.bytes, pos+l, strLen) | ||
} | ||
|
||
res := make([]byte, len(front)+len(newstr.bytes)+len(back)) | ||
|
||
copy(res[:len(front)], front) | ||
copy(res[len(front):], newstr.bytes) | ||
copy(res[len(front)+len(newstr.bytes):], back) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vmg Do you think it's worth extending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make for a more awkward API, don't you think? Right now Furthermore, if the With the current non-allocating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah, good point. It's already non-allocating so it doesn't really help in any way. |
||
|
||
return res | ||
} | ||
|
||
func (call *builtinInsert) eval(env *ExpressionEnv) (eval, error) { | ||
args, err := call.args(env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if args[0] == nil || args[1] == nil || args[2] == nil || args[3] == nil { | ||
return nil, nil | ||
} | ||
beingnoble03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
str, ok := args[0].(*evalBytes) | ||
if !ok { | ||
str, err = evalToVarchar(args[0], call.collate, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
pos := evalToInt64(args[1]).i | ||
l := evalToInt64(args[2]).i | ||
|
||
newstr, err := evalToVarchar(args[3], str.col.Collation, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := insert(str, newstr, int(pos), int(l)) | ||
if !validMaxLength(int64(len(res)), 1) { | ||
return nil, nil | ||
} | ||
return newEvalText(res, str.col), nil | ||
} | ||
|
||
func (call *builtinInsert) compile(c *compiler) (ctype, error) { | ||
str, err := call.Arguments[0].compile(c) | ||
if err != nil { | ||
return ctype{}, err | ||
} | ||
|
||
pos, err := call.Arguments[1].compile(c) | ||
if err != nil { | ||
return ctype{}, err | ||
} | ||
|
||
l, err := call.Arguments[2].compile(c) | ||
if err != nil { | ||
return ctype{}, err | ||
} | ||
|
||
newstr, err := call.Arguments[3].compile(c) | ||
if err != nil { | ||
return ctype{}, err | ||
} | ||
|
||
skip := c.compileNullCheck4(str, pos, l, newstr) | ||
|
||
_ = c.compileToInt64(pos, 3) | ||
_ = c.compileToInt64(l, 2) | ||
|
||
if err != nil { | ||
return ctype{}, nil | ||
} | ||
|
||
col := str.Col | ||
|
||
switch { | ||
case str.isTextual(): | ||
default: | ||
c.asm.Convert_xce(4, sqltypes.VarChar, c.collation) | ||
col = typedCoercionCollation(sqltypes.VarChar, c.collation) | ||
} | ||
|
||
switch { | ||
case newstr.isTextual(): | ||
fromCharset := colldata.Lookup(newstr.Col.Collation).Charset() | ||
toCharset := colldata.Lookup(col.Collation).Charset() | ||
if fromCharset != toCharset && !toCharset.IsSuperset(fromCharset) { | ||
c.asm.Convert_xce(1, sqltypes.VarChar, col.Collation) | ||
} | ||
default: | ||
c.asm.Convert_xce(1, sqltypes.VarChar, col.Collation) | ||
} | ||
|
||
c.asm.Fn_INSERT(col) | ||
c.asm.jumpDestination(skip) | ||
|
||
return ctype{Type: sqltypes.VarChar, Col: col, Flag: flagNullable}, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there cases where this function can return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if the resultant string size is greater than max_allowed_packet, I think I forgot to add the validation for that. Pushed now. 😅 |
||
} | ||
|
||
func (call *builtinChangeCase) eval(env *ExpressionEnv) (eval, error) { | ||
arg, err := call.arg1(env) | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,6 +199,41 @@ var inputStrings = []string{ | |
// "_ucs2 'AabcÅå'", | ||
} | ||
|
||
var insertStrings = []string{ | ||
"NULL", | ||
"\"\"", | ||
"\"a\"", | ||
"\"abc\"", | ||
"1", | ||
"-1", | ||
"0123", | ||
"0xAACC", | ||
"3.1415926", | ||
// MySQL has broken behavior for these inputs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can reference mysql/mysql-server#517 here so we know what this is about in the future too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
// see https://github.com/mysql/mysql-server/pull/517 | ||
// "\"Å å\"", | ||
// "\"中文测试\"", | ||
// "\"日本語テスト\"", | ||
// "\"한국어 시험\"", | ||
// "\"😊😂🤢\"", | ||
// "_utf8mb4 'abcABCÅå'", | ||
"DATE '2022-10-11'", | ||
"TIME '11:02:23'", | ||
"'123'", | ||
"9223372036854775807", | ||
"-9223372036854775808", | ||
"999999999999999999999999", | ||
"-999999999999999999999999", | ||
"_binary 'Müller' ", | ||
"_latin1 0xFF", | ||
// TODO: support other multibyte encodings | ||
// "_dec8 'ÒòÅå'", | ||
// "_utf8mb3 'abcABCÅå'", | ||
// "_utf16 'AabcÅå'", | ||
// "_utf32 'AabcÅå'", | ||
// "_ucs2 'AabcÅå'", | ||
} | ||
|
||
var inputConversionTypes = []string{ | ||
"BINARY", "BINARY(1)", "BINARY(0)", "BINARY(16)", "BINARY(-1)", | ||
"CHAR", "CHAR(1)", "CHAR(0)", "CHAR(16)", "CHAR(-1)", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pos <= math.MaxInt-l
can be used here to check overflow, as both pos and l are positive (checks added above).