aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/ASTSampleRatio.h
blob: 220f938335b3ccab49c783642407b10b271152db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include <Parsers/IAST.h>


namespace DB
{

/** Sampling factor in the form 0.1 or 1/10.
  * It's important to save it as a rational number without converting it to IEEE-754.
  */
class ASTSampleRatio : public IAST
{
public:
    using BigNum = __uint128_t;    /// Must contain the result of multiplying two UInt64.

    struct Rational
    {
        BigNum numerator = 0;
        BigNum denominator = 1;
    };

    Rational ratio;

    explicit ASTSampleRatio(const Rational & ratio_) : ratio(ratio_) {}

    String getID(char delim) const override { return "SampleRatio" + (delim + toString(ratio)); }

    ASTPtr clone() const override { return std::make_shared<ASTSampleRatio>(*this); }

    static String toString(BigNum num);
    static String toString(Rational ratio);

    void formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override;
};

inline bool operator==(const ASTSampleRatio::Rational & lhs, const ASTSampleRatio::Rational & rhs)
{
    return lhs.numerator == rhs.numerator && lhs.denominator == rhs.denominator;
}

inline bool operator!=(const ASTSampleRatio::Rational & lhs, const ASTSampleRatio::Rational & rhs)
{
    return !(lhs == rhs);
}

}