ClickHouse/dbms/src/Parsers/ASTSampleRatio.h

45 lines
1.0 KiB
C++
Raw Normal View History

#pragma once
#include <Parsers/IAST.h>
namespace DB
{
2017-05-27 17:27:16 +00:00
/** 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:
#ifdef __SIZEOF_INT128__
2017-05-27 17:27:16 +00:00
using BigNum = __uint128_t; /// Must contain the result of multiplying two UInt64.
#else
2017-12-26 19:22:16 +00:00
#warning "No uint128_t type. Sampling ratios cannot work correctly."
using BigNum = uint64_t;
#endif
struct Rational
{
BigNum numerator = 0;
BigNum denominator = 1;
};
Rational ratio;
2018-02-26 03:37:08 +00:00
ASTSampleRatio(Rational & ratio_) : ratio(ratio_) {}
String getID() const override { return "SampleRatio_" + toString(ratio); }
ASTPtr clone() const override { return std::make_shared<ASTSampleRatio>(*this); }
static String toString(BigNum num);
static String toString(Rational ratio);
2017-12-01 18:36:55 +00:00
void formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override
{
settings.ostr << toString(ratio);
}
};
}