Skip to content

Commit 3d94f19

Browse files
Addressed review comments
1 parent 0342716 commit 3d94f19

5 files changed

+44
-62
lines changed

src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <Parsers/Kusto/ParserKQLQuery.h>
1818
#include <Parsers/Kusto/ParserKQLStatement.h>
1919
#include <Parsers/ParserSetQuery.h>
20+
#include <boost/lexical_cast.hpp>
21+
2022

2123
#include <pcg_random.hpp>
2224

@@ -284,7 +286,7 @@ String IParserKQLFunction::getExpression(IParser::Pos & pos)
284286
Expected expected;
285287

286288
if (time_span.parse(pos, node, expected))
287-
arg = std::to_string(time_span.toSeconds());
289+
arg = boost::lexical_cast<std::string>(time_span.toSeconds());
288290
}
289291
}
290292
else if (pos->type == TokenType::QuotedIdentifier)

src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bool ToTimeSpan::convertImpl(String & out, IParser::Pos & pos)
9090
{
9191
const auto function_name = getKQLFunctionName(pos);
9292
if (function_name.empty())
93-
return false;
93+
return false;
9494
++pos;
9595
String arg;
9696
if (pos->type == TokenType::QuotedIdentifier)
@@ -121,7 +121,6 @@ bool ToTimeSpan::convertImpl(String & out, IParser::Pos & pos)
121121

122122
bool ToDecimal::convertImpl(String & out, IParser::Pos & pos)
123123
{
124-
125124
const String fn_name = getKQLFunctionName(pos);
126125
if (fn_name.empty())
127126
return false;
@@ -150,7 +149,7 @@ bool ToDecimal::convertImpl(String & out, IParser::Pos & pos)
150149
else if (std::regex_match(res, expr))
151150
{
152151
auto exponential_pos = res.find("e");
153-
if (res[exponential_pos +1] == '+' || res[exponential_pos + 1] == '-' )
152+
if (res[exponential_pos + 1] == '+' || res[exponential_pos + 1] == '-')
154153
scale = std::stoi(res.substr(exponential_pos + 2, res.length()));
155154
else
156155
scale = std::stoi(res.substr(exponential_pos + 1, res.length()));

src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp

+32-43
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace DB::ErrorCodes
2121
{
2222
extern const int SYNTAX_ERROR;
2323
}
24-
2524
namespace DB
2625
{
2726

@@ -59,18 +58,18 @@ bool DatetimeAdd::convertImpl(String & out, IParser::Pos & pos)
5958
String period = getConvertedArgument(fn_name, pos);
6059
//remove quotes from period.
6160
trim(period);
62-
if ( period.front() == '\"' || period.front() == '\'' )
61+
if (period.front() == '\"' || period.front() == '\'')
6362
{
6463
//period.remove
65-
period.erase( 0, 1 ); // erase the first quote
66-
period.erase( period.size() - 1 ); // erase the last quote
64+
period.erase( 0, 1); // erase the first quote
65+
period.erase( period.size() - 1); // erase the last quote
6766
}
6867
++pos;
6968
const String offset = getConvertedArgument(fn_name, pos);
7069
++pos;
7170
const String datetime = getConvertedArgument(fn_name, pos);
7271

73-
out = std::format("date_add({}, {}, {} )",period,offset,datetime);
72+
out = std::format("date_add({}, {}, {})",period,offset,datetime);
7473

7574
return true;
7675

@@ -85,10 +84,10 @@ bool DatetimePart::convertImpl(String & out, IParser::Pos & pos)
8584
++pos;
8685
String part = Poco::toUpper(getConvertedArgument(fn_name, pos));
8786
trim(part);
88-
if (part.front() == '\"' || part.front() == '\'' )
87+
if (part.front() == '\"' || part.front() == '\'')
8988
{
9089
//period.remove
91-
part.erase( 0, 1 ); // erase the first quote
90+
part.erase( 0, 1); // erase the first quote
9291
part.erase( part.size() - 1); // erase the last quote
9392
}
9493
String date;
@@ -99,9 +98,9 @@ bool DatetimePart::convertImpl(String & out, IParser::Pos & pos)
9998
}
10099
String format;
101100

102-
if (part == "YEAR" )
101+
if (part == "YEAR")
103102
format = "%G";
104-
else if (part == "QUARTER" )
103+
else if (part == "QUARTER")
105104
format = "%Q";
106105
else if (part == "MONTH")
107106
format = "%m";
@@ -118,10 +117,9 @@ bool DatetimePart::convertImpl(String & out, IParser::Pos & pos)
118117
else if (part == "SECOND")
119118
format = "%S";
120119
else
121-
throw Exception("Unexpected argument " + part + " for " + fn_name, ErrorCodes::SYNTAX_ERROR);
122-
123-
out = std::format("formatDateTime({}, '{}' )", date, format);
120+
throw Exception("Unexpected argument " + part + " for " + fn_name, ErrorCodes::SYNTAX_ERROR);
124121

122+
out = std::format("formatDateTime({}, '{}')", date, format);
125123
return true;
126124
}
127125

@@ -140,9 +138,7 @@ bool DatetimeDiff::convertImpl(String & out, IParser::Pos & pos)
140138
arguments = arguments + getConvertedArgument(fn_name, pos);
141139

142140
out = std::format("DateDiff({}) * -1",arguments);
143-
144141
return true;
145-
146142
}
147143

148144
bool DayOfMonth::convertImpl(String & out, IParser::Pos & pos)
@@ -156,7 +152,6 @@ bool DayOfWeek::convertImpl(String & out, IParser::Pos & pos)
156152
if (fn_name.empty())
157153
return false;
158154
++pos;
159-
160155
const String datetime_str = getConvertedArgument(fn_name, pos);
161156

162157
out = std::format("concat((toDayOfWeek({})%7)::String , '.00:00:00')",datetime_str);
@@ -188,7 +183,6 @@ bool EndOfMonth::convertImpl(String & out, IParser::Pos & pos)
188183
out = std::format("toDateTime(toStartOfDay({}),9,'UTC') + (INTERVAL {} +1 MONTH) - (INTERVAL 1 microsecond)", datetime_str, toString(offset));
189184

190185
return true;
191-
192186
}
193187

