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

Add new function startsWithUTF8 and endsWithUTF8 #52555

Merged
merged 10 commits into from Aug 10, 2023

Conversation

taiyang-li
Copy link
Contributor

Changelog category (leave one):

  • New Feature

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

Add new function startsWithUTF8 and endsWithUTF8

 :) select startsWithUTF8('中国', '中国') 

SELECT startsWithUTF8('中国', '中国')

Query id: 8a3ade25-f55d-455e-8745-8386fb617d6c

┌─startsWithUTF8('中国', '中国')─┐
│                              1 │
└────────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

 :) select startsWithUTF8('中国', '')

SELECT startsWithUTF8('中国', '')

Query id: f9656d22-d6b9-452e-b1b6-c791f297bc12

┌─startsWithUTF8('中国', '')─┐
│                            1 │
└──────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

 :) 
 :) select startsWithUTF8('中国', '\xe4') 

SELECT startsWithUTF8('中国', '

Query id: 95b06f56-fe4a-40ff-9190-81b1591ef230

┌─startsWithUTF8('中国', '⥩─┐
│                          0 │
└────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

 :) select endsWithUTF8('中国', '\xe4')

SELECT endsWithUTF8('中国', '

Query id: 27b8105a-90cd-4b1f-a95d-ae7b3b8c0ab8

┌─endsWithUTF8('中国', '⥩─┐
│                        0 │
└──────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

 :) select endsWithUTF8('中国', '')

SELECT endsWithUTF8('中国', '')

Query id: 88469adf-3f9b-4203-980c-e77330cff839

┌─endsWithUTF8('中国', '')─┐
│                          0 │
└────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

 :) select endsWithUTF8('中国', '')

SELECT endsWithUTF8('中国', '')

Query id: 29c70be7-b529-43f7-b42e-5e251dfaac86

┌─endsWithUTF8('中国', '')─┐
│                          1 │
└────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 
 

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-feature Pull request with new product feature label Jul 25, 2023
@robot-ch-test-poll4
Copy link
Contributor

robot-ch-test-poll4 commented Jul 25, 2023

This is an automated comment for commit d15ae5e with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🔴 failure

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
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR🟡 pending
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process🟢 success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help🟢 success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub🟢 success
Docs CheckBuilds and tests the documentation🟡 pending
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. Integrational 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🔴 failure
Mergeable Check.There's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS🟢 success
Mergeable CheckChecks if all other necessary checks are successful🔴 failure
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests🔴 failure
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub🟢 success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool🟢 success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed🟢 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

@Avogar Avogar self-assigned this Jul 25, 2023
@Avogar
Copy link
Member

Avogar commented Jul 25, 2023

Can you please share the motivation to have these functions?

@taiyang-li
Copy link
Contributor Author

Can you please share the motivation to have these functions?

Yes of course. When string contains UTF8 characters, current function startsWith and endsWith is not enough to handle this case, because they are processing by bytes instead of characters. So I added startsWithUTF8 and endsWithUTF8 functions, just like other functions like substring and substringUTF8

@taiyang-li
Copy link
Contributor Author

BTW, can you also review this pr #51472, it had been blocked a little long.

@Avogar
Copy link
Member

Avogar commented Jul 25, 2023

BTW, can you also review this pr #51472, it had been blocked a little long.

Let me first ask Yakov in DM to continue review.

UPD: he will check the PR today

@alexey-milovidov
Copy link
Member

I've run your test with startsWith/endsWith, and the only difference to startsWithUTF8/endsWithUTF8 is in the cases where the result is documented as undefined:

milovidov@milovidov-desktop:~/work/ClickHouse$ clickhouse-local -n
ClickHouse local version 23.7.1.1.

milovidov-desktop :) select startsWith('富强民主文明和谐', '富强');
select startsWith('富强民主文明和谐', '\xe5');
select startsWith('富强民主文明和谐', '');

SELECT startsWith('123', '123');
SELECT startsWith('123', '12');
SELECT startsWith('123', '1234');
SELECT startsWith('123', '');

select endsWith('富强民主文明和谐', '和谐');
select endsWith('富强民主文明和谐', '\x90');
select endsWith('富强民主文明和谐', '');

SELECT endsWith('123', '3');
SELECT endsWith('123', '23');
SELECT endsWith('123', '32');
SELECT endsWith('123', '');

SELECT startsWith('富强民主文明和谐', '富强')

Query id: 81b491c8-919c-431a-a71d-9adb50f2fc31

┌─startsWith('富强民主文明和谐', '富强')─┐
│                                      1 │
└────────────────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 


SELECT startsWith('富强民主文明和谐', '�')

Query id: 20b97e86-2a7e-40ae-8114-202afa0ca7c0

┌─startsWith('富强民主文明和谐', '�')─┐
│                                  1 │
└────────────────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT startsWith('富强民主文明和谐', '')

Query id: b1bfc3d7-9924-4ee2-873d-622785fc0b90

