GIN jsonb_ops misses root strings in strict jsonpath queries

Поиск
Список
Период
Сортировка
Искать

GIN jsonb_ops misses root strings in strict jsonpath queries

От:
Rui Zhao <zhaorui126@gmail.com>
Дата:
Вложения:
Hi,

A GIN jsonb_ops scan can miss a root string when a jsonpath comparison
uses strict mode. I reproduced this on master at e75fcc9867f.

Using the existing test_jsonb data in jsonb.sql, which includes the
root string "a scalar":

SELECT test_json FROM test_jsonb
WHERE test_json @@ 'strict $ == "a scalar"';
-- "a scalar"

SELECT test_json FROM test_jsonb
WHERE test_json @? 'strict $ ? (@ == "a scalar")';
-- "a scalar"

CREATE INDEX test_jsonb_idx ON test_jsonb USING gin (test_json);
SET enable_seqscan = off;

SELECT test_json FROM test_jsonb
WHERE test_json @@ 'strict $ == "a scalar"';
-- 0 rows

SELECT test_json FROM test_jsonb
WHERE test_json @? 'strict $ ? (@ == "a scalar")';
-- 0 rows

The index stores root strings with JGINFLAG_KEY. Internally, a root
scalar is wrapped in an array. JsonbValueToJsonb() does this inside
its IsAJsonbScalar(val) branch:

    scalarArray.type = jbvArray;
    scalarArray.val.array.rawScalar = true;
    scalarArray.val.array.nElems = 1;

    pushJsonbValue(&pstate, WJB_BEGIN_ARRAY, &scalarArray); /* [ */
    pushJsonbValue(&pstate, WJB_ELEM, val); /* original value */
    pushJsonbValue(&pstate, WJB_END_ARRAY, NULL); /* ] */

gin_extract_jsonb() therefore sees WJB_ELEM and stores the root string
in the GIN index with the JGINFLAG_KEY marker.
But when processing the SQL query, jsonb_ops__extract_nodes() sets
key_entry to GIN_FALSE for the root in strict mode. That searches for
JGINFLAG_STR instead, so it cannot find the matching root string.

The attached patch changes that root case to GIN_TRUE. Both queries
then return "a scalar" using the existing index; no REINDEX is needed.
This bug affects only strict-mode queries using jsonb_ops.

The patch adds the queries above to jsonb.sql. make check passed all
239 tests. Without the code fix, only the two new indexed queries fail.

Regards,
Rui

Re: GIN jsonb_ops misses root strings in strict jsonpath queries

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Rui,

I ran into the same bug on my side and ended up with the same one-line
fix, so +1 to v1.

Thanks,
Shihao

Re: GIN jsonb_ops misses root strings in strict jsonpath queries

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
shihao zhong  writes:
> Hi Rui,
> I ran into the same bug on my side and ended up with the same one-line
> fix, so +1 to v1.

Pushed with minor editorialization.

			regards, tom lane


FAQ