Coverage Report

Created: 2024-11-20 15:52

/root/doris/be/src/util/quantile_state.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <stddef.h>
21
#include <stdint.h>
22
23
#include <algorithm>
24
#include <memory>
25
#include <vector>
26
27
#include "slice.h"
28
29
namespace doris {
30
31
class TDigest;
32
33
const static int QUANTILE_STATE_EXPLICIT_NUM = 2048;
34
const static int QUANTILE_STATE_COMPRESSION_MIN = 2048;
35
const static int QUANTILE_STATE_COMPRESSION_MAX = 10000;
36
37
enum QuantileStateType {
38
    EMPTY = 0,
39
    SINGLE = 1,   // single element
40
    EXPLICIT = 2, // more than one elements,stored in vector
41
    TDIGEST = 3   // TDIGEST object
42
};
43
44
class QuantileState {
45
public:
46
    QuantileState();
47
    explicit QuantileState(float compression);
48
    explicit QuantileState(const Slice& slice);
49
0
    QuantileState& operator=(const QuantileState& other) noexcept = default;
50
44.0k
    QuantileState(const QuantileState& other) noexcept = default;
51
0
    QuantileState& operator=(QuantileState&& other) noexcept = default;
52
34.8k
    QuantileState(QuantileState&& other) noexcept = default;
53
54
    void set_compression(float compression);
55
    bool deserialize(const Slice& slice);
56
    size_t serialize(uint8_t* dst) const;
57
    void merge(const QuantileState& other);
58
    void add_value(const double& value);
59
    void clear();
60
    bool is_valid(const Slice& slice);
61
    size_t get_serialized_size();
62
    double get_value_by_percentile(float percentile) const;
63
    double get_explicit_value_by_percentile(float percentile) const;
64
200k
    ~QuantileState() = default;
65
66
private:
67
    QuantileStateType _type = EMPTY;
68
    std::shared_ptr<TDigest> _tdigest_ptr;
69
    double _single_data;
70
    std::vector<double> _explicit_data;
71
    float _compression;
72
};
73
74
} // namespace doris