diff --git a/src/AggregateFunctions/AggregateFunctionSparkbar.h b/src/AggregateFunctions/AggregateFunctionSparkbar.h index 344bcce3ec2..87db938dac4 100644 --- a/src/AggregateFunctions/AggregateFunctionSparkbar.h +++ b/src/AggregateFunctions/AggregateFunctionSparkbar.h @@ -29,10 +29,9 @@ struct AggregateFunctionSparkbarData it->second += y; else points.emplace(x, y); - if (y < min_y) - min_y = y; - if (y > max_y) - max_y = y; + + min_y = std::min(y, min_y); + max_y = std::max(y, max_y); } void merge(const AggregateFunctionSparkbarData & other) @@ -49,11 +48,8 @@ struct AggregateFunctionSparkbarData points.emplace(point.first, point.second); } - if (other.min_y < min_y) - min_y = other.min_y; - - if (other.max_y > max_y) - max_y = other.max_y; + min_y = std::min(other.min_y, min_y); + max_y = std::max(other.max_y, max_y); } void serialize(WriteBuffer & buf) const @@ -133,6 +129,8 @@ private: String render(const AggregateFunctionSparkbarData & data) const { String value; + if (data.points.empty()) + return value; X local_min_x = data.points.begin()->first; X local_max_x = data.points.rbegin()->first; size_t diff_x = local_max_x - local_min_x; diff --git a/tests/queries/0_stateless/02003_aggregation_spark_bar.reference b/tests/queries/0_stateless/02003_aggregation_spark_bar.reference index f027083dd1b..8e13e4b6f71 100644 --- a/tests/queries/0_stateless/02003_aggregation_spark_bar.reference +++ b/tests/queries/0_stateless/02003_aggregation_spark_bar.reference @@ -9,3 +9,10 @@ ▁▅▄▃██▅ ▁ ▁▄▄▂▅▇█▂ ▂ ▁▄▅▂▃▇▆█ ▂ + + + +▁ +▆█▁▃ +▅▁▂█▇ +▁▂▇▆█ ▁ diff --git a/tests/queries/0_stateless/02003_aggregation_spark_bar.sql b/tests/queries/0_stateless/02003_aggregation_spark_bar.sql index 5457a433fe8..fb4100d8bb3 100644 --- a/tests/queries/0_stateless/02003_aggregation_spark_bar.sql +++ b/tests/queries/0_stateless/02003_aggregation_spark_bar.sql @@ -1,17 +1,26 @@ -drop table if exists spark_bar_test; +DROP TABLE IF EXISTS spark_bar_test; CREATE TABLE spark_bar_test (`cnt` UInt64,`event_date` Date) ENGINE = MergeTree ORDER BY event_date SETTINGS index_granularity = 8192; -insert into spark_bar_test values(1,'2020-01-01'),(4,'2020-01-02'),(5,'2020-01-03'),(2,'2020-01-04'),(3,'2020-01-05'),(7,'2020-01-06'),(6,'2020-01-07'),(8,'2020-01-08'),(2,'2020-01-11'); +INSERT INTO spark_bar_test VALUES(1,'2020-01-01'),(4,'2020-01-02'),(5,'2020-01-03'),(2,'2020-01-04'),(3,'2020-01-05'),(7,'2020-01-06'),(6,'2020-01-07'),(8,'2020-01-08'),(2,'2020-01-11'); -select sparkbar(1)(event_date,cnt) from spark_bar_test; -select sparkbar(2)(event_date,cnt) from spark_bar_test; -select sparkbar(3)(event_date,cnt) from spark_bar_test; -select sparkbar(4)(event_date,cnt) from spark_bar_test; -select sparkbar(5)(event_date,cnt) from spark_bar_test; -select sparkbar(6)(event_date,cnt) from spark_bar_test; -select sparkbar(7)(event_date,cnt) from spark_bar_test; -select sparkbar(8)(event_date,cnt) from spark_bar_test; -select sparkbar(9)(event_date,cnt) from spark_bar_test; -select sparkbar(10)(event_date,cnt) from spark_bar_test; -select sparkbar(11)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(1)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(2)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(3)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(4)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(5)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(6)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(7)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(8)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(9)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(10)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(11)(event_date,cnt) from spark_bar_test; + +SELECT sparkbar(11,2,5)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(11,3,7)(event_date,cnt) from spark_bar_test; +SELECT sparkbar(11,4,11)(event_date,cnt) from spark_bar_test; + +SELECT sparkbar(11,toDate('2020-01-02'),toDate('2020-01-02'))(event_date,cnt) from spark_bar_test; +SELECT sparkbar(11,toDate('2020-01-02'),toDate('2020-01-05'))(event_date,cnt) from spark_bar_test; +SELECT sparkbar(11,toDate('2020-01-03'),toDate('2020-01-07'))(event_date,cnt) from spark_bar_test; +SELECT sparkbar(11,toDate('2020-01-04'),toDate('2020-01-11'))(event_date,cnt) from spark_bar_test;