Skip to content

Commit 8493798

Browse files
committed
add ALL quantifier for *, % and * path elements
1 parent 7d55ad0 commit 8493798

File tree

8 files changed

+328
-30
lines changed

8 files changed

+328
-30
lines changed

Diff for: expected/jsquery.out

+157
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,163 @@ select 'a = /* noindex */ 5'::jsquery;
12381238
"a" = 5
12391239
(1 row)
12401240

1241+
--ALL
1242+
select 'a.*: = 4';
1243+
?column?
1244+
----------
1245+
a.*: = 4
1246+
(1 row)
1247+
1248+
select '%: = 4';
1249+
?column?
1250+
----------
1251+
%: = 4
1252+
(1 row)
1253+
1254+
select '#:.i = 4';
1255+
?column?
1256+
----------
1257+
#:.i = 4
1258+
(1 row)
1259+
1260+
select '[]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
1261+
?column?
1262+
----------
1263+
f
1264+
(1 row)
1265+
1266+
select '[2,3,4]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
1267+
?column?
1268+
----------
1269+
t
1270+
(1 row)
1271+
1272+
select '[2,3,5]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
1273+
?column?
1274+
----------
1275+
f
1276+
(1 row)
1277+
1278+
select '[2,3,5]' @@ '# ($ > 1 and $ < 5)'::jsquery;
1279+
?column?
1280+
----------
1281+
t
1282+
(1 row)
1283+
1284+
select '[2,3,"x"]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
1285+
?column?
1286+
----------
1287+
f
1288+
(1 row)
1289+
1290+
select '{}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
1291+
?column?
1292+
----------
1293+
f
1294+
(1 row)
1295+
1296+
select '{"a":2,"b":3,"c":4}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
1297+
?column?
1298+
----------
1299+
t
1300+
(1 row)
1301+
1302+
select '{"a":2,"b":3,"c":5}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
1303+
?column?
1304+
----------
1305+
f
1306+
(1 row)
1307+
1308+
select '{"a":2,"b":3,"c":5}' @@ '% ($ > 1 and $ < 5)'::jsquery;
1309+
?column?
1310+
----------
1311+
t
1312+
(1 row)
1313+
1314+
select '{"a":2,"b":3,"c":"x"}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
1315+
?column?
1316+
----------
1317+
f
1318+
(1 row)
1319+
1320+
select '{"a":2,"b":3,"c":4}' @@ '*: ($ > 1 and $ < 5)'::jsquery;
1321+
?column?
1322+
----------
1323+
f
1324+
(1 row)
1325+
1326+
select '{"a":2,"b":3,"c":5}' @@ '*: ($ > 1 and $ < 5)'::jsquery;
1327+
?column?
1328+
----------
1329+
f
1330+
(1 row)
1331+
1332+
select '{"a":2,"b":3,"c":4}' @@ '*: ($ is object OR ($> 1 and $ < 5))'::jsquery;
1333+
?column?
1334+
----------
1335+
t
1336+
(1 row)
1337+
1338+
select '{"a":2,"b":3,"c":5}' @@ '*: ($ is object OR ($> 1 and $ < 5))'::jsquery;
1339+
?column?
1340+
----------
1341+
f
1342+
(1 row)
1343+
1344+
select '{"b":{"ba":3, "bb":4}}' @@ '*: ($ is object OR ($ > 1 and $ < 5))'::jsquery;
1345+
?column?
1346+
----------
1347+
t
1348+
(1 row)
1349+
1350+
select '{"b":{"ba":3, "bb":5}}' @@ '*: ($ is object OR ($> 1 and $ < 5))'::jsquery;
1351+
?column?
1352+
----------
1353+
f
1354+
(1 row)
1355+
1356+
select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":4}}' @@ '*: ($ is object OR ($ > 0 and $ < 5))'::jsquery;
1357+
?column?
1358+
----------
1359+
t
1360+
(1 row)
1361+
1362+
select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":5}}' @@ '*: ($ is object OR ($> 0 and $ < 5))'::jsquery;
1363+
?column?
1364+
----------
1365+
f
1366+
(1 row)
1367+
1368+
select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":5}}' @@ '* ($ > 0 and $ < 5)'::jsquery;
1369+
?column?
1370+
----------
1371+
t
1372+
(1 row)
1373+
1374+
select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":5}}' @@ '*: ($ is object OR $ is numeric)'::jsquery;
1375+
?column?
1376+
----------
1377+
t
1378+
(1 row)
1379+
1380+
select '{"a":{"aa":1, "ab":2}, "b":[5,6]}' @@ '*: ($ is object OR $ is numeric)'::jsquery;
1381+
?column?
1382+
----------
1383+
f
1384+
(1 row)
1385+
1386+
select '{"a":{"aa":1, "ab":2}, "b":[5,6]}' @@ '*: ($ is object OR $ is array OR $ is numeric)'::jsquery;
1387+
?column?
1388+
----------
1389+
t
1390+
(1 row)
1391+
1392+
select '{"a":{"aa":1, "ab":2}, "b":[5,6, {"c":8}]}' @@ '*: ($ is object OR $ is array OR $ is numeric)'::jsquery;
1393+
?column?
1394+
----------
1395+
t
1396+
(1 row)
1397+
12411398
--extract entries for index scan
12421399
SELECT gin_debug_query_path_value('NOT NOT NOT x(y(NOT (a=1) and NOT (b=2)) OR NOT NOT (c=3)) and z = 5');
12431400
gin_debug_query_path_value

