From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id AEKgItaU82CZGAAAgWs5BA (envelope-from ) for ; Sun, 18 Jul 2021 04:41:26 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id WGJPHtaU82CwGAAAB5/wlQ (envelope-from ) for ; Sun, 18 Jul 2021 02:41:26 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 14CC92267C for ; Sun, 18 Jul 2021 04:41:26 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 6A55C29915; Sat, 17 Jul 2021 22:41:00 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id EA5D7290A8 for ; Sat, 17 Jul 2021 22:40:37 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id E0F105FD21; Sat, 17 Jul 2021 22:40:37 -0400 (EDT) Received: (nullmailer pid 3853969 invoked by uid 1000); Sun, 18 Jul 2021 02:40:27 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 24/25] lib/parse-sexp: support infix subqueries Date: Sat, 17 Jul 2021 23:40:20 -0300 Message-Id: <20210718024021.3850340-25-david@tethera.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210718024021.3850340-1-david@tethera.net> References: <20210718024021.3850340-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: NOM66NZ4JTFZ2C6CVXRAVVO22WMEHQ37 X-Message-ID-Hash: NOM66NZ4JTFZ2C6CVXRAVVO22WMEHQ37 X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1626576086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=eelTBm5OsJsLJKT1BKIHJ5Wif/Mz+VfU97eZy/FojwU=; b=fKOGUjRryyMZsI2e1y9kxCxnRREOmq8gt+9IDnrpgFCT3FrUYLMowNkyu4w/NALc2GJ+ML l8PF9OcshEjCE+EFX34RfSio3T3g/epkLwfwk4heMWwemZH0zOpXE+n+nHUd8HqCpjd9g3 IHmaJFUueGzbZ7c0W0AxVZHig0Q9Q+s6q7yZKb8eAmQCNJil8NWF0kgM9KR4gaNMdRDsRb l/ONMN7mUVhj73rfKMclwOsYdh8NgUDJ6rDrOg9jpGDQpB3rLkLx2N9UcNuU3BaqufQL0/ hYUgL9jZCwwWt6iwGaSW1BtLRL+uAj7lwzxv9g2Dlq64Ld7ocBw7eaTs1W+rZQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1626576086; a=rsa-sha256; cv=none; b=VccnAOdMx8tabbAxGD3pOFEn04nO2RWbGFUr5zOPU1P0dHsCKzmJvfIGe1r4woFqLshidF wQ6SZN4d0ItJqYGNwIqQvOXiO2vERtkDV7u/A3kIwi/lJYfSdxKjChtmEEnr1qls/PnMA1 tYfSN1h2C96p3KERZY+LQ8GA/nEs0fcC3Lh8VkEwEYemIeNVdUpLokaYfkcem+U99JPvKR ycBpwN6pjhmfo5H32PIaNZUpuH4vGdZUT+O64CZ1MdGWvbpCd6DpokVTbZYDLLQiITJplT zs67/634VoaMEZKHCqxVvOAhR/ksuaBpeIIIJv/v0cXm30OAbZ3FnGr4PcyZRg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.05 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 14CC92267C X-Spam-Score: -1.05 X-Migadu-Scanner: scn0.migadu.com X-TUID: I57b8SxslAIT This is necessary so that programs can take infix syntax queries from a user and use the sexp query syntax to construct e.g. a refinement of that query. --- lib/parse-sexp.cc | 32 ++++++++++++++++++++++++++++++++ test/T081-sexpr-search.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc index df3629af..502b1be0 100644 --- a/lib/parse-sexp.cc +++ b/lib/parse-sexp.cc @@ -56,6 +56,7 @@ static _sexp_field_t fields[] = { "from", Xapian::Query::OP_PHRASE, SEXP_FLAG_REGEXP }, { "folder", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "id", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, + { "infix", Xapian::Query::OP_INVALID, SEXP_FLAG_NONE }, { "is", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEXP }, { "mid", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "mimetype", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, @@ -236,6 +237,34 @@ _sexp_parse_keywords (notmuch_database_t *notmuch, const char *prefix, const sex return NOTMUCH_STATUS_SUCCESS; } +static notmuch_status_t +_sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output) +{ + + if (sx->ty != SEXP_VALUE) { + _notmuch_database_log (notmuch, "infix argument must be value, not list\n"); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + try { + output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS); + } catch (const Xapian::QueryParserError &error) { + _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } catch (const Xapian::Error &error) { + if (! notmuch->exception_reported) { + _notmuch_database_log (notmuch, + "A Xapian exception occurred parsing query: %s\n", + error.get_msg ().c_str ()); + _notmuch_database_log_append (notmuch, + "Query string was: %s\n", + sx->val); + notmuch->exception_reported = true; + return NOTMUCH_STATUS_XAPIAN_EXCEPTION; + } + } + return NOTMUCH_STATUS_SUCCESS; +} + /* Here we expect the s-expression to be a proper list, with first * element defining and operation, or as a special case the empty * list */ @@ -296,6 +325,9 @@ _notmuch_sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xa return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output); } } + if (strcasecmp (field->name, "infix") == 0) { + return _sexp_parse_infix (notmuch, rest, output); + } term_prefix = _find_prefix (field->name); diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh index f0a7efb0..45568ba5 100755 --- a/test/T081-sexpr-search.sh +++ b/test/T081-sexpr-search.sh @@ -471,4 +471,39 @@ missing subquery EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "infix query" +notmuch search to:searchbyto | notmuch_search_sanitize > EXPECTED +notmuch search --query-syntax=sexp '(infix "to:searchbyto")' | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "bad infix query 1" +notmuch search --query-syntax=sexp '(infix "from:/unbalanced")' 2>&1| notmuch_search_sanitize > OUTPUT +cat < EXPECTED +notmuch search: Syntax error in query +Syntax error in infix query: from:/unbalanced +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "bad infix query 2" +notmuch search --query-syntax=sexp '(infix "thread:{unbalanced")' 2>&1| notmuch_search_sanitize > OUTPUT +cat < EXPECTED +notmuch search: Syntax error in query +Syntax error in infix query: thread:{unbalanced +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "infix query that matches no messages" +notmuch search --query-syntax=sexp '(and (infix "from:keithp") (infix "to:keithp"))' > OUTPUT +test_expect_equal_file /dev/null OUTPUT + +test_begin_subtest "compound infix query" +notmuch search date:2009-11-18..2009-11-18 and tag:unread > EXPECTED +notmuch search --query-syntax=sexp '(infix "date:2009-11-18..2009-11-18 and tag:unread")' > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "compound infix query 2" +notmuch search date:2009-11-18..2009-11-18 and tag:unread > EXPECTED +notmuch search --query-syntax=sexp '(and (infix "date:2009-11-18..2009-11-18") (infix "tag:unread"))' > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + test_done -- 2.30.2