-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for escaping characters in KQL key names. #560
Changes from all commits
33d13f0
c84c771
723d49d
e8cb65c
3714295
56b89c9
a62542b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,18 +427,34 @@ bool StringUtils::convert_string_to_double(std::string const& raw, double& conve | |
return true; | ||
} | ||
|
||
void StringUtils::tokenize_column_descriptor( | ||
bool StringUtils::tokenize_column_descriptor( | ||
std::string const& descriptor, | ||
std::vector<std::string>& tokens | ||
) { | ||
// TODO: handle escaped . correctly | ||
auto start = 0U; | ||
auto end = descriptor.find('.'); | ||
while (end != std::string::npos) { | ||
tokens.push_back(descriptor.substr(start, end - start)); | ||
start = end + 1; | ||
end = descriptor.find('.', start); | ||
// TODO: add support for unicode sequences e.g. \u263A | ||
std::string cur_tok; | ||
for (size_t cur = 0; cur < descriptor.size(); ++cur) { | ||
if ('\\' == descriptor[cur]) { | ||
++cur; | ||
if (cur >= descriptor.size()) { | ||
return false; | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else if ('.' == descriptor[cur]) { | ||
if (cur_tok.empty()) { | ||
return false; | ||
} | ||
tokens.push_back(cur_tok); | ||
cur_tok.clear(); | ||
continue; | ||
} | ||
cur_tok.push_back(descriptor[cur]); | ||
} | ||
tokens.push_back(descriptor.substr(start)); | ||
|
||
if (cur_tok.empty()) { | ||
return false; | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
tokens.push_back(cur_tok); | ||
return true; | ||
Comment on lines
+430
to
+458
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Add comprehensive tests for Ensure that various KQL key patterns, including those with escaped characters like periods, are thoroughly tested.
🔗 Analysis chainVerify handling of various KQL key patterns Let's verify the implementation handles various key patterns correctly. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Search for test cases covering various KQL key patterns
# Expected: Find comprehensive test coverage for escaped characters
# Search for test cases
rg -A 5 "TEST.*tokenize_column_descriptor"
# Search for actual usage patterns
rg -A 3 "tokenize_column_descriptor\(" --type cpp
Length of output: 2291 |
||
} | ||
} // namespace clp_s |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,7 +96,7 @@ fragment ESCAPED_SPACE | |
; | ||
|
||
fragment SPECIAL_CHARACTER | ||
: [\\():<>"*?{}] | ||
: [\\():<>"*?{}.] | ||
; | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,4 +187,49 @@ TEST_CASE("Test parsing KQL", "[KQL]") { | |
auto failure = parse_kql_expression(incorrect_query); | ||
REQUIRE(nullptr == failure); | ||
} | ||
|
||
SECTION("Escape sequences in column name") { | ||
auto query = GENERATE( | ||
"a\\.b.c: *", | ||
"\"a\\.b.c\": *", | ||
"a\\.b: {c: *}", | ||
"\"a\\.b\": {\"c\": *}" | ||
); | ||
stringstream escaped_column_query{query}; | ||
auto filter | ||
= std::dynamic_pointer_cast<FilterExpr>(parse_kql_expression(escaped_column_query)); | ||
REQUIRE(nullptr != filter); | ||
REQUIRE(nullptr != filter->get_operand()); | ||
REQUIRE(nullptr != filter->get_column()); | ||
REQUIRE(false == filter->has_only_expression_operands()); | ||
REQUIRE(false == filter->is_inverted()); | ||
REQUIRE(FilterOperation::EQ == filter->get_operation()); | ||
REQUIRE(2 == filter->get_column()->get_descriptor_list().size()); | ||
auto it = filter->get_column()->descriptor_begin(); | ||
REQUIRE(DescriptorToken{"a.b"} == *it++); | ||
REQUIRE(DescriptorToken{"c"} == *it++); | ||
} | ||
|
||
SECTION("Illegal escape sequences in column name") { | ||
auto query = GENERATE( | ||
//"a\\:*", this case is technically legal since ':' gets escaped | ||
"\"a\\\":*", | ||
"a\\ :*", | ||
"\"a\\\" :*", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference between line 218 and 220 (or line 219 and 221)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just confirming that the tokenizing done by ANTLR respects the space really. E.g. if the space were (unexpectedly) included in the column token then it would actually be legal, so we're confirming that this is not the case. |
||
"a.:*", | ||
"\"a.\":*", | ||
"a. :*", | ||
"\"a.\" :*" | ||
); | ||
stringstream illegal_escape{query}; | ||
auto filter = parse_kql_expression(illegal_escape); | ||
REQUIRE(nullptr == filter); | ||
} | ||
|
||
SECTION("Empty token in column name") { | ||
auto query = GENERATE(".a:*", "a.:*", "a..c:*", "a.b.:*"); | ||
stringstream empty_token_column{query}; | ||
auto filter = parse_kql_expression(empty_token_column); | ||
REQUIRE(nullptr == filter); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think
.a
is an illegal case at this step, or leave it topopulate_column_mapping
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree that it is. Currently the code only complains if the last token is "", but I'll change it to complain if any token is "" and add some tests.