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