forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_utils.cc
More file actions
232 lines (204 loc) · 7.19 KB
/
Copy pathdate_utils.cc
File metadata and controls
232 lines (204 loc) · 7.19 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <algorithm>
#include <cstdint>
#include <memory>
#include <sstream>
#include <vector>
#include "gandiva/date_utils.h"
namespace gandiva {
std::vector<std::string> DateUtils::GetMatches(std::string pattern, bool exactMatch) {
// we are case insensitive
std::transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower);
std::vector<std::string> matches;
for (const auto& it : sql_date_format_to_boost_map_) {
if (it.first.find(pattern) != std::string::npos &&
(!exactMatch || (it.first.length() == pattern.length()))) {
matches.push_back(it.first);
}
}
return matches;
}
std::vector<std::string> DateUtils::GetPotentialMatches(const std::string& pattern) {
return GetMatches(pattern, false);
}
std::vector<std::string> DateUtils::GetExactMatches(const std::string& pattern) {
return GetMatches(pattern, true);
}
/**
* Validates and converts format to the strptime equivalent
*
*/
Status DateUtils::ToInternalFormat(const std::string& format,
std::shared_ptr<std::string>* internal_format) {
std::stringstream builder;
std::stringstream buffer;
bool is_in_quoted_text = false;
for (size_t i = 0; i < format.size(); i++) {
char currentChar = format[i];
// logic before we append to the buffer
if (currentChar == '"') {
if (is_in_quoted_text) {
// we are done with a quoted block
is_in_quoted_text = false;
// use ' for quoting
builder << '\'';
builder << buffer.str();
builder << '\'';
// clear buffer
buffer.str("");
continue;
} else {
ARROW_RETURN_IF(buffer.str().length() > 0,
Status::Invalid("Invalid date format string '", format, "'"));
is_in_quoted_text = true;
continue;
}
}
// handle special characters we want to simply pass through, but only if not in quoted
// and the buffer is empty
std::string special_characters = "*-/,.;: ";
if (!is_in_quoted_text && buffer.str().length() == 0 &&
(special_characters.find_first_of(currentChar) != std::string::npos)) {
builder << currentChar;
continue;
}
// append to the buffer
buffer << currentChar;
// nothing else to do if we are in quoted text
if (is_in_quoted_text) {
continue;
}
// check how many matches we have for our buffer
std::vector<std::string> potentialList = GetPotentialMatches(buffer.str());
int64_t potentialCount = potentialList.size();
if (potentialCount >= 1) {
// one potential and the length match
if (potentialCount == 1 && potentialList[0].length() == buffer.str().length()) {
// we have a match!
builder << sql_date_format_to_boost_map_[potentialList[0]];
buffer.str("");
} else {
// Some patterns (like MON, MONTH) can cause ambiguity, such as "MON:". "MON"
// will have two potential matches, but "MON:" will match nothing, so we want to
// look ahead when we match "MON" and check if adding the next char leads to 0
// potentials. If it does, we go ahead and treat the buffer as matched (if a
// potential match exists that matches the buffer)
if (format.length() - 1 > i) {
std::string lookAheadPattern = (buffer.str() + format.at(i + 1));
std::transform(lookAheadPattern.begin(), lookAheadPattern.end(),
lookAheadPattern.begin(), ::tolower);
bool lookAheadMatched = false;
// we can query potentialList to see if it has anything that matches the
// lookahead pattern
for (std::string potential : potentialList) {
if (potential.find(lookAheadPattern) != std::string::npos) {
lookAheadMatched = true;
break;
}
}
if (!lookAheadMatched) {
// check if any of the potential matches are the same length as our buffer, we
// do not want to match "MO:"
bool matched = false;
for (std::string potential : potentialList) {
if (potential.length() == buffer.str().length()) {
matched = true;
break;
}
}
if (matched) {
std::string match = buffer.str();
std::transform(match.begin(), match.end(), match.begin(), ::tolower);
builder << sql_date_format_to_boost_map_[match];
buffer.str("");
continue;
}
}
}
}
} else {
return Status::Invalid("Invalid date format string '", format, "'");
}
}
if (buffer.str().length() > 0) {
// Some patterns (like MON, MONTH) can cause us to reach this point with a valid
// buffer value as MON has 2 valid potential matches, so double check here
std::vector<std::string> exactMatches = GetExactMatches(buffer.str());
if (exactMatches.size() == 1 && exactMatches[0].length() == buffer.str().length()) {
builder << sql_date_format_to_boost_map_[exactMatches[0]];
} else {
// Format partially parsed
int64_t pos = format.length() - buffer.str().length();
return Status::Invalid("Invalid date format string '", format, "' at position ",
pos);
}
}
std::string final_pattern = builder.str();
internal_format->reset(new std::string(final_pattern));
return Status::OK();
}
DateUtils::date_format_converter DateUtils::sql_date_format_to_boost_map_ = InitMap();
DateUtils::date_format_converter DateUtils::InitMap() {
date_format_converter map;
// Era
map["ad"] = "%EC";
map["bc"] = "%EC";
// Meridian
map["am"] = "%p";
map["pm"] = "%p";
// Century
map["cc"] = "%C";
// Week of year
map["ww"] = "%W";
// Day of week
map["d"] = "%u";
// Day name of week
map["dy"] = "%a";
map["day"] = "%a";
// Year
map["yyyy"] = "%Y";
map["yy"] = "%y";
// Day of year
map["ddd"] = "%j";
// Month
map["mm"] = "%m";
map["mon"] = "%b";
map["month"] = "%b";
// Day of month
map["dd"] = "%d";
// Hour of day
map["hh"] = "%I";
map["hh12"] = "%I";
map["hh24"] = "%H";
// Minutes
map["mi"] = "%M";
// Seconds
map["ss"] = "%S";
// Milliseconds
map["f"] = "S";
map["ff"] = "SS";
map["fff"] = "SSS";
/*
// Timezone not tested/supported yet fully.
map["tzd"] = "%Z";
map["tzo"] = "%z";
map["tzh:tzm"] = "%z";
*/
return map;
}
} // namespace gandiva