1 | #pragma once |
---|---|
2 | |
3 | #include <Parsers/IAST.h> |
4 | |
5 | namespace DB |
6 | { |
7 | |
8 | /** Sampling factor in the form 0.1 or 1/10. |
9 | * It's important to save it as a rational number without converting it to IEEE-754. |
10 | */ |
11 | class ASTSampleRatio : public IAST |
12 | { |
13 | public: |
14 | #ifdef __SIZEOF_INT128__ |
15 | using BigNum = __uint128_t; /// Must contain the result of multiplying two UInt64. |
16 | #else |
17 | #warning "No uint128_t type. Sampling ratios cannot work correctly." |
18 | using BigNum = uint64_t; |
19 | #endif |
20 | |
21 | struct Rational |
22 | { |
23 | BigNum numerator = 0; |
24 | BigNum denominator = 1; |
25 | }; |
26 | |
27 | Rational ratio; |
28 | |
29 | ASTSampleRatio(Rational & ratio_) : ratio(ratio_) {} |
30 | |
31 | String getID(char delim) const override { return "SampleRatio"+ (delim + toString(ratio)); } |
32 | |
33 | ASTPtr clone() const override { return std::make_shared<ASTSampleRatio>(*this); } |
34 | |
35 | static String toString(BigNum num); |
36 | static String toString(Rational ratio); |
37 | |
38 | void formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override |
39 | { |
40 | settings.ostr << toString(ratio); |
41 | } |
42 | }; |
43 | |
44 | } |
45 |