Skip to content

Throw an error in bitShift* if the shift position is out-of-bounds#65838

Merged
pamarcos merged 11 commits intoClickHouse:masterfrom
pamarcos:bitshift-throw-error-for-negative-shift-positions
Jul 5, 2024
Merged

Throw an error in bitShift* if the shift position is out-of-bounds#65838
pamarcos merged 11 commits intoClickHouse:masterfrom
pamarcos:bitshift-throw-error-for-negative-shift-positions

Conversation

@pamarcos
Copy link
Copy Markdown
Member

@pamarcos pamarcos commented Jun 28, 2024

Fixes #65516

Fix bitShift by throwing an error for out of bounds shift positions.
Negative values or those greater than the bit width of the value are considered out of bounds.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-bugfix Pull request with bugfix, not backported by default label Jun 28, 2024
@robot-ch-test-poll1
Copy link
Copy Markdown
Contributor

robot-ch-test-poll1 commented Jun 28, 2024

This is an automated comment for commit 13f4d0d with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
Bugfix validationChecks that either a new test (functional or integration) or there some changed tests that fail with the binary built on master branch❌ failure
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
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❌ failure
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
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
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

@nikitamikhaylov
Copy link
Copy Markdown
Member

I think it should also throw an exception when we are trying to shift for more than type width.
While this behaviour seems different from the MySQL for example, I'd better have the same behaviour as C++ compiler https://godbolt.org/z/asjGWMnbj

@divanik divanik self-assigned this Jul 1, 2024
@pamarcos pamarcos changed the title Fix bitShift by throwing an error for negative shift positions Fix bitShift by throwing an error for out of bounds shift positions Jul 1, 2024
@pamarcos
Copy link
Copy Markdown
Member Author

pamarcos commented Jul 1, 2024

I think it should also throw an exception when we are trying to shift for more than type width. While this behaviour seems different from the MySQL for example, I'd better have the same behaviour as C++ compiler https://godbolt.org/z/asjGWMnbj

Done! Even though at first I was a bit reluctant of this change because for bitShift I didn't see it causing any harm. However, it's true that most likely if you ever try to shift more positions than the value bit width, you're making a mistake.

One of tests actually uncovered a casting error :)
@pamarcos pamarcos requested a review from divanik July 1, 2024 15:33
@pamarcos pamarcos changed the title Fix bitShift by throwing an error for out of bounds shift positions Throw an error in bitShift* if the shift positions is out-of-bounds Jul 2, 2024
@pamarcos pamarcos changed the title Throw an error in bitShift* if the shift positions is out-of-bounds Throw an error in bitShift* if the shift position is out-of-bounds Jul 2, 2024
Copy link
Copy Markdown
Member

@divanik divanik left a comment

Choose a reason for hiding this comment

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

Please, pay attention to all broken test in CI after resolving the issues and right a comment with a report, why do you think that broken tests have nothing in common with your pr

pamarcos added 3 commits July 2, 2024 14:48
The case for > bit_limit is already covered in previous branch,
so we just need to cover the other case.

This also fixes an overflow that was caused in previous check. e.g.
b > B(word_size * n) if sizeof(B) is 1 byte but n is huge
@pamarcos
Copy link
Copy Markdown
Member Author

pamarcos commented Jul 3, 2024

New settings are not reflected in settings changes history (see new_settings.txt)

Unrelated

Unrelated possible deadlock. The test that hung has 0 references to any bitShift function. I've created #66043 and #66044 to address them

Unrelated test failure, 02247_read_bools_as_numbers_json failed due to an assertion in jemalloc. It's failed quite a few times lately, so I've created an issue for it: #66029

All of the tests reported have no relation with these changes. None use bitShift

Unrelated test error: test_materialized_mysql_database/test.py::test_materialized_database_mysql_date_type_to_date32
I've created #66047

Seems it failed to install the deb package:

dpkg: error: cannot access archive 'package_folder/clickhouse-common-static-dbg_*.deb': No such file or directory

Unrelated to the PR. There's already an issue for it: #65946

Unrelated test 03037_dynamic_merges_1_vertical failed. I've created #66048

@pamarcos pamarcos requested a review from divanik July 3, 2024 09:35
@divanik
Copy link
Copy Markdown
Member

divanik commented Jul 3, 2024

I'm sorry, my request to enumerate all the crashed tests was formulated a bit harsh. Next time just write a comment, that you scanned all the crashed jobs and all of them seems unrelated to your PR or are already flaky

