-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
expression: rewrite builtin function: FIND_IN_SET #4247
Changes from 1 commit
8399f96
5e87b40
877e309
020f33f
58c91dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1999,49 +1999,46 @@ func (c *findInSetFunctionClass) getFunction(args []Expression, ctx context.Cont | |
if err := c.verifyArgs(args); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
sig := &builtinFindInSetSig{newBaseBuiltinFunc(args, ctx)} | ||
bf, err := newBaseBuiltinFuncWithTp(args, ctx, tpInt, tpString, tpString) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
bf.tp.Flen = 3 | ||
sig := &builtinFindInSetSig{baseIntBuiltinFunc{bf}} | ||
return sig.setSelf(sig), nil | ||
} | ||
|
||
type builtinFindInSetSig struct { | ||
baseBuiltinFunc | ||
baseIntBuiltinFunc | ||
} | ||
|
||
// eval evals a builtinFindInSetSig. | ||
// evalInt evals FIND_IN_SET(str,strlist). | ||
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set | ||
// TODO: This function can be optimized by using bit arithmetic when the first argument is | ||
// a constant string and the second is a column of type SET. | ||
func (b *builtinFindInSetSig) eval(row []types.Datum) (d types.Datum, err error) { | ||
args, err := b.evalArgs(row) | ||
if err != nil { | ||
return types.Datum{}, errors.Trace(err) | ||
} | ||
// args[0] -> Str | ||
// args[1] -> StrList | ||
if args[0].IsNull() || args[1].IsNull() { | ||
return | ||
} | ||
func (b *builtinFindInSetSig) evalInt(row []types.Datum) (int64, bool, error) { | ||
sc := b.ctx.GetSessionVars().StmtCtx | ||
|
||
str, err := args[0].ToString() | ||
if err != nil { | ||
return d, errors.Trace(err) | ||
str, isNull, err := b.args[0].EvalString(row, sc) | ||
if isNull || err != nil { | ||
return 0, isNull, errors.Trace(err) | ||
} | ||
strlst, err := args[1].ToString() | ||
if err != nil { | ||
return d, errors.Trace(err) | ||
|
||
strlist, isNull, err := b.args[1].EvalString(row, sc) | ||
if isNull || err != nil { | ||
return 0, isNull, errors.Trace(err) | ||
} | ||
|
||
d.SetInt64(0) | ||
if len(strlst) == 0 { | ||
return | ||
if len(strlist) == 0 { | ||
return 0, false, nil | ||
} | ||
for i, s := range strings.Split(strlst, ",") { | ||
if s == str { | ||
d.SetInt64(int64(i + 1)) | ||
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.
|
||
for i, strInSet := range strings.Split(strlist, ",") { | ||
if str == strInSet { | ||
return int64(i + 1), false, nil | ||
} | ||
} | ||
return | ||
return 0, false, nil | ||
} | ||
|
||
type fieldFunctionClass struct { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -792,6 +792,12 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) { | |
result.Check(testkit.Rows("0 1777777777777777777777 1777777777777777777777 1777777777777777777777")) | ||
result = tk.MustQuery(`select oct(-1.9), oct(1.9), oct(-1), oct(1), oct(-9999999999999999999999999), oct(9999999999999999999999999);`) | ||
result.Check(testkit.Rows("1777777777777777777777 1 1777777777777777777777 1 1777777777777777777777 1777777777777777777777")) | ||
|
||
// for find_in_set | ||
result = tk.MustQuery(`select find_in_set("", ""), find_in_set("", ","), find_in_set("中文", "字符串,中文");`) | ||
result.Check(testkit.Rows("0 1 2")) | ||
result = tk.MustQuery(`select find_in_set(NULL, ""), find_in_set("", NULL), find_in_set(1, "2,3,1");`) | ||
result.Check(testkit.Rows("<nil> <nil> 3")) | ||
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. add MySQL:
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. added |
||
} | ||
|
||
func (s *testIntegrationSuite) TestEncryptionBuiltin(c *C) { | ||
|
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.
why set 3 here?
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.
copy from mysql: "Item_func_find_in_set::fix_length_and_dec()"