Repository navigation
Support deletion in function translate - #71441
Conversation
|
This is an automated comment for commit d735d0f with description of existing statuses. It's updated for the latest CI running ✅ Click here to open a full report in a separate page Successful checks
|
There was a problem hiding this comment.
Hi! Actually, I cannot understand why we need this logic for the translate function.
As far as I understood, if we have translate(string, 'abc', 'ab'), it will be identical to translate(string, 'ab', 'ab'), right? If yes, why do we need to specify an extra letter If we can just not use this extra letter?
Thank you for reply. |
|
Okay, thank you, now it makes sense for me |
|
@shuai-xu could you please merge with the master? Some tests were broken previously, but now they are fixed. |
There was a problem hiding this comment.
Why not to add ++dst; ++data_size; as two lines after if-statement?
There was a problem hiding this comment.
If *src <= ascii_upper_bound && map[*src] == ascii_upper_bound + 1, which means the from str is valid, but the letter should be translated to empty, the dst should not add 1.
There was a problem hiding this comment.
At least we can do it in the way it was before:
while (src < src_end)
{
if (*src <= ascii_upper_bound && map[*src] != ascii_upper_bound + 1)
{
*dst++ = map[*src];
++data_size;
}
else if (*src > ascii_upper_bound)
{
*dst++ = *src;
++data_size;
}
++src;
}There was a problem hiding this comment.
| ++data_size; | |
| res_offsets[i] = data_size; | |
| res_offsets[i] = ++data_size; |
There was a problem hiding this comment.
Optimization note: this part can be optimized in a way so that if we get into this else statement, we change all the remaining symbols to the blank symbol and exit the for loop. (not obligatory)
There was a problem hiding this comment.
Sorry, I am not sure what do you mean?
There was a problem hiding this comment.
If we have |map_from| > |map_to|, we don't need to check the arguments of map_to again.
| else | |
| { | |
| if (!isASCII(map_from[i])) | |
| throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second argument must be ASCII strings"); | |
| map[map_from[i]] = ascii_upper_bound + 1; | |
| } | |
| else | |
| { | |
| while (i < map_from.size()) | |
| { | |
| if (!isASCII(map_from[i])) | |
| throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second argument must be ASCII strings"); | |
| map[map_from[i]] = ascii_upper_bound + 1; | |
| i++; | |
| } | |
| return; | |
| } |
|
@yariks5s can you help to review this pr again? |
|
Yes, I will, sorry for the delay |
yariks5s
left a comment
There was a problem hiding this comment.
Also, organizational note:
Should we support the case if |to| > |from|?
If not, we don't need some part of logic implemented in this PR (checking overlapping values of map_to)
There was a problem hiding this comment.
If we have |map_from| > |map_to|, we don't need to check the arguments of map_to again.
| else | |
| { | |
| if (!isASCII(map_from[i])) | |
| throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second argument must be ASCII strings"); | |
| map[map_from[i]] = ascii_upper_bound + 1; | |
| } | |
| else | |
| { | |
| while (i < map_from.size()) | |
| { | |
| if (!isASCII(map_from[i])) | |
| throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second argument must be ASCII strings"); | |
| map[map_from[i]] = ascii_upper_bound + 1; | |
| i++; | |
| } | |
| return; | |
| } |
|
Tbh, I have a feeling that we can do #71441 (comment) even better. |
@yariks5s Thank you for review again, I have modified it according to your comments. |
yariks5s
left a comment
There was a problem hiding this comment.
Please leave your thoughts about it if you have some, these changes are not mandatory but will improve the readability of the code, and sorry for the long responses :(
There was a problem hiding this comment.
At least we can do it in the way it was before:
while (src < src_end)
{
if (*src <= ascii_upper_bound && map[*src] != ascii_upper_bound + 1)
{
*dst++ = map[*src];
++data_size;
}
else if (*src > ascii_upper_bound)
{
*dst++ = *src;
++data_size;
}
++src;
}
|
|
@yariks5s Thank you very much , all comments are addressed. |
|
Overall except the issue about the test is good, thank you🙂 |
@yariks5s Thank you, test added now。 |
|
@shuai-xu Thank you, everything is nice |
translate
| iota(map.data(), map.size(), UInt8(0)); | ||
|
|
||
| for (size_t i = 0; i < map_from.size(); ++i) | ||
| size_t min_size = std::min(map_from.size(), map_to.size()); |
There was a problem hiding this comment.
The case that the third argument is longer than the second argument is weird, it should throw an exception: select translate('abc', 'ab', 'abc'). @yariks5s ?
There was a problem hiding this comment.
Yes, you are right, I missed it, will fix it today
Docs: Follow-up to #71441
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Function
translatenow supports character deletion if thefromargument contains more characters than thetoargument. Example:SELECT translate('clickhouse', 'clickhouse', 'CLICK')now returnsCLICK.