Skip to content

Fix sumMapFiltered with NaN values - #58959

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
Algunenano:fix_sum_map
Jan 25, 2024
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
Algunenano:fix_sum_map

Conversation

@Algunenano

@Algunenano Algunenano commented Jan 18, 2024 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Backward Incompatible Change

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

  • Fix sumMapFiltered with NaN values. NaN values are now placed at the end (instead of randomly) and considered different from any values. -0 is now also treated as equal to 0; since 0 values are discarded, -0 values are discarded too.

Documentation entry for user-facing changes

Implement NaN handling in DB::Field::operator<, which is used by std::set<DB::Field>

Closes #58741

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-bugfix Pull request with bugfix, not backported by default label Jan 18, 2024
@robot-ch-test-poll3

robot-ch-test-poll3 commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit f5ffe4e 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
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
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
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 server and keeper imagesThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Docs checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Fast testsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ 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✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ 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
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
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Unit testsRuns the unit tests for different release types✅ success
Check nameDescriptionStatus
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
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

Comment thread src/Core/Field.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reimplementing with intHash and hash(x) ^ y.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After implementing it I've decided to instead fix Field::operator<(). I might keep the implementation in a separate file in case we want to use it in the future (although in a different file, to avoid pushing even more deps to Field.h

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

.

@alexey-milovidov alexey-milovidov self-assigned this Jan 19, 2024
Comment thread src/Core/Field.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not good.

Comment thread src/Core/FieldHash.h Outdated
@@ -0,0 +1,147 @@
#pragma once

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I introduced this but it's not used. We could remove it or keep it in case we want to use std::unordered_map / std::unordered_set of DB::Field. I did not include it into Field.h itself to avoid adding more dependencies to a file that's used in almost 3000 other files.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nevermind, the style did not like it, so I've removed it.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like the newly added test is passed on the old version.

@Algunenano

Copy link
Copy Markdown
Member Author

It looks like the check is broken. I don’t see it running the tests. Here is fiddle: https://fiddle.clickhouse.com/7720d824-6c43-46b7-b708-20538042bd2a

@Algunenano

Copy link
Copy Markdown
Member Author

Need to investigate the perf failures. Something odd is going on

@Algunenano

Copy link
Copy Markdown
Member Author

Unrelated failures. The perf failures (Arrays passed to arrayMap must have equal size) did not happen again after improving the error message to get better diagnostics.

@Algunenano

Copy link
Copy Markdown
Member Author

Failures:

@alexey-milovidov
alexey-milovidov merged commit aedffea into ClickHouse:master Jan 25, 2024
@azat

azat commented Jan 27, 2024

Copy link
Copy Markdown
Member

This PR indeed breaks perf tests, see #59287 for details, though maybe it worth to fix the behavior of sumMap for negative zeroes (since it is a backward incompatible change), or at least document it in Backward incompatible section.

throw Exception(
ErrorCodes::SIZES_OF_ARRAYS_DONT_MATCH,
"Arrays passed to {} must have equal size", getName());
"Arrays passed to {} must have equal size. Argument {} has size {}, but expected {}",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works incorrectly, it will always match, since this is the number of rows in the column, not number of elements in array.

@Algunenano

Copy link
Copy Markdown
Member Author

This PR indeed breaks perf tests, see #59287 for details, though maybe it worth to fix the behavior of sumMap for negative zeroes (since it is a backward incompatible change), or at least document it in Backward incompatible section.

TBH, I don't understand why sumMap doesn't return zeroes (positive or now negative). It's not documented anywhere and it seems to be caused by bad code design. This doesn't make any sense to me:

SELECT sumMap(['foo', 'foo'], [-5., 5])

Query id: b81635c5-31a6-4fff-9661-a86420136cad

┌─sumMap(['foo', 'foo'], [-5., 5])─┐
│ ([],[])                          │
└──────────────────────────────────┘

It seems it was added as a bug and then people started to rely on it

@Algunenano Algunenano removed the pr-bugfix Pull request with bugfix, not backported by default label Jan 29, 2024
rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Aug 5, 2026
The `Float64` branch of `Field::operator<=` read both operands from
`*this`, so it compared a value with itself and returned `true` for every
`rhs`. `Field::operator>=` is implemented as `rhs <= *this`, so it
inherited the same defect.

Read the second operand from `rhs`, and pin the ordering of `Float64`
fields - including `NaN`, which is ordered after every number and is
equal to itself - with unit tests.

There are no `Field`-to-`Field` uses of `<=` or `>=` in the codebase
today, so no query result can change; this removes a trap for the first
caller.

Closes: ClickHouse#112778
Caused by: ClickHouse#58959

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sumMapFiltered aggregate function wrong result starting from 23.11

4 participants