Skip to content

Commit bbd2a61

Browse files
More fixed bugs
1 parent 62ccb4a commit bbd2a61

7 files changed

+96
-69
lines changed

src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ bool ToTimeSpan::convertImpl(String & out, IParser::Pos & pos)
9898
else if (pos->type == TokenType::StringLiteral)
9999
arg = String(pos->begin, pos->end);
100100
else
101-
arg = getConvertedArgument(function_name,pos);
101+
arg = getConvertedArgument(function_name, pos);
102102

103-
if(pos->type == TokenType::StringLiteral || pos->type == TokenType::QuotedIdentifier)
103+
if (pos->type == TokenType::StringLiteral || pos->type == TokenType::QuotedIdentifier)
104104
{
105105
++pos;
106106
try
@@ -110,7 +110,7 @@ bool ToTimeSpan::convertImpl(String & out, IParser::Pos & pos)
110110
}
111111
catch(...)
112112
{
113-
out = std::format("NULL");
113+
out = "NULL";
114114
}
115115
}
116116
else
@@ -128,7 +128,7 @@ bool ToDecimal::convertImpl(String & out, IParser::Pos & pos)
128128

129129
++pos;
130130
String res;
131-
int scale =0;
131+
int scale = 0;
132132
int precision;
133133

134134
if (pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral)
@@ -143,29 +143,29 @@ bool ToDecimal::convertImpl(String & out, IParser::Pos & pos)
143143
precision = 17;
144144
}
145145
static const std::regex expr{"^[0-9]+e[+-]?[0-9]+"};
146-
bool is_string = std::any_of(res.begin(), res.end(), ::isalpha) && !(std::regex_match(res , expr));
146+
bool is_string = std::any_of(res.begin(), res.end(), ::isalpha) && !(std::regex_match(res, expr));
147147

148148
if (is_string)
149-
out = std::format("NULL");
149+
out = "NULL";
150150
else if (std::regex_match(res , expr))
151151
{
152152
auto exponential_pos = res.find("e");
153-
if(res[exponential_pos +1] == '+' || res[exponential_pos +1] == '-' )
154-
scale = std::stoi(res.substr(exponential_pos+2 , res.length()));
153+
if (res[exponential_pos +1] == '+' || res[exponential_pos + 1] == '-' )
154+
scale = std::stoi(res.substr(exponential_pos + 2, res.length()));
155155
else
156-
scale = std::stoi(res.substr(exponential_pos+1,res.length()));
156+
scale = std::stoi(res.substr(exponential_pos + 1, res.length()));
157157

158158
out = std::format("toDecimal128({}::String,{})", res, scale);
159159
}
160160
else
161161
{
162162
auto dot_pos = res.find(".");
163-
if(dot_pos != String::npos)
163+
if (dot_pos != String::npos)
164164
scale = (precision - (res.substr(0,dot_pos-1)).length()) > 0 ? precision - (res.substr(0,dot_pos-1)).length() : 0;
165-
if(scale < 0)
166-
out = std::format("NULL");
165+
if (scale < 0)
166+
out = "NULL";
167167
else
168-
out = std::format("toDecimal128({}::String,{})", res, scale);
168+
out = std::format("toDecimal128({}::String,{})", res, scale);
169169
}
170170

171171
return true;

src/Parsers/Kusto/KustoFunctions/KQLDataTypeFunctions.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ bool DatatypeInt::convertImpl(String & out, IParser::Pos & pos)
128128

129129
++pos;
130130
if (pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral)
131-
throw Exception("String is not parsed as double literal " , ErrorCodes::BAD_ARGUMENTS);
131+
throw Exception("String is not parsed as double literal", ErrorCodes::BAD_ARGUMENTS);
132132
else
133133
{
134134
auto arg = getConvertedArgument(fn_name, pos);
135-
out = std::format("toInt32({})",arg);
135+
out = std::format("toInt32({})", arg);
136136
}
137137
return true;
138138
}
@@ -150,11 +150,11 @@ bool DatatypeReal::convertImpl(String & out, IParser::Pos & pos)
150150

151151
++pos;
152152
if (pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral)
153-
throw Exception("String is not parsed as double literal " , ErrorCodes::BAD_ARGUMENTS);
153+
throw Exception("String is not parsed as double literal.", ErrorCodes::BAD_ARGUMENTS);
154154
else
155155
{
156156
auto arg = getConvertedArgument(fn_name, pos);
157-
out = std::format("toFloat64({})",arg);
157+
out = std::format("toFloat64({})", arg);
158158
}
159159
return true;
160160
}
@@ -185,9 +185,9 @@ bool DatatypeTimespan::convertImpl(String & out, IParser::Pos & pos)
185185
if (time_span.parse(pos, node, expected))
186186
{
187187
if (sign)
188-
out = std::format("-{}", std::to_string(time_span.toSeconds()));
188+
out = std::format("-{}::Float64", time_span.toSeconds());
189189
else
190-
out = std::to_string(time_span.toSeconds());
190+
out = std::format("{}::Float64", time_span.toSeconds());
191191
++pos;
192192
}
193193
else
@@ -240,7 +240,7 @@ bool DatatypeDecimal::convertImpl(String & out, IParser::Pos & pos)
240240
throw Exception("Failed to parse String as decimal Literal: " + fn_name, ErrorCodes::BAD_ARGUMENTS);
241241

242242
if (scale < 0 || Poco::toUpper(arg) == "NULL")
243-
out = std::format("NULL");
243+
out = "NULL";
244244
else
245245
out = std::format("toDecimal128({}::String,{})", arg, scale);
246246

src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp

+27-36
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ bool TimeSpan::convertImpl(String & out, IParser::Pos & pos)
3131
out = res;
3232
return false;
3333
}
34-
/*
35-
bool DateTime::convertImpl(String & out, IParser::Pos & pos)
36-
{
37-
String res = String(pos->begin, pos->end);
38-
out = res;
39-
return false;
40-
}*/
4134

4235
bool Ago::convertImpl(String & out, IParser::Pos & pos)
4336
{
@@ -290,7 +283,7 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
290283
while (i < format.size())
291284
{
292285
char c = format[i];
293-
if(!isalpha(c))
286+
if (!isalpha(c))
294287
{
295288
//delimeter
296289
if (c == ' ' || c == '-' || c == '_' || c == '[' || c == ']' || c == '/' || c == ',' || c == '.' || c == ':')
@@ -304,7 +297,7 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
304297
//format specifier
305298
String arg = res.back();
306299

307-
if(arg == "y" || arg == "yy" )
300+
if (arg == "y" || arg == "yy" )
308301
formatspecifier = formatspecifier + "%y";
309302
else if (arg == "yyyy")
310303
formatspecifier = formatspecifier + "%Y";
@@ -341,7 +334,7 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
341334
"substring(toString(formatDateTime( {0},'{1}')), position(toString(formatDateTime({0},'{1}')),'.')+1 ,length (toString(formatDateTime({0},'{1}'))))) " ,datetime, formatspecifier,decimal);
342335
}
343336
else
344-
out = std::format("formatDateTime( {0},'{1}')" ,datetime, formatspecifier);
337+
out = std::format("formatDateTime( {0},'{1}')" ,datetime, formatspecifier);
345338

346339
return true;
347340
}
@@ -355,13 +348,13 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
355348
const auto datetime = getConvertedArgument(fn_name, pos);
356349
++pos;
357350
auto format = getConvertedArgument(fn_name, pos);
358-
size_t decimal=0;
351+
size_t decimal = 0;
359352
trim(format);
360353
//remove quotes and end space from format argument.
361354
if (format.front() == '\"' || format.front() == '\'' )
362355
{
363-
format.erase( 0, 1 ); // erase the first quote
364-
format.erase( format.size() - 1 ); // erase the last quote
356+
format.erase(0, 1 ); // erase the first quote
357+
format.erase(format.size() - 1 ); // erase the last quote
365358
}
366359
std::vector<String> res;
367360
getTokens(format, res);
@@ -370,13 +363,13 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
370363

371364

372365
bool is_day_in_format = false;
373-
String day_val = std::to_string(std::stoi(datetime)/86400);
366+
String day_val = std::to_string(std::stoi(datetime) / 86400);
374367
bool is_hour_zero = std::stoi(datetime)%86400 >3600 ? false : true;
375368

