1 | #include <Common/IntervalKind.h> |
---|---|
2 | #include <Common/Exception.h> |
3 | |
4 | |
5 | namespace DB |
6 | { |
7 | namespace ErrorCodes |
8 | { |
9 | extern const int SYNTAX_ERROR; |
10 | } |
11 | |
12 | const char * IntervalKind::toString() const |
13 | { |
14 | switch (kind) |
15 | { |
16 | case IntervalKind::Second: return "Second"; |
17 | case IntervalKind::Minute: return "Minute"; |
18 | case IntervalKind::Hour: return "Hour"; |
19 | case IntervalKind::Day: return "Day"; |
20 | case IntervalKind::Week: return "Week"; |
21 | case IntervalKind::Month: return "Month"; |
22 | case IntervalKind::Quarter: return "Quarter"; |
23 | case IntervalKind::Year: return "Year"; |
24 | } |
25 | __builtin_unreachable(); |
26 | } |
27 | |
28 | |
29 | Int32 IntervalKind::toAvgSeconds() const |
30 | { |
31 | switch (kind) |
32 | { |
33 | case IntervalKind::Second: return 1; |
34 | case IntervalKind::Minute: return 60; |
35 | case IntervalKind::Hour: return 3600; |
36 | case IntervalKind::Day: return 86400; |
37 | case IntervalKind::Week: return 604800; |
38 | case IntervalKind::Month: return 2629746; /// Exactly 1/12 of a year. |
39 | case IntervalKind::Quarter: return 7889238; /// Exactly 1/4 of a year. |
40 | case IntervalKind::Year: return 31556952; /// The average length of a Gregorian year is equal to 365.2425 days |
41 | } |
42 | __builtin_unreachable(); |
43 | } |
44 | |
45 | |
46 | IntervalKind IntervalKind::fromAvgSeconds(Int64 num_seconds) |
47 | { |
48 | if (num_seconds) |
49 | { |
50 | if (!(num_seconds % 31556952)) |
51 | return IntervalKind::Year; |
52 | if (!(num_seconds % 7889238)) |
53 | return IntervalKind::Quarter; |
54 | if (!(num_seconds % 604800)) |
55 | return IntervalKind::Week; |
56 | if (!(num_seconds % 2629746)) |
57 | return IntervalKind::Month; |
58 | if (!(num_seconds % 86400)) |
59 | return IntervalKind::Day; |
60 | if (!(num_seconds % 3600)) |
61 | return IntervalKind::Hour; |
62 | if (!(num_seconds % 60)) |
63 | return IntervalKind::Minute; |
64 | } |
65 | return IntervalKind::Second; |
66 | } |
67 | |
68 | |
69 | const char * IntervalKind::toKeyword() const |
70 | { |
71 | switch (kind) |
72 | { |
73 | case IntervalKind::Second: return "SECOND"; |
74 | case IntervalKind::Minute: return "MINUTE"; |
75 | case IntervalKind::Hour: return "HOUR"; |
76 | case IntervalKind::Day: return "DAY"; |
77 | case IntervalKind::Week: return "WEEK"; |
78 | case IntervalKind::Month: return "MONTH"; |
79 | case IntervalKind::Quarter: return "QUARTER"; |
80 | case IntervalKind::Year: return "YEAR"; |
81 | } |
82 | __builtin_unreachable(); |
83 | } |
84 | |
85 | |
86 | const char * IntervalKind::toDateDiffUnit() const |
87 | { |
88 | switch (kind) |
89 | { |
90 | case IntervalKind::Second: |
91 | return "second"; |
92 | case IntervalKind::Minute: |
93 | return "minute"; |
94 | case IntervalKind::Hour: |
95 | return "hour"; |
96 | case IntervalKind::Day: |
97 | return "day"; |
98 | case IntervalKind::Week: |
99 | return "week"; |
100 | case IntervalKind::Month: |
101 | return "month"; |
102 | case IntervalKind::Quarter: |
103 | return "quarter"; |
104 | case IntervalKind::Year: |
105 | return "year"; |
106 | } |
107 | __builtin_unreachable(); |
108 | } |
109 | |
110 | |
111 | const char * IntervalKind::toNameOfFunctionToIntervalDataType() const |
112 | { |
113 | switch (kind) |
114 | { |
115 | case IntervalKind::Second: |
116 | return "toIntervalSecond"; |
117 | case IntervalKind::Minute: |
118 | return "toIntervalMinute"; |
119 | case IntervalKind::Hour: |
120 | return "toIntervalHour"; |
121 | case IntervalKind::Day: |
122 | return "toIntervalDay"; |
123 | case IntervalKind::Week: |
124 | return "toIntervalWeek"; |
125 | case IntervalKind::Month: |
126 | return "toIntervalMonth"; |
127 | case IntervalKind::Quarter: |
128 | return "toIntervalQuarter"; |
129 | case IntervalKind::Year: |
130 | return "toIntervalYear"; |
131 | } |
132 | __builtin_unreachable(); |
133 | } |
134 | |
135 | |
136 | const char * IntervalKind::toNameOfFunctionExtractTimePart() const |
137 | { |
138 | switch (kind) |
139 | { |
140 | case IntervalKind::Second: |
141 | return "toSecond"; |
142 | case IntervalKind::Minute: |
143 | return "toMinute"; |
144 | case IntervalKind::Hour: |
145 | return "toHour"; |
146 | case IntervalKind::Day: |
147 | return "toDayOfMonth"; |
148 | case IntervalKind::Week: |
149 | // TODO: SELECT toRelativeWeekNum(toDate('2017-06-15')) - toRelativeWeekNum(toStartOfYear(toDate('2017-06-15'))) |
150 | // else if (ParserKeyword("WEEK").ignore(pos, expected)) |
151 | // function_name = "toRelativeWeekNum"; |
152 | throw Exception("The syntax 'EXTRACT(WEEK FROM date)' is not supported, cannot extract the number of a week", ErrorCodes::SYNTAX_ERROR); |
153 | case IntervalKind::Month: |
154 | return "toMonth"; |
155 | case IntervalKind::Quarter: |
156 | return "toQuarter"; |
157 | case IntervalKind::Year: |
158 | return "toYear"; |
159 | } |
160 | __builtin_unreachable(); |
161 | } |
162 | } |
163 |