xiaokang commented on code in PR #39546: URL: https://github.com/apache/doris/pull/39546#discussion_r1721182865
########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; Review Comment: doris common code style: `_interval = interval;` ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { Review Comment: It's not always right to return if key < 0. ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; Review Comment: I suggest you add fields that will be used in the future, eg. upper/lower, to be compatible with future versions' read/write. ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { + return; + } + if (key >= MAX_BUCKETS) { + throw doris::Exception(ErrorCode::INVALID_ARGUMENT, + "{} is too large to fit in the buckets", + value); + } + buckets[static_cast<unsigned>(key)]++; + } + + // merge + void merge(const AggregateFunctionLinearHistogramData& rhs) { + if (rhs.interval == 0) { + return; + } + + for (const auto& [key, count] : rhs.buckets) { + buckets[key] += count; + } + } + + // write + void write(BufferWritable& buf) const { + write_binary(offset, buf); + write_binary(interval, buf); + write_binary(buckets.size(), buf); + for (const auto& [key, count] : buckets) { + write_binary(key, buf); + write_binary(count, buf); + } + } + + // read + void read(BufferReadable& buf) { + read_binary(offset, buf); + read_binary(interval, buf); + size_t size; + read_binary(size, buf); + for (size_t i = 0; i < size; i++) { + unsigned key; + size_t count; + read_binary(key, buf); + read_binary(count, buf); + buckets[key] = count; + } + } + + // insert_result_into + void insert_result_into_test(IColumn& to) const { Review Comment: just for test? ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { + return; + } + if (key >= MAX_BUCKETS) { + throw doris::Exception(ErrorCode::INVALID_ARGUMENT, + "{} is too large to fit in the buckets", + value); + } + buckets[static_cast<unsigned>(key)]++; + } + + // merge + void merge(const AggregateFunctionLinearHistogramData& rhs) { + if (rhs.interval == 0) { + return; + } + + for (const auto& [key, count] : rhs.buckets) { + buckets[key] += count; + } + } + + // write + void write(BufferWritable& buf) const { + write_binary(offset, buf); + write_binary(interval, buf); + write_binary(buckets.size(), buf); + for (const auto& [key, count] : buckets) { + write_binary(key, buf); + write_binary(count, buf); + } + } + + // read + void read(BufferReadable& buf) { + read_binary(offset, buf); + read_binary(interval, buf); + size_t size; + read_binary(size, buf); + for (size_t i = 0; i < size; i++) { + unsigned key; + size_t count; + read_binary(key, buf); + read_binary(count, buf); + buckets[key] = count; + } + } + + // insert_result_into + void insert_result_into_test(IColumn& to) const { + auto& column = assert_cast<ColumnString&>(to); + std::string res = "result: "; + for (const auto& [key, count] : buckets) { + res += std::to_string(key * interval + offset) + ":" + std::to_string(count) + ","; + } + column.insert_data(res.c_str(), res.length()); + } + + void insert_result_into(IColumn& to) const { + rapidjson::Document doc; + doc.SetObject(); + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); + + unsigned num_buckets = buckets.empty() ? 0 : buckets.rbegin()->first + 1; + doc.AddMember("num_buckets", num_buckets, allocator); + + rapidjson::Value bucket_arr(rapidjson::kArrayType); + bucket_arr.Reserve(num_buckets, allocator); + + unsigned idx = 0; + auto data_type = std::make_shared<DataTypeFloat64>(); + double left = offset; + std::string left_str = data_type->to_string(left); + size_t count = 0; + size_t pre_sum = 0; + rapidjson::Value buf; + + for (const auto& [key, count_] : buckets) { + for (; idx <= key; ++idx) { + rapidjson::Value bucket_json(rapidjson::kObjectType); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("lower", buf, allocator); + left += interval; + left_str = data_type->to_string(left); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("upper", buf, allocator); + count = (idx == key) ? count_ : 0; + bucket_json.AddMember("count", count, allocator); + bucket_json.AddMember("pre_sum", pre_sum, allocator); + pre_sum += count; + + bucket_arr.PushBack(bucket_json, allocator); + } + } + + doc.AddMember("buckets", bucket_arr, allocator); + + rapidjson::StringBuffer buffer; + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); + doc.Accept(writer); + auto res = std::string(buffer.GetString()); Review Comment: avoid copy string here. ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { + return; + } + if (key >= MAX_BUCKETS) { + throw doris::Exception(ErrorCode::INVALID_ARGUMENT, + "{} is too large to fit in the buckets", + value); + } + buckets[static_cast<unsigned>(key)]++; + } + + // merge + void merge(const AggregateFunctionLinearHistogramData& rhs) { + if (rhs.interval == 0) { + return; + } + + for (const auto& [key, count] : rhs.buckets) { + buckets[key] += count; + } + } + + // write + void write(BufferWritable& buf) const { + write_binary(offset, buf); + write_binary(interval, buf); + write_binary(buckets.size(), buf); + for (const auto& [key, count] : buckets) { + write_binary(key, buf); + write_binary(count, buf); + } + } + + // read + void read(BufferReadable& buf) { + read_binary(offset, buf); + read_binary(interval, buf); + size_t size; + read_binary(size, buf); + for (size_t i = 0; i < size; i++) { + unsigned key; + size_t count; + read_binary(key, buf); + read_binary(count, buf); + buckets[key] = count; + } + } + + // insert_result_into + void insert_result_into_test(IColumn& to) const { + auto& column = assert_cast<ColumnString&>(to); + std::string res = "result: "; + for (const auto& [key, count] : buckets) { + res += std::to_string(key * interval + offset) + ":" + std::to_string(count) + ","; + } + column.insert_data(res.c_str(), res.length()); + } + + void insert_result_into(IColumn& to) const { + rapidjson::Document doc; + doc.SetObject(); + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); + + unsigned num_buckets = buckets.empty() ? 0 : buckets.rbegin()->first + 1; + doc.AddMember("num_buckets", num_buckets, allocator); + + rapidjson::Value bucket_arr(rapidjson::kArrayType); + bucket_arr.Reserve(num_buckets, allocator); + + unsigned idx = 0; + auto data_type = std::make_shared<DataTypeFloat64>(); + double left = offset; + std::string left_str = data_type->to_string(left); + size_t count = 0; + size_t pre_sum = 0; + rapidjson::Value buf; + + for (const auto& [key, count_] : buckets) { + for (; idx <= key; ++idx) { + rapidjson::Value bucket_json(rapidjson::kObjectType); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("lower", buf, allocator); + left += interval; + left_str = data_type->to_string(left); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("upper", buf, allocator); + count = (idx == key) ? count_ : 0; + bucket_json.AddMember("count", count, allocator); + bucket_json.AddMember("pre_sum", pre_sum, allocator); + pre_sum += count; Review Comment: I think `pre_sum` should include current `count` ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { + return; + } + if (key >= MAX_BUCKETS) { + throw doris::Exception(ErrorCode::INVALID_ARGUMENT, + "{} is too large to fit in the buckets", + value); + } + buckets[static_cast<unsigned>(key)]++; + } + + // merge + void merge(const AggregateFunctionLinearHistogramData& rhs) { + if (rhs.interval == 0) { + return; + } + + for (const auto& [key, count] : rhs.buckets) { + buckets[key] += count; + } + } + + // write + void write(BufferWritable& buf) const { + write_binary(offset, buf); + write_binary(interval, buf); + write_binary(buckets.size(), buf); + for (const auto& [key, count] : buckets) { + write_binary(key, buf); + write_binary(count, buf); + } + } + + // read + void read(BufferReadable& buf) { + read_binary(offset, buf); + read_binary(interval, buf); + size_t size; + read_binary(size, buf); + for (size_t i = 0; i < size; i++) { + unsigned key; + size_t count; + read_binary(key, buf); + read_binary(count, buf); + buckets[key] = count; + } + } + + // insert_result_into + void insert_result_into_test(IColumn& to) const { + auto& column = assert_cast<ColumnString&>(to); + std::string res = "result: "; + for (const auto& [key, count] : buckets) { + res += std::to_string(key * interval + offset) + ":" + std::to_string(count) + ","; + } + column.insert_data(res.c_str(), res.length()); + } + + void insert_result_into(IColumn& to) const { + rapidjson::Document doc; + doc.SetObject(); + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); + + unsigned num_buckets = buckets.empty() ? 0 : buckets.rbegin()->first + 1; + doc.AddMember("num_buckets", num_buckets, allocator); + + rapidjson::Value bucket_arr(rapidjson::kArrayType); + bucket_arr.Reserve(num_buckets, allocator); + + unsigned idx = 0; + auto data_type = std::make_shared<DataTypeFloat64>(); + double left = offset; + std::string left_str = data_type->to_string(left); + size_t count = 0; + size_t pre_sum = 0; + rapidjson::Value buf; + + for (const auto& [key, count_] : buckets) { + for (; idx <= key; ++idx) { + rapidjson::Value bucket_json(rapidjson::kObjectType); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("lower", buf, allocator); + left += interval; + left_str = data_type->to_string(left); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("upper", buf, allocator); + count = (idx == key) ? count_ : 0; + bucket_json.AddMember("count", count, allocator); + bucket_json.AddMember("pre_sum", pre_sum, allocator); + pre_sum += count; + + bucket_arr.PushBack(bucket_json, allocator); + } + } + + doc.AddMember("buckets", bucket_arr, allocator); + + rapidjson::StringBuffer buffer; + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); + doc.Accept(writer); + auto res = std::string(buffer.GetString()); + + auto& column = assert_cast<ColumnString&>(to); + column.insert_data(res.c_str(), res.length()); + } +}; + +template <typename T, typename Data, bool has_offset> +class AggregateFunctionLinearHistogram final + : public IAggregateFunctionDataHelper<Data, AggregateFunctionLinearHistogram<T, Data, has_offset>> { +public: + using ColVecType = ColumnVectorOrDecimal<T>; + + AggregateFunctionLinearHistogram(const DataTypes& argument_types_) + : IAggregateFunctionDataHelper<Data, AggregateFunctionLinearHistogram<T, Data, has_offset>>(argument_types_) {} + + std::string get_name() const override { return "linear_histogram"; } + + DataTypePtr get_return_type() const override { return std::make_shared<DataTypeString>(); } + + void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, + Arena* arena) const override { + double interval = assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]) Review Comment: refer to const optimization in other agg functions ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { + return; + } + if (key >= MAX_BUCKETS) { + throw doris::Exception(ErrorCode::INVALID_ARGUMENT, + "{} is too large to fit in the buckets", + value); + } + buckets[static_cast<unsigned>(key)]++; + } + + // merge + void merge(const AggregateFunctionLinearHistogramData& rhs) { + if (rhs.interval == 0) { + return; + } + + for (const auto& [key, count] : rhs.buckets) { + buckets[key] += count; + } + } + + // write + void write(BufferWritable& buf) const { + write_binary(offset, buf); + write_binary(interval, buf); + write_binary(buckets.size(), buf); + for (const auto& [key, count] : buckets) { + write_binary(key, buf); + write_binary(count, buf); + } + } + + // read + void read(BufferReadable& buf) { + read_binary(offset, buf); + read_binary(interval, buf); + size_t size; + read_binary(size, buf); + for (size_t i = 0; i < size; i++) { + unsigned key; + size_t count; + read_binary(key, buf); + read_binary(count, buf); + buckets[key] = count; + } + } + + // insert_result_into + void insert_result_into_test(IColumn& to) const { + auto& column = assert_cast<ColumnString&>(to); + std::string res = "result: "; + for (const auto& [key, count] : buckets) { + res += std::to_string(key * interval + offset) + ":" + std::to_string(count) + ","; + } + column.insert_data(res.c_str(), res.length()); + } + + void insert_result_into(IColumn& to) const { + rapidjson::Document doc; + doc.SetObject(); + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); + + unsigned num_buckets = buckets.empty() ? 0 : buckets.rbegin()->first + 1; + doc.AddMember("num_buckets", num_buckets, allocator); + + rapidjson::Value bucket_arr(rapidjson::kArrayType); + bucket_arr.Reserve(num_buckets, allocator); + + unsigned idx = 0; + auto data_type = std::make_shared<DataTypeFloat64>(); + double left = offset; + std::string left_str = data_type->to_string(left); + size_t count = 0; + size_t pre_sum = 0; + rapidjson::Value buf; + + for (const auto& [key, count_] : buckets) { + for (; idx <= key; ++idx) { + rapidjson::Value bucket_json(rapidjson::kObjectType); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("lower", buf, allocator); + left += interval; + left_str = data_type->to_string(left); + buf.SetString(left_str.data(), static_cast<rapidjson::SizeType>(left_str.size()), allocator); + bucket_json.AddMember("upper", buf, allocator); + count = (idx == key) ? count_ : 0; + bucket_json.AddMember("count", count, allocator); + bucket_json.AddMember("pre_sum", pre_sum, allocator); + pre_sum += count; + + bucket_arr.PushBack(bucket_json, allocator); + } + } + + doc.AddMember("buckets", bucket_arr, allocator); + + rapidjson::StringBuffer buffer; + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); + doc.Accept(writer); + auto res = std::string(buffer.GetString()); + + auto& column = assert_cast<ColumnString&>(to); + column.insert_data(res.c_str(), res.length()); + } +}; + +template <typename T, typename Data, bool has_offset> +class AggregateFunctionLinearHistogram final + : public IAggregateFunctionDataHelper<Data, AggregateFunctionLinearHistogram<T, Data, has_offset>> { +public: + using ColVecType = ColumnVectorOrDecimal<T>; + + AggregateFunctionLinearHistogram(const DataTypes& argument_types_) + : IAggregateFunctionDataHelper<Data, AggregateFunctionLinearHistogram<T, Data, has_offset>>(argument_types_) {} + + std::string get_name() const override { return "linear_histogram"; } + + DataTypePtr get_return_type() const override { return std::make_shared<DataTypeString>(); } + + void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, + Arena* arena) const override { + double interval = assert_cast<const ColumnFloat64&, TypeCheckOnRelease::DISABLE>(*columns[1]) Review Comment: should not parse const arguments like interval and offset for each add. ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; Review Comment: You can also add a version field to control read/write. ########## be/src/vec/aggregate_functions/aggregate_function_linear_histogram.h: ########## @@ -0,0 +1,227 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <map> + +#include <rapidjson/document.h> +#include <rapidjson/prettywriter.h> +#include <rapidjson/stringbuffer.h> + +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/io/io_helper.h" + +// TODO: 支持时间类型 +// TODO: 完善单元测试 +// TODO: 确定最大桶数 + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionLinearHistogramData { + // max buckets number + const static unsigned MAX_BUCKETS = std::numeric_limits<unsigned>::max(); + +private: + // influxdb use double + double interval; + double offset; + std::map<unsigned, size_t> buckets; + +public: + // reset + void reset() { + offset = 0; + interval = 0; + buckets.clear(); + } + + void set_parameters(double interval_, double offset_) { + interval = interval_; + offset = offset_; + } + + // add + void add(const T& value) { + auto key = std::floor((value - offset) / interval); + if (key < 0) { + return; + } + if (key >= MAX_BUCKETS) { + throw doris::Exception(ErrorCode::INVALID_ARGUMENT, + "{} is too large to fit in the buckets", + value); + } + buckets[static_cast<unsigned>(key)]++; + } + + // merge + void merge(const AggregateFunctionLinearHistogramData& rhs) { + if (rhs.interval == 0) { + return; + } + + for (const auto& [key, count] : rhs.buckets) { + buckets[key] += count; + } + } + + // write + void write(BufferWritable& buf) const { + write_binary(offset, buf); + write_binary(interval, buf); + write_binary(buckets.size(), buf); + for (const auto& [key, count] : buckets) { + write_binary(key, buf); + write_binary(count, buf); + } + } + + // read + void read(BufferReadable& buf) { + read_binary(offset, buf); + read_binary(interval, buf); + size_t size; + read_binary(size, buf); + for (size_t i = 0; i < size; i++) { + unsigned key; + size_t count; + read_binary(key, buf); + read_binary(count, buf); + buckets[key] = count; + } + } + + // insert_result_into + void insert_result_into_test(IColumn& to) const { + auto& column = assert_cast<ColumnString&>(to); + std::string res = "result: "; + for (const auto& [key, count] : buckets) { + res += std::to_string(key * interval + offset) + ":" + std::to_string(count) + ","; + } + column.insert_data(res.c_str(), res.length()); + } + + void insert_result_into(IColumn& to) const { + rapidjson::Document doc; + doc.SetObject(); + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); + + unsigned num_buckets = buckets.empty() ? 0 : buckets.rbegin()->first + 1; Review Comment: why not just buckets.size()? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org