Skip to content

Support deletion in function translate - #71441

Merged
yariks5s merged 10 commits into
ClickHouse:masterfrom
bigo-sg:translate
Dec 4, 2024
Merged

yariks5s merged 10 commits into
ClickHouse:masterfrom
bigo-sg:translate

Conversation

@shuai-xu

@shuai-xu shuai-xu commented Nov 5, 2024 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Function translate now supports character deletion if the from argument contains more characters than the to argument. Example: SELECT translate('clickhouse', 'clickhouse', 'CLICK') now returns CLICK.

@yariks5s yariks5s self-assigned this Nov 5, 2024
@yariks5s yariks5s added the can be tested Allows running workflows for external contributors label Nov 6, 2024
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-improvement Pull request with some product improvements label Nov 6, 2024
@robot-ch-test-poll3

robot-ch-test-poll3 commented Nov 6, 2024 •

Copy link
Copy Markdown
Contributor

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
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@yariks5s yariks5s left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@shuai-xu

shuai-xu commented Nov 8, 2024

Copy link
Copy Markdown
Contributor Author

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. translate(string, 'abc', 'ab') is not identical to translate(string, 'ab', 'ab'). For translate(string, 'abc', 'ab') , the letter c in string will be translated to empty. In our production, users may use translate(string, '"', '') to remove the letters.

@yariks5s

yariks5s commented Nov 8, 2024

Copy link
Copy Markdown
Member

Okay, thank you, now it makes sense for me

@yariks5s

yariks5s commented Nov 8, 2024

Copy link
Copy Markdown
Member

@shuai-xu could you please merge with the master? Some tests were broken previously, but now they are fixed.

Comment thread src/Functions/translate.cpp Resolved
Comment thread src/Functions/translate.cpp Outdated
Comment on lines 89 to 96

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to add ++dst; ++data_size; as two lines after if-statement?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/Functions/translate.cpp Outdated
Comment on lines 106 to 107

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
++data_size;
res_offsets[i] = data_size;
res_offsets[i] = ++data_size;

Comment thread src/Functions/translate.cpp Resolved
Comment thread src/Functions/translate.cpp Outdated
Comment on lines 46 to 57

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I am not sure what do you mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have |map_from| > |map_to|, we don't need to check the arguments of map_to again.

Suggested change
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;
}

@shuai-xu

Copy link
Copy Markdown
Contributor Author

@yariks5s can you help to review this pr again?

@yariks5s

Copy link
Copy Markdown
Member

Yes, I will, sorry for the delay

@yariks5s yariks5s left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread src/Functions/translate.cpp Outdated
Comment on lines 46 to 57

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have |map_from| > |map_to|, we don't need to check the arguments of map_to again.

Suggested change
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

Copy link
Copy Markdown
Member

Tbh, I have a feeling that we can do #71441 (comment) even better.

@shuai-xu

shuai-xu commented Nov 22, 2024 •

Copy link
Copy Markdown
Contributor Author

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 yariks5s left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :(

Comment thread src/Functions/translate.cpp Resolved
Comment thread src/Functions/translate.cpp Outdated
Comment on lines 89 to 96

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/Functions/translate.cpp Resolved
@yariks5s

Copy link
Copy Markdown
Member
  • Please update your pull request with master, because we have some tests fixed on master.

@shuai-xu

shuai-xu commented Dec 2, 2024

Copy link
Copy Markdown
Contributor Author

@yariks5s Thank you very much , all comments are addressed.

@yariks5s

yariks5s commented Dec 2, 2024

Copy link
Copy Markdown
Member

Overall except the issue about the test is good, thank you🙂

@shuai-xu

shuai-xu commented Dec 3, 2024

Copy link
Copy Markdown
Contributor Author

Overall except the issue about the test is good, thank you🙂

@yariks5s Thank you, test added now。

@yariks5s
yariks5s added this pull request to the merge queue Dec 4, 2024
@yariks5s

yariks5s commented Dec 4, 2024

Copy link
Copy Markdown
Member

@shuai-xu Thank you, everything is nice

Merged via the queue into ClickHouse:master with commit 3cf4cd0 Dec 4, 2024
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 4, 2024
@shuai-xu
shuai-xu deleted the translate branch December 5, 2024 02:22
@rschu1ze rschu1ze changed the title translate support second arg longer than the third Support deletion in function translate Dec 6, 2024
rschu1ze added a commit to rschu1ze/ClickHouse that referenced this pull request Dec 6, 2024
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());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I missed it, will fix it today

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github-merge-queue Bot pushed a commit that referenced this pull request Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants