2018-12-27 00:02:11 +00:00
|
|
|
#include <Common/intExp.h>
|
2016-12-24 01:03:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ParserSampleRatio.h>
|
|
|
|
#include <Parsers/ASTSampleRatio.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2015-11-19 21:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
static bool parseDecimal(const char * pos, const char * end, ASTSampleRatio::Rational & res)
|
2015-11-19 21:34:53 +00:00
|
|
|
{
|
|
|
|
UInt64 num_before = 0;
|
|
|
|
UInt64 num_after = 0;
|
|
|
|
Int64 exponent = 0;
|
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
const char * pos_after_first_num = tryReadIntText(num_before, pos, end);
|
2015-11-19 21:34:53 +00:00
|
|
|
|
|
|
|
bool has_num_before_point = pos_after_first_num > pos;
|
|
|
|
pos = pos_after_first_num;
|
|
|
|
bool has_point = pos < end && *pos == '.';
|
|
|
|
|
|
|
|
if (has_point)
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (!has_num_before_point && !has_point)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t number_of_digits_after_point = 0;
|
|
|
|
|
|
|
|
if (has_point)
|
|
|
|
{
|
2017-07-12 01:49:20 +00:00
|
|
|
const char * pos_after_second_num = tryReadIntText(num_after, pos, end);
|
2015-11-19 21:34:53 +00:00
|
|
|
number_of_digits_after_point = pos_after_second_num - pos;
|
|
|
|
pos = pos_after_second_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_exponent = pos < end && (*pos == 'e' || *pos == 'E');
|
|
|
|
|
|
|
|
if (has_exponent)
|
|
|
|
{
|
|
|
|
++pos;
|
2017-07-12 01:49:20 +00:00
|
|
|
const char * pos_after_exponent = tryReadIntText(exponent, pos, end);
|
2015-11-19 21:34:53 +00:00
|
|
|
|
|
|
|
if (pos_after_exponent == pos)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:46:34 +00:00
|
|
|
res.numerator = num_before * intExp10(number_of_digits_after_point) + num_after;
|
|
|
|
res.denominator = intExp10(number_of_digits_after_point);
|
2015-11-19 21:34:53 +00:00
|
|
|
|
|
|
|
if (exponent > 0)
|
2017-12-21 23:46:34 +00:00
|
|
|
res.numerator *= intExp10(exponent);
|
2015-11-19 21:34:53 +00:00
|
|
|
if (exponent < 0)
|
2017-12-21 23:46:34 +00:00
|
|
|
res.denominator *= intExp10(-exponent);
|
2015-11-19 21:34:53 +00:00
|
|
|
|
2017-06-13 04:12:51 +00:00
|
|
|
/// NOTE You do not need to remove the common power of ten from the numerator and denominator.
|
2015-11-19 21:34:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** Possible options:
|
2015-11-19 21:34:53 +00:00
|
|
|
*
|
|
|
|
* 12345
|
2017-04-02 17:37:49 +00:00
|
|
|
* - an integer
|
2015-11-19 21:34:53 +00:00
|
|
|
*
|
|
|
|
* 0.12345
|
|
|
|
* .12345
|
|
|
|
* 0.
|
2017-04-02 17:37:49 +00:00
|
|
|
* - fraction in ordinary decimal notation
|
2015-11-19 21:34:53 +00:00
|
|
|
*
|
|
|
|
* 1.23e-1
|
2017-06-13 04:12:51 +00:00
|
|
|
* - fraction in scientific decimal notation
|
2015-11-19 21:34:53 +00:00
|
|
|
*
|
|
|
|
* 123 / 456
|
2017-04-02 17:37:49 +00:00
|
|
|
* - fraction with an ordinary denominator
|
2015-11-19 21:34:53 +00:00
|
|
|
*
|
2017-04-02 17:37:49 +00:00
|
|
|
* Just in case, in the numerator and denominator of the fraction, we support the previous cases.
|
|
|
|
* Example:
|
2015-11-19 21:34:53 +00:00
|
|
|
* 123.0 / 456e0
|
|
|
|
*/
|
2017-12-01 18:36:55 +00:00
|
|
|
bool ParserSampleRatio::parseImpl(Pos & pos, ASTPtr & node, Expected &)
|
2015-11-19 21:34:53 +00:00
|
|
|
{
|
|
|
|
ASTSampleRatio::Rational numerator;
|
|
|
|
ASTSampleRatio::Rational denominator;
|
|
|
|
ASTSampleRatio::Rational res;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!parseDecimal(pos->begin, pos->end, numerator))
|
2015-11-19 21:34:53 +00:00
|
|
|
return false;
|
2017-07-13 01:12:13 +00:00
|
|
|
++pos;
|
2015-11-19 21:34:53 +00:00
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
bool has_slash = pos->type == TokenType::Slash;
|
2015-11-19 21:34:53 +00:00
|
|
|
|
|
|
|
if (has_slash)
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!parseDecimal(pos->begin, pos->end, denominator))
|
2015-11-19 21:34:53 +00:00
|
|
|
return false;
|
2017-07-13 01:12:13 +00:00
|
|
|
++pos;
|
2015-11-19 21:34:53 +00:00
|
|
|
|
|
|
|
res.numerator = numerator.numerator * denominator.denominator;
|
|
|
|
res.denominator = numerator.denominator * denominator.numerator;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = numerator;
|
|
|
|
}
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
node = std::make_shared<ASTSampleRatio>(res);
|
2015-11-19 21:34:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|