-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
support function rpad #2270
support function rpad #2270
Conversation
@silentred Thanks for your PR! Please sign the CLA. |
@silentred Please add typeinfer info in typeinferer.go. |
weird, I have signed the CLA. Should I close this one and redo a PR? |
no, wait for CI pass, and we'll review it. |
{"hi", 5, "?", "hi???"}, | ||
{"hi", 1, "?", "h"}, | ||
{"hi", -1, "?", ""}, | ||
{"hi", 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.
Have you run this test case in MySQL?
The result of rpad("hi", 1, "")
is h
.
@silentred |
if len(str) >= l { | ||
d.SetString(str[:l]) | ||
} else { | ||
dest := str strings.Repeat(padStr, l-len(str)) |
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.
I think it's not strings.Repeat(padStr, l-len(str))
, padStr may not just one char, the total length after Rpad
is l:
mysql> SELECT RPAD('h',5,'ab');
------------------
| RPAD('h',5,'ab') |
------------------
| habab |
------------------
1 row in set (0.00 sec)
{"hi", 5, "?", "hi???"}, | ||
{"hi", 1, "?", "h"}, | ||
{"hi", -1, "?", ""}, | ||
{"hi", 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.
please add case for padStr not one char
@shenli @tiancaiamao @coocood Thanks for the advises. I will fix these. |
{"hi", 1, "", "h"}, | ||
{"hi", 5, "", ""}, | ||
{"hi", 5, "ab", "hiaba"}, | ||
{"hi", 6, "ab", "hiabab"}, |
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.
The test can't distinguish null and empty string.
rpad("hi", 0, "?")
is empty string, not NULL.
if err != nil { | ||
return d, errors.Trace(err) | ||
} | ||
|
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.
IMHO, the following logic too much complex than it should be, and the loop is hard to read.
Here is a simpler example:
func test(str string, length int, pad string) {
for len(str) < length {
str = str pad
}
return str[:length]
}
just check pad != ""
before this logic.
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.
Thanks, it is simpler indeed. I worried that str = str pad
would allocate memory every time, so I chose pre-allocated []byte.
} | ||
|
||
tailLen := l - len(str) | ||
padStrBytes := []byte(padStr) |
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.
How about
repeatCount := tailLen/len(padStr) 1
dest := str strings.Repeat(padStr, repeatCount)
dest = dest[:l]
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.
Looks better. I will change.
@silentred |
LGTM |
1 similar comment
LGTM |
@silentred |
Tried to support the function rpad. It goes alright with
make gotest
andmake server
. The rpad query works with my build on mac.