imay commented on a change in pull request #1432: Add percentile_approx aggregate function URL: https://github.com/apache/incubator-doris/pull/1432#discussion_r301364484
########## File path: be/src/util/tdigest.h ########## @@ -0,0 +1,728 @@ +/* + * Licensed to Derrick R. Burns under one or more + * contributor license agreements. See the NOTICES 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. + */ + +// T-Digest : Percentile and Quantile Estimation of Big Data +// A new data structure for accurate on-line accumulation of rank-based statistics +// such as quantiles and trimmed means. +// See original paper: "Computing extremely accurate quantiles using t-digest" +// by Ted Dunning and Otmar Ertl for more details +// https://github.com/tdunning/t-digest/blob/07b8f2ca2be8d0a9f04df2feadad5ddc1bb73c88/docs/t-digest-paper/histo.pdf. +// https://github.com/derrickburns/tdigest + +#ifndef TDIGEST2_TDIGEST_H_ +#define TDIGEST2_TDIGEST_H_ + +#include <iostream> +#include <algorithm> +#include <cfloat> +#include <cmath> +#include <queue> +#include <utility> +#include <vector> +#include <memory> + +#include "common/logging.h" +#include "util/debug_util.h" +#include "udf/udf.h" + +namespace doris { + + using Value = double; + using Weight = double; + using Index = size_t; + + const size_t kHighWater = 40000; + + class Centroid { + public: + Centroid() : Centroid(0.0, 0.0) {} + + Centroid(Value mean, Weight weight) : mean_(mean), weight_(weight) {} + + inline Value mean() const noexcept { return mean_; } + + inline Weight weight() const noexcept { return weight_; } + + inline void add(const Centroid& c) { + DCHECK_GT(c.weight_, 0); + if( weight_ != 0.0 ) { + weight_ += c.weight_; + mean_ += c.weight_ * (c.mean_ - mean_) / weight_; + } else { + weight_ = c.weight_; + mean_ = c.mean_; + } + } + + private: + Value mean_ = 0; + Weight weight_ = 0; + }; + + struct CentroidList { + CentroidList(const std::vector<Centroid>& s) : iter(s.cbegin()), end(s.cend()) {} + std::vector<Centroid>::const_iterator iter; + std::vector<Centroid>::const_iterator end; + + bool advance() { return ++iter != end; } + }; + + class CentroidListComparator { + public: + CentroidListComparator() {} + + bool operator()(const CentroidList& left, const CentroidList& right) const { + return left.iter->mean() > right.iter->mean(); + } + }; + + using CentroidListQueue = std::priority_queue<CentroidList, std::vector<CentroidList>, CentroidListComparator>; + + struct CentroidComparator { + bool operator()(const Centroid& a, const Centroid& b) const { return a.mean() < b.mean(); } + }; + + class TDigest { Review comment: You should add more UT for this class ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
