Skip to content

Commit

Permalink
add ALL quantifier for *, % and * path elements
Browse files Browse the repository at this point in the history
  • Loading branch information
feodor committed Mar 3, 2015
1 parent 7d55ad0 commit 8493798
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 30 deletions.
157 changes: 157 additions & 0 deletions expected/jsquery.out
Expand Up @@ -1238,6 +1238,163 @@ select 'a = /* noindex */ 5'::jsquery;
"a" = 5
(1 row)

--ALL
select 'a.*: = 4';
?column?
----------
a.*: = 4
(1 row)

select '%: = 4';
?column?
----------
%: = 4
(1 row)

select '#:.i = 4';
?column?
----------
#:.i = 4
(1 row)

select '[]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '[2,3,4]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
t
(1 row)

select '[2,3,5]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '[2,3,5]' @@ '# ($ > 1 and $ < 5)'::jsquery;
?column?
----------
t
(1 row)

select '[2,3,"x"]' @@ '#: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '{}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '{"a":2,"b":3,"c":4}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
t
(1 row)

select '{"a":2,"b":3,"c":5}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '{"a":2,"b":3,"c":5}' @@ '% ($ > 1 and $ < 5)'::jsquery;
?column?
----------
t
(1 row)

select '{"a":2,"b":3,"c":"x"}' @@ '%: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '{"a":2,"b":3,"c":4}' @@ '*: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '{"a":2,"b":3,"c":5}' @@ '*: ($ > 1 and $ < 5)'::jsquery;
?column?
----------
f
(1 row)

select '{"a":2,"b":3,"c":4}' @@ '*: ($ is object OR ($> 1 and $ < 5))'::jsquery;
?column?
----------
t
(1 row)

select '{"a":2,"b":3,"c":5}' @@ '*: ($ is object OR ($> 1 and $ < 5))'::jsquery;
?column?
----------
f
(1 row)

select '{"b":{"ba":3, "bb":4}}' @@ '*: ($ is object OR ($ > 1 and $ < 5))'::jsquery;
?column?
----------
t
(1 row)

select '{"b":{"ba":3, "bb":5}}' @@ '*: ($ is object OR ($> 1 and $ < 5))'::jsquery;
?column?
----------
f
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":4}}' @@ '*: ($ is object OR ($ > 0 and $ < 5))'::jsquery;
?column?
----------
t
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":5}}' @@ '*: ($ is object OR ($> 0 and $ < 5))'::jsquery;
?column?
----------
f
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":5}}' @@ '* ($ > 0 and $ < 5)'::jsquery;
?column?
----------
t
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":{"ba":3, "bb":5}}' @@ '*: ($ is object OR $ is numeric)'::jsquery;
?column?
----------
t
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":[5,6]}' @@ '*: ($ is object OR $ is numeric)'::jsquery;
?column?
----------
f
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":[5,6]}' @@ '*: ($ is object OR $ is array OR $ is numeric)'::jsquery;
?column?
----------
t
(1 row)

select '{"a":{"aa":1, "ab":2}, "b":[5,6, {"c":8}]}' @@ '*: ($ is object OR $ is array OR $ is numeric)'::jsquery;
?column?
----------
t
(1 row)

--extract entries for index scan
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');
gin_debug_query_path_value
Expand Down
25 changes: 14 additions & 11 deletions jsquery.h
Expand Up @@ -30,11 +30,11 @@ typedef struct
#define PG_RETURN_JSQUERY(p) PG_RETURN_POINTER(p)