Diff for: jsquery.h

+14-11
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ typedef struct
3030
#define PG_RETURN_JSQUERY(p) PG_RETURN_POINTER(p)
3131

3232
typedef enum JsQueryItemType {
33-
jqiNull = jbvNull,
34-
jqiString = jbvString,
35-
jqiNumeric = jbvNumeric,
36-
jqiBool = jbvBool,
37-
jqiArray = jbvArray,
33+
jqiNull = jbvNull,
34+
jqiString = jbvString,
35+
jqiNumeric = jbvNumeric,
36+
jqiBool = jbvBool,
37+
jqiArray = jbvArray,
3838
jqiAnd,
3939
jqiOr,
4040
jqiNot,
@@ -49,6 +49,9 @@ typedef enum JsQueryItemType {
4949
jqiAny,
5050
jqiAnyArray,
5151
jqiAnyKey,
52+
jqiAll,
53+
jqiAllArray,
54+
jqiAllKey,
5255
jqiKey,
5356
jqiCurrent,
5457
jqiIn,
@@ -60,7 +63,7 @@ typedef enum JsQueryItemType {
6063
* JsQueryItemType should not use two high bits
6164
*/
6265
typedef enum JsQueryHint {
63-
jsqIndexDefault = 0x00,
66+
jsqIndexDefault = 0x00,
6467
jsqForceIndex = 0x80,
6568
jsqNoIndex = 0x40
6669
} JsQueryHint;
@@ -70,10 +73,10 @@ typedef enum JsQueryHint {
7073
/*
7174
* Support functions to parse/construct binary value.
7275
* Unlike many other representation of expression the first/main
73-
* node is not an operation but left operand of expression. That
76+
* node is not an operation but left operand of expression. That
7477
* allows to implement cheep follow-path descending in jsonb
75-
* structure and then execute operator with right operand which
76-
* is always a constant.
78+
* structure and then execute operator with right operand which
79+
* is always a constant.
7780
*/
7881

7982
typedef struct JsQueryItem {
@@ -86,7 +89,7 @@ typedef struct JsQueryItem {
8689
struct {
8790
char *data; /* for bool, numeric and string/key */
8891
int datalen; /* filled only for string/key */
89-
} value;
92+
} value;
9093

9194
struct {
9295
int32 left;
@@ -126,7 +129,7 @@ typedef struct JsQueryParseItem JsQueryParseItem;
126129

127130
struct JsQueryParseItem {
128131
JsQueryItemType type;
129-
JsQueryHint hint;
132+
JsQueryHint hint;
130133
JsQueryParseItem *next; /* next in path */
131134

132135
union {

Diff for: jsquery_gram.y

+3
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ key:
306306
'*' { $$ = makeItemType(jqiAny); }
307307
| '#' { $$ = makeItemType(jqiAnyArray); }
308308
| '%' { $$ = makeItemType(jqiAnyKey); }
309+
| '*' ':' { $$ = makeItemType(jqiAll); }
310+
| '#' ':' { $$ = makeItemType(jqiAllArray); }
311+
| '%' ':' { $$ = makeItemType(jqiAllKey); }
309312
| '$' { $$ = makeItemType(jqiCurrent); }
310313
| STRING_P { $$ = makeItemKey(&$1); }
311314
| IN_P { $$ = makeItemKey(&$1); }

Diff for: jsquery_io.c

+25-4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ flattenJsQueryParseItem(StringInfo buf, JsQueryParseItem *item)
116116
case jqiAny:
117117
case jqiAnyArray:
118118
case jqiAnyKey:
119+
case jqiAll:
120+
case jqiAllArray:
121+
case jqiAllKey:
119122
break;
120123
default:
121124
elog(ERROR, "Unknown type: %d", item->type);
@@ -310,15 +313,15 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes
310313
printJsQueryItem(buf, &elem, false, true);
311314
appendStringInfoChar(buf, ')');
312315
break;
313-
case jqiAny:
316+
case jqiCurrent:
314317
if (inKey)
315318
appendStringInfoChar(buf, '.');
316-
appendStringInfoChar(buf, '*');
319+
appendStringInfoChar(buf, '$');
317320
break;
318-
case jqiCurrent:
321+
case jqiAny:
319322
if (inKey)
320323
appendStringInfoChar(buf, '.');
321-
appendStringInfoChar(buf, '$');
324+
appendStringInfoChar(buf, '*');
322325
break;
323326
case jqiAnyArray:
324327
if (inKey)
@@ -330,6 +333,24 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes
330333
appendStringInfoChar(buf, '.');
331334
appendStringInfoChar(buf, '%');
332335
break;
336+
case jqiAll:
337+
if (inKey)
338+
appendStringInfoChar(buf, '.');
339+
appendStringInfoChar(buf, '*');
340+
appendStringInfoChar(buf, ':');
341+
break;
342+
case jqiAllArray:
343+
if (inKey)
344+
appendStringInfoChar(buf, '.');
345+
appendStringInfoChar(buf, '#');
346+
appendStringInfoChar(buf, ':');
347+
break;
348+
case jqiAllKey:
349+
if (inKey)
350+
appendStringInfoChar(buf, '.');
351+
appendStringInfoChar(buf, '%');
352+
appendStringInfoChar(buf, ':');
353+
break;
333354
default:
334355
elog(ERROR, "Unknown JsQueryItem type: %d", v->type);
335356
}

0 commit comments

Comments
 (0)