Skip to content

Reject non-UInt8 arguments of __bitSwapLastTwo during analysis - #122748

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:bit-swap-last-two-reject-non-uint8
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:bit-swap-last-two-reject-non-uint8

Conversation

@groeneai

@groeneai groeneai commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117613 (comment)
Related: #117613

Changelog category (leave one):

  • Improvement

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

The internal function __bitSwapLastTwo rejects arguments of types other than UInt8 while the query is analyzed, with ILLEGAL_TYPE_OF_ARGUMENT, instead of raising BAD_ARGUMENTS when a row is processed. EXPLAIN and queries that process no rows now also reject such arguments.

Description

Improvement, not Bug Fix: __bitSwapLastTwo is an internal function, not a documented user surface.

On master EXPLAIN header = 1 SELECT __bitSwapLastTwo(toFloat64(c0)) FROM t succeeds, and so does the SELECT over an empty t; with rows it fails at execution with Code: 36 ... It's a bug! Only UInt8 type is supported by __bitSwapLastTwo, or returns a value once the expression is JIT-compiled (#117613).

FunctionBitSwapLastTwo::getReturnTypeImpl now accepts only UInt8 (Bool included, Nullable and LowCardinality are stripped before it) and raises the message String arguments already get. The throw in apply is then unreachable from SQL, so it is a LOGICAL_ERROR again.

The set skip index is the only other caller: it rewrites NOT into __bitSwapLastTwo over UInt8 masks. Measured on 332 cases (17 column types, 22 predicates, a set(0) index and a never-executed set(1) index), each under 8 combinations of the skip index settings: all 2656 queries return the same count or error on both builds, and in 1952 of them system.query_log.used_functions shows that the index built __bitSwapLastTwo.

Compatibility: a table that applies __bitSwapLastTwo to another type in a MergeTree sorting or partition key, TTL or skip index (every INSERT into it already failed) or in a Distributed sharding key no longer loads, so queries to it and to system.tables of its database fail; with async_load_databases = 0 the server does not start. I added no compatibility setting: system.functions says this internal function "may change or be removed at any time".

__bitWrapperFunc, __bitBoolMaskAnd and __bitBoolMaskOr have the same shape (accepted during analysis, BAD_ARGUMENTS on the first row). I left them unchanged and can extend this PR to them if wanted.

Test 05056 of #117613 expects BAD_ARGUMENTS for these arguments; whichever PR merges second updates it to ILLEGAL_TYPE_OF_ARGUMENT.


Workflow [PR]
Sync PR [sync-upstream/pr/122748]

`__bitSwapLastTwo` computes only on `UInt8`, but its return type was resolved for
every integer width and both float types, so the argument type was checked only in
`BitSwapLastTwoImpl::apply`, when a row was processed. `EXPLAIN` and a query over an
empty table accepted `__bitSwapLastTwo(toFloat64(c0))`, and with rows it failed with
`BAD_ARGUMENTS` at execution. `getReturnTypeImpl` now rejects every argument other
than (`Nullable`/`LowCardinality` of) `UInt8` or `Bool` with `ILLEGAL_TYPE_OF_ARGUMENT`,
the code the function already returned for `String` and `Enum8`.

Requested in ClickHouse#117613 (comment)

The only in-tree caller besides SQL is the set skip index, which rewrites `NOT` into
`__bitSwapLastTwo` over the `UInt8` masks built by `__bitWrapperFunc`, the unknown
constant, or the other mask functions, so it never passes another type; a
type x predicate x settings matrix over set indexes measured no difference in counts
or errors between the old and the new build.

With analysis rejecting other types, the throw in `apply` is unreachable from SQL, so
it is a `LOGICAL_ERROR` again and the comment saying it was callable from SQL is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-review-human PR origin: human review feedback on a groeneai PR labels Sep 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding), plus my own review (2 nits, both addressed in the PR description: the sibling functions and the compatibility line).

# Sev Finding Verdict Evidence / action
1 ❌ A mixed Variant(Bool, UInt16) argument passes analysis, and only a UInt16 row fails DISAGREE This is the documented Variant contract for every function (variant_throw_on_type_mismatch, e.g. length(v) over Variant(String, UInt64)). A Variant with no UInt8 alternative is rejected during analysis. The mismatched rows now get ILLEGAL_TYPE_OF_ARGUMENT, or NULL with the setting disabled, where master raised BAD_ARGUMENTS under both settings.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20260928-191100

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [29023c0]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
05056_jit_bit_swap_last_two_argument_type FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_asan_ubsan) DROPPED
Build (arm_binary) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 28, 2026

DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isUInt8(arguments[0]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Moving this check into getReturnTypeImpl changes table-metadata restoration, not just ad-hoc query analysis. Existing MergeTree metadata such as ORDER BY __bitSwapLastTwo(toUInt16(x)) currently loads and only fails once the expression is executed during INSERT; after this change the table itself stops loading, and with async_load_databases = 0 that can prevent server startup. Per the compatibility rule for new validation on existing metadata, this needs a gate or to be limited to newly created metadata rather than unconditional analysis-time rejection.

@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d30d28a46 with master 1e5291bf9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline.

Job report

This branch has not been deployed

No deployments
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 comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-review-human PR origin: human review feedback on a groeneai PR pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants