Skip to content
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

clp-s: Add support for using "?" in wildcard queries on single-word values (resolves #392). #409

Merged
merged 7 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/core/src/clp_s/search/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,9 @@ void Output::populate_string_queries(std::shared_ptr<Expression> const& expr) {
}

std::unordered_set<int64_t>& matching_vars = m_string_var_match_map[query_string];
if (query_string.find('*') == std::string::npos) {
if (query_string.find('*') == std::string::npos
&& query_string.find('?') == std::string::npos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my other comment we should be handling escaping here as well. Specifically if there are no unescaped * or ? we should enter this code block, process the string to unescape escaped characters as necessary, then do the precise search.

E.g. \\var\? should enter this code block and get unescaped to become \var? before performing exact match.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function wildcard_match_unsafe_case_sensitive can handle escape characters correctly. The corner case was mentioned above and will be fixed later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but this code block uses m_var_dict->get_entry_matching_value() which doesn't respect escape characters.

Copy link
Contributor

@gibber9809 gibber9809 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for the condition here we can add a method like

bool has_unescaped_wildchards(std::string const& query) {
    bool escaped{false};
    for (size_t i = 0; i < query.size(); ++i) {
        if ('*' == query[i] || '?' == query[i]) {
            return true;
        }
        if ('\' == query[i]) {
            ++i;
        }
    }
    return false;
}

and replace the if condition with false == has_unescaped_wildcards(query)?

{
auto entry = m_var_dict->get_entry_matching_value(query_string, m_ignore_case);

if (entry != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion components/core/src/clp_s/search/StringLiteral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StringLiteral : public Literal {
m_string_type = LiteralType::VarStringT;
}

if (m_v.find('*') != std::string::npos) {
if (m_v.find('*') != std::string::npos || m_v.find('?') != std::string::npos) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's wrong in the original code as well, but could we fix this to handle escaped * and ? properly as well? This should really be checking just for unescaped * or ?, and ignoring them when they're escaped. E.g. var\? is just a variable string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

m_string_type |= LiteralType::ClpStringT;
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/core/src/clp_s/search/kql/Kql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fragment UNQUOTED_CHARACTER
| ~[\\():<>"{} \r\n\t]
;

fragment WILDCARD: '*';
fragment WILDCARD: '*' | '?';

// TODO: unescape keywords
fragment ESCAPED_KEYWORD
Expand Down Expand Up @@ -96,7 +96,7 @@ fragment ESCAPED_SPACE
;

fragment SPECIAL_CHARACTER
: [\\():<>"*{}]
: [\\():<>"*?{}]
;


Expand Down
Loading