Skip to content

Implement Shannon entropy aggregate function - #4238

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
Quid37:yandex_open_code_competition
Feb 4, 2019
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
Quid37:yandex_open_code_competition

Conversation

@Quid37

@Quid37 Quid37 commented Feb 2, 2019

Copy link
Copy Markdown
Contributor

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

For changelog. Remove if this is non-significant change.

Category (leave one):

  • New Feature

Short description (up to few sentences):
Aggregate Function which computes Shannon entropy
...

@alexey-milovidov alexey-milovidov changed the title Yandex open code competition Implement Shannon entropy aggregate function Feb 2, 2019
#include <AggregateFunctions/FactoryHelpers.h>

/// These must be exposed in header for the purpose of dynamic compilation.
#include <AggregateFunctions/QuantileReservoirSampler.h>

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.

Excessive includes.

#include <AggregateFunctions/IAggregateFunction.h>
#include <AggregateFunctions/QuantilesCommon.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnDecimal.h>

@alexey-milovidov alexey-milovidov Feb 2, 2019 •

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.

Please remove unneeded includes.


namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;

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.

Unused variable.

using Weight = UInt64;
using Map = HashMap <
Value, Weight,
HashCRC32<Value>,

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.

If the value is already hashed (UInt128), we should use TrivialHash instead of HashCRC32.


Map map;

void add(const Value &x)

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 write spaces at both sides of &.

map.write(buf);
}

void deserialize(ReadBuffer &buf)

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.

To test serialization, we can write distributed query by select from remote table function with remote('127.0.0.{1,2}'). See examples in other tests.

Note: To run this test on local machine, the server should be configured to accept remote connections (see listen in config.xml).


Float64 get() const
{
Float64 ShannonEntropy = 0;

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.

Variables are in snake_case.

@alexey-milovidov
alexey-milovidov merged commit 2216250 into ClickHouse:master Feb 4, 2019
}
else if (which.isUInt128())
{
return std::make_shared<AggregateFunctionEntropy<UInt128, true>>();

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.

That's not correct.
UInt128 data type is not a hash.

@alexey-milovidov

Copy link
Copy Markdown
Member

It doesn't work for mutual entropy:

SELECT entropy(number % 2, number % 5), log2(10), log2(uniq(number % 2, number % 5)) FROM numbers(10)

@adevyatova

Copy link
Copy Markdown
Contributor

Internal documentation ticket: DOCSUP-10111

pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request Jun 23, 2026
…House#23727)

Before this change, `ALTER TABLE dst REPLACE PARTITION X FROM src`
silently dropped the destination partition whenever the source table
had no parts in partition X. The destination's data was simply removed
and nothing was written in its place — an obvious data-loss footgun
that has been tracked since 2021 as "easy task" / "unexpected behaviour".

The pre-existing `max_partition_size_to_drop` check did not help in
practice: its default is 50 GiB, so any reasonably-sized destination
partition was silently wiped without any safety prompt.

Per the maintainer thread on ClickHouse#23727 (alexey-milovidov 2022, azat 2024),
the fix introduces a new query setting
`allow_replace_partition_from_empty_source` (default `false`) and
rejects the operation by default with `BAD_ARGUMENTS` and a clear
remediation message. Users who genuinely depend on the previous
"silent clear" semantics can opt in per-query by setting it to `1`,
or switch to `ALTER TABLE ... DROP PARTITION` when they actually want
to drop data.

The check is added to both `StorageMergeTree::replacePartitionFrom`
and `StorageReplicatedMergeTree::replacePartitionFromImpl`. The
`is_all` branch is excluded because `REPLACE PARTITION ALL` is already
rejected with `SUPPORT_IS_DISABLED`.

A new stateless regression test (ClickHouse#4238) covers both engines and the
opt-in case. The two pre-existing tests that intentionally exercised
the silent-clear behavior (`00626_replace_partition_from_table.sql`
and `_zookeeper.sh`) are updated to set the opt-in flag, preserving
their intent.

Closes: ClickHouse#23727
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…House#23727)

Before this change, `ALTER TABLE dst REPLACE PARTITION X FROM src`
silently dropped the destination partition whenever the source table
had no parts in partition X. The destination's data was simply removed
and nothing was written in its place — an obvious data-loss footgun
that has been tracked since 2021 as "easy task" / "unexpected behaviour".

The pre-existing `max_partition_size_to_drop` check did not help in
practice: its default is 50 GiB, so any reasonably-sized destination
partition was silently wiped without any safety prompt.

Per the maintainer thread on ClickHouse#23727 (alexey-milovidov 2022, azat 2024),
the fix introduces a new query setting
`allow_replace_partition_from_empty_source` (default `false`) and
rejects the operation by default with `BAD_ARGUMENTS` and a clear
remediation message. Users who genuinely depend on the previous
"silent clear" semantics can opt in per-query by setting it to `1`,
or switch to `ALTER TABLE ... DROP PARTITION` when they actually want
to drop data.

The check is added to both `StorageMergeTree::replacePartitionFrom`
and `StorageReplicatedMergeTree::replacePartitionFromImpl`. The
`is_all` branch is excluded because `REPLACE PARTITION ALL` is already
rejected with `SUPPORT_IS_DISABLED`.

A new stateless regression test (ClickHouse#4238) covers both engines and the
opt-in case. The two pre-existing tests that intentionally exercised
the silent-clear behavior (`00626_replace_partition_from_table.sql`
and `_zookeeper.sh`) are updated to set the opt-in flag, preserving
their intent.

Closes: ClickHouse#23727
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.

3 participants