194188
bool EndOfDay::convertImpl(String & out, IParser::Pos & pos)
@@ -231,8 +225,7 @@ bool EndOfWeek::convertImpl(String & out, IParser::Pos & pos)
231225
}
232226
out = std::format("toDateTime(toStartOfDay({}),9,'UTC') + (INTERVAL {} +1 WEEK) - (INTERVAL 1 microsecond)", datetime_str, toString(offset));
233227

234-
return true;
235-
228+
return true;
236229
}
237230

238231
bool EndOfYear::convertImpl(String & out, IParser::Pos & pos)
@@ -254,7 +247,6 @@ bool EndOfYear::convertImpl(String & out, IParser::Pos & pos)
254247
out = std::format("toDateTime(toStartOfDay({}),9,'UTC') + (INTERVAL {} +1 YEAR) - (INTERVAL 1 microsecond)", datetime_str, toString(offset));
255248

256249
return true;
257-
258250
}
259251

260252
bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
@@ -270,10 +262,10 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
270262
trim(format);
271263

272264
//remove quotes and end space from format argument.
273-
if (format.front() == '\"' || format.front() == '\'' )
265+
if (format.front() == '\"' || format.front() == '\'')
274266
{
275-
format.erase( 0, 1 ); // erase the first quote
276-
format.erase( format.size() - 1 ); // erase the last quote
267+
format.erase( 0, 1); // erase the first quote
268+
format.erase( format.size() - 1); // erase the last quote
277269
}
278270

279271
std::vector<String> res;
@@ -297,7 +289,7 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
297289
//format specifier
298290
String arg = res.back();
299291

300-
if (arg == "y" || arg == "yy" )
292+
if (arg == "y" || arg == "yy")
301293
formatspecifier = formatspecifier + "%y";
302294
else if (arg == "yyyy")
303295
formatspecifier = formatspecifier + "%Y";
@@ -325,16 +317,16 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
325317
i = i + arg.size();
326318
}
327319
}
328-
if (decimal > 0 && formatspecifier.find('.')!=String::npos)
320+
if (decimal > 0 && formatspecifier.find('.') != String::npos)
329321
{
330322

331323
out = std::format("concat("
332-
"substring(toString(formatDateTime( {0} , '{1}' )),1, position(toString(formatDateTime({0},'{1}')),'.')) ,"
324+
"substring(toString(formatDateTime( {0} , '{1}')),1, position(toString(formatDateTime({0},'{1}')),'.')) ,"
333325
"substring(substring(toString({0}), position(toString({0}),'.')+1),1,{2}),"
334-
"substring(toString(formatDateTime( {0},'{1}')), position(toString(formatDateTime({0},'{1}')),'.')+1 ,length (toString(formatDateTime({0},'{1}'))))) " ,datetime, formatspecifier,decimal);
326+
"substring(toString(formatDateTime( {0},'{1}')), position(toString(formatDateTime({0},'{1}')),'.')+1 ,length (toString(formatDateTime({0},'{1}'))))) ", datetime, formatspecifier,decimal);
335327
}
336328
else
337-
out = std::format("formatDateTime( {0},'{1}')" ,datetime, formatspecifier);
329+
out = std::format("formatDateTime( {0},'{1}')",datetime, formatspecifier);
338330

