Skip to content

Normalize named tuple arguments in agg states - #70853

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
amosbird:fix-69732
Oct 23, 2024
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
amosbird:fix-69732

Conversation

@amosbird

@amosbird amosbird commented Oct 19, 2024 •

Copy link
Copy Markdown
Collaborator

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):

Normalize named tuple arguments in aggregation states. This fixes #69732 .

This PR also re-enables enable_named_columns_in_function_tuple, as all known bugs related to it have been resolved. The only remaining exception is issue #70830, which simplifies to the following scenario:

create table x (t Tuple(a int, b int)) engine MergeTree order by ();
insert into x select (1 as a2, 2 as b2);
select * from x format JSONEachRow;

{"t":{"a":0,"b":0}}

Since ClickHouse does not support inserting tuples with different names (e.g., Tuple(a2 int, b2 int)) into a tuple structure defined as Tuple(a int, b int), enabling enable_named_columns_in_function_tuple would naturally break this compatibility.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-bugfix Pull request with bugfix, not backported by default label Oct 19, 2024
@robot-ch-test-poll4

robot-ch-test-poll4 commented Oct 19, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 2393d2f 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
Bugfix validationChecks that either a new test (functional or integration) or there some changed tests that fail with the binary built on master branch✅ 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
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ 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
Stateless testsRuns stateless 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
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✅ success

@serxa serxa self-assigned this Oct 19, 2024
Comment thread src/DataTypes/IDataType.h Resolved

@serxa serxa 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.

Looks good

@amosbird

Copy link
Copy Markdown
Collaborator Author

@serxa It would be better to wait for @alexey-milovidov 's input before merging this PR, as it breaks compatibility for certain INSERT SELECT queries.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 23, 2024
Merged via the queue into ClickHouse:master with commit 95c5291 Oct 23, 2024
@alexey-milovidov
alexey-milovidov deleted the fix-69732 branch October 23, 2024 21:17
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 23, 2024
@Algunenano

Copy link
Copy Markdown
Member

I don't understand why we are ok with breaking the INSERT behaviour, from a expected one to a completely unexpected one.

In the original message:

create table x (t Tuple(a int, b int)) engine MergeTree order by ();
insert into x select (1 as a2, 2 as b2);
select * from x format JSONEachRow;

Without this setting: {"t":{"a":1,"b":2}}
With this setting: {"t":{"a":0,"b":0}}

I would expect it to either have the same behaviour as before and match columns in order, or throw because the columns don't match, but inserting default values is totally unexpected.

Comment thread src/Core/Settings.cpp
M(Bool, enable_named_columns_in_function_tuple, false, R"(
M(Bool, enable_named_columns_in_function_tuple, true, R"(
Generate named tuples in function tuple() when all names are unique and can be treated as unquoted identifiers.
Beware that this setting might currently result in broken queries. It's not recommended to use in production

@Algunenano Algunenano Oct 23, 2024 •

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.

We enabled it while there is still broken behaviour, or at least I understand so (#70853 (comment)), and the description still says it's broken and not to use it in production.

@alexey-milovidov

Copy link
Copy Markdown
Member

@amosbird, please edit the inline documentation accordingly.

@amosbird

Copy link
Copy Markdown
Collaborator Author

I would expect it to either have the same behaviour as before and match columns in order, or throw because the columns don't match, but inserting default values is totally unexpected.

@Algunenano The "unexpected" behavior originates from the different conversion rules applied to named and unnamed tuples:

if (from_type->haveExplicitNames() && to_type->haveExplicitNames())
{
const auto & from_names = from_type->getElementNames();
std::unordered_map<String, size_t> from_positions;
from_positions.reserve(from_names.size());
for (size_t i = 0; i < from_names.size(); ++i)
from_positions[from_names[i]] = i;
const auto & to_names = to_type->getElementNames();
element_wrappers.reserve(to_names.size());
to_reverse_index.reserve(from_names.size());
for (size_t i = 0; i < to_names.size(); ++i)
{
auto it = from_positions.find(to_names[i]);
if (it != from_positions.end())
{
element_wrappers.emplace_back(prepareUnpackDictionaries(from_element_types[it->second], to_element_types[i]));
to_reverse_index.emplace_back(it->second);
}
else
{
element_wrappers.emplace_back();
to_reverse_index.emplace_back();
}
}
}
else
{
if (from_element_types.size() != to_element_types.size())
throw Exception(ErrorCodes::TYPE_MISMATCH, "CAST AS Tuple can only be performed between tuple types "
"with the same number of elements or from String.\nLeft type: {}, right type: {}",
from_type->getName(), to_type->getName());
element_wrappers = getElementWrappers(from_element_types, to_element_types);
to_reverse_index.reserve(to_element_types.size());
for (size_t i = 0; i < to_element_types.size(); ++i)
to_reverse_index.emplace_back(i);
}

select (1, 2)::Tuple(a int, b int) by_pos, (1 as a2, 2 as b2)::Tuple(a int, b int) by_name format JSONEachRow;

{"by_pos":{"a":1,"b":2},"by_name":{"a":0,"b":0}}

To my knowledge this behavior difference between named and unnamed tuple conversions is not explicitly documented in ClickHouse, nor is it standardized in SQL specifications. In order to help users (who rely on such implementation defined behavior) catch potential data loss issues during INSERT-SELECT, we can introduce a strict_named_tuple_conversion setting which throws an exception whenever subcolumns from the source tuple are lost during conversion. I'll do that together with inline documentation adjustments in next PR. @alexey-milovidov WDYT?

@alexey-milovidov

Copy link
Copy Markdown
Member

If we enable it by default - ok, otherwise don't bother.

@Algunenano

Copy link
Copy Markdown
Member

If we enable it by default - ok, otherwise don't bother.

The strict feature is being added in #71030, as a breaking change, and it's not part of 24.10. I'm going to keep enable_named_columns_in_function_tuple disabled in 24.10 and we can enable it in #71030 cc @amosbird

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.

DB::Exception: Conversion from AggregateFunction(uniq, Tuple(city String, name String))

7 participants