376369
while (i < format.size())
377370
{
378371
char c = format[i];
379-
if(!isalpha(c))
372+
if (!isalpha(c))
380373
{
381374
//delimeter
382375
if (c == ' ' || c == '-' || c == '_' || c == '[' || c == ']' || c == '/' || c == ',' || c == '.' || c == ':')
@@ -396,7 +389,7 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
396389
formatspecifier = formatspecifier + "%M";
397390
else if (arg == "h" || arg == "hh")
398391
{
399-
if(is_hour_zero) //To handle the CH limit for 12hr format(01-12). If not handled , 1.00:00:00 returned as 1.12:00:00(in 12 hr format)
392+
if (is_hour_zero) //To handle the CH limit for 12hr format(01-12). If not handled , 1.00:00:00 returned as 1.12:00:00(in 12 hr format)
400393
formatspecifier = formatspecifier + "%H";
401394
else
402395
formatspecifier = formatspecifier + "%I";
@@ -405,7 +398,7 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
405398
formatspecifier = formatspecifier + "%H";
406399
else if (arg.starts_with('d')) //&& arg.size() >2)
407400
{
408-
pad = std::max(arg.size(),day_val.length());
401+
pad = std::max(arg.size(), day_val.length());
409402
is_day_in_format = true;
410403
}
411404
else if (arg.starts_with('f') || arg.starts_with('F'))
@@ -416,29 +409,26 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
416409
i = i + arg.size();
417410
}
418411
}
419-
auto last_delim = formatspecifier.substr(formatspecifier.length()-1,formatspecifier.length());
412+
auto last_delim = formatspecifier.substr(formatspecifier.length()-1, formatspecifier.length());
420413

421-
if(!is_day_in_format)
414+
if (!is_day_in_format)
422415
{
423-
if(decimal > 0)
416+
if (decimal > 0)
424417
{
425-
if(format.substr(format.length()- decimal -1,1) == last_delim)
426-
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}'))),'{4}')+1),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,last_delim);
427-
else
428-
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}'))),'{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,last_delim);
418+
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}'))),'{4}')+1),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,last_delim);
420+
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}'))),'{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,last_delim);
429422
}
430423
else
431424
out = std::format("formatDateTime(toDateTime64({0},9,'UTC'),'{1}')", datetime,formatspecifier);
432425

433426
}
434427
else
435428
{
436-
if(decimal > 0)
429+
if (decimal > 0)
437430
{
438-
String heena = format.substr(format.length()- decimal - 1, 1);
439-
if(heena == last_delim)
440-
std::cout <<"yes";
441-
if(format.substr(format.length()- decimal - 1, 1) == last_delim)
431+
if (format.substr(format.length()- decimal - 1, 1) == last_delim)
442432
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);
443433
else
444434
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);
@@ -491,7 +481,7 @@ bool MakeTimeSpan::convertImpl(String & out, IParser::Pos & pos)
491481
if (arg_count < 2 || arg_count > 4)
492482
throw Exception("argument count out of bound in function: " + fn_name, ErrorCodes::SYNTAX_ERROR);
493483

494-
if(arg_count == 2)
484+
if (arg_count == 2)
495485
{
496486
hour = args.back();
497487
args.pop_back();
@@ -559,9 +549,9 @@ bool MakeDateTime::convertImpl(String & out, IParser::Pos & pos)
559549
if (arg_count < 1 || arg_count > 7)
560550
throw Exception("argument count out of bound in function: " + fn_name, ErrorCodes::SYNTAX_ERROR);
561551

562-
if(arg_count < 7)
552+
if (arg_count < 7)
563553
{
564-
for(int i = arg_count;i < 7 ; ++i)
554+
for (int i = arg_count;i < 7 ; ++i)
565555
arguments = arguments + "0 ,";
566556
}
567557

@@ -714,15 +704,16 @@ bool UnixTimeSecondsToDateTime::convertImpl(String & out, IParser::Pos & pos)
714704
return false;
715705

716706
++pos;
717-
if(pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral)
707+
if (pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral)
718708
throw Exception("String Literal is not accepted." + fn_name + " accepts only long, int and double " , ErrorCodes::BAD_ARGUMENTS);
719709

720-
const String expression = getConvertedArgument(fn_name, pos);
710+
String expression = getConvertedArgument(fn_name, pos);
721711

722-
if(std::any_of(expression.begin(), expression.end(), ::isalpha))
712+
if (std::any_of(expression.begin(), expression.end(), ::isalpha))
723713
throw Exception("String Literal is not accepted." + fn_name + " accepts only long, int and double " , ErrorCodes::BAD_ARGUMENTS);
724714

725-
out = std::format("toDateTime64({0},9,'UTC')", expression);
715+
std::remove(expression.begin(), expression.end(), ' ');
716+
out = std::format("multiIf(toTypeName({0}) == 'String' or toTypeName({0}) == 'UUID' or toTypeName({0}) == 'DateTime64(3)' or toTypeName({0}) == 'DateTime64(6), or toTypeName({0}) == 'DateTime64(9) , 'Only Accepted arguments are float , int and double', toString(toDateTime64(abs({0}) , 9,'UTC')))", expression);
726717

727718
return true;
728719
}

