blob: 9c653eefedbeb1a0ac05c916d36d722ee47a3886 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
#include <Common/IntervalKind.h>
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
extern const int BAD_ARGUMENTS;
}
Int64 IntervalKind::toAvgNanoseconds() const
{
static constexpr Int64 NANOSECONDS_PER_MICROSECOND = 1000;
static constexpr auto NANOSECONDS_PER_MILLISECOND = NANOSECONDS_PER_MICROSECOND * 1000;
static constexpr auto NANOSECONDS_PER_SECOND = NANOSECONDS_PER_MILLISECOND * 1000;
switch (kind)
{
case IntervalKind::Millisecond:
return NANOSECONDS_PER_MILLISECOND;
case IntervalKind::Microsecond:
return NANOSECONDS_PER_MICROSECOND;
case IntervalKind::Nanosecond:
return 1;
default:
return toAvgSeconds() * NANOSECONDS_PER_SECOND;
}
UNREACHABLE();
}
Int32 IntervalKind::toAvgSeconds() const
{
switch (kind)
{
case IntervalKind::Nanosecond:
case IntervalKind::Microsecond:
case IntervalKind::Millisecond: return 0;
case IntervalKind::Second: return 1;
case IntervalKind::Minute: return 60;
case IntervalKind::Hour: return 3600;
case IntervalKind::Day: return 86400;
case IntervalKind::Week: return 604800;
case IntervalKind::Month: return 2629746; /// Exactly 1/12 of a year.
case IntervalKind::Quarter: return 7889238; /// Exactly 1/4 of a year.
case IntervalKind::Year: return 31556952; /// The average length of a Gregorian year is equal to 365.2425 days
}
UNREACHABLE();
}
Float64 IntervalKind::toSeconds() const
{
switch (kind)
{
case IntervalKind::Nanosecond:
return 0.000000001;
case IntervalKind::Microsecond:
return 0.000001;
case IntervalKind::Millisecond:
return 0.001;
case IntervalKind::Second:
return 1;
case IntervalKind::Minute:
return 60;
case IntervalKind::Hour:
return 3600;
case IntervalKind::Day:
return 86400;
case IntervalKind::Week:
return 604800;
default:
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Not possible to get precise number of seconds in non-precise interval");
}
UNREACHABLE();
}
bool IntervalKind::isFixedLength() const
{
switch (kind)
{
case IntervalKind::Nanosecond:
case IntervalKind::Microsecond:
case IntervalKind::Millisecond:
case IntervalKind::Second:
case IntervalKind::Minute:
case IntervalKind::Hour:
case IntervalKind::Day:
case IntervalKind::Week: return true;
case IntervalKind::Month:
case IntervalKind::Quarter:
case IntervalKind::Year: return false;
}
UNREACHABLE();
}
IntervalKind IntervalKind::fromAvgSeconds(Int64 num_seconds)
{
if (num_seconds)
{
if (!(num_seconds % 31556952))
return IntervalKind::Year;
if (!(num_seconds % 7889238))
return IntervalKind::Quarter;
if (!(num_seconds % 2629746))
return IntervalKind::Month;
if (!(num_seconds % 604800))
return IntervalKind::Week;
if (!(num_seconds % 86400))
return IntervalKind::Day;
if (!(num_seconds % 3600))
return IntervalKind::Hour;
if (!(num_seconds % 60))
return IntervalKind::Minute;
}
return IntervalKind::Second;
}
const char * IntervalKind::toKeyword() const
{
switch (kind)
{
case IntervalKind::Nanosecond: return "NANOSECOND";
case IntervalKind::Microsecond: return "MICROSECOND";
case IntervalKind::Millisecond: return "MILLISECOND";
case IntervalKind::Second: return "SECOND";
case IntervalKind::Minute: return "MINUTE";
case IntervalKind::Hour: return "HOUR";
case IntervalKind::Day: return "DAY";
case IntervalKind::Week: return "WEEK";
case IntervalKind::Month: return "MONTH";
case IntervalKind::Quarter: return "QUARTER";
case IntervalKind::Year: return "YEAR";
}
UNREACHABLE();
}
const char * IntervalKind::toLowercasedKeyword() const
{
switch (kind)
{
case IntervalKind::Nanosecond: return "nanosecond";
case IntervalKind::Microsecond: return "microsecond";
case IntervalKind::Millisecond: return "millisecond";
case IntervalKind::Second: return "second";
case IntervalKind::Minute: return "minute";
case IntervalKind::Hour: return "hour";
case IntervalKind::Day: return "day";
case IntervalKind::Week: return "week";
case IntervalKind::Month: return "month";
case IntervalKind::Quarter: return "quarter";
case IntervalKind::Year: return "year";
}
UNREACHABLE();
}
const char * IntervalKind::toDateDiffUnit() const
{
switch (kind)
{
case IntervalKind::Nanosecond:
return "nanosecond";
case IntervalKind::Microsecond:
return "microsecond";
case IntervalKind::Millisecond:
return "millisecond";
case IntervalKind::Second:
return "second";
case IntervalKind::Minute:
return "minute";
case IntervalKind::Hour:
return "hour";
case IntervalKind::Day:
return "day";
case IntervalKind::Week:
return "week";
case IntervalKind::Month:
return "month";
case IntervalKind::Quarter:
return "quarter";
case IntervalKind::Year:
return "year";
}
UNREACHABLE();
}
const char * IntervalKind::toNameOfFunctionToIntervalDataType() const
{
switch (kind)
{
case IntervalKind::Nanosecond:
return "toIntervalNanosecond";
case IntervalKind::Microsecond:
return "toIntervalMicrosecond";
case IntervalKind::Millisecond:
return "toIntervalMillisecond";
case IntervalKind::Second:
return "toIntervalSecond";
case IntervalKind::Minute:
return "toIntervalMinute";
case IntervalKind::Hour:
return "toIntervalHour";
case IntervalKind::Day:
return "toIntervalDay";
case IntervalKind::Week:
return "toIntervalWeek";
case IntervalKind::Month:
return "toIntervalMonth";
case IntervalKind::Quarter:
return "toIntervalQuarter";
case IntervalKind::Year:
return "toIntervalYear";
}
UNREACHABLE();
}
const char * IntervalKind::toNameOfFunctionExtractTimePart() const
{
switch (kind)
{
case IntervalKind::Nanosecond:
return "toNanosecond";
case IntervalKind::Microsecond:
return "toMicrosecond";
case IntervalKind::Millisecond:
return "toMillisecond";
case IntervalKind::Second:
return "toSecond";
case IntervalKind::Minute:
return "toMinute";
case IntervalKind::Hour:
return "toHour";
case IntervalKind::Day:
return "toDayOfMonth";
case IntervalKind::Week:
// TODO: SELECT toRelativeWeekNum(toDate('2017-06-15')) - toRelativeWeekNum(toStartOfYear(toDate('2017-06-15')))
// else if (ParserKeyword("WEEK").ignore(pos, expected))
// function_name = "toRelativeWeekNum";
throw Exception(ErrorCodes::SYNTAX_ERROR, "The syntax 'EXTRACT(WEEK FROM date)' is not supported, cannot extract the number of a week");
case IntervalKind::Month:
return "toMonth";
case IntervalKind::Quarter:
return "toQuarter";
case IntervalKind::Year:
return "toYear";
}
UNREACHABLE();
}
bool IntervalKind::tryParseString(const std::string & kind, IntervalKind::Kind & result)
{
if ("nanosecond" == kind)
{
result = IntervalKind::Nanosecond;
return true;
}
if ("microsecond" == kind)
{
result = IntervalKind::Microsecond;
return true;
}
if ("millisecond" == kind)
{
result = IntervalKind::Millisecond;
return true;
}
if ("second" == kind)
{
result = IntervalKind::Second;
return true;
}
if ("minute" == kind)
{
result = IntervalKind::Minute;
return true;
}
if ("hour" == kind)
{
result = IntervalKind::Hour;
return true;
}
if ("day" == kind)
{
result = IntervalKind::Day;
return true;
}
if ("week" == kind)
{
result = IntervalKind::Week;
return true;
}
if ("month" == kind)
{
result = IntervalKind::Month;
return true;
}
if ("quarter" == kind)
{
result = IntervalKind::Quarter;
return true;
}
if ("year" == kind)
{
result = IntervalKind::Year;
return true;
}
return false;
}
}
|