It would be great if you create issues about some unreported crashes from your CI run. Usually these crashes are already reported, but sometimes some new bugs can be found

@pamarcos
Copy link
Copy Markdown
Member Author

pamarcos commented Jul 3, 2024

I'm sorry, my request to enumerate all the crashed tests was formulated a bit harsh. Next time just write a comment, that you scanned all the crashed jobs and all of them seems unrelated to your PR or are already flaky

Ok, no worries. Understood.

It would be great if you create issues about some unreported crashes from your CI run. Usually these crashes are already reported, but sometimes some new bugs can be found

I had already created an issue that it was clearly a flaky test. I've reviewed again the rest and created issues for those missing. I edited my comment with the new issues created.

Let me know if there's anything else missing in the PR that I need to address.

@divanik
Copy link
Copy Markdown
Member

divanik commented Jul 4, 2024

Let me know if there's anything else missing in the PR that I need to address.

Could you take a look at comment about the right part of the expression const UInt256 bit_limit = word_size * n?

@pamarcos
Copy link
Copy Markdown
Member Author

pamarcos commented Jul 4, 2024

Could you take a look at comment about the right part of the expression const UInt256 bit_limit = word_size * n?

@divanik I don't see any comment anywhere 😢 . Are you sure you submitted the review/comment? If so, please share a link to the comment, because there's nothing I can see as of now.

@pamarcos
Copy link
Copy Markdown
Member Author

pamarcos commented Jul 5, 2024

Two more issues created after checking that the some CI check failed: #66116 and #66117

@pamarcos pamarcos added this pull request to the merge queue Jul 5, 2024
Merged via the queue into ClickHouse:master with commit 6848c2e Jul 5, 2024
@pamarcos pamarcos deleted the bitshift-throw-error-for-negative-shift-positions branch July 5, 2024 07:34
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 5, 2024
baibaichen added a commit to apache/gluten that referenced this pull request Jul 6, 2024
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20240706)

* Fix build due to ClickHouse/ClickHouse#63636

* Revert "[GLUTEN-1632][CH]Daily Update Clickhouse Version (20240705) (#6338)"

This reverts commit 4a674e5.

* exclude"shift left", "shift right","shift right unsigned" due to ClickHouse/ClickHouse#65838

---------

Co-authored-by: kyligence-git <[email protected]>
Co-authored-by: Chang Chen <[email protected]>
@UnamedRus
Copy link
Copy Markdown
Contributor

UnamedRus commented Nov 7, 2024

However, it's true that most likely if you ever try to shift more positions than the value bit width, you're making a mistake.

It's "wrong"

CH always try to use least type possible, so in case of literals, you will get different behavior based on typed value itself.
And this behavior is somewhat special, other DBMS usually use much wider type like 64 bit by default

SELECT bitShiftLeft(256, 10);

0

SELECT bitShiftLeft(1, 10)

Received exception from server (version 24.11.1):
Code: 69. DB::Exception: Received from localhost:9000. DB::Exception: The number of shift positions needs to be a non-negative value and less or equal to the bit width of the value to shift: In scope SELECT bitShiftLeft(1, 10). (ARGUMENT_OUT_OF_BOUND)
(query: SELECT bitShiftLeft(1, 10))

https://fiddle.clickhouse.com/9c0486da-172c-4b1b-b145-248eb5a3c51e

@pamarcos
Copy link
Copy Markdown
Member Author

pamarcos commented Nov 7, 2024

And this behavior is somewhat special, other DBMS usually use much wider type like 64 bit by default

As a workaround in ClickHouse you can always cast to whatever you want to bypass this exception:

SELECT bitShiftLeft(256, 10);
SELECT bitShiftLeft(1::Int64, 10)

https://fiddle.clickhouse.com/856ae8e4-746d-4966-a2c1-6922512d7d09

@UnamedRus
Copy link
Copy Markdown
Contributor

UnamedRus commented Nov 7, 2024

As a workaround in ClickHouse you can always cast to whatever you want to bypass this exception:

So, basically, it does mean that you "always" need to do additional cast within bitShift operator, because there are functions exist in CH, which return smaller types than arguments. (modulo as example).

If "accurate" behavior is needed, it make more sense to introduce additional set of functions, like cast vs accurateCast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

bitShiftLeft may produce unexpected result.

7 participants