src/Parsers/tests/KQL/gtest_KQL_Conversion.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ INSTANTIATE_TEST_SUITE_P(
6767
{
6868
"print decimal(1e-5)",
6969
"SELECT toDecimal128(CAST('1e-5', 'String'), 5)"
70+
},
71+
{
72+
"print time(9nanoseconds)",
73+
"SELECT CAST('9e-09', 'Float64')"
74+
},
75+
{
76+
"print time(1tick)",
77+
"SELECT CAST('1e-10', 'Float64')"
7078
}
7179

7280
})));

src/Parsers/tests/KQL/gtest_KQL_DataType.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_DataType, ParserTest,
2020
},
2121
{
2222
"print dynamic(timespan(1d))",
23-
"SELECT 86400."
23+
"SELECT CAST('86400', 'Float64')"
2424
},
2525
{
2626
"print dynamic(parse_ipv4('127.0.0.1'))",
@@ -52,6 +52,18 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_DataType, ParserTest,
5252
},
5353
{
5454
"print dynamic([date(1), time(1d), 1, 2])",
55-
"SELECT [parseDateTime64BestEffortOrNull('1', 9, 'UTC'), 86400., 1, 2]"
55+
"SELECT [parseDateTime64BestEffortOrNull('1', 9, 'UTC'), CAST('86400', 'Float64'), 1, 2]"
56+
},
57+
{
58+
"print time('13:00:40.00000')",
59+
"SELECT CAST('46840', 'Float64')"
60+
},
61+
{
62+
"print timespan('12.23:12:23');",
63+
"SELECT CAST('1120343', 'Float64')"
64+
},
65+
{
66+
"print timespan(12.23:12:23)",
67+
"SELECT CAST('1120343', 'Float64')"
5668
}
5769
})));

src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,19 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_String, ParserTest,
8888
},
8989
{
9090
"print time('1.22:34:8.128')",
91-
"SELECT 167648.128"
91+
"SELECT CAST('167648.128', 'Float64')"
9292
},
9393
{
9494
"print time('1d')",
95-
"SELECT 86400."
95+
"SELECT CAST('86400', 'Float64')"
9696
},
9797
{
9898
"print time('1.5d')",
99-
"SELECT 129600."
99+
"SELECT CAST('129600', 'Float64')"
100100
},
101101
{
102102
"print timespan('1.5d')",
103-
"SELECT 129600."
103+
"SELECT CAST('129600', 'Float64')"
104104
},
105105
{
106106
"print res = bin_at(6.5, 2.5, 7)",
@@ -120,11 +120,19 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_String, ParserTest,
120120
},
121121
{
122122
"print bin(time(16d), 7d)",
123-
"SELECT concat(toString(toInt32(((toInt64(toFloat64(1382400.) / 604800) * 604800) AS x) / 3600)), ':', toString(toInt32((x % 3600) / 60)), ':', toString(toInt32((x % 3600) % 60)))"
123+
"SELECT concat(toString(toInt32(((toInt64(toFloat64(CAST('1382400', 'Float64')) / 604800) * 604800) AS x) / 3600)), ':', toString(toInt32((x % 3600) / 60)), ':', toString(toInt32((x % 3600) % 60)))"
124124
},
125125
{
126126
"print bin(datetime(1970-05-11 13:45:07), 1d)",
127127
"SELECT toDateTime64(toInt64(toFloat64(parseDateTime64BestEffortOrNull('1970-05-11 13:45:07', 9, 'UTC')) / 86400) * 86400, 9, 'UTC')"
128+
},
129+
{
130+
"print bin(datetime(1970-05-11 13:45:07.456345672), 1ms)",
131+
"SELECT toDateTime64(toInt64(toFloat64(parseDateTime64BestEffortOrNull('1970-05-11 13:45:07.456345672', 9, 'UTC')) / 0.001) * 0.001, 9, 'UTC')"
132+
},
133+
{
134+
"print bin(datetime(1970-05-11 13:45:07.456345672), 1microseconds)",
135+
"SELECT toDateTime64(toInt64(toFloat64(parseDateTime64BestEffortOrNull('1970-05-11 13:45:07.456345672', 9, 'UTC')) / 0.000001) * 0.000001, 9, 'UTC')"
128136
}
129137

130138
})));

0 commit comments

Comments
 (0)