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

support function rpad #2270

Merged
merged 7 commits into from
Dec 19, 2016
Merged

support function rpad #2270

merged 7 commits into from
Dec 19, 2016

Conversation

silentred
Copy link
Contributor

Tried to support the function rpad. It goes alright with make gotest and make server. The rpad query works with my build on mac.

@CLAassistant
Copy link

CLAassistant commented Dec 18, 2016

CLA assistant check
All committers have signed the CLA.

@shenli
Copy link
Member

shenli commented Dec 19, 2016

@silentred Thanks for your PR! Please sign the CLA.

@shenli
Copy link
Member

shenli commented Dec 19, 2016

@silentred Please add typeinfer info in typeinferer.go.
See https://github.com/ngaut/builddatabase/blob/master/tidb/builtin.md

@silentred
Copy link
Contributor Author

weird, I have signed the CLA. Should I close this one and redo a PR?

@tiancaiamao
Copy link
Contributor

no, wait for CI pass, and we'll review it.
it would be merged if two or more reviewer think it's ok.

{"hi", 5, "?", "hi???"},
{"hi", 1, "?", "h"},
{"hi", -1, "?", ""},
{"hi", 1, "", ""},
Copy link
Member

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.

@coocood
Copy link
Member

coocood commented Dec 19, 2016

@silentred
Please add typeinfer on this function, like this PR
#2249

if len(str) >= l {
d.SetString(str[:l])
} else {
dest := str strings.Repeat(padStr, l-len(str))
Copy link
Contributor

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, "", ""},
Copy link
Contributor

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

@silentred
Copy link
Contributor Author

@shenli @tiancaiamao @coocood Thanks for the advises. I will fix these.

{"hi", 1, "", "h"},
{"hi", 5, "", ""},
{"hi", 5, "ab", "hiaba"},
{"hi", 6, "ab", "hiabab"},
Copy link
Member

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)
}

Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Member

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]

Copy link
Contributor Author

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.

@coocood
Copy link
Member

coocood commented Dec 19, 2016

@silentred
Please add a test case in typeinferer_test.go

@coocood
Copy link
Member

coocood commented Dec 19, 2016

LGTM

1 similar comment
@tiancaiamao
Copy link
Contributor

LGTM

@coocood
Copy link
Member

coocood commented Dec 19, 2016

@silentred
Thank you for your contribution!

@coocood coocood merged commit b200cef into pingcap:master Dec 19, 2016
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants