Skip to content

Commit

Permalink
query-parser: handle naked NOT, add tests
Browse files Browse the repository at this point in the history
We weren't correctly expanding "naked NOT" -> AND_NOT

Fixes #2559.
  • Loading branch information
djcb committed Sep 21, 2023
1 parent 8fc13ab commit b771fd6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/mu-query-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ unit(Sexp& tokens, ParseContext& ctx)

/* special case: interpret "not" as a matcher instead; */
if (sub.empty())
return Sexp{placeholder_sym, not_sym.name};
return matcher(prepend(tokens, Sexp{placeholder_sym, not_sym.name}), ctx);

/* we try to optimize: double negations are removed */
if (sub.head_symbolp(not_sym))
Expand Down Expand Up @@ -214,6 +214,8 @@ factor(Sexp& tokens, ParseContext& ctx)
auto implicit_and = [&]() {
if (tokens.head_symbolp(open_sym))
return true;
else if (tokens.head_symbolp(not_sym)) // turn a lone 'not' -> 'and not'
return true;
else if (auto&& head{tokens.head()}; head)
return looks_like_matcher(*head);
else
Expand All @@ -222,7 +224,6 @@ factor(Sexp& tokens, ParseContext& ctx)

Sexp uns;
while (true) {

if (tokens.head_symbolp(and_sym))
tokens.pop_front();
else if (!implicit_and())
Expand Down Expand Up @@ -362,7 +363,8 @@ test_parser_basic()
TestCase{R"(a and (b or c))", R"((and (_ "a") (or (_ "b") (_ "c"))))"},
// not a and not b
TestCase{R"(not a and b)", R"((and (not (_ "a")) (_ "b")))"},
// TODO: add more...
// a not b
TestCase{R"(a not b)", R"((and (_ "a") (not (_ "b"))))"},
};

for (auto&& test: cases) {
Expand Down
7 changes: 7 additions & 0 deletions lib/mu-query-xapianizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ test_xapian()
R"(Query((Shello world OR (Shello PHRASE 2 Sworld))))"},
TestCase{R"(subject:/boo/")", R"(Query())"},

// logic
TestCase{R"(not)", R"(Query((Tnot OR Cnot OR Hnot OR Fnot OR Snot OR Bnot OR Enot)))"},
TestCase{R"(from:a and (from:b or from:c))", R"(Query((Fa AND (Fb OR Fc))))"},
// optimize?
TestCase{R"(not from:a and to:b)", R"(Query(((<alldocuments> AND_NOT Fa) AND Tb)))"},
TestCase{R"(cc:a not bcc:b)", R"(Query((Ca AND (<alldocuments> AND_NOT Hb))))"},

// ranges.
TestCase{R"(size:1..10")", R"(Query(VALUE_RANGE 17 g1 ga))"},
TestCase{R"(size:10..1")", R"(Query(VALUE_RANGE 17 g1 ga))"},
Expand Down

0 comments on commit b771fd6

Please sign in to comment.