-
Notifications
You must be signed in to change notification settings - Fork 109
sql/expression: implement substring function #55
Conversation
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.
Can you add a test using that function on engine_test.go
please?
41bb530
to
454063e
Compare
Done @ajnavarro |
454063e
to
49f8464
Compare
sql/expression/function.go
Outdated
return nil, fmt.Errorf("substring: invalid length %d", length) | ||
} | ||
|
||
if startIdx < 0 { |
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.
Take into account that:
- The first position in string is 1.
- If start_position is a positive number, then the SUBSTRING function starts from the beginning of the string.
- If start_position is a negative number, then the SUBSTRING function starts from the end of the string and counts backwards. Negative values for start_position was introduced in MySQL 4.1.
- If start_position is bigger than the string length, the result is NULL
- If start_position is 0, the result is NULL
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.
According to the link you provided:
If start position is 0, the result is ""
if start_position is bigger than string length result is ""
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 the UI is wrong. Check with: SELECT SUBSTRING("SQL Tutorial", 5, 3) AS ExtractString, NULL as test;
the output of test
appears to be "" but it is NULL
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.
If you do SELECT NULL is null
returns 1
, and if you do SELECT SUBSTRING("fooo bar", 0) is null
it returns 0
, so in this case is not null what it returns. From what I see in that UI, the only way SUBSTRING returns null is if the string itself is null.
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.
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring
If len is less than 1, the result is the empty string.
sql/expression/function.go
Outdated
startIdx := start.(int64) | ||
runeCount := int64(len(text)) | ||
|
||
if length < 0 { |
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.
In mysql substring implementation length is optional
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.
Also, If length is bigger than the remained characters, no error happens
@erizocosmico You can check all corner cases here: https://www.w3schools.com/sql/trymysql.asp?filename=trysql_func_mysql_substring Do you mind to add tests for them? |
Yes, sure |
Fixes src-d#42 This PR implements the substring(str, start[, length]) function with the same behaviour as its homonym MySQL function. Since Go strings are UTF8, this function does not return a direct substring str[start:start+length], instead returns the substring of runes. That is, "á"[0:1] does not return a partial unicode glyph, but "á" itself. Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
49f8464
to
1fe11f5
Compare
Fixed @ajnavarro |
Fixes #42
This PR implements the substring(str, start[, length]) function with the same rules as its homonym function in MySQL.
Since Go strings are UTF8, this function does not return a direct substring
str[start:start+length]
, instead returns the substring of runes. That is,"á"[0:1]
does not return a partial unicode glyph, but"á"
itself.