Skip to content

Commit 0342716

Browse files
Functional testcas added
1 parent bbd2a61 commit 0342716

12 files changed

+35
-38
lines changed

src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ bool ToDecimal::convertImpl(String & out, IParser::Pos & pos)
147147

148148
if (is_string)
149149
out = "NULL";
150-
else if (std::regex_match(res , expr))
150+
else if (std::regex_match(res, expr))
151151
{
152152
auto exponential_pos = res.find("e");
153153
if (res[exponential_pos +1] == '+' || res[exponential_pos + 1] == '-' )

src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp

+8-13
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ bool DatetimePart::convertImpl(String & out, IParser::Pos & pos)
9999
}
100100
String format;
101101

102-
if(part == "YEAR" )
102+
if (part == "YEAR" )
103103
format = "%G";
104104
else if (part == "QUARTER" )
105105
format = "%Q";
@@ -325,7 +325,7 @@ bool FormatDateTime::convertImpl(String & out, IParser::Pos & pos)
325325
i = i + arg.size();
326326
}
327327
}
328-
if(decimal > 0 && formatspecifier.find('.')!=String::npos)
328+
if (decimal > 0 && formatspecifier.find('.')!=String::npos)
329329
{
330330

331331
out = std::format("concat("
@@ -416,13 +416,12 @@ bool FormatTimeSpan::convertImpl(String & out, IParser::Pos & pos)
416416
if (decimal > 0)
417417
{
418418
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);
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);
420420
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);
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);
422422
}
423423
else
424424
out = std::format("formatDateTime(toDateTime64({0},9,'UTC'),'{1}')", datetime,formatspecifier);
425-
426425
}
427426
else
428427
{
@@ -473,7 +472,7 @@ bool MakeTimeSpan::convertImpl(String & out, IParser::Pos & pos)
473472
{
474473
String arg = getConvertedArgument(fn_name, pos);
475474
args.insert(args.begin(),arg);
476-
if(pos->type == TokenType::Comma)
475+
if (pos->type == TokenType::Comma)
477476
++pos;
478477
++arg_count;
479478
}
@@ -540,7 +539,7 @@ bool MakeDateTime::convertImpl(String & out, IParser::Pos & pos)
540539
while (!pos->isEnd() && pos->type != TokenType::ClosingRoundBracket)
541540
{
542541
String arg = getConvertedArgument(fn_name, pos);
543-
if(pos->type == TokenType::Comma)
542+
if (pos->type == TokenType::Comma)
544543
++pos;
545544
arguments = arguments + arg + ",";
546545
++arg_count;
@@ -705,15 +704,11 @@ bool UnixTimeSecondsToDateTime::convertImpl(String & out, IParser::Pos & pos)
705704

706705
++pos;
707706
if (pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral)
708-
throw Exception("String Literal is not accepted." + fn_name + " accepts only long, int and double " , ErrorCodes::BAD_ARGUMENTS);
707+
throw Exception(fn_name + " accepts only long, int and double type of arguments " , ErrorCodes::BAD_ARGUMENTS);
709708

710709
String expression = getConvertedArgument(fn_name, pos);
711710

712-
if (std::any_of(expression.begin(), expression.end(), ::isalpha))
713-
throw Exception("String Literal is not accepted." + fn_name + " accepts only long, int and double " , ErrorCodes::BAD_ARGUMENTS);
714-
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);
711+
out = std::format("toDateTime64({0} , 9,'UTC')", expression);
717712

718713
return true;
719714
}

src/Parsers/Kusto/ParserKQLDateTypeTimespan.cpp

+7-18
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,8 @@ bool ParserKQLDateTypeTimespan :: parseImpl(Pos & pos, [[maybe_unused]] ASTPtr
2020
if (pos->type == TokenType::QuotedIdentifier || pos->type == TokenType::StringLiteral )
2121
token = String(pos->begin + 1, pos->end -1);
2222
else
23-
{
24-
auto start = pos;
25-
while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon)
26-
{
27-
++pos;
28-
if (pos->type == TokenType::ClosingRoundBracket)
29-
break;
30-
}
31-
--pos;
32-
token = String(start->begin,pos->end);
33-
}
34-
23+
token = String(pos->begin, pos->end);
24+
3525
if (!parseConstKQLTimespan(token))
3626
return false;
3727

@@ -114,7 +104,7 @@ bool ParserKQLDateTypeTimespan :: parseConstKQLTimespan(const String & text)
114104
++index;
115105
return index > start ? index - start : -1;
116106
};
117-
if(*ptr == '-')
107+
if (*ptr == '-')
118108
{
119109
sign = true;
120110
++ptr;
@@ -140,9 +130,6 @@ bool ParserKQLDateTypeTimespan :: parseConstKQLTimespan(const String & text)
140130
days = 0;
141131
}
142132

143-
if (hours > 23)
144-
return false;
145-
146133
if (*(ptr + number_len) != ':')
147134
{
148135
String timespan_suffix(ptr + number_len, ptr + text.size());
@@ -157,7 +144,9 @@ bool ParserKQLDateTypeTimespan :: parseConstKQLTimespan(const String & text)
157144
return true;
158145
}
159146

160-
147+
if (hours > 23)
148+
return false;
149+
161150
auto min_len = scanDigit(ptr + number_len + 1);
162151
if (min_len < 0)
163152
return false;
@@ -193,7 +182,7 @@ bool ParserKQLDateTypeTimespan :: parseConstKQLTimespan(const String & text)
193182
auto exponent = 9 - sec_scale_len; // max supported length of fraction of seconds is 9
194183
nanoseconds = nanoseconds * pow(10, exponent );
195184

196-
if(sign)
185+
if (sign)
197186
time_span = -(days * 86400 + hours * 3600 + minutes * 60 + seconds + (nanoseconds /1000000000 ));
198187
else
199188
time_span = days * 86400 + hours * 3600 + minutes * 60 + seconds + (nanoseconds /1000000000 );

src/Parsers/tests/KQL/gtest_KQL_dateTimeFunctions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery, ParserTest,
5252
},
5353
{
5454
"print unixtime_seconds_todatetime(1546300899)",
55-
"SELECT toDateTime64(1546300899, 9, 'UTC')"
55+
"SELECT multiIf((toTypeName(1546300899) = 'String') OR (toTypeName(1546300899) = 'UUID') OR (toTypeName(1546300899) = 'DateTime64(3)'), 'Only Accepted arguments are float , int and double', toString(toDateTime64(1546300899, 9, 'UTC')))"
5656
},
5757
{
5858
"print dayofweek(datetime(2015-12-20))",

tests/queries/0_stateless/02366_kql_datatype.reference

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ inf
2727
2014-05-25 08:20:03.123456000
2828
2014-11-08 15:55:55.123456000
2929
-- time
30-
1038655.123
30+
45055.123
3131
86400
3232
-86400
33+
6e-9
34+
6e-10
3335
-- guid
3436
172800
3537
5400
@@ -38,7 +40,7 @@ inf
3840
0.1
3941
0.1
4042
0.00001
41-
0
43+
1e-10
4244
3
4345
-- null
4446
1

tests/queries/0_stateless/02366_kql_datatype.sql

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ print time('14.02:03:04.12345');
3434
print time('12:30:55.123');
3535
print time(1d);
3636
print time(-1d);
37+
print time(6nanoseconds);
38+
print time(6tick);
3739
print '-- guid'
3840
print guid(74be27de-1e4e-49d9-b579-fe0b331d3642)
3941
-- print guid(null)

tests/queries/0_stateless/02366_kql_func_datetime.reference

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- dayofmonth()
22
31
33
-- dayofweek()
4-
4
4+
4.00:00:00
55
-- dayofyear()
66
365
77
-- getmonth()
@@ -63,6 +63,8 @@
6363
17-01-29 [09:00:05] 2017-01-29 [09:00:05] 17-01-29 [09:00:05 AM]
6464
-- format_timespan()
6565
02:03:04.1234500
66+
29.09:00:05:12
67+
029.09:00:05 [1234500]
6668
-- ago()
6769
-- datetime_diff()
6870
17 2 13 4 29 2 5 10

tests/queries/0_stateless/02366_kql_func_datetime.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ print format_datetime(datetime(2015-12-14 02:03:04.12345), 'y-M-d h:m:s.fffffff'
6565
print v1=format_datetime(datetime(2017-01-29 09:00:05),'yy-MM-dd [HH:mm:ss]'), v2=format_datetime(datetime(2017-01-29 09:00:05), 'yyyy-M-dd [H:mm:ss]'), v3=format_datetime(datetime(2017-01-29 09:00:05), 'yy-MM-dd [hh:mm:ss tt]');
6666
print '-- format_timespan()';
6767
print format_timespan(time('14.02:03:04.12345'), 'h:m:s.fffffff');
68-
-- print v1=format_timespan(time('29.09:00:05.12345'), 'dd.hh:mm:ss:FF'), v2=format_timespan(time('29.09:00:05.12345'), 'ddd.h:mm:ss [fffffff]');
68+
print v1=format_timespan(time('29.09:00:05.12345'), 'dd.hh:mm:ss:FF');
69+
print v2=format_timespan(time('29.09:00:05.12345'), 'ddd.h:mm:ss [fffffff]');
6970
print '-- ago()';
7071
-- print ago(1d) - now();
7172
print '-- datetime_diff()';

tests/queries/0_stateless/02366_kql_func_scalar.reference

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
4
1111
1970-05-11 00:00:00.000000000
1212
336:0:0
13+
1970-05-11 13:45:07.345000000
14+
1970-05-11 13:45:07.345620000

tests/queries/0_stateless/02366_kql_func_scalar.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ Bin_at_test | summarize sum(Num) by toDateTime(bin_at(Date, 1d, datetime('2018-0
1616
print '-- bin()';
1717
print bin(4.5, 1);
1818
print bin(datetime(1970-05-11 13:45:07), 1d);
19-
print bin(16d, 7d);
19+
print bin(16d, 7d);
20+
print bin(datetime(1970-05-11 13:45:07.345623), 1ms);
21+
print bin(datetime(1970-05-11 13:45:07.345623), 6microsecond);

tests/queries/0_stateless/02366_kql_func_string.reference

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ PINEAPPLE
262262
-1
263263
-- base64_encode_fromguid()
264264
YWUzMTMzZjItNmUyMi00OWFlLWIwNmEtMTZlNmE5YjIxMmVi
265+
\N
265266
-- base64_decode_toarray()
266267
[75,117,115,116,111]
267268
-- base64_decode_toguid()

tests/queries/0_stateless/02366_kql_func_string.sql

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Customers | project indexof('abcdefg','cde',2) | take 1;
184184
Customers | project indexof('abcdefg','cde',6) | take 1;
185185
print '-- base64_encode_fromguid()';
186186
print base64_encode_fromguid('ae3133f2-6e22-49ae-b06a-16e6a9b212eb');
187+
print base64_encode_fromguid("abcd1231");
187188
print '-- base64_decode_toarray()';
188189
print base64_decode_toarray('S3VzdG8=');
189190
print '-- base64_decode_toguid()';

0 commit comments

Comments
 (0)