Repository navigation
Support of non-map columns for -Map combinator (also support for multi argument aggregate functions) - #56335
Support of non-map columns for -Map combinator (also support for multi argument aggregate functions)#56335UnamedRus wants to merge 55 commits into
Conversation
|
This is an automated comment for commit f0ec2b5 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
|
|
Unfortunately, we need to mix support for 3(4) different syntaxes in that Combinator Probably overall logic can looks like that:
Because of #54537 logic should be more complicated, because 2.2 & 3 can clash if support for array-based key will be implemented for maps. Probably overall logic can looks like that: Probably, we can have setting which can change preference between 2.2 and 3. |
f3b022a to
c7a2c03
Compare
|
Just ran into this today - this addition would be very convenient. |
|
Hi, can you take a look on that as well? It doesn't support complex key maps, but old implementation did not support them as well. |
|
this would be really great! |
|
Dear @yakov-olkhovskiy, 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. |
Adds the BFloat16 member of the reinterpretAs* family, matching the existing reinterpretAsFloat32 / reinterpretAsFloat64. It goes through the same generic FunctionReinterpret path (BFloat16 is accepted by canBeReinterpretedAsNumeric via isFloat), so it reinterprets the raw bytes of the argument as a BFloat16. Needed by 01852_map_combinator, which builds specific BFloat16 bit patterns (NaN payloads) with reinterpretAsBFloat16(reverse(unhex(...))); the function did not exist, so the test aborted with UNKNOWN_FUNCTION. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
🕵 Updated the branch: merged current The only red on the previous head was @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=56335&sha=a3c5c38948ecdf544994b54043184abfa82c5e60&name_0=PR&name_1=Integration%20tests%20%28amd_binary_excluded_from_llvm%29 and provide a fix in a separate PR ( Also replied to the open review thread: the upgrade-compatibility concern about float-key |
|
Acknowledged. No fix is in flight: no open PR by me or anyone else targets |
|
The fix is #116855. The test asserted I rebuilt this run's |
Resolved the conflict in `AggregateFunctionMap.cpp`: master made the per-key state creation in `add` exception-safe (reserve the hash-table slot first, then create the nested state and erase the slot if creation throws), while this branch moved that logic into the shared `addSingleKey` helper used by both the map form and the plain-key form. The resolution keeps the helper and applies master's exception-safe sequence inside it, so both forms get the fix.
…ction, refresh in-server docs
Two review findings on `AggregateFunctionMap.cpp` (the source changes are
part of the preceding master merge commit, this commit adds their tests):
1. The `Map` branch of `transformArguments` / `transformAggregateFunction`
triggered on `arguments[0]` alone, so the new plain-key syntax was
unreachable whenever the nested aggregate function's own first argument
is a `Map`: `groupArrayMap(map('a', 1), key)` threw
`Map combinator takes only one map argument` before `groupArray` was
even instantiated. Only a lone `Map(K, V)` argument now selects the
map-input mode; with more arguments the last one is the key and the
`Map` is passed through to the nested function. `getKeyType` already
used `types.back()` for `types.size() > 1`, so it needed no change.
Misuse such as `sumMap(map('a', 1), map('b', 2))` still fails cleanly
with `ILLEGAL_TYPE_OF_ARGUMENT` from the nested function.
2. The `Documentation{}` block fed into `system.documentation` (used by
`help` and the built-in `/docs` UI) still claimed that `-Map` only
aggregates `Map` values key-wise; it now describes both forms, matching
`combinators.mdx`.
Tests: `groupArrayMap`, `anyMap` and `groupArrayMapState` over a `Map`
argument with an explicit key, in `01852_map_combinator`.
|
🕵 Updated the branch and addressed both open review threads. Merged Review thread on Review thread on Verified locally: CI on the previous head was fully green (156 passing, 0 failing). The remaining open item is the author's |
Since the `-Map` combinator only treats a lone `Map` argument as the map-input form, `sumMap(map(1,2), map(1,3))` now passes the first `Map` through to the nested `sum` with the second one as key, and the nested function rejects it with `ILLEGAL_TYPE_OF_ARGUMENT` instead of the combinator throwing `NUMBER_OF_ARGUMENTS_DOESNT_MATCH`. Fast test report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=56335&sha=896e1e6541776c828cafeadba6cd4e438ecc9404&name_0=PR&name_1=Fast%20test PR: ClickHouse#56335 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Merged current master (clean, single merge base, no conflicts) and fixed the only CI red, the |
| { | ||
| using SearchType = KeyType; | ||
| UnorderedMapWithMemoryTracking<KeyType, AggregateDataPtr> merged_maps; | ||
| UnorderedMapWithMemoryTracking<KeyType, AggregateDataPtr, CompareHelperHash<KeyType>, CompareHelperEqual<KeyType>> merged_maps; |
There was a problem hiding this comment.
Supporting floating-point keys here also widens the result type to Map(Float*, R) / Map(BFloat16, R), but the downstream keyed-read helper still uses different equality.
Concrete traces:
src/DataTypes/DataTypeMap.cpp:229-232pre-extracts per-keyMapreads throughextractKeyValueFromMap, andsrc/DataTypes/DataTypeMapHelpers.cpp:45-52comparesFloat32/Float64keys there with raw==, so aNaNkey produced bysumMapcannot be read back through that keyed lookup surface even when it was just aggregated.BFloat16has no specialized matcher there, so it falls back tocompareAt(..., 0)and aNaNprobe compares equal to the first key of any row.
So this PR exposes map results whose own key lookup path is incorrect for the new NaN semantics. Either keep rejecting floating-point keys for now, or update DataTypeMapHelpers to use the same CompareHelper semantics and cover it with an end-to-end lookup test.
There was a problem hiding this comment.
🕵 Not changing this here. Map(Float*, V), Map(BFloat16, V) and Map(Decimal, V) are already valid on master and can be produced by map(...), CAST or read from a table, and on current master they already behave as you describe: map(1.5, 2)[1.5] and map(CAST(1, 'Decimal(10,2)'), 1)[CAST(1, 'Decimal(10,2)')] throw ILLEGAL_TYPE_OF_ARGUMENT, and has(map(nan, 1), nan) returns 0. So the keyed-read gap is a pre-existing limitation of the Map type itself, not something this combinator introduces; mapKeys / mapValues / arrayJoin work on the result. Canonicalizing NaN and -0 during aggregation matches how GROUP BY treats these keys. Extending arrayElement / mapContainsKey to more key types with the same equality is worth a separate PR. Leaving this thread open so a maintainer can decide. The work continues in #123449.
| if (auto * res = createWithNumericBasedType<AggregateFunctionMap, false>(*key_type, nested_function, arguments)) | ||
| return AggregateFunctionPtr(res); | ||
|
|
||
| if (auto * res = createWithDecimalType<AggregateFunctionMap, false>(*key_type, nested_function, arguments)) | ||
| return AggregateFunctionPtr(res); | ||
|
|
||
| if (key_type->getTypeId() == TypeIndex::FixedString || key_type->getTypeId() == TypeIndex::String) | ||
| return std::make_shared<AggregateFunctionMap<String, false>>(nested_function, arguments); | ||
|
|
||
| throw Exception( | ||
| ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, | ||
| "Map key type {} is not supported by combinator {}", key_type->getName(), getName()); | ||
| } | ||
| else | ||
| { | ||
| // in case of tuple of arrays or just arrays (checked in transformArguments), try to redirect to sum/min/max-MappedArrays to implement old behavior | ||
| auto nested_func_name = nested_function->getName(); | ||
| if (nested_func_name == "sum" || nested_func_name == "min" || nested_func_name == "max") | ||
| { | ||
| AggregateFunctionProperties out_properties; | ||
| auto & aggr_func_factory = AggregateFunctionFactory::instance(); | ||
| auto action = NullsAction::EMPTY; | ||
| return aggr_func_factory.get(nested_func_name + "MappedArrays", action, arguments, params, out_properties); | ||
| } | ||
| const DataTypePtr & key_type = arguments.back(); | ||
|
|
||
| if (auto * res = createWithNumericBasedType<AggregateFunctionMap, true>(*key_type, nested_function, arguments)) | ||
| return AggregateFunctionPtr(res); | ||
|
|
||
| if (auto * res = createWithDecimalType<AggregateFunctionMap, true>(*key_type, nested_function, arguments)) |
There was a problem hiding this comment.
Supporting Float* / BFloat16 / Decimal / DateTime64 keys here widens the result type, but the ordinary Map lookup surface is still wired to the old key set and equality. arrayElement(Map, key) only dispatches integer / UUID / IP / string keys (src/Functions/array/arrayElement.cpp:2059-2136), so a query like arrayElement(sumMap(1, CAST(number % 3, 'Decimal(10, 2)')), CAST(1, 'Decimal(10, 2)')) still throws ILLEGAL_TYPE_OF_ARGUMENT instead of reading the value back. And the path that does accept float keys, has(map, key) / mapContainsKey, compares through accurate::equalsOp (NaN != NaN), so has(sumMap(...), nan) still returns 0 even though this combinator now canonicalizes all NaN payloads into one logical key. Please either keep rejecting these key types for now, or update the lookup helpers to cover the same key types and use the same CompareHelper semantics.
There was a problem hiding this comment.
🕵 Not changing this here. Map(Float*, V), Map(BFloat16, V) and Map(Decimal, V) are already valid on master and can be produced by map(...), CAST or read from a table, and on current master they already behave as you describe: map(1.5, 2)[1.5] and map(CAST(1, 'Decimal(10,2)'), 1)[CAST(1, 'Decimal(10,2)')] throw ILLEGAL_TYPE_OF_ARGUMENT, and has(map(nan, 1), nan) returns 0. So the keyed-read gap is a pre-existing limitation of the Map type itself, not something this combinator introduces; mapKeys / mapValues / arrayJoin work on the result. Canonicalizing NaN and -0 during aggregation matches how GROUP BY treats these keys. Extending arrayElement / mapContainsKey to more key types with the same equality is worth a separate PR. Leaving this thread open so a maintainer can decide. The work continues in #123449.
|
🕵 Superseded by #123449. I merged the latest |
Fix for #38857
Now, -Map combinator support syntax like that:
argMaxMap(arg, val, key)It's also somewhat faster, than using
sumMap(map(key, value)Plus
aggMapStatelooks much more user friendly:Than
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
-Map combinator can be used with non-map columns and support multiple argument aggregate functions
Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/56335]