1 | #include <Common/intExp.h> |
2 | |
3 | #include <Parsers/CommonParsers.h> |
4 | #include <Parsers/ParserSampleRatio.h> |
5 | #include <Parsers/ASTSampleRatio.h> |
6 | #include <IO/ReadHelpers.h> |
7 | |
8 | |
9 | namespace DB |
10 | { |
11 | |
12 | |
13 | static bool parseDecimal(const char * pos, const char * end, ASTSampleRatio::Rational & res) |
14 | { |
15 | UInt64 num_before = 0; |
16 | UInt64 num_after = 0; |
17 | Int64 exponent = 0; |
18 | |
19 | const char * pos_after_first_num = tryReadIntText(num_before, pos, end); |
20 | |
21 | bool has_num_before_point = pos_after_first_num > pos; |
22 | pos = pos_after_first_num; |
23 | bool has_point = pos < end && *pos == '.'; |
24 | |
25 | if (has_point) |
26 | ++pos; |
27 | |
28 | if (!has_num_before_point && !has_point) |
29 | return false; |
30 | |
31 | size_t number_of_digits_after_point = 0; |
32 | |
33 | if (has_point) |
34 | { |
35 | const char * pos_after_second_num = tryReadIntText(num_after, pos, end); |
36 | number_of_digits_after_point = pos_after_second_num - pos; |
37 | pos = pos_after_second_num; |
38 | } |
39 | |
40 | bool has_exponent = pos < end && (*pos == 'e' || *pos == 'E'); |
41 | |
42 | if (has_exponent) |
43 | { |
44 | ++pos; |
45 | const char * pos_after_exponent = tryReadIntText(exponent, pos, end); |
46 | |
47 | if (pos_after_exponent == pos) |
48 | return false; |
49 | } |
50 | |
51 | res.numerator = num_before * intExp10(number_of_digits_after_point) + num_after; |
52 | res.denominator = intExp10(number_of_digits_after_point); |
53 | |
54 | if (exponent > 0) |
55 | res.numerator *= intExp10(exponent); |
56 | if (exponent < 0) |
57 | res.denominator *= intExp10(-exponent); |
58 | |
59 | /// NOTE You do not need to remove the common power of ten from the numerator and denominator. |
60 | return true; |
61 | } |
62 | |
63 | |
64 | /** Possible options: |
65 | * |
66 | * 12345 |
67 | * - an integer |
68 | * |
69 | * 0.12345 |
70 | * .12345 |
71 | * 0. |
72 | * - fraction in ordinary decimal notation |
73 | * |
74 | * 1.23e-1 |
75 | * - fraction in scientific decimal notation |
76 | * |
77 | * 123 / 456 |
78 | * - fraction with an ordinary denominator |
79 | * |
80 | * Just in case, in the numerator and denominator of the fraction, we support the previous cases. |
81 | * Example: |
82 | * 123.0 / 456e0 |
83 | */ |
84 | bool ParserSampleRatio::parseImpl(Pos & pos, ASTPtr & node, Expected &) |
85 | { |
86 | ASTSampleRatio::Rational numerator; |
87 | ASTSampleRatio::Rational denominator; |
88 | ASTSampleRatio::Rational res; |
89 | |
90 | if (!parseDecimal(pos->begin, pos->end, numerator)) |
91 | return false; |
92 | ++pos; |
93 | |
94 | bool has_slash = pos->type == TokenType::Slash; |
95 | |
96 | if (has_slash) |
97 | { |
98 | ++pos; |
99 | |
100 | if (!parseDecimal(pos->begin, pos->end, denominator)) |
101 | return false; |
102 | ++pos; |
103 | |
104 | res.numerator = numerator.numerator * denominator.denominator; |
105 | res.denominator = numerator.denominator * denominator.numerator; |
106 | } |
107 | else |
108 | { |
109 | res = numerator; |
110 | } |
111 | |
112 | node = std::make_shared<ASTSampleRatio>(res); |
113 | return true; |
114 | } |
115 | |
116 | } |
117 | |