┌─startsWith('富强民主文明和谐', '')─┐
│                                  1 │
└────────────────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT startsWith('123', '123')

Query id: c4b67995-ca2a-405a-a6d8-3f85aaf6b974

┌─startsWith('123', '123')─┐
│                        1 │
└──────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT startsWith('123', '12')

Query id: ff96daa9-a8cd-497d-9535-ee9f8f0e2b04

┌─startsWith('123', '12')─┐
│                       1 │
└─────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT startsWith('123', '1234')

Query id: a50ae898-d8d6-45d0-837b-b83706b685f6

┌─startsWith('123', '1234')─┐
│                         0 │
└───────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT startsWith('123', '')

Query id: b86f6905-096a-4334-88fb-69b03661f2a0

┌─startsWith('123', '')─┐
│                     1 │
└───────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('富强民主文明和谐', '和谐')

Query id: cbe60202-4015-4c83-b204-8bc6710f9cf6

┌─endsWith('富强民主文明和谐', '和谐')─┐
│                                    1 │
└──────────────────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('富强民主文明和谐', '�')

Query id: 512e52bd-4706-44af-ae65-b8832bda14ad

┌─endsWith('富强民主文明和谐', '�')─┐
│                                1 │
└──────────────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('富强民主文明和谐', '')

Query id: 57af6c52-c169-4d95-9043-c53497db606d

┌─endsWith('富强民主文明和谐', '')─┐
│                                1 │
└──────────────────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('123', '3')

Query id: 8829b817-b359-4cd5-9ef2-bb3275690150

┌─endsWith('123', '3')─┐
│                    1 │
└──────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('123', '23')

Query id: b8a1b4c8-ad21-4871-ad92-0c749e4b3011

┌─endsWith('123', '23')─┐
│                     1 │
└───────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('123', '32')

Query id: d802bf37-b326-4741-9c45-6748d1bfb2f8

┌─endsWith('123', '32')─┐
│                     0 │
└───────────────────────┘

1 row in set. Elapsed: 0.000 sec. 


SELECT endsWith('123', '')

Query id: d5c42529-6c43-4bb0-a137-266520ff894e

┌─endsWith('123', '')─┐
│                   1 │
└─────────────────────┘

1 row in set. Elapsed: 0.000 sec. 

milovidov-desktop :)

It means no logical difference, and the motivation of these functions remains unclear.

I cannot imagine a case where there will be a difference.

@taiyang-li
Copy link
Contributor Author

taiyang-li commented Jul 26, 2023

startsWith matchs two strings by byte, but startsWithUTF8 match two strings by UTF8 character, that's the difference, which is also the difference between substring and substringUTF8 or any other functions with or without UTF8 suffix

 :) select startsWith('富强民主文明和谐', '\xe5');

SELECT startsWith('富强民主文明和谐', '

Query id: 5c0b30b1-877b-443b-b427-6fb48ac017d5

┌─startsWith('富强民主文明和谐', '㥩─┐
│                                  1 │
└────────────────────────────────────┘

1 row in set. Elapsed: 0.014 sec. 

 :) select startsWithUTF8('富强民主文明和谐', '\xe5');
SELECT startsWithUTF8('富强民主文明和谐', '

Query id: f522aa30-a747-459e-b5ac-c91169f2a4a5

┌─startsWithUTF8('富强民主文明和谐', '㥩─┐
│                                      0 │
└────────────────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

@alexey-milovidov
Copy link
Member

@taiyang-li but you state in the docs that this particular case is undefined behavior.

@alexey-milovidov
Copy link
Member

alexey-milovidov commented Jul 26, 2023

substring and substringUTF8 differ in the way how they treat positions inside a string; the first uses byte numbers and the second - code point numbers.

@alexey-milovidov
Copy link
Member

But startsWith/endsWith don't deal with positions, and every Unicode code point is unambiguously represented by a sequence of bytes in UTF-8, assuming UTF-8 is valid*, so there is no difference.

* there is "overlong encoding" and "CESU-8" as variants of invalid UTF-8, but we don't care.

@taiyang-li
Copy link
Contributor Author

taiyang-li commented Jul 26, 2023

@alexey-milovidov I see. What I originally want to solve is below issue. I expect startsWithUTF8 match two strings by characters instead of bytes. The assumptions in the doc are wrong. I'll fix it.

select startsWith('富强民主文明和谐', '\xe5'); 

┌─startsWith('富强民主文明和谐', '㥩─┐
│                                  1 │
└────────────────────────────────────┘

@taiyang-li
Copy link
Contributor Author

taiyang-li commented Aug 9, 2023

@Avogar I don't know how to solve those failed tests, do you have any ideas?

@Avogar Avogar merged commit 14aad35 into ClickHouse:master Aug 10, 2023
217 of 279 checks passed
@Avogar
Copy link
Member

Avogar commented Aug 10, 2023

Let's merge it, failed tests is not related and I will investigate it later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-feature Pull request with new product feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants