Repository navigation
Conversation
…nction `uniqApacheHLL` counts distinct values into an Apache DataSketches HyperLogLog sketch, and its `-State` serializes the sketch in the DataSketches format, so states can be exchanged with Java, Python and C++ services through the standard `-State` and `-Merge` combinators. Syntax: `uniqApacheHLL([lg_k, [type]])(x)`, with `lg_k` in `[4, 21]` (default 12) and `type` one of `'HLL_4'`, `'HLL_6'`, `'HLL_8'` (default `'HLL_4'`). The parameters do not change what a state means, so states built with different parameters can be merged; the sketch carries its own resolution. Only argument types that the other implementations hash the same way are accepted, so no state can be built here that an external consumer cannot reproduce: integers of at most 64 bits are widened by value to a 64-bit integer, `Float32` and `Float64` are hashed as `double`, strings and `FixedString` as raw bytes, `UUID` as its 16 canonical bytes, `IPv6` as its 16 bytes in network order, and `Date`, `Date32`, `DateTime`, `DateTime64`, `IPv4`, `Enum8` and `Enum16` by their underlying value. `NULL` values and empty strings are ignored, as in the Java, Python and C++ libraries and in Spark. The state of a `Nullable` argument is the same bare sketch, with the same state type, as the state of a plain one, instead of a sketch behind the flag byte of the `Null` combinator. That needs a new hook, `IAggregateFunction::stateIsIndependentOfNullability`, `false` by default, which the `Null` combinator and the `If` combinator over `Nullable` arguments honour by using the existing wrappers without the flag, and by giving the wrapper the state type of the nested function. `AggregateFunctionIf` forwards the question to its nested function. Nothing changes for any function that does not override it. Inserted rows go into the update sketch, which is folded into the union only when the state is merged, read or serialized. A union always holds an `HLL_8` sketch, so reading its estimate and merging do not convert it to the declared type; only serialization does. The DataSketches contrib already carries the HLL union fix, so only the include path of the `hll` module is added to its CMake target. Tests: byte-exact states for every supported type on border values, checked against the Apache DataSketches C++ library; empty strings and `NULL` handling; the `Nullable` and `If` state types and mixing them with plain states; import of sketches built outside ClickHouse; and rejection of corrupted states. Ported from the Antalya build, where it was reviewed and extended: Altinity#2398 Co-Authored-By: Andrey Zvonov <[email protected]> Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…rapper `master` already gives `AggregateFunctionNullBase` its own `getStateType`, which builds the state type with the version of the nested function, so the previous commit defined it a second time and the file did not compile. Keep one override: a nested function that opts out through `stateIsIndependentOfNullability` gets the state type of the nested function, whose own explicit version it carries, and any other function keeps the type with the version of the nested function, as before. Related: Altinity#2398 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
|
Workflow [PR], commit [f4c31f7] Summary: ❌
AI ReviewSummaryThis PR adds the Final Verdict
|
The `arm_tidy` build of the pull request failed on two findings, both reported as errors:
- `readability-ambiguous-smartptr-reset-call`: `sk_update.reset()` on a smart pointer whose
pointee also has `reset`; assign `nullptr` instead.
- `readability-container-size-empty`: `value.size() == 0`; use `value.empty()`.
No change in behavior.
Related: ClickHouse#124667
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: UnamedRus <[email protected]>
`HllSketchData::read` fed every deserialized sketch into an `hll_union` at the declared `lg_k`. A state read through a type with a lower `lg_k` was therefore downsampled right away, before any merge, and writing it back, as a part rewrite or an `INSERT SELECT` does, persisted the loss: a sketch of 8258 bytes at `lg_k = 14` came back as 170 bytes at `lg_k = 8`. Its estimate survived only because the downsampling carries the estimator over. Keep the deserialized sketch as `sk_update`, with its own resolution and storage type, and fold it into a union, at the declared resolution, only when it is merged. A state that is only read and written back is reproduced byte for byte, and no union is allocated and filled for it. An empty deserialized sketch is skipped by `merge`, as an empty state was before. The new test `05344_uniq_apache_hll_import_keeps_sketch` fails without the change on the first two checks and pins the rest: the estimate of an imported state, the declared resolution after a merge, and an empty imported state. Review comment: ClickHouse#124667 (comment) Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
The parameters of `uniqApacheHLL`, `lg_k` and the storage type, do not change what a state means, and `haveSameStateRepresentationImpl` already said so, which makes a `CAST` between them work. But the class inherited the default `getNormalizedStateType`, which keeps the parameters, and the data type comparison (`DataTypeAggregateFunction::equals`, `nameMatchesState`) goes through the normalized types. So `AggregateFunction(uniqApacheHLL(8), UInt64)` and `AggregateFunction(uniqApacheHLL, UInt64)` were different types wherever a common type is needed: a `UNION ALL` of such states became a `Variant`, which `-Merge` rejects, and an array literal of them had `Array(Variant(...))` as its type. Override `getNormalizedStateType` as `quantile` does: the same function with the default parameters, taken from the factory so the defaults are not repeated here, stands for every spelling of the parameters. Argument types still count, so states of different argument types stay different types, and a `Nullable` argument is the same type as a plain one, as its state already is. The new test `05345_uniq_apache_hll_state_types_unify` fails without the change and pins `UNION ALL`, array and `if` types, the `Nullable` and argument-type cases, and that `ALTER TABLE ... MODIFY COLUMN` to other parameters is still applied. Review comment: ClickHouse#124667 (comment) Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionUniqApacheHLL… |
new | 1.46 MiB | +1.46 MiB |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/varUIntLengthPrefix.cpp.o |
new | 284.86 KiB | +284.86 KiB |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/Combinators/AggregateFunctionN… |
749.45 KiB | 837.50 KiB | +88.05 KiB (+11.75%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/AggregateFunctionAvgWeighted.c… |
13.57 MiB | 13.59 MiB | +18.83 KiB (+0.14%) |
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/Combinators/AggregateFunctionI… |
734.14 KiB | 750.86 KiB | +16.72 KiB (+2.28%) |
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
336 translation units recompiled, 2599 s compile time in total, 334 of them have a recent master baseline.
Translation units without a recent master baseline:
src/AggregateFunctions/AggregateFunctionUniqApacheHLL.cpp: 5.4 s
… of the host in `uniqApacheHLL` `UUIDHelpers::getHighBytes` and `getLowBytes` return the two halves of a `UUID` as numbers in host order, and choose the underlying limb by `std::endian::native`, so that the first half is the first half on every platform. The canonical bytes are the big-endian representation of each half. The code swapped the bytes of each half unconditionally, which gives canonical bytes only on a little-endian host: on a big-endian one, such as s390x, memory already holds the big-endian representation and the swap reversed it. Convert the halves with `transformEndianness<std::endian::big>`, which swaps on a little-endian host and does nothing on a big-endian one. Nothing changes on a little-endian host, and the byte-exact `UUID` states of the existing tests still hold. This makes the input bytes right, but not the sketch: the DataSketches library hashes with a `MurmurHash3` that reads its blocks in host order, so a big-endian build still computes other hashes than a little-endian one, for every type. That is a limit of the library. Review comment: ClickHouse#124667 (comment) Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
The estimate of a union of small sketches is the HIP estimate of the library, which depends on the order in which the sketches were merged, so the result can change between runs and with the partitioning of the data. The third parameter, `estimator`, chooses the estimate of a merged state: `'DEFAULT'` is the one of the library, `'COMPOSITE'` is computed from the merged sketches only and does not depend on the order. It affects only the result and not the state or its type. `'DEFAULT'` is the default, so existing queries are unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…Int` A sketch that another system writes, for example Spark into Parquet, has no length in front of it, while the state of `uniqApacheHLL` stores its sketch as a `VarUInt` length and the bytes. `CAST` of such a sketch to `AggregateFunction(uniqApacheHLL, ...)` fails with `CORRUPTED_DATA`, and `toString` of a state gives bytes that the other system cannot read. `addVarUIntLengthPrefix` puts the length of a string in front of it, `removeVarUIntLengthPrefix` takes it off and throws an exception when the length is not complete or is not the number of the bytes that follow, and `toVarUInt` encodes one unsigned number. The length is read as the `RowBinary` reader reads it. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
The HIP estimate and the bytes of the sketch depend on the order of the updates and of the merges, so the planner must keep an `ORDER BY` below the function and below its `-State`, `-Merge` and `-If` forms. The property was copied from `uniq`, whose result does not depend on the order. Related: ClickHouse#95045 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
Apache DataSketches hashes values and serializes sketches in the byte order of the host, so on a big-endian platform the sketches of `uniqApacheHLL` would not be the ones that other implementations produce and read. The function is registered only on little-endian platforms, as if DataSketches were not built, and its tests are skipped on `s390x`. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…ered The macro that stood for the condition was used at two places only, and the other registrations in the file use the condition of their library directly. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
The HIP estimate of a sketch that is built by inserts depends on the order of the inserts as well, and a state that is read keeps the accumulator it was written with. With the `COMPOSITE` estimator the result is now computed from the registers of the sketch only, whether the sketch was merged, built by inserts or read. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
The default estimate of a sketch differs between orders only sometimes, depending on how the branches of `UNION ALL` are merged, so asserting that it differs made the test flaky. The test checks only that the composite estimate does not depend on the order. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…pacheHLL` `uniqApacheHLL` hashes `UInt8`, `UInt16` and `UInt32` by value as 64-bit integers, as Java and Spark hash a number that they hold as a `long`, while the C++ library hashes them through the signed type of the same width. For the values with the high bit set, the sketches are different. The documentation now tells to cast to the signed type of the same width with a wrapping cast to get the sketch of the C++ library, and a test pins these casts. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
A state may hold a union and a sketch of inserts. `size` and `write` fold the sketch into the union, the union first, while `merge` took the sketch of inserts first, so the same state could give a different HIP estimate and different bytes of the sketch depending on whether it was merged directly or written and read first. `merge` now takes the union first, as the fold does. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…o it A state can hold a union and a sketch of inserts that came after a merge, for example when the rows of the tail of a block are added to a state that has already merged the lookup tables of the block, as the aggregation by a `UInt8` key does. `merge` took the new sketches first and folded the pending sketch at its end, so the older rows came after the newer ones. As the HIP estimate and the bytes of the sketch depend on this order, the pending rows are now folded into the union before the sketches are taken. The result of the default estimator can still differ with the size of the blocks, since the lookup tables merge the rows in another order; the composite estimator does not, and a test checks it. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…o it A state can hold a union and a sketch of inserts that came after a merge, for example when the rows of the tail of a block are added to a state that has already merged the lookup tables of the block, as the aggregation by a `UInt8` key does. `merge` took the new sketches first and folded the pending sketch at its end, so the older rows came after the newer ones. As the HIP estimate and the bytes of the sketch depend on this order, the pending rows are now folded into the union before the sketches are taken. The result of the default estimator can still differ with the size of the blocks, since the lookup tables merge the rows in another order; the composite estimator does not, and a test checks it. Related: ClickHouse#124667 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
Related: ClickHouse#124667 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
…o it A state can hold a union and a sketch of inserts that came after a merge, for example when the rows of the tail of a block are added to a state that has already merged the lookup tables of the block, as the aggregation by a `UInt8` key does. `merge` took the new sketches first and folded the pending sketch at its end, so the older rows came after the newer ones. As the HIP estimate and the bytes of the sketch depend on this order, the pending rows are now folded into the union before the sketches are taken. The result of the default estimator can still differ with the size of the blocks, since the lookup tables merge the rows in another order; the composite estimator does not, and a test checks it. Related: ClickHouse#124667 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
Related: ClickHouse#124667 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: UnamedRus <[email protected]>
|
Dear @scanhex12, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
-Merge/-State are compatible with externally produced states by java/python/c++ impl of datasketches lib.
Few limitations to match behavior:
Only some types supported, nulls ignored (therefore there is no 01 prefix in state, and representation is identical to non nullable arguments), empty string is also ignored.
states with different parameters can be merged. (lower internal state accuracy/size, but it is not reflected in data type parameters)
Not build for big endian systems, bc apache Datasketches is not portable across big vs little endian.
In order to import/export AggregateFunction states of CH to String/BYTE_ARRAY (used in other systems to hold sketches),
addVarUIntLengthPrefix/removeVarUIntLengthPrefixfunctions introduced.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Adds the
uniqApacheHLLaggregate function for exchanging Apache DataSketches HLL states with external systems.Workflow [PR]
Sync PR [sync-upstream/pr/124667]