Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xi7hI-0008U3-Va for pgsql-bugs@arkaria.postgresql.org; Sat, 25 Oct 2014 20:12:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xi7hH-0003sL-9T for pgsql-bugs@arkaria.postgresql.org; Sat, 25 Oct 2014 20:12:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xi7hF-0003sC-6h for pgsql-bugs@postgresql.org; Sat, 25 Oct 2014 20:12:21 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Xi7h6-0003Q6-M8 for pgsql-bugs@postgresql.org; Sat, 25 Oct 2014 20:12:16 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 6336528230 for ; Sat, 25 Oct 2014 13:12:07 -0700 (PDT) Received: from EX13-CAS-008.vmware.com (EX13-CAS-008.vmware.com [10.113.191.58]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 5DB80B16D7 for ; Sat, 25 Oct 2014 13:12:07 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-009.vmware.com (10.113.191.29) with Microsoft SMTP Server (TLS) id 15.0.775.38; Sat, 25 Oct 2014 13:11:42 -0700 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.775.38; Sat, 25 Oct 2014 13:11:23 -0700 Message-ID: <544C03E8.4020402@vmware.com> Date: Sat, 25 Oct 2014 23:11:20 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.1.2 MIME-Version: 1.0 To: Pg Bugs Subject: tsquery @> operator bugs Content-Type: multipart/mixed; boundary="------------000808090905040507020900" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -8.4 (--------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --------------000808090905040507020900 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit While looking at all the places where we currently use CRC, I bumped into this: postgres=# select 'penomaha'::tsquery @> 'lbgimpca'::tsquery; ?column? ---------- t (1 row) The @> operator is supposed to return true if the first query contains all the terms of the second query. The above result is bogus; the strings are completely different. It returns true because both terms have the same CRC (with our funky CRC algorithm), and the tsq_mcontains function only compares the CRCs, not the actual values. Another bug is that the function performs a length check first, and returns false if the second string is larger than the first. The thinking goes that the first string cannot possibly contain the second string if the second string is larger. But that doesn't take into account that there can be duplicate strings (this is basically the same bug that was recently fixed in jsonb): postgres=# select 'a & b' @> 'a & a'::tsquery; /* CORRECT */ ?column? ---------- t (1 row) postgres-# select 'a' @> 'a & a'::tsquery; /* WRONG */ ?column? ---------- f (1 row) I propose the attached fix. It completely rewrites the tsq_mcontains function, so that it first extracts all the strings from both tsqueries, then sorts them and removes duplicates, and then compares the arrays. (I actually find the whole operator pretty useless. What is it good for? But that's a different story..) - Heikki --------------000808090905040507020900 Content-Type: text/x-diff; name="fix-tsquery-contains-op-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-tsquery-contains-op-1.patch" diff --git a/src/backend/utils/adt/tsquery_op.c b/src/backend/utils/adt/tsquery_op.c index 111ec62..d06a5b2 100644 --- a/src/backend/utils/adt/tsquery_op.c +++ b/src/backend/utils/adt/tsquery_op.c @@ -213,63 +213,112 @@ makeTSQuerySign(TSQuery a) return sign; } -Datum -tsq_mcontains(PG_FUNCTION_ARGS) +static char ** +collectTSQueryValues(TSQuery a, int *nvalues_p) { - TSQuery query = PG_GETARG_TSQUERY(0); - TSQuery ex = PG_GETARG_TSQUERY(1); - TSQuerySign sq, - se; - int i, - j; - QueryItem *iq, - *ie; - - if (query->size < ex->size) + QueryItem *ptr = GETQUERY(a); + char *operand = GETOPERAND(a); + char **values; + int nvalues = 0; + int i; + + values = (char **) palloc(sizeof(char *) * a->size); + + for (i = 0; i < a->size; i++) { - PG_FREE_IF_COPY(query, 0); - PG_FREE_IF_COPY(ex, 1); + if (ptr->type == QI_VAL) + { + int len = ptr->qoperand.length; + char *val; + + val = palloc(len + 1); + memcpy(val, operand + ptr->qoperand.distance, len); + val[len] = '\0'; - PG_RETURN_BOOL(false); + values[nvalues++] = val; + } + ptr++; } - sq = makeTSQuerySign(query); - se = makeTSQuerySign(ex); + *nvalues_p = nvalues; + return values; +} + +static int +cmp_string(const void *a, const void *b) +{ + const char *sa = *((const char **) a); + const char *sb = *((const char **) b); + return strcmp(sa, sb); +} - if ((sq & se) != se) +static int +remove_duplicates(char **strings, int n) +{ + if (n <= 1) + return n; + else { - PG_FREE_IF_COPY(query, 0); - PG_FREE_IF_COPY(ex, 1); + int i; + char *prev = strings[0]; + int new_n = 1; - PG_RETURN_BOOL(false); + for (i = 1; i < n; i++) + { + if (strcmp(strings[i], prev) != 0) + { + strings[new_n++] = strings[i]; + prev = strings[i]; + } + } + return new_n; } +} - iq = GETQUERY(query); - ie = GETQUERY(ex); - - for (i = 0; i < ex->size; i++) +Datum +tsq_mcontains(PG_FUNCTION_ARGS) +{ + TSQuery query = PG_GETARG_TSQUERY(0); + TSQuery ex = PG_GETARG_TSQUERY(1); + char **query_values; + int query_nvalues; + char **ex_values; + int ex_nvalues; + bool result = true; + + /* Extract the query terms into arrays */ + query_values = collectTSQueryValues(query, &query_nvalues); + ex_values = collectTSQueryValues(ex, &ex_nvalues); + + /* Sort and remove duplicates from both arrays */ + qsort(query_values, query_nvalues, sizeof(char *), cmp_string); + query_nvalues = remove_duplicates(query_values, query_nvalues); + qsort(ex_values, ex_nvalues, sizeof(char *), cmp_string); + ex_nvalues = remove_duplicates(ex_values, ex_nvalues); + + if (ex_nvalues > query_nvalues) + result = false; + else { - if (ie[i].type != QI_VAL) - continue; - for (j = 0; j < query->size; j++) + int i; + int j = 0; + + for (i = 0; i < ex_nvalues; i++) { - if (iq[j].type == QI_VAL && - ie[i].qoperand.valcrc == iq[j].qoperand.valcrc) + for (; j < query_nvalues; j++) + { + if (strcmp(ex_values[i], query_values[j]) == 0) + break; + } + if (j == query_nvalues) + { + result = false; break; - } - if (j >= query->size) - { - PG_FREE_IF_COPY(query, 0); - PG_FREE_IF_COPY(ex, 1); - - PG_RETURN_BOOL(false); + } } } - PG_FREE_IF_COPY(query, 0); - PG_FREE_IF_COPY(ex, 1); - - PG_RETURN_BOOL(true); + PG_RETURN_BOOL(result); } Datum --------------000808090905040507020900 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------000808090905040507020900--