typedef enum JsQueryItemType {
jqiNull = jbvNull,
jqiString = jbvString,
jqiNumeric = jbvNumeric,
jqiBool = jbvBool,
jqiArray = jbvArray,
jqiNull = jbvNull,
jqiString = jbvString,
jqiNumeric = jbvNumeric,
jqiBool = jbvBool,
jqiArray = jbvArray,
jqiAnd,
jqiOr,
jqiNot,
Expand All @@ -49,6 +49,9 @@ typedef enum JsQueryItemType {
jqiAny,
jqiAnyArray,
jqiAnyKey,
jqiAll,
jqiAllArray,
jqiAllKey,
jqiKey,
jqiCurrent,
jqiIn,
Expand All @@ -60,7 +63,7 @@ typedef enum JsQueryItemType {
* JsQueryItemType should not use two high bits
*/
typedef enum JsQueryHint {
jsqIndexDefault = 0x00,
jsqIndexDefault = 0x00,
jsqForceIndex = 0x80,
jsqNoIndex = 0x40
} JsQueryHint;
Expand All @@ -70,10 +73,10 @@ typedef enum JsQueryHint {
/*
* Support functions to parse/construct binary value.
* Unlike many other representation of expression the first/main
* node is not an operation but left operand of expression. That
* node is not an operation but left operand of expression. That
* allows to implement cheep follow-path descending in jsonb
* structure and then execute operator with right operand which
* is always a constant.
* structure and then execute operator with right operand which
* is always a constant.
*/

typedef struct JsQueryItem {
Expand All @@ -86,7 +89,7 @@ typedef struct JsQueryItem {
struct {
char *data; /* for bool, numeric and string/key */
int datalen; /* filled only for string/key */
} value;
} value;

struct {
int32 left;
Expand Down Expand Up @@ -126,7 +129,7 @@ typedef struct JsQueryParseItem JsQueryParseItem;

struct JsQueryParseItem {
JsQueryItemType type;
JsQueryHint hint;
JsQueryHint hint;
JsQueryParseItem *next; /* next in path */

union {
Expand Down
3 changes: 3 additions & 0 deletions jsquery_gram.y
Expand Up @@ -306,6 +306,9 @@ key:
'*' { $$ = makeItemType(jqiAny); }
| '#' { $$ = makeItemType(jqiAnyArray); }
| '%' { $$ = makeItemType(jqiAnyKey); }
| '*' ':' { $$ = makeItemType(jqiAll); }
| '#' ':' { $$ = makeItemType(jqiAllArray); }
| '%' ':' { $$ = makeItemType(jqiAllKey); }
| '$' { $$ = makeItemType(jqiCurrent); }
| STRING_P { $$ = makeItemKey(&$1); }
| IN_P { $$ = makeItemKey(&$1); }
Expand Down
29 changes: 25 additions & 4 deletions jsquery_io.c
Expand Up @@ -116,6 +116,9 @@ flattenJsQueryParseItem(StringInfo buf, JsQueryParseItem *item)
case jqiAny:
case jqiAnyArray:
case jqiAnyKey:
case jqiAll:
case jqiAllArray:
case jqiAllKey:
break;
default:
elog(ERROR, "Unknown type: %d", item->type);
Expand Down Expand Up @@ -310,15 +313,15 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes
printJsQueryItem(buf, &elem, false, true);
appendStringInfoChar(buf, ')');
break;
case jqiAny:
case jqiCurrent:
if (inKey)
appendStringInfoChar(buf, '.');
appendStringInfoChar(buf, '*');
appendStringInfoChar(buf, '$');
break;
case jqiCurrent:
case jqiAny:
if (inKey)
appendStringInfoChar(buf, '.');
appendStringInfoChar(buf, '$');
appendStringInfoChar(buf, '*');
break;
case jqiAnyArray:
if (inKey)
Expand All @@ -330,6 +333,24 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes
appendStringInfoChar(buf, '.');
appendStringInfoChar(buf, '%');
break;
case jqiAll:
if (inKey)
appendStringInfoChar(buf, '.');
appendStringInfoChar(buf, '*');
appendStringInfoChar(buf, ':');
break;
case jqiAllArray:
if (inKey)
appendStringInfoChar(buf, '.');
appendStringInfoChar(buf, '#');
appendStringInfoChar(buf, ':');
break;
case jqiAllKey:
if (inKey)
appendStringInfoChar(buf, '.');
appendStringInfoChar(buf, '%');
appendStringInfoChar(buf, ':');
break;
default:
elog(ERROR, "Unknown JsQueryItem type: %d", v->type);
}
Expand Down

0 comments on commit 8493798

Please sign in to comment.