339331
return true;
340332
}
@@ -351,10 +343,10 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
351343
size_t decimal = 0;
352344
trim(format);
353345
//remove quotes and end space from format argument.
354-
if (format.front() == '\"' || format.front() == '\'' )
346+
if (format.front() == '\"' || format.front() == '\'')
355347
{
356-
format.erase(0, 1 ); // erase the first quote
357-
format.erase(format.size() - 1 ); // erase the last quote
348+
format.erase(0, 1); // erase the first quote
349+
format.erase(format.size() - 1); // erase the last quote
358350
}
359351
std::vector<String> res;
360352
getTokens(format, res);
@@ -364,7 +356,7 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
364356

365357
bool is_day_in_format = false;
366358
String day_val = std::to_string(std::stoi(datetime) / 86400);
367-
bool is_hour_zero = std::stoi(datetime)%86400 >3600 ? false : true;
359+
bool is_hour_zero = std::stoi(datetime) % 86400 > 3600 ? false : true;
368360

369361
while (i < format.size())
370362
{
@@ -416,9 +408,9 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
416408
if (decimal > 0)
417409
{
418410
if (format.substr(format.length()- decimal -1, 1) == last_delim)
419-
out = std::format("concat(substring(toString(formatDateTime( toDateTime64({0},9,'UTC') ,'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{3}')+1),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}))", datetime,formatspecifier,decimal,last_delim);
411+
out = std::format("concat(substring(toString(formatDateTime( toDateTime64({0},9,'UTC') ,'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{3}')+1),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}))", datetime, formatspecifier, decimal, last_delim);
420412
else
421-
out = std::format("concat(substring(toString(formatDateTime( toDateTime64({0},9,'UTC') ,'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{3}')),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}))", datetime,formatspecifier,decimal,last_delim);
413+
out = std::format("concat(substring(toString(formatDateTime( toDateTime64({0},9,'UTC') ,'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{3}')),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}))", datetime, formatspecifier, decimal, last_delim);
422414
}
423415
else
424416
out = std::format("formatDateTime(toDateTime64({0},9,'UTC'),'{1}')", datetime,formatspecifier);
@@ -428,9 +420,9 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
428420
if (decimal > 0)
429421
{
430422
if (format.substr(format.length()- decimal - 1, 1) == last_delim)
431-
out = std::format("concat( leftPad('{5}' , {3} ,'0'),substring(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{4}') +1),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}))", datetime,formatspecifier,decimal,pad,last_delim,day_val);
423+
out = std::format("concat( leftPad('{5}' , {3} ,'0'),substring(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{4}') +1),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}))", datetime, formatspecifier, decimal,pad, last_delim, day_val);
432424
else
433-
out = std::format("concat( leftPad('{5}' , {3} ,'0') ,substring(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{4}')),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}),substring(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}')),position( toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')),'{4}'),length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')))))", datetime,formatspecifier,decimal,pad,last_delim,day_val);
425+
out = std::format("concat( leftPad('{5}' , {3} ,'0') ,substring(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')),1, length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}'))) - position( reverse(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}'))),'{4}')),substring(SUBSTRING(toString(toDateTime64({0},9,'UTC')),position(toString(toDateTime64({0},9,'UTC')),'.')+1),1,{2}),substring(toString(formatDateTime(toDateTime64({0},9,'UTC'),'{1}')),position( toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')),'{4}'),length(toString(formatDateTime( toDateTime64({0},9,'UTC'),'{1}')))))", datetime, formatspecifier, decimal, pad, last_delim, day_val);
434426
}
435427
else if (decimal == 0)
436428
out = std::format("concat( leftPad('{3}' , {2} ,'0'),toString(formatDateTime(toDateTime64({0},9,'UTC') ,'{1}')))", datetime,formatspecifier,pad,day_val);
@@ -478,7 +470,7 @@ bool MakeTimeSpan::convertImpl(String & out, IParser::Pos & pos)
478470
}
479471

480472
if (arg_count < 2 || arg_count > 4)
481-
throw Exception("argument count out of bound in function: " + fn_name, ErrorCodes::SYNTAX_ERROR);
473+
throw Exception("argument count out of bound in function: " + fn_name, ErrorCodes::SYNTAX_ERROR);
482474

483475
if (arg_count == 2)
484476
{
@@ -512,17 +504,15 @@ bool MakeTimeSpan::convertImpl(String & out, IParser::Pos & pos)
512504

513505
datetime_str = hour + ":" + minute + ":" + second;
514506
day = day + ".";
515-
516507
}
517508
else
518-
throw Exception("argument count out of bound in function: " + fn_name, ErrorCodes::SYNTAX_ERROR);
509+
throw Exception("argument count out of bound in function: " + fn_name, ErrorCodes::SYNTAX_ERROR);
519510

520511
//Add dummy yyyy-mm-dd to parse datetime in CH
521512
datetime_str = "0000-00-00 " + datetime_str;
522513

523-
out = std::format("CONCAT('{}',toString(SUBSTRING(toString(toTime(parseDateTime64BestEffortOrNull('{}', 9 ,'UTC' ))),12)))" ,day ,datetime_str );
524-
525-
return true;
514+
out = std::format("CONCAT('{}',toString(SUBSTRING(toString(toTime(parseDateTime64BestEffortOrNull('{}', 9 ,'UTC'))),12)))" ,day ,datetime_str);
515+
return true;
526516
}
527517

528518
bool MakeDateTime::convertImpl(String & out, IParser::Pos & pos)
@@ -550,7 +540,7 @@ bool MakeDateTime::convertImpl(String & out, IParser::Pos & pos)
550540

551541
if (arg_count < 7)
552542
{
553-
for (int i = arg_count;i < 7 ; ++i)
543+
for (int i = arg_count;i < 7; ++i)
554544
arguments = arguments + "0 ,";
555545
}
556546

@@ -726,7 +716,6 @@ bool WeekOfYear::convertImpl(String & out, IParser::Pos & pos)
726716

727717
bool MonthOfYear::convertImpl(String & out, IParser::Pos & pos)
728718
{
729-
730719
return directMapping(out, pos, "toMonth");
731720
}
732721

src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h

+6-13
Original file line numberDiff line numberDiff line change
@@ -223,34 +223,27 @@ class MonthOfYear : public IParserKQLFunction
223223
bool convertImpl(String &out,IParser::Pos &pos) override;
224224
};
225225

226-
void inline getTokens(String format , std::vector<String> & res )
226+
void inline getTokens(String format, std::vector<String> & res )
227227
{
228228
String str = format;
229229
String token;
230230
auto pos = str.find_first_not_of("abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM");
231-
while (pos != String::npos )
231+
while (pos != String::npos)
232232
{
233-
if ( pos != 0 )
233+
if (pos != 0)
234234
{
235235
// Found a token
236236
token = str.substr(0, pos);
237-
res.insert(res.begin(),token);
237+
res.insert(res.begin(), token);
238238
}
239-
/* else
240-
{
241-
// Found another delimiter
242-
// Just move on to next one
243-
244-
}
245-
*/
246239
str.erase(0, pos+1); // Always remove pos+1 to get rid of delimiter
247240
pos = str.find_first_not_of("abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM");
248241
}
249242
// Cover the last (or only) token
250-
if ( str.length() > 0 )
243+
if (str.length() > 0)
251244
{
252245
token = str;
253-
res.insert(res.begin(),token);
246+
res.insert(res.begin(), token);
254247
}
255248
}
256249

src/Parsers/Kusto/ParserKQLDateTypeTimespan.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ bool ParserKQLDateTypeTimespan :: parseImpl(Pos & pos, [[maybe_unused]] ASTPtr
2121
token = String(pos->begin + 1, pos->end -1);
2222
else
2323
token = String(pos->begin, pos->end);
24-
2524
if (!parseConstKQLTimespan(token))
2625
return false;
2726

@@ -47,7 +46,7 @@ double ParserKQLDateTypeTimespan :: toSeconds()
4746
case KQLTimespanUint::nanosec:
4847
return time_span / 1000000000.0;
4948
case KQLTimespanUint::tick:
50-
return time_span / 10000000000.0;
49+
return time_span / 10000000.0;
5150
}
5251
}
5352

0 commit comments

Comments
 (0)