From be2cf70da869c0407d3a09f5b7d9e8d659e79674 Mon Sep 17 00:00:00 2001 From: Baudouin Giard Date: Fri, 8 May 2020 15:10:47 -0400 Subject: [PATCH] Fix serialize/deserialize of denominator of Average functions Use writeBinary/readBinary to serialize/deserialize the denominator of Average functions. Previously it would be transmitted as an unsigned int, which means its value would get corrupted when doing an avgWeighted over a floating point number. Note: this commit is not backwards compatible! Signed-off-by: Baudouin Giard --- src/AggregateFunctions/AggregateFunctionAvg.h | 4 ++-- .../0_stateless/01268_shard_avgweighted.reference | 6 ++++++ tests/queries/0_stateless/01268_shard_avgweighted.sql | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/01268_shard_avgweighted.reference create mode 100644 tests/queries/0_stateless/01268_shard_avgweighted.sql diff --git a/src/AggregateFunctions/AggregateFunctionAvg.h b/src/AggregateFunctions/AggregateFunctionAvg.h index 06cc8c3bd7d..a269dd74ad5 100644 --- a/src/AggregateFunctions/AggregateFunctionAvg.h +++ b/src/AggregateFunctions/AggregateFunctionAvg.h @@ -71,13 +71,13 @@ public: void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { writeBinary(this->data(place).numerator, buf); - writeVarUInt(this->data(place).denominator, buf); + writeBinary(this->data(place).denominator, buf); } void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override { readBinary(this->data(place).numerator, buf); - readVarUInt(this->data(place).denominator, buf); + readBinary(this->data(place).denominator, buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override diff --git a/tests/queries/0_stateless/01268_shard_avgweighted.reference b/tests/queries/0_stateless/01268_shard_avgweighted.reference new file mode 100644 index 00000000000..95c2075a352 --- /dev/null +++ b/tests/queries/0_stateless/01268_shard_avgweighted.reference @@ -0,0 +1,6 @@ +100 +10 +0 +nan +nan +100 diff --git a/tests/queries/0_stateless/01268_shard_avgweighted.sql b/tests/queries/0_stateless/01268_shard_avgweighted.sql new file mode 100644 index 00000000000..3a101ec4754 --- /dev/null +++ b/tests/queries/0_stateless/01268_shard_avgweighted.sql @@ -0,0 +1,9 @@ +CREATE TABLE dummy(foo Int64) ENGINE = Memory(); +INSERT INTO dummy VALUES (1); +SELECT avgWeighted(100., .1) FROM remote('127.0.0.{2,3}', currentDatabase(), dummy); +SELECT avgWeighted(10, 100) FROM remote('127.0.0.{2,3}', currentDatabase(), dummy); +SELECT avgWeighted(0, 1) FROM remote('127.0.0.{2,3}', currentDatabase(), dummy); +SELECT avgWeighted(0., 0.) FROM remote('127.0.0.{2,3}', currentDatabase(), dummy); +SELECT avgWeighted(1., 0.) FROM remote('127.0.0.{2,3}', currentDatabase(), dummy); +SELECT avgWeighted(toInt8(100), -1) FROM remote('127.0.0.{2,3}', currentDatabase(), dummy); +DROP TABLE dummy;