diff --git a/.gitignore b/.gitignore index 8e194bc..acd374a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .deps +/log/ +/results/ +/tmp_check/ *.so *.o jsquery_gram.c diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..fec84a6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,35 @@ +os: + - linux + +sudo: required +dist: trusty + +language: c + +compiler: + - clang + - gcc + +before_install: + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -y install -qq wget ca-certificates; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then source ./travis/dep-ubuntu-postgres.sh; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then source ./travis/dep-ubuntu-llvm.sh; fi + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -qq; fi + +env: + global: + - LLVM_VER=4.0 + matrix: + - PG_VER=11 CHECK_TYPE=normal + - PG_VER=11 CHECK_TYPE=static + - PG_VER=10 CHECK_TYPE=normal + - PG_VER=10 CHECK_TYPE=static + - PG_VER=10.5 CHECK_TYPE=valgrind + - PG_VER=9.6 CHECK_TYPE=normal + - PG_VER=9.6 CHECK_TYPE=static + - PG_VER=9.5 CHECK_TYPE=normal + - PG_VER=9.5 CHECK_TYPE=static + - PG_VER=9.4 CHECK_TYPE=normal + - PG_VER=9.4 CHECK_TYPE=static + +script: bash ./travis/pg-travis-test.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b5411fb --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +JsQuery is released under the PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses. + +Copyright (c) 2014-2018, Postgres Professional +Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group +Portions Copyright (c) 1994, The Regents of the University of California + +Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies. + +IN NO EVENT SHALL POSTGRES PROFESSIONAL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF POSTGRES PROFESSIONAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +POSTGRES PROFESSIONAL SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND POSTGRES PROFESSIONAL HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. diff --git a/META.json b/META.json index 694210c..56d37f6 100644 --- a/META.json +++ b/META.json @@ -2,7 +2,7 @@ "name": "JsQuery", "abstract": "JSON Query Language with GIN indexing support", "description": "JsQuery provides additional functionality for JSONB, such as a simple and effective way to search in nested objects and arrays, and more comparison operators with index support. It does this by implementing a specialized search syntax, the @@ operator, and the jsquery type for search strings.", - "version": "1.0.1", + "version": "1.1.1", "maintainer": [ "Teodor Sigaev ", "Alexander Korotkov ", @@ -14,10 +14,10 @@ "prereqs": { "runtime": { "requires": { - "PostgreSQL": "9.4.0" + "PostgreSQL": "9.4" }, "recommends": { - "PostgreSQL": "9.6.5" + "PostgreSQL": "10.0" } } }, @@ -25,7 +25,7 @@ "jsquery": { "file": "sql/jsquery.sql", "docfile": "README.md", - "version": "1.0.1", + "version": "1.1.1", "abstract": "JSON query language with GIN indexing support" } }, @@ -42,7 +42,7 @@ }, "generated_by": "Josh Berkus", "meta-spec": { - "version": "1.0.0", + "version": "1.1.1", "url": "http://pgxn.org/meta/spec.txt" }, "tags": [ @@ -50,4 +50,4 @@ "index", "search" ] -} \ No newline at end of file +} diff --git a/Makefile b/Makefile index 0892a24..b11a9b1 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ OBJS = jsonb_gin_ops.o jsquery_constr.o jsquery_extract.o \ jsquery_gram.o jsquery_io.o jsquery_op.o jsquery_support.o EXTENSION = jsquery -DATA = jsquery--1.0.sql +DATA = jsquery--1.1.sql jsquery--1.0--1.1.sql INCLUDES = jsquery.h REGRESS = jsquery @@ -26,6 +26,10 @@ include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif +ifdef USE_ASSERT_CHECKING +override CFLAGS += -DUSE_ASSERT_CHECKING +endif + jsquery_gram.o: jsquery_scan.c jsquery_gram.c: BISONFLAGS += -d diff --git a/README.md b/README.md index 4a019b4..96e6274 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +[![Build Status](https://travis-ci.org/postgrespro/jsquery.svg?branch=master)](https://travis-ci.org/postgrespro/jsquery) +[![codecov](https://codecov.io/gh/postgrespro/jsquery/branch/master/graph/badge.svg)](https://codecov.io/gh/postgrespro/jsquery) +[![GitHub license](https://img.shields.io/badge/license-PostgreSQL-blue.svg)](https://raw.githubusercontent.com/postgrespro/jsquery/master/LICENSE) + JsQuery – json query language with GIN indexing support ======================================================= @@ -36,7 +40,7 @@ and supports PostgreSQL 9.4+. Regards ------- -Development is sponsored by [Wargaming.net](http://wargaming.net). +Development was sponsored by [Wargaming.net](http://wargaming.net). Installation ------------ @@ -80,28 +84,27 @@ Simple expression is specified as `path binary_operator value` or than 5; * `similar_product_ids.# = "0684824396"` – array "similar\_product\_ids" contains string "0684824396". - * `*.color = "red"` – there is object somewhere which key "color" has value - "red". + * `*.color = "red"` – there is object somewhere which key "color" has value "red". * `foo = *` – key "foo" exists in object. -Path selects set of JSON values to be checked using given operators. In -the simplest case path is just an key name. In general path is key names and -placeholders combined by dot signs. Path can use following placeholders: +Path selects a set of JSON values to be checked using given operators. In +the simplest case path is just a key name. In general path is key names and +placeholders combined by dot signs. Path can use the following placeholders: - * `#` – any index of array; - * `#N` – N-th index of array; - * `%` – any key of object; + * `#` – any index of an array; + * `#N` – N-th index of an array; + * `%` – any key of an object; * `*` – any sequence of array indexes and object keys; - * `@#` – length of array or object, could be only used as last component of - path; - * `$` – the whole JSON document as single value, could be only the whole path. + * `@#` – length of array or object, may only be used as the last component of + a path; + * `$` – the whole JSON document as single value, may only be the whole path. Expression is true when operator is true against at least one value selected by path. Key names could be given either with or without double quotes. Key names -without double quotes shouldn't contain spaces, start with number or concur -with jsquery keyword. +without double quotes may not contain spaces, start with a number or match +a jsquery keyword. The supported binary operators are: @@ -117,78 +120,77 @@ The supported unary operators are: * Check for type operators: `IS ARRAY`, `IS NUMERIC`, `IS OBJECT`, `IS STRING` and `IS BOOLEAN`. -Expressions could be complex. Complex expression is a set of expressions +Expressions can be complex. Complex expression is a set of expressions combined by logical operators (`AND`, `OR`, `NOT`) and grouped using braces. -Examples of complex expressions are given below. +Examples of complex expressions: * `a = 1 AND (b = 2 OR c = 3) AND NOT d = 1` * `x.% = true OR x.# = true` -Prefix expressions are expressions given in the form path (subexpression). -In this case path selects JSON values to be checked using given subexpression. +Prefix expressions are expressions given in the form `path (subexpression)`. +In this case path selects JSON values to be checked using the given subexpression. Check results are aggregated in the same way as in simple expressions. * `#(a = 1 AND b = 2)` – exists element of array which a key is 1 and b key is 2 * `%($ >= 10 AND $ <= 20)` – exists object key which values is between 10 and 20 -Path also could contain following special placeholders with "every" semantics: +Path can also contain the following special placeholders with "every" semantics: - * `#:` – every indexes of array; - * `%:` – every key of object; + * `#:` – every index of an array; + * `%:` – every key of an object; * `*:` – every sequence of array indexes and object keys. Consider following example. %.#:($ >= 0 AND $ <= 1) -This example could be read as following: there is at least one key which value -is array of numerics between 0 and 1. +This example could be read as following: there is at least one key whose value +is an array of numerics between 0 and 1. -We can rewrite this example in the following form with extra braces. +We can rewrite this example in the following form with extra braces: %(#:($ >= 0 AND $ <= 1)) -The first placeholder `%` checks that expression in braces is true for at least -one value in object. The second placeholder `#:` checks value to be array and -all its elements satisfy expressions in braces. +The first placeholder `%` checks that the expression in braces is true for at least +one value in the object. The second placeholder `#:` checks if the value is an array +and that all its elements satisfy the expressions in braces. -We can rewrite this example without `#:` placeholder as follows. +We can rewrite this example without the `#:` placeholder as follows: %(NOT #(NOT ($ >= 0 AND $ <= 1)) AND $ IS ARRAY) -In this example we transform assertion that every element of array satisfy some -condition to assertion that there is no one element which doesn't satisfy the -same condition. +In this example we transform the assertion that every element of array satisfy some +condition to an assertion that there are no elements which don't satisfy the same +condition. -Some examples of using paths are given below. +Some examples of using paths: * `numbers.#: IS NUMERIC` – every element of "numbers" array is numeric. * `*:($ IS OBJECT OR $ IS BOOLEAN)` – JSON is a structure of nested objects with booleans as leaf values. - * `#:.%:($ >= 0 AND $ <= 1)` – each element of array is object containing + * `#:.%:($ >= 0 AND $ <= 1)` – each element of array is an object containing only numeric values between 0 and 1. - * `documents.#:.% = *` – "documents" is array of objects containing at least + * `documents.#:.% = *` – "documents" is an array of objects containing at least one key. * `%.#: ($ IS STRING)` – JSON object contains at least one array of strings. - * `#.% = true` – at least one array element is objects which contains at least + * `#.% = true` – at least one array element is an object which contains at least one "true" value. -Usage of path operators and braces need some explanation. When same path -operators are used multiple times they may refer different values while you can -refer same value multiple time by using braces and `$` operator. See following -examples. +The use of path operators and braces need some further explanation. When the same path +operators are used multiple times, they may refer to different values. If you want them +to always refer to the same value, you must use braces and the `$` operator. For example: - * `# < 10 AND # > 20` – exists element less than 10 and exists another element - greater than 20. - * `#($ < 10 AND $ > 20)` – exists element which both less than 10 and greater - than 20 (impossible). - * `#($ >= 10 AND $ <= 20)` – exists element between 10 and 20. - * `# >= 10 AND # <= 20` – exists element great or equal to 10 and exists - another element less or equal to 20. Query can be satisfied by array with - no elements between 10 and 20, for instance [0,30]. + * `# < 10 AND # > 20` – an element less than 10 exists, and another element + greater than 20 exists. + * `#($ < 10 AND $ > 20)` – an element which is both less than 10 and greater + than 20 exists (impossible). + * `#($ >= 10 AND $ <= 20)` – an element between 10 and 20 exists. + * `# >= 10 AND # <= 20` – an element greater or equal to 10 exists, and another + element less or equal to 20 exists. Please note that this query also can be + satisfied by an array with no elements between 10 and 20, for instance [0,30]. -Same rules apply when you search inside objects and branchy structures. +Same rules apply when searching inside objects and branch structures. Type checking operators and "every" placeholders are useful for document schema validation. JsQuery matchig operator `@@` is immutable and can be used @@ -204,9 +206,9 @@ CREATE TABLE js ( points.#:(x IS NUMERIC AND y IS NUMERIC)'::jsquery)); ``` -In this example check constraint validates that in "data" jsonb column: -value of "name" key is string, value of "similar_ids" key is array of numerics, -value of "points" key is array of objects which contain numeric values in +In this example the check constraint validates that in the "data" jsonb column +the value of the "name" key is a string, the value of the "similar_ids" key is an array of numerics, +and the value of the "points" key is an array of objects which contain numeric values in "x" and "y" keys. See our @@ -223,11 +225,11 @@ provide different kinds of query optimization. * jsonb\_value\_path\_ops In each of two GIN opclasses jsonb documents are decomposed into entries. Each -entry is associated with particular value and it's path. Difference between +entry is associated with a particular value and its path. The difference between opclasses is in the entry representation, comparison and usage for search optimization. -For example, jsonb document +For example, the jsonb document `{"a": [{"b": "xyz", "c": true}, 10], "d": {"e": [7, false]}}` would be decomposed into following entries: @@ -237,57 +239,57 @@ would be decomposed into following entries: * "d"."e".#.7 * "d"."e".#.false -Since JsQuery doesn't support search in particular array index, we consider +Since JsQuery doesn't support searching in a particular array index, we consider all array elements to be equivalent. Thus, each array element is marked with -same `#` sign in the path. +the same `#` sign in its path. Major problem in the entries representation is its size. In the given example -key "a" is presented three times. In the large branchy documents with long -keys size of naive entries representation becomes unreasonable. Both opclasses -address this issue but in a slightly different way. +the key "a" is presented three times. In large branchy documents with long +keys sizes of naive entries, the representation becomes unreasonably large. +Both opclasses address this issue, but in slightly different ways. ### jsonb\_path\_value\_ops jsonb\_path\_value\_ops represents entry as pair of path hash and value. -Following pseudocode illustrates it. +Following pseudocode illustrates it: (hash(path_item_1.path_item_2. ... .path_item_n); value) -In comparison of entries path hash is the higher part of entry and value is -its lower part. This determines the features of this opclass. Since path -is hashed and it is higher part of entry we need to know the full path to -the value in order to use it for search. However, once path is specified +When comparison entries, the path hash is the higher part of entry and the value is +the lower part. This determines the features of this opclass. Since the path +is hashed and it's the higher part of the entry, we need to know the full path to +a value in order to use the it for searching. However, once the path is specified we can use both exact and range searches very efficiently. ### jsonb\_value\_path\_ops -jsonb\_value\_path\_ops represents entry as pair of value and bloom filter -of path. +jsonb\_value\_path\_ops represents entry as pair of the value and a bloom filter +of paths: (value; bloom(path_item_1) | bloom(path_item_2) | ... | bloom(path_item_n)) In comparison of entries value is the higher part of entry and bloom filter of path is its lower part. This determines the features of this opclass. Since -value is the higher part of entry we can perform only exact value search -efficiently. Range value search is possible as well but we would have to -filter all the the different paths where matching values occur. Bloom filter -over path items allows index usage for conditions containing `%` and `*` in +the value is the higher part of an entry, we can only perform exact value search +effectively. A search over a range of values is possible as well, but we have to +filter all the the different paths where matching values occur. The Bloom filter +over path items allows the index to be used for conditions containing `%` and `*` in their paths. ### Query optimization -JsQuery opclasses perform complex query optimization. Thus it's valuable for +JsQuery opclasses perform complex query optimization. It's valuable for a developer or administrator to see the result of such optimization. -Unfortunately, opclasses aren't allowed to do any custom output to the -EXPLAIN. That's why JsQuery provides following functions which allows to see -how particular opclass optimizes given query. +Unfortunately, opclasses aren't allowed to put any custom output in an +EXPLAIN. That's why JsQuery provides these functions to let you see +how particular opclass optimizes given query: * gin\_debug\_query\_path\_value(jsquery) – for jsonb\_path\_value\_ops * gin\_debug\_query\_value\_path(jsquery) – for jsonb\_value\_path\_ops -Result of these functions is a textual representation of query tree which -leafs are GIN search entries. Following examples show different results of -query optimization by different opclasses. +The result of these functions is a textual representation of the query tree +where leaves are GIN search entries. Following examples show different results of +query optimization by different opclasses: # SELECT gin_debug_query_path_value('x = 1 AND (*.y = 1 OR y = 2)'); gin_debug_query_path_value @@ -305,9 +307,9 @@ query optimization by different opclasses. Unfortunately, jsonb have no statistics yet. That's why JsQuery optimizer has to do imperative decision while selecting conditions to be evaluated using -index. This decision is made by assumtion that some condition types are less -selective than others. Optimizer divides conditions into following selectivity -class (listed by descending of selectivity). +index. This decision is made by assuming that some condition types are less +selective than others. The optimizer divides conditions into following selectivity +classes (listed in descending order of selectivity): 1. Equality (x = c) 2. Range (c1 < x < c2) @@ -315,19 +317,19 @@ class (listed by descending of selectivity). 4. Is (x is type) 5. Any (x = \*) -Optimizer evades index evaluation of less selective conditions when possible. +The optimizer avoids index evaluation of less selective conditions when possible. For example, in the `x = 1 AND y > 0` query `x = 1` is assumed to be more -selective than `y > 0`. That's why index isn't used for evaluation of `y > 0`. +selective than `y > 0`. That's why the index isn't used for evaluation of `y > 0`. # SELECT gin_debug_query_path_value('x = 1 AND y > 0'); gin_debug_query_path_value ---------------------------- x = 1 , entry 0 + -With lack of statistics decisions made by optimizer can be inaccurate. That's -why JsQuery supports hints. Comments `/*-- index */` and `/*-- noindex */` -placed in the conditions forces optimizer to use and not use index -correspondingly. +With the lack of statistics, decisions made by optimizer can be inaccurate. That's +why JsQuery supports hints. The comments `/*-- index */` or `/*-- noindex */` +placed in the conditions force the optimizer to use or not use an index +correspondingly: SELECT gin_debug_query_path_value('x = 1 AND y /*-- index */ > 0'); gin_debug_query_path_value @@ -344,11 +346,11 @@ correspondingly. Contribution ------------ -Please, notice, that JsQuery is still under development and while it's -stable and tested, it may contains some bugs. Don't hesitate to raise +Please note that JsQuery is still under development. While it's +stable and tested, it may contain some bugs. Don't hesitate to create [issues at github](https://github.com/postgrespro/jsquery/issues) with your bug reports. -If you're lacking of some functionality in JsQuery and feeling power to -implement it then you're welcome to make pull requests. +If there's some functionality you'd like to see added to JsQuery and you feel +like you can implement it, then you're welcome to make pull requests. diff --git a/expected/jsquery.out b/expected/jsquery.out index afb988d..8c8d3b1 100644 --- a/expected/jsquery.out +++ b/expected/jsquery.out @@ -1,7 +1,13 @@ CREATE EXTENSION jsquery; set escape_string_warning=off; +set standard_conforming_strings=on; CREATE TABLE test_jsquery (v jsonb); \copy test_jsquery from 'data/test_jsquery.data' +select ''::jsquery; +ERROR: bad jsquery representation +LINE 1: select ''::jsquery; + ^ +DETAIL: No symbols read at the end of input select 'asd.zzz = 13'::jsquery; jsquery ------------------ @@ -66,7 +72,7 @@ select '* < 13 AND #.zxc"x" = "true"'::jsquery; ERROR: bad jsquery representation LINE 1: select '* < 13 AND #.zxc"x" = "true"'::jsquery; ^ -DETAIL: syntax error, unexpected STRING_P at or near """ +DETAIL: syntax error, unexpected STRING_P, expecting $end at or near """ select 'a < 1'::jsquery; jsquery --------- @@ -529,343 +535,343 @@ select 'a.b.#10203.* > 4'::jsquery; "a"."b".#10203.* > 4 (1 row) -select '{"a": {"b": null}}'::jsonb @@ 'a.b = 1'; +select '{"a": {"b": null}}'::jsonb @@ 'a.b = 1'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": null}}'::jsonb @@ 'a.b = null'; +select '{"a": {"b": null}}'::jsonb @@ 'a.b = null'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": null}}'::jsonb @@ 'a.b = false'; +select '{"a": {"b": null}}'::jsonb @@ 'a.b = false'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": false}}'::jsonb @@ 'a.b = false'; +select '{"a": {"b": false}}'::jsonb @@ 'a.b = false'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": false}}'::jsonb @@ 'a.b = true'; +select '{"a": {"b": false}}'::jsonb @@ 'a.b = true'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": true}}'::jsonb @@ 'a.b = true'; +select '{"a": {"b": true}}'::jsonb @@ 'a.b = true'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 1'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 1'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 1'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 1'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 1'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 1'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 1'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 1'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 1'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 1'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 2'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 2'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 2'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 2'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 2'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 2'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 2'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 2'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 2'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 2'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 0'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 0'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 0'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 0'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 0'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'; +select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'; +select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'; +select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 1 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 1 ]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1 ]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1 ]'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1,2,3,4 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1,2,3,4 ]'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1,2,3,4 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1,2,3,4 ]'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 4'; +select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 4'::jsquery; ?column? ---------- f (1 row) -select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 3'; +select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 3'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a = 4'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a = 4'::jsquery; ?column? ---------- f (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a = 4'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a = 4'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 1 OR a=3)'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 1 OR a=3)'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 OR a=1)'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 OR a=1)'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=1)'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=1)'::jsquery; ?column? ---------- f (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=2)' as "false"; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=2)'::jsquery as "false"; false ------- f (1 row) -select '[{"a": 2, "b":3}, {"a": 3, "b": 1}]'::jsonb @@ '#(b = 1 and a=3)'; +select '[{"a": 2, "b":3}, {"a": 3, "b": 1}]'::jsonb @@ '#(b = 1 and a=3)'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a.a = 4'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a.a = 4'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a.a = 4'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a.a = 4'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.#.a.a = 4'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.#.a.a = 4'::jsquery; ?column? ---------- t (1 row) -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.*.a.a = 4'; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.*.a.a = 4'::jsquery; ?column? ---------- t (1 row) -select '{"a": 1}'::jsonb @@ 'a in (0,1,2)'; +select '{"a": 1}'::jsonb @@ 'a in (0,1,2)'::jsquery; ?column? ---------- t (1 row) -select '{"a": 1}'::jsonb @@ 'a in (0,2)'; +select '{"a": 1}'::jsonb @@ 'a in (0,2)'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#=2'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#=2'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 5 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 5 ]'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a=*'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a=*'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b=*'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b=*'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.c=*'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.c=*'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b = [1,2,3]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b = [1,2,3]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# = [1,2,3]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# = [1,2,3]'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b && [1,2,3]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b && [1,2,3]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# && [1,2,3]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# && [1,2,3]'::jsquery; ?column? ---------- f (1 row) -select 'asd.# = 3'::jsquery & 'zzz = true' | 'xxx.# = zero'; +select 'asd.# = 3'::jsquery & 'zzz = true' | 'xxx.# = zero'::jsquery; ?column? ------------------------------------------------------ (("asd".# = 3 AND "zzz" = true) OR "xxx".# = "zero") (1 row) -select !'asd.# = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'; +select !'asd.# = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'::jsquery; ?column? ------------------------------------------------------------------ (((NOT "asd".# = 3) AND "zzz" = true) OR (NOT "xxx".# = "zero")) (1 row) -select !'asd.#3.f = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'; +select !'asd.#3.f = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'::jsquery; ?column? ----------------------------------------------------------------------- (((NOT "asd".#3."f" = 3) AND "zzz" = true) OR (NOT "xxx".# = "zero")) @@ -889,175 +895,175 @@ select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0,3]'::jsquery; f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 2 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 2 ]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b($ && [ 2 ])'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b($ && [ 2 ])'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b && [ 2 ]'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b && [ 2 ]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b ($ && [ 2 ])'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b ($ && [ 2 ])'::jsquery; ?column? ---------- t (1 row) -select '[1,2,3]'::jsonb @@ '# && [2]'; +select '[1,2,3]'::jsonb @@ '# && [2]'::jsquery; ?column? ---------- f (1 row) -select '[1,2,3]'::jsonb @@ '#($ && [2])'; +select '[1,2,3]'::jsonb @@ '#($ && [2])'::jsquery; ?column? ---------- f (1 row) -select '[1,2,3]'::jsonb @@ '$ && [2]'; +select '[1,2,3]'::jsonb @@ '$ && [2]'::jsquery; ?column? ---------- t (1 row) -select '[1,2,3]'::jsonb @@ '$ ($ && [2])'; +select '[1,2,3]'::jsonb @@ '$ ($ && [2])'::jsquery; ?column? ---------- t (1 row) -select '[1,2,3]'::jsonb @@ '$ = 2'; +select '[1,2,3]'::jsonb @@ '$ = 2'::jsquery; ?column? ---------- f (1 row) -select '[1,2,3]'::jsonb @@ '# = 2'; +select '[1,2,3]'::jsonb @@ '# = 2'::jsquery; ?column? ---------- t (1 row) -select '[1,2,3]'::jsonb @@ '#.$ = 2'; +select '[1,2,3]'::jsonb @@ '#.$ = 2'::jsquery; ?column? ---------- t (1 row) -select '[1,2,3]'::jsonb @@ '#($ = 2)'; +select '[1,2,3]'::jsonb @@ '#($ = 2)'::jsquery; ?column? ---------- t (1 row) -select '[3,4]'::jsonb @@ '#($ > 2 and $ < 5)'; +select '[3,4]'::jsonb @@ '#($ > 2 and $ < 5)'::jsquery; ?column? ---------- t (1 row) -select '[3,4]'::jsonb @@ '# > 2 and # < 5'; +select '[3,4]'::jsonb @@ '# > 2 and # < 5'::jsquery; ?column? ---------- t (1 row) -select '[1,6]'::jsonb @@ '#($ > 2 and $ < 5)'; +select '[1,6]'::jsonb @@ '#($ > 2 and $ < 5)'::jsquery; ?column? ---------- f (1 row) -select '[1,6]'::jsonb @@ '# > 2 and # < 5'; +select '[1,6]'::jsonb @@ '# > 2 and # < 5'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.b=3'; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.b=3'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ 'a.%=3'; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ 'a.%=3'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.%="hey"'; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.%="hey"'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%="hey"'; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%="hey"'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%=[5,6]'; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%=[5,6]'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#1 = 2'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#1 = 2'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#2 = 2'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#2 = 2'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#3 = 2'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#3 = 2'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#1.x = 2'; +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#1.x = 2'::jsquery; ?column? ---------- t (1 row) -select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#2.x = 2'; +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#2.x = 2'::jsquery; ?column? ---------- f (1 row) -select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#3.x = 2'; +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#3.x = 2'::jsquery; ?column? ---------- f (1 row) -select '"XXX"'::jsonb @@ '$="XXX"'; +select '"XXX"'::jsonb @@ '$="XXX"'::jsquery; ?column? ---------- t (1 row) -select '"XXX"'::jsonb @@ '#.$="XXX"'; +select '"XXX"'::jsonb @@ '#.$="XXX"'::jsquery; ?column? ---------- f @@ -1070,19 +1076,19 @@ select 'a\t = "dollar \u0024 character"'::jsquery; "a\t" = "dollar $ character" (1 row) -select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar \u0024 character"'; +select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar \u0024 character"'::jsquery; ?column? ---------- t (1 row) -select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar $ character"'; +select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar $ character"'::jsquery; ?column? ---------- t (1 row) -select '{ "a": "dollar $ character" }'::jsonb @@ '* = "dollar \u0024 character"'; +select '{ "a": "dollar $ character" }'::jsonb @@ '* = "dollar \u0024 character"'::jsquery; ?column? ---------- t @@ -1445,6 +1451,187 @@ select '{"a":[1,2]}' @@ '@# = 1'::jsquery; t (1 row) +--filter +select '?( not b>0). x'::jsquery; + jsquery +------------------------ + ?((NOT "b" > 0)) ."x" +(1 row) + +select 'a.?(b>0 and x= 0 ) .c'::jsquery; + jsquery +------------------------------------ + "a". ?(("b" > 0 AND "x" = 0)) ."c" +(1 row) + +select 'a.$. ?(b>0 and x= 0 ) . c.k'::jsquery; + jsquery +------------------------------------------ + "a".$. ?(("b" > 0 AND "x" = 0)) ."c"."k" +(1 row) + +select 'a.$.? (b>0 and x.*= 0 ).c.k'::jsquery; + jsquery +-------------------------------------------- + "a".$. ?(("b" > 0 AND "x".* = 0)) ."c"."k" +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a < 0) (b=20)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 0) (b=20)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 1) (b=20)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 2) (b=20)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 3) (b=20)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}]'::jsonb ~~ '#.a'::jsquery; + ?column? +---------- + [1, 2] +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}]'::jsonb ~~ '#. ?(a > 1). b'::jsquery; + ?column? +---------- + [20] +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '# . ?(a > 1)'::jsquery; + ?column? +---------------------------------------- + [{"a": 2, "b": 20}, {"a": 3, "b": 30}] +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '%'::jsquery; + ?column? +---------- + +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '%'::jsquery; + ?column? +----------- + [1, 2, 3] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '% . ? ( $ > 2 )'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '% . ? ( $ > 2 ).$'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( % > 2 )'::jsquery; + ?column? +---------------------------- + [{"a": 1, "b": 2, "c": 3}] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( %: > 0 )'::jsquery; + ?column? +---------------------------- + [{"a": 1, "b": 2, "c": 3}] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( %: > 2 )'::jsquery; + ?column? +---------- + +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '#'::jsquery; + ?column? +----------------------------------------------------------- + [{"a": 1, "b": 10}, {"a": 2, "b": 20}, {"a": 3, "b": 30}] +(1 row) + +select '[1,2,3]'::jsonb ~~ '#'::jsquery; + ?column? +----------- + [1, 2, 3] +(1 row) + +select '[1,2,3]'::jsonb ~~ '#. ?($ > 2)'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '[1,2,3]'::jsonb ~~ '#. ?($ > 2).$'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '[1,2,3]'::jsonb ~~ ' ?(#.$ > 2).$'::jsquery; + ?column? +------------- + [[1, 2, 3]] +(1 row) + +select '[1,2,3]'::jsonb ~~ ' ?(#:.$ > 2).$'::jsquery; + ?column? +---------- + +(1 row) + +select '[1,2,3]'::jsonb ~~ ' ?(#:.$ > 0).$'::jsquery; + ?column? +------------- + [[1, 2, 3]] +(1 row) + +select '{"a": {"b": {"c": 1}}}'::jsonb ~~ '*.?(c >0)'::jsquery; + ?column? +------------ + [{"c": 1}] +(1 row) + +select '{"a": {"b": {"c": 1}}}'::jsonb ~~ '?(*.c >0)'::jsquery; + ?column? +-------------------------- + [{"a": {"b": {"c": 1}}}] +(1 row) + +select '{"tags":[{"term":["NYC", "CYN"]}, {"term":["1NYC", "1CYN"]} ]}'::jsonb ~~ 'tags.#.term.#. ? ( $ = "NYC")'::jsquery; + ?column? +---------- + ["NYC"] +(1 row) + +select '{"tags":[{"term":["NYC", "CYN"]}, {"term":["1NYC", "1CYN"]} ]}'::jsonb ~~ 'tags.#.term. ? ( # = "NYC")'::jsquery; + ?column? +------------------ + [["NYC", "CYN"]] +(1 row) + --ALL select 'a.*: = 4'::jsquery; jsquery @@ -1674,6 +1861,138 @@ select '[16]' @@ '(@# > 0 and #: = 16)'::jsquery; t (1 row) +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.b or b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c or b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g or b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g or b.c'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.b and b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c and b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c and b.b'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g and b.d'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and b.d'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and b.c'::jsquery; + ?column? +---------- + +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and (b.d or b.c)'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and (b.c or b.d)'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and a.c and b.d'::jsquery; + ?column? +----------- + [1, 2, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.b or a.c) and b.d'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.e or a.c) and b.d'::jsquery; + ?column? +---------- + [2, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.e or a.g) and b.d'::jsquery; + ?column? +---------- + +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b or (b.d or b.c)'::jsquery; + ?column? +---------- + [1] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d or (a.b or a.c)'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d or (a.b and a.c)'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.f or (a.b and a.c)'::jsquery; + ?column? +---------- + [1, 2] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d and (a.b and a.c)'::jsquery; + ?column? +----------- + [3, 1, 2] +(1 row) + +select '{"a": {"b": [6,5,4], "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d and (a.b and a.c)'::jsquery; + ?column? +------------------- + [3, [6, 5, 4], 2] +(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 @@ -1851,6 +2170,29 @@ SELECT gin_debug_query_path_value('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT * (1 row) +SELECT gin_debug_query_path_value('$ = true'); + gin_debug_query_path_value +---------------------------- + $ = true , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('$ . ? (review_votes > 10) . review_rating < 7'); + gin_debug_query_path_value +-------------------------------- + AND + + review_rating < 7 , entry 0 + + review_votes > 10 , entry 1 + + +(1 row) + +SELECT gin_debug_query_path_value('similar_product_ids . ? (# = "B0002W4TL2") . $'); + gin_debug_query_path_value +------------------------------------------------- + similar_product_ids.# = "B0002W4TL2" , entry 0 + + +(1 row) + SELECT gin_debug_query_value_path('NOT NOT NOT x(y(NOT (a=1) and NOT (b=2)) OR NOT NOT (c=3)) and z = 5'); gin_debug_query_value_path ---------------------------- @@ -2054,6 +2396,36 @@ SELECT gin_debug_query_value_path('*.@# ($ = 4 or $ = 2)'); (1 row) +SELECT gin_debug_query_value_path('tags.#.term. ? ( # = "NYC").x > 0'); + gin_debug_query_value_path +---------------------------------- + tags.#.term.# = "NYC" , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('$ = true'); + gin_debug_query_value_path +---------------------------- + $ = true , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('$ . ? (review_votes > 10) . review_rating < 7'); + gin_debug_query_value_path +-------------------------------- + AND + + review_rating < 7 , entry 0 + + review_votes > 10 , entry 1 + + +(1 row) + +SELECT gin_debug_query_value_path('similar_product_ids . ? (# = "B0002W4TL2") . $'); + gin_debug_query_value_path +------------------------------------------------- + similar_product_ids.# = "B0002W4TL2" , entry 0 + + +(1 row) + ---table and index select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 0; count @@ -2098,219 +2470,249 @@ select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 16 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; count ------- 654 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; count ------- 13 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; count ------- 985 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; count ------- 16 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; count ------- 988 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16' AND - v @@ 'review_helpful_votes < 20'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '; +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; count ------- 4 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; count ------- 54 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ 'customer_id = null'; +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 'review_votes = true'; +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 'product_group = false'; +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 't = *'; +select count(*) from test_jsquery where v @@ 't = *'::jsquery; count ------- 10 (1 row) -select count(*) from test_jsquery where v @@ 't is boolean'; +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is string'; +select count(*) from test_jsquery where v @@ 't is string'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is numeric'; +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is array'; +select count(*) from test_jsquery where v @@ 't is array'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is object'; +select count(*) from test_jsquery where v @@ 't is object'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ '$ is boolean'; +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ '$ is string'; +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; count ------- 4 (1 row) -select count(*) from test_jsquery where v @@ '$ is numeric'; +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; count ------- 5 (1 row) -select count(*) from test_jsquery where v @@ '$ is array'; +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ '$ is object'; +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; count ------- 1017 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; count ------- 51 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; count ------- 1001 (1 row) -select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'; +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; count ------- 40 (1 row) -select count(*) from test_jsquery where v @@ '$ > 2'; +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ '$ = false'; +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; count ------- 2 (1 row) -select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; +select count(*) from test_jsquery where v @@ 't'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ '$'::jsquery; + count +------- + 1034 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; + count +------- + 79 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + count +------- + 3 +(1 row) + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; v ------------------- {"array": [2]} {"array": [2, 3]} (2 rows) -select v from test_jsquery where v @@ 'array && [2,3]' order by v; +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; v ---------------------- {"array": [2]} @@ -2320,7 +2722,7 @@ select v from test_jsquery where v @@ 'array && [2,3]' order by v; {"array": [3, 4, 5]} (5 rows) -select v from test_jsquery where v @@ 'array @> [2,3]' order by v; +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; v ---------------------- {"array": [2, 3]} @@ -2328,7 +2730,7 @@ select v from test_jsquery where v @@ 'array @> [2,3]' order by v; {"array": [2, 3, 4]} (3 rows) -select v from test_jsquery where v @@ 'array = [2,3]' order by v; +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; v ------------------- {"array": [2, 3]} @@ -2336,7 +2738,7 @@ select v from test_jsquery where v @@ 'array = [2,3]' order by v; create index t_idx on test_jsquery using gin (v jsonb_value_path_ops); set enable_seqscan = off; -explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; +explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; QUERY PLAN ------------------------------------------------------------------------ Aggregate @@ -2346,212 +2748,242 @@ explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful Index Cond: (v @@ '"review_helpful_votes" > 0'::jsquery) (5 rows) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; count ------- 654 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; count ------- 13 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; count ------- 985 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; count ------- 16 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; count ------- 988 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16' AND - v @@ 'review_helpful_votes < 20'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '; +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; count ------- 4 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; count ------- 54 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ 'customer_id = null'; +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 'review_votes = true'; +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 'product_group = false'; +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 't = *'; +select count(*) from test_jsquery where v @@ 't = *'::jsquery; count ------- 10 (1 row) -select count(*) from test_jsquery where v @@ 't is boolean'; +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is string'; +select count(*) from test_jsquery where v @@ 't is string'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is numeric'; +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is array'; +select count(*) from test_jsquery where v @@ 't is array'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is object'; +select count(*) from test_jsquery where v @@ 't is object'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ '$ is boolean'; +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ '$ is string'; +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; count ------- 4 (1 row) -select count(*) from test_jsquery where v @@ '$ is numeric'; +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; count ------- 5 (1 row) -select count(*) from test_jsquery where v @@ '$ is array'; +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ '$ is object'; +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; count ------- 1017 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; count ------- 51 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; count ------- 1001 (1 row) -select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'; +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ '$ > 2'; +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ '$ = false'; +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; count ------- 2 (1 row) -explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; +select count(*) from test_jsquery where v @@ 't'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ '$'::jsquery; + count +------- + 1034 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; + count +------- + 79 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + count +------- + 3 +(1 row) + +explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; QUERY PLAN --------------------------------------------------------------- Sort @@ -2562,7 +2994,7 @@ explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order Index Cond: (v @@ '"array" <@ [2, 3]'::jsquery) (6 rows) -explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v; +explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; QUERY PLAN --------------------------------------------------------------- Sort @@ -2573,7 +3005,7 @@ explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order Index Cond: (v @@ '"array" && [2, 3]'::jsquery) (6 rows) -explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order by v; +explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; QUERY PLAN --------------------------------------------------------------- Sort @@ -2584,7 +3016,7 @@ explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order Index Cond: (v @@ '"array" @> [2, 3]'::jsquery) (6 rows) -explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v; +explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; QUERY PLAN -------------------------------------------------------------- Sort @@ -2595,14 +3027,14 @@ explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order Index Cond: (v @@ '"array" = [2, 3]'::jsquery) (6 rows) -select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; v ------------------- {"array": [2]} {"array": [2, 3]} (2 rows) -select v from test_jsquery where v @@ 'array && [2,3]' order by v; +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; v ---------------------- {"array": [2]} @@ -2612,7 +3044,7 @@ select v from test_jsquery where v @@ 'array && [2,3]' order by v; {"array": [3, 4, 5]} (5 rows) -select v from test_jsquery where v @@ 'array @> [2,3]' order by v; +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; v ---------------------- {"array": [2, 3]} @@ -2620,7 +3052,7 @@ select v from test_jsquery where v @@ 'array @> [2,3]' order by v; {"array": [2, 3, 4]} (3 rows) -select v from test_jsquery where v @@ 'array = [2,3]' order by v; +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; v ------------------- {"array": [2, 3]} @@ -2629,7 +3061,7 @@ select v from test_jsquery where v @@ 'array = [2,3]' order by v; drop index t_idx; create index t_idx on test_jsquery using gin (v jsonb_path_value_ops); set enable_seqscan = off; -explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; +explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; QUERY PLAN ------------------------------------------------------------------------ Aggregate @@ -2639,212 +3071,242 @@ explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful Index Cond: (v @@ '"review_helpful_votes" > 0'::jsquery) (5 rows) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; count ------- 654 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; count ------- 13 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; count ------- 985 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; count ------- 16 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; count ------- 988 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16' AND - v @@ 'review_helpful_votes < 20'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'; +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; count ------- 8 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '; +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; count ------- 4 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; count ------- 54 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'; +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ 'customer_id = null'; +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 'review_votes = true'; +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 'product_group = false'; +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; count ------- 1 (1 row) -select count(*) from test_jsquery where v @@ 't = *'; +select count(*) from test_jsquery where v @@ 't = *'::jsquery; count ------- 10 (1 row) -select count(*) from test_jsquery where v @@ 't is boolean'; +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is string'; +select count(*) from test_jsquery where v @@ 't is string'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is numeric'; +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is array'; +select count(*) from test_jsquery where v @@ 't is array'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ 't is object'; +select count(*) from test_jsquery where v @@ 't is object'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ '$ is boolean'; +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ '$ is string'; +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; count ------- 4 (1 row) -select count(*) from test_jsquery where v @@ '$ is numeric'; +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; count ------- 5 (1 row) -select count(*) from test_jsquery where v @@ '$ is array'; +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; count ------- 2 (1 row) -select count(*) from test_jsquery where v @@ '$ is object'; +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; count ------- 1017 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; count ------- 51 (1 row) -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; count ------- 1001 (1 row) -select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'; +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; count ------- 7 (1 row) -select count(*) from test_jsquery where v @@ '$ > 2'; +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; count ------- 3 (1 row) -select count(*) from test_jsquery where v @@ '$ = false'; +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; count ------- 2 (1 row) -explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; +select count(*) from test_jsquery where v @@ 't'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ '$'::jsquery; + count +------- + 1034 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; + count +------- + 950 +(1 row) + +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; + count +------- + 79 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + count +------- + 3 +(1 row) + +explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; QUERY PLAN --------------------------------------------------------------- Sort @@ -2855,7 +3317,7 @@ explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order Index Cond: (v @@ '"array" <@ [2, 3]'::jsquery) (6 rows) -explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v; +explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; QUERY PLAN --------------------------------------------------------------- Sort @@ -2866,7 +3328,7 @@ explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order Index Cond: (v @@ '"array" && [2, 3]'::jsquery) (6 rows) -explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order by v; +explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; QUERY PLAN --------------------------------------------------------------- Sort @@ -2877,7 +3339,7 @@ explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order Index Cond: (v @@ '"array" @> [2, 3]'::jsquery) (6 rows) -explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v; +explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; QUERY PLAN -------------------------------------------------------------- Sort @@ -2888,14 +3350,14 @@ explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order Index Cond: (v @@ '"array" = [2, 3]'::jsquery) (6 rows) -select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; v ------------------- {"array": [2]} {"array": [2, 3]} (2 rows) -select v from test_jsquery where v @@ 'array && [2,3]' order by v; +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; v ---------------------- {"array": [2]} @@ -2905,7 +3367,7 @@ select v from test_jsquery where v @@ 'array && [2,3]' order by v; {"array": [3, 4, 5]} (5 rows) -select v from test_jsquery where v @@ 'array @> [2,3]' order by v; +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; v ---------------------- {"array": [2, 3]} @@ -2913,7 +3375,7 @@ select v from test_jsquery where v @@ 'array @> [2,3]' order by v; {"array": [2, 3, 4]} (3 rows) -select v from test_jsquery where v @@ 'array = [2,3]' order by v; +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; v ------------------- {"array": [2, 3]} diff --git a/expected/jsquery_1.out b/expected/jsquery_1.out new file mode 100644 index 0000000..ea1f6dc --- /dev/null +++ b/expected/jsquery_1.out @@ -0,0 +1,3384 @@ +CREATE EXTENSION jsquery; +set escape_string_warning=off; +set standard_conforming_strings=on; +CREATE TABLE test_jsquery (v jsonb); +\copy test_jsquery from 'data/test_jsquery.data' +select ''::jsquery; +ERROR: bad jsquery representation +LINE 1: select ''::jsquery; + ^ +DETAIL: No symbols read at the end of input +select 'asd.zzz = 13'::jsquery; + jsquery +------------------ + "asd"."zzz" = 13 +(1 row) + +select 'asd.zzz < 13'::jsquery; + jsquery +------------------ + "asd"."zzz" < 13 +(1 row) + +select 'asd(zzz < 13)'::jsquery; + jsquery +------------------ + "asd"."zzz" < 13 +(1 row) + +select 'asd(zzz < 13 AND x = true)'::jsquery; + jsquery +---------------------------------- + "asd"("zzz" < 13 AND "x" = true) +(1 row) + +select 'asd(zzz < 13 AND x = "true")'::jsquery; + jsquery +------------------------------------ + "asd"("zzz" < 13 AND "x" = "true") +(1 row) + +select 'asd(zzz < 13 AND x.zxc = "true")'::jsquery; + jsquery +------------------------------------------ + "asd"("zzz" < 13 AND "x"."zxc" = "true") +(1 row) + +select 'asd(zzz < 13 OR #.zxc = "true" /* test */)'::jsquery; + jsquery +--------------------------------------- + "asd"("zzz" < 13 OR #."zxc" = "true") +(1 row) + +select 'asd(* < 13 AND /* ttt */ #.zxc = "true")'::jsquery; + jsquery +------------------------------------ + "asd"(* < 13 AND #."zxc" = "true") +(1 row) + +select '(* < 13 AND #.zxc = "true")'::jsquery; + jsquery +------------------------------- + (* < 13 AND #."zxc" = "true") +(1 row) + +select '* < 13 AND #.zxc/* t2 */ = "true"'::jsquery; + jsquery +------------------------------- + (* < 13 AND #."zxc" = "true") +(1 row) + +select '* < 13 AND #.zxc"x" = "true"'::jsquery; +ERROR: bad jsquery representation +LINE 1: select '* < 13 AND #.zxc"x" = "true"'::jsquery; + ^ +DETAIL: syntax error, unexpected STRING_P, expecting end of file at or near """ +select 'a < 1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < -1'::jsquery; + jsquery +---------- + "a" < -1 +(1 row) + +select 'a < +1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < .1'::jsquery; + jsquery +----------- + "a" < 0.1 +(1 row) + +select 'a < -.1'::jsquery; + jsquery +------------ + "a" < -0.1 +(1 row) + +select 'a < +.1'::jsquery; + jsquery +----------- + "a" < 0.1 +(1 row) + +select 'a < 0.1'::jsquery; + jsquery +----------- + "a" < 0.1 +(1 row) + +select 'a < -0.1'::jsquery; + jsquery +------------ + "a" < -0.1 +(1 row) + +select 'a < +0.1'::jsquery; + jsquery +----------- + "a" < 0.1 +(1 row) + +select 'a < 10.1'::jsquery; + jsquery +------------ + "a" < 10.1 +(1 row) + +select 'a < -10.1'::jsquery; + jsquery +------------- + "a" < -10.1 +(1 row) + +select 'a < +10.1'::jsquery; + jsquery +------------ + "a" < 10.1 +(1 row) + +select 'a < 1e1'::jsquery; + jsquery +---------- + "a" < 10 +(1 row) + +select 'a < -1e1'::jsquery; + jsquery +----------- + "a" < -10 +(1 row) + +select 'a < +1e1'::jsquery; + jsquery +---------- + "a" < 10 +(1 row) + +select 'a < .1e1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < -.1e1'::jsquery; + jsquery +---------- + "a" < -1 +(1 row) + +select 'a < +.1e1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < 0.1e1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < -0.1e1'::jsquery; + jsquery +---------- + "a" < -1 +(1 row) + +select 'a < +0.1e1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < 10.1e1'::jsquery; + jsquery +----------- + "a" < 101 +(1 row) + +select 'a < -10.1e1'::jsquery; + jsquery +------------ + "a" < -101 +(1 row) + +select 'a < +10.1e1'::jsquery; + jsquery +----------- + "a" < 101 +(1 row) + +select 'a < 1e-1'::jsquery; + jsquery +----------- + "a" < 0.1 +(1 row) + +select 'a < -1e-1'::jsquery; + jsquery +------------ + "a" < -0.1 +(1 row) + +select 'a < +1e-1'::jsquery; + jsquery +----------- + "a" < 0.1 +(1 row) + +select 'a < .1e-1'::jsquery; + jsquery +------------ + "a" < 0.01 +(1 row) + +select 'a < -.1e-1'::jsquery; + jsquery +------------- + "a" < -0.01 +(1 row) + +select 'a < +.1e-1'::jsquery; + jsquery +------------ + "a" < 0.01 +(1 row) + +select 'a < 0.1e-1'::jsquery; + jsquery +------------ + "a" < 0.01 +(1 row) + +select 'a < -0.1e-1'::jsquery; + jsquery +------------- + "a" < -0.01 +(1 row) + +select 'a < +0.1e-1'::jsquery; + jsquery +------------ + "a" < 0.01 +(1 row) + +select 'a < 10.1e-1'::jsquery; + jsquery +------------ + "a" < 1.01 +(1 row) + +select 'a < -10.1e-1'::jsquery; + jsquery +------------- + "a" < -1.01 +(1 row) + +select 'a < +10.1e-1'::jsquery; + jsquery +------------ + "a" < 1.01 +(1 row) + +select 'a < 1e+1'::jsquery; + jsquery +---------- + "a" < 10 +(1 row) + +select 'a < -1e+1'::jsquery; + jsquery +----------- + "a" < -10 +(1 row) + +select 'a < +1e+1'::jsquery; + jsquery +---------- + "a" < 10 +(1 row) + +select 'a < .1e+1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < -.1e+1'::jsquery; + jsquery +---------- + "a" < -1 +(1 row) + +select 'a < +.1e+1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < 0.1e+1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < -0.1e+1'::jsquery; + jsquery +---------- + "a" < -1 +(1 row) + +select 'a < +0.1e+1'::jsquery; + jsquery +--------- + "a" < 1 +(1 row) + +select 'a < 10.1e+1'::jsquery; + jsquery +----------- + "a" < 101 +(1 row) + +select 'a < -10.1e+1'::jsquery; + jsquery +------------ + "a" < -101 +(1 row) + +select 'a < +10.1e+1'::jsquery; + jsquery +----------- + "a" < 101 +(1 row) + +select 'a in (0,1,2)'::jsquery; + jsquery +------------------ + "a" IN (0, 1, 2) +(1 row) + +select 'a IN (0,null, "null", xxx, "zzz", 2)'::jsquery; + jsquery +------------------------------------------- + "a" IN (0, null, "null", "xxx", "zzz", 2) +(1 row) + +select 'not < 1'::jsquery; + jsquery +----------- + "not" < 1 +(1 row) + +select 'not not < 1'::jsquery; + jsquery +----------------- + (NOT "not" < 1) +(1 row) + +select 'not( not < 1)'::jsquery; + jsquery +----------------- + (NOT "not" < 1) +(1 row) + +select 'not.x < 1'::jsquery; + jsquery +--------------- + "not"."x" < 1 +(1 row) + +select 'x.not < 1'::jsquery; + jsquery +--------------- + "x"."not" < 1 +(1 row) + +select 'a.%(not x > 0 and not (y < 0 or z = 0))'::jsquery; + jsquery +----------------------------------------------------- + "a".%((NOT "x" > 0) AND (NOT ("y" < 0 OR "z" = 0))) +(1 row) + +select 'is < 1'::jsquery; + jsquery +---------- + "is" < 1 +(1 row) + +select 'in < 1'::jsquery; + jsquery +---------- + "in" < 1 +(1 row) + +select 'not is < 1'::jsquery; + jsquery +---------------- + (NOT "is" < 1) +(1 row) + +select 'not in < 1'::jsquery; + jsquery +---------------- + (NOT "in" < 1) +(1 row) + +select 'in in (1,2)'::jsquery; + jsquery +---------------- + "in" IN (1, 2) +(1 row) + +select 'is in (1,2)'::jsquery; + jsquery +---------------- + "is" IN (1, 2) +(1 row) + +select 'not in (1,2)'::jsquery; + jsquery +----------------- + "not" IN (1, 2) +(1 row) + +select 'in is numeric'::jsquery; + jsquery +----------------- + "in" IS NUMERIC +(1 row) + +select 'is is numeric'::jsquery; + jsquery +----------------- + "is" IS NUMERIC +(1 row) + +select 'not is numeric'::jsquery; + jsquery +------------------ + "not" IS NUMERIC +(1 row) + +select 'not.in < 1'::jsquery; + jsquery +---------------- + "not"."in" < 1 +(1 row) + +select 'not.is < 1'::jsquery; + jsquery +---------------- + "not"."is" < 1 +(1 row) + +select 'not.not < 1'::jsquery; + jsquery +----------------- + "not"."not" < 1 +(1 row) + +select 'in.in < 1'::jsquery; + jsquery +--------------- + "in"."in" < 1 +(1 row) + +select 'in.is < 1'::jsquery; + jsquery +--------------- + "in"."is" < 1 +(1 row) + +select 'in.not < 1'::jsquery; + jsquery +---------------- + "in"."not" < 1 +(1 row) + +select 'is.in < 1'::jsquery; + jsquery +--------------- + "is"."in" < 1 +(1 row) + +select 'is.is < 1'::jsquery; + jsquery +--------------- + "is"."is" < 1 +(1 row) + +select 'is.not < 1'::jsquery; + jsquery +---------------- + "is"."not" < 1 +(1 row) + +select 'a.b.#4 > 4'::jsquery; + jsquery +---------------- + "a"."b".#4 > 4 +(1 row) + +select 'a.b.#10203.* > 4'::jsquery; + jsquery +---------------------- + "a"."b".#10203.* > 4 +(1 row) + +select '{"a": {"b": null}}'::jsonb @@ 'a.b = 1'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": null}}'::jsonb @@ 'a.b = null'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": null}}'::jsonb @@ 'a.b = false'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": false}}'::jsonb @@ 'a.b = false'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": false}}'::jsonb @@ 'a.b = true'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": true}}'::jsonb @@ 'a.b = true'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 1'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 1'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 1'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 1'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 1'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 0'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 0'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 0'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 1 ]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1 ]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1 ]'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1,2,3,4 ]'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1,2,3,4 ]'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 4'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 3'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a = 4'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a = 4'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 1 OR a=3)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 OR a=1)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=1)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=2)'::jsquery as "false"; + false +------- + f +(1 row) + +select '[{"a": 2, "b":3}, {"a": 3, "b": 1}]'::jsonb @@ '#(b = 1 and a=3)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a.a = 4'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a.a = 4'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.#.a.a = 4'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.*.a.a = 4'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": 1}'::jsonb @@ 'a in (0,1,2)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": 1}'::jsonb @@ 'a in (0,2)'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#=2'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 5 ]'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a=*'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b=*'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.c=*'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b = [1,2,3]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# = [1,2,3]'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b && [1,2,3]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# && [1,2,3]'::jsquery; + ?column? +---------- + f +(1 row) + +select 'asd.# = 3'::jsquery & 'zzz = true' | 'xxx.# = zero'::jsquery; + ?column? +------------------------------------------------------ + (("asd".# = 3 AND "zzz" = true) OR "xxx".# = "zero") +(1 row) + +select !'asd.# = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'::jsquery; + ?column? +------------------------------------------------------------------ + (((NOT "asd".# = 3) AND "zzz" = true) OR (NOT "xxx".# = "zero")) +(1 row) + +select !'asd.#3.f = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'::jsquery; + ?column? +----------------------------------------------------------------------- + (((NOT "asd".#3."f" = 3) AND "zzz" = true) OR (NOT "xxx".# = "zero")) +(1 row) + +select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0,1]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0,3]'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 2 ]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b($ && [ 2 ])'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b && [ 2 ]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b ($ && [ 2 ])'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2,3]'::jsonb @@ '# && [2]'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,2,3]'::jsonb @@ '#($ && [2])'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,2,3]'::jsonb @@ '$ && [2]'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2,3]'::jsonb @@ '$ ($ && [2])'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2,3]'::jsonb @@ '$ = 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,2,3]'::jsonb @@ '# = 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2,3]'::jsonb @@ '#.$ = 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2,3]'::jsonb @@ '#($ = 2)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[3,4]'::jsonb @@ '#($ > 2 and $ < 5)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[3,4]'::jsonb @@ '# > 2 and # < 5'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,6]'::jsonb @@ '#($ > 2 and $ < 5)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,6]'::jsonb @@ '# > 2 and # < 5'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.b=3'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ 'a.%=3'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.%="hey"'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%="hey"'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%=[5,6]'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#1 = 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#2 = 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#3 = 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#1.x = 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#2.x = 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#3.x = 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '"XXX"'::jsonb @@ '$="XXX"'::jsquery; + ?column? +---------- + t +(1 row) + +select '"XXX"'::jsonb @@ '#.$="XXX"'::jsquery; + ?column? +---------- + f +(1 row) + +--Unicode +select 'a\t = "dollar \u0024 character"'::jsquery; + jsquery +------------------------------ + "a\t" = "dollar $ character" +(1 row) + +select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar \u0024 character"'::jsquery; + ?column? +---------- + t +(1 row) + +select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar $ character"'::jsquery; + ?column? +---------- + t +(1 row) + +select '{ "a": "dollar $ character" }'::jsonb @@ '* = "dollar \u0024 character"'::jsquery; + ?column? +---------- + t +(1 row) + +select 'a\r = "\n\""'::jsquery; + jsquery +---------------- + "a\r" = "\n\"" +(1 row) + +select 'a\r = "\u0000"'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = "\u0000"'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = \u0000'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = \u0000'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = "\abcd"'::jsquery AS err; +ERROR: bad jsquery representation +LINE 1: select 'a\r = "\abcd"'::jsquery AS err; + ^ +DETAIL: Escape sequence is invalid at or near "\a" +select 'a\r = "\\abcd"'::jsquery; + jsquery +------------------ + "a\r" = "\\abcd" +(1 row) + +select 'a\r = "x\u0000"'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = "x\u0000"'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = x\u0000'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = x\u0000'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = "x\abcd"'::jsquery AS err; +ERROR: bad jsquery representation +LINE 1: select 'a\r = "x\abcd"'::jsquery AS err; + ^ +DETAIL: Escape sequence is invalid at or near "\a" +select 'a\r = "x\\abcd"'::jsquery; + jsquery +------------------- + "a\r" = "x\\abcd" +(1 row) + +select 'a\r = "x\u0000x"'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = "x\u0000x"'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = x\u0000x'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = x\u0000x'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = "x\abcdx"'::jsquery AS err; +ERROR: bad jsquery representation +LINE 1: select 'a\r = "x\abcdx"'::jsquery AS err; + ^ +DETAIL: Escape sequence is invalid at or near "\a" +select 'a\r = "x\\abcdx"'::jsquery; + jsquery +-------------------- + "a\r" = "x\\abcdx" +(1 row) + +select 'a\r = "\u0000x"'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = "\u0000x"'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = \u0000x'::jsquery; +ERROR: unsupported Unicode escape sequence +LINE 1: select 'a\r = \u0000x'::jsquery; + ^ +DETAIL: \u0000 cannot be converted to text. +select 'a\r = "\abcdx"'::jsquery AS err; +ERROR: bad jsquery representation +LINE 1: select 'a\r = "\abcdx"'::jsquery AS err; + ^ +DETAIL: Escape sequence is invalid at or near "\a" +select 'a\r = "\\abcdx"'::jsquery; + jsquery +------------------- + "a\r" = "\\abcdx" +(1 row) + +select 'a\r = x"\\abcd"'::jsquery AS err; +ERROR: bad jsquery representation +LINE 1: select 'a\r = x"\\abcd"'::jsquery AS err; + ^ +DETAIL: syntax error, unexpected STRING_P, expecting end of file at or near """ +--IS +select 'as IS boolean OR as is ARRAY OR as is ObJect OR as is Numeric OR as is string'::jsquery; + jsquery +------------------------------------------------------------------------------------------------- + (((("as" IS BOOLEAN OR "as" IS ARRAY) OR "as" IS OBJECT) OR "as" IS NUMERIC) OR "as" IS STRING) +(1 row) + +select '{"as": "xxx"}' @@ 'as IS string'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"as": "xxx"}' @@ 'as IS boolean OR as is ARRAY OR as is ObJect OR as is Numeric'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"as": 5}' @@ 'as is Numeric'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"as": true}' @@ 'as is boolean'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"as": false}' @@ 'as is boolean'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"as": "false"}' @@ 'as is boolean'::jsquery; + ?column? +---------- + f +(1 row) + +select '["xxx"]' @@ '$ IS array'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"as": false}' @@ '$ IS object'::jsquery; + ?column? +---------- + t +(1 row) + +select '"xxx"' @@ '$ IS string'::jsquery; + ?column? +---------- + t +(1 row) + +select '"xxx"' @@ '$ IS numeric'::jsquery; + ?column? +---------- + f +(1 row) + +--hint +select 'a /*-- noindex */ = 5'::jsquery; + jsquery +-------------------------- + "a" /*-- noindex */ = 5 +(1 row) + +select 'a /*-- index */ = 5'::jsquery; + jsquery +------------------------ + "a" /*-- index */ = 5 +(1 row) + +select 'asd.# = 3'::jsquery & 'zzz /*-- noindex */ = true' | 'xxx.# /*-- index */ = zero'; + ?column? +-------------------------------------------------------------------------------------- + (("asd".# = 3 AND "zzz" /*-- noindex */ = true) OR "xxx".# /*-- index */ = "zero") +(1 row) + +select 'a /*-- xxx */ = 5'::jsquery; + jsquery +--------- + "a" = 5 +(1 row) + +select 'a /* index */ = 5'::jsquery; + jsquery +--------- + "a" = 5 +(1 row) + +select 'a /* noindex */ = 5'::jsquery; + jsquery +--------- + "a" = 5 +(1 row) + +select 'a = /*-- noindex */ 5'::jsquery; +ERROR: bad jsquery representation +LINE 1: select 'a = /*-- noindex */ 5'::jsquery; + ^ +DETAIL: syntax error, unexpected HINT_P at or near "*/" +select 'a = /* noindex */ 5'::jsquery; + jsquery +--------- + "a" = 5 +(1 row) + +--LENGTH +select 'a.@# = 4'::jsquery; + jsquery +------------ + "a".@# = 4 +(1 row) + +select 'a.@#.$ = 4'::jsquery as noerror; + noerror +-------------- + "a".@#.$ = 4 +(1 row) + +select 'a.@#.a = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.a = 4'::jsquery as error; + ^ +select 'a.@#.* = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.* = 4'::jsquery as error; + ^ +select 'a.@#.% = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.% = 4'::jsquery as error; + ^ +select 'a.@#.# = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.# = 4'::jsquery as error; + ^ +select 'a.@#.*: = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.*: = 4'::jsquery as error; + ^ +select 'a.@#.%: = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.%: = 4'::jsquery as error; + ^ +select 'a.@#.#: = 4'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@#.#: = 4'::jsquery as error; + ^ +select 'a.@# (a = 5 or b = 6)'::jsquery as error; +ERROR: Array length should be last in path +LINE 1: select 'a.@# (a = 5 or b = 6)'::jsquery as error; + ^ +select '[]' @@ '@# = 0'::jsquery; + ?column? +---------- + t +(1 row) + +select '[]' @@ '@# < 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '[]' @@ '@# > 1'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1]' @@ '@# = 0'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1]' @@ '@# < 2'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1]' @@ '@# > 1'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,2]' @@ '@# = 0'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,2]' @@ '@# < 2'::jsquery; + ?column? +---------- + f +(1 row) + +select '[1,2]' @@ '@# > 1'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2]' @@ '@# in (1, 2)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[1,2]' @@ '@# in (1, 3)'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a":[1,2]}' @@ '@# in (2, 4)'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a":[1,2]}' @@ 'a.@# in (2, 4)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a":[1,2]}' @@ '%.@# in (2, 4)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a":[1,2]}' @@ '*.@# in (2, 4)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a":[1,2]}' @@ '*.@# ($ = 4 or $ = 2)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a":[1,2]}' @@ '@# = 1'::jsquery; + ?column? +---------- + t +(1 row) + +--filter +select '?( not b>0). x'::jsquery; + jsquery +------------------------ + ?((NOT "b" > 0)) ."x" +(1 row) + +select 'a.?(b>0 and x= 0 ) .c'::jsquery; + jsquery +------------------------------------ + "a". ?(("b" > 0 AND "x" = 0)) ."c" +(1 row) + +select 'a.$. ?(b>0 and x= 0 ) . c.k'::jsquery; + jsquery +------------------------------------------ + "a".$. ?(("b" > 0 AND "x" = 0)) ."c"."k" +(1 row) + +select 'a.$.? (b>0 and x.*= 0 ).c.k'::jsquery; + jsquery +-------------------------------------------- + "a".$. ?(("b" > 0 AND "x".* = 0)) ."c"."k" +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a < 0) (b=20)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 0) (b=20)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 1) (b=20)'::jsquery; + ?column? +---------- + t +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 2) (b=20)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 3) (b=20)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}]'::jsonb ~~ '#.a'::jsquery; + ?column? +---------- + [1, 2] +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}]'::jsonb ~~ '#. ?(a > 1). b'::jsquery; + ?column? +---------- + [20] +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '# . ?(a > 1)'::jsquery; + ?column? +---------------------------------------- + [{"a": 2, "b": 20}, {"a": 3, "b": 30}] +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '%'::jsquery; + ?column? +---------- + +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '%'::jsquery; + ?column? +----------- + [1, 2, 3] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '% . ? ( $ > 2 )'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '% . ? ( $ > 2 ).$'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( % > 2 )'::jsquery; + ?column? +---------------------------- + [{"a": 1, "b": 2, "c": 3}] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( %: > 0 )'::jsquery; + ?column? +---------------------------- + [{"a": 1, "b": 2, "c": 3}] +(1 row) + +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( %: > 2 )'::jsquery; + ?column? +---------- + +(1 row) + +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '#'::jsquery; + ?column? +----------------------------------------------------------- + [{"a": 1, "b": 10}, {"a": 2, "b": 20}, {"a": 3, "b": 30}] +(1 row) + +select '[1,2,3]'::jsonb ~~ '#'::jsquery; + ?column? +----------- + [1, 2, 3] +(1 row) + +select '[1,2,3]'::jsonb ~~ '#. ?($ > 2)'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '[1,2,3]'::jsonb ~~ '#. ?($ > 2).$'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '[1,2,3]'::jsonb ~~ ' ?(#.$ > 2).$'::jsquery; + ?column? +------------- + [[1, 2, 3]] +(1 row) + +select '[1,2,3]'::jsonb ~~ ' ?(#:.$ > 2).$'::jsquery; + ?column? +---------- + +(1 row) + +select '[1,2,3]'::jsonb ~~ ' ?(#:.$ > 0).$'::jsquery; + ?column? +------------- + [[1, 2, 3]] +(1 row) + +select '{"a": {"b": {"c": 1}}}'::jsonb ~~ '*.?(c >0)'::jsquery; + ?column? +------------ + [{"c": 1}] +(1 row) + +select '{"a": {"b": {"c": 1}}}'::jsonb ~~ '?(*.c >0)'::jsquery; + ?column? +-------------------------- + [{"a": {"b": {"c": 1}}}] +(1 row) + +select '{"tags":[{"term":["NYC", "CYN"]}, {"term":["1NYC", "1CYN"]} ]}'::jsonb ~~ 'tags.#.term.#. ? ( $ = "NYC")'::jsquery; + ?column? +---------- + ["NYC"] +(1 row) + +select '{"tags":[{"term":["NYC", "CYN"]}, {"term":["1NYC", "1CYN"]} ]}'::jsonb ~~ 'tags.#.term. ? ( # = "NYC")'::jsquery; + ?column? +------------------ + [["NYC", "CYN"]] +(1 row) + +--ALL +select 'a.*: = 4'::jsquery; + jsquery +------------ + "a".*: = 4 +(1 row) + +select '%: = 4'::jsquery; + jsquery +--------- + %: = 4 +(1 row) + +select '#:.i = 4'::jsquery; + jsquery +------------ + #:."i" = 4 +(1 row) + +select '[]' @@ '#: ($ > 1 and $ < 5)'::jsquery; + ?column? +---------- + t +(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? +---------- + t +(1 row) + +select '{}' @@ '*: ($ is object)'::jsquery; + ?column? +---------- + t +(1 row) + +select '"a"' @@ '*: is string'::jsquery; + ?column? +---------- + t +(1 row) + +select '1' @@ '*: is string'::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) + +select '{"a":{"aa":1, "ab":2}, "b":[5,6, {"c":"x"}]}' @@ '*: ($ is object OR $ is array OR $ is numeric)'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a":{"aa":1, "ab":2}, "b":[5,6, {"c":null}]}' @@ '*: ($ is object OR $ is array OR $ is numeric)'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a":{"aa":1}, "b":{"aa":1, "bb":2}}' @@ '%:.aa is numeric'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a":{"aa":1}, "b":{"aa":true, "bb":2}}' @@ '%:.aa is numeric'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a":{"aa":1}, "b":{"aa":1, "bb":2}, "aa":16}' @@ '*: (not $ is object or $.aa is numeric)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a":{"aa":1}, "b":{"aa":1, "bb":2}}' @@ '*: (not $ is object or $.aa is numeric or % is object)'::jsquery; + ?column? +---------- + t +(1 row) + +SELECT 'test.# IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64)'::jsquery; + jsquery +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + "test".# IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64) +(1 row) + +select '[]' @@ '(@# > 0 and #: = 16)'::jsquery; + ?column? +---------- + f +(1 row) + +select '[16]' @@ '(@# > 0 and #: = 16)'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.b or b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c or b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g or b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g or b.c'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.b and b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c and b.d'::jsquery; + ?column? +---------- + t +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c and b.b'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g and b.d'::jsquery; + ?column? +---------- + f +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and b.d'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and b.c'::jsquery; + ?column? +---------- + +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and (b.d or b.c)'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and (b.c or b.d)'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and a.c and b.d'::jsquery; + ?column? +----------- + [1, 2, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.b or a.c) and b.d'::jsquery; + ?column? +---------- + [1, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.e or a.c) and b.d'::jsquery; + ?column? +---------- + [2, 3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.e or a.g) and b.d'::jsquery; + ?column? +---------- + +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b or (b.d or b.c)'::jsquery; + ?column? +---------- + [1] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d or (a.b or a.c)'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d or (a.b and a.c)'::jsquery; + ?column? +---------- + [3] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.f or (a.b and a.c)'::jsquery; + ?column? +---------- + [1, 2] +(1 row) + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d and (a.b and a.c)'::jsquery; + ?column? +----------- + [3, 1, 2] +(1 row) + +select '{"a": {"b": [6,5,4], "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d and (a.b and a.c)'::jsquery; + ?column? +------------------- + [3, [6, 5, 4], 2] +(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 +---------------------------- + AND + + z = 5 , entry 0 + + OR + + x.y.a = 1 , entry 1 + + x.y.b = 2 , entry 2 + + +(1 row) + +SELECT gin_debug_query_path_value('NOT #(x=1) and NOT *(y=1) and NOT %(z=1) '); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('#(NOT x=1) and *(NOT y=1) and %(NOT z=1) '); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('NOT #(NOT x=1) and NOT *(NOT y=1) and NOT %(NOT z=1) '); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('#(x = "a" and y > 0 and y < 1 and z > 0)'); + gin_debug_query_path_value +---------------------------- + #.x = "a" , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('#(x = "a" and y /*-- index */ >= 0 and y < 1 and z > 0)'); + gin_debug_query_path_value +----------------------------- + AND + + #.x = "a" , entry 0 + + #.y >= 0 , < 1 , entry 1 + + +(1 row) + +SELECT gin_debug_query_path_value('#(x /*-- noindex */ = "a" and y > 0 and y <= 1 and z /*-- index */ > 0)'); + gin_debug_query_path_value +----------------------------- + AND + + #.y > 0 , <= 1 , entry 0 + + #.z > 0 , entry 1 + + +(1 row) + +SELECT gin_debug_query_path_value('x = 1 and (y /*-- index */ > 0 and y < 1 OR z > 0)'); + gin_debug_query_path_value +---------------------------- + AND + + x = 1 , entry 0 + + OR + + y > 0 , < 1 , entry 1 + + z > 0 , entry 2 + + +(1 row) + +SELECT gin_debug_query_path_value('%.x = 1'); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('*.x = "b"'); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('x && [1,2,3]'); + gin_debug_query_path_value +---------------------------- + OR + + x.# = 1 , entry 0 + + x.# = 2 , entry 1 + + x.# = 3 , entry 2 + + +(1 row) + +SELECT gin_debug_query_path_value('x @> [1,2,3]'); + gin_debug_query_path_value +---------------------------- + AND + + x.# = 1 , entry 0 + + x.# = 2 , entry 1 + + x.# = 3 , entry 2 + + +(1 row) + +SELECT gin_debug_query_path_value('x <@ [1,2,3]'); + gin_debug_query_path_value +---------------------------- + OR + + x = [] , entry 0 + + x.# = 1 , entry 1 + + x.# = 2 , entry 2 + + x.# = 3 , entry 3 + + +(1 row) + +SELECT gin_debug_query_path_value('x = *'); + gin_debug_query_path_value +---------------------------- + x = * , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('x is boolean'); + gin_debug_query_path_value +---------------------------- + x IS boolean , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('x is string'); + gin_debug_query_path_value +---------------------------- + x IS string , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('x is numeric'); + gin_debug_query_path_value +---------------------------- + x IS numeric , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('x is array'); + gin_debug_query_path_value +---------------------------- + x IS array , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('x is object'); + gin_debug_query_path_value +---------------------------- + x IS object , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('#:(x=1) AND %:(y=1) AND *:(z=1)'); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('#:(NOT x=1) AND %:(NOT y=1) AND *:(NOT z=1)'); + gin_debug_query_path_value +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_path_value('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT *:(NOT z=1)'); + gin_debug_query_path_value +---------------------------- + #.x = 1 , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('$ = true'); + gin_debug_query_path_value +---------------------------- + $ = true , entry 0 + + +(1 row) + +SELECT gin_debug_query_path_value('$ . ? (review_votes > 10) . review_rating < 7'); + gin_debug_query_path_value +-------------------------------- + AND + + review_rating < 7 , entry 0 + + review_votes > 10 , entry 1 + + +(1 row) + +SELECT gin_debug_query_path_value('similar_product_ids . ? (# = "B0002W4TL2") . $'); + gin_debug_query_path_value +------------------------------------------------- + similar_product_ids.# = "B0002W4TL2" , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('NOT NOT NOT x(y(NOT (a=1) and NOT (b=2)) OR NOT NOT (c=3)) and z = 5'); + gin_debug_query_value_path +---------------------------- + AND + + z = 5 , entry 0 + + OR + + x.y.a = 1 , entry 1 + + x.y.b = 2 , entry 2 + + +(1 row) + +SELECT gin_debug_query_value_path('NOT #(x=1) and NOT *(y=1) and NOT %(z=1) '); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('#(NOT x=1) and *(NOT y=1) and %(NOT z=1) '); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('NOT #(NOT x=1) and NOT *(NOT y=1) and NOT %(NOT z=1) '); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('#(x = "a" and y > 0 and y < 1 and z > 0)'); + gin_debug_query_value_path +---------------------------- + #.x = "a" , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('#(x = "a" and y /*-- index */ >= 0 and y < 1 and z > 0)'); + gin_debug_query_value_path +----------------------------- + AND + + #.x = "a" , entry 0 + + #.y >= 0 , < 1 , entry 1 + + +(1 row) + +SELECT gin_debug_query_value_path('#(x /*-- noindex */ = "a" and y > 0 and y <= 1 and z /*-- index */ > 0)'); + gin_debug_query_value_path +----------------------------- + AND + + #.y > 0 , <= 1 , entry 0 + + #.z > 0 , entry 1 + + +(1 row) + +SELECT gin_debug_query_value_path('x = 1 and (y /*-- index */ > 0 and y < 1 OR z > 0)'); + gin_debug_query_value_path +---------------------------- + AND + + x = 1 , entry 0 + + OR + + y > 0 , < 1 , entry 1 + + z > 0 , entry 2 + + +(1 row) + +SELECT gin_debug_query_value_path('%.x = 1'); + gin_debug_query_value_path +---------------------------- + %.x = 1 , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('*.x = "b"'); + gin_debug_query_value_path +---------------------------- + *.x = "b" , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('x && [1,2,3]'); + gin_debug_query_value_path +---------------------------- + OR + + x.# = 1 , entry 0 + + x.# = 2 , entry 1 + + x.# = 3 , entry 2 + + +(1 row) + +SELECT gin_debug_query_value_path('x @> [1,2,3]'); + gin_debug_query_value_path +---------------------------- + AND + + x.# = 1 , entry 0 + + x.# = 2 , entry 1 + + x.# = 3 , entry 2 + + +(1 row) + +SELECT gin_debug_query_value_path('x <@ [1,2,3]'); + gin_debug_query_value_path +---------------------------- + OR + + x = [] , entry 0 + + x.# = 1 , entry 1 + + x.# = 2 , entry 2 + + x.# = 3 , entry 3 + + +(1 row) + +SELECT gin_debug_query_value_path('x = [1,2,3]'); + gin_debug_query_value_path +---------------------------- + AND + + x.# = 1 , entry 0 + + x.# = 2 , entry 1 + + x.# = 3 , entry 2 + + +(1 row) + +SELECT gin_debug_query_value_path('x = *'); + gin_debug_query_value_path +---------------------------- + x = * , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('x is boolean'); + gin_debug_query_value_path +---------------------------- + x IS boolean , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('x is string'); + gin_debug_query_value_path +---------------------------- + x IS string , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('x is numeric'); + gin_debug_query_value_path +---------------------------- + x IS numeric , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('x is array'); + gin_debug_query_value_path +---------------------------- + x IS array , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('x is object'); + gin_debug_query_value_path +---------------------------- + x IS object , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('#:(x=1) AND %:(y=1) AND *:(z=1)'); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('#:(NOT x=1) AND %:(NOT y=1) AND *:(NOT z=1)'); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT *:(NOT z=1)'); + gin_debug_query_value_path +---------------------------- + AND + + #.x = 1 , entry 0 + + %.y = 1 , entry 1 + + *.z = 1 , entry 2 + + +(1 row) + +SELECT gin_debug_query_value_path('(@# > 0 and #: = 16)'); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('*.@# ($ = 4 or $ = 2)'); + gin_debug_query_value_path +---------------------------- + NULL + + +(1 row) + +SELECT gin_debug_query_value_path('tags.#.term. ? ( # = "NYC").x > 0'); + gin_debug_query_value_path +---------------------------------- + tags.#.term.# = "NYC" , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('$ = true'); + gin_debug_query_value_path +---------------------------- + $ = true , entry 0 + + +(1 row) + +SELECT gin_debug_query_value_path('$ . ? (review_votes > 10) . review_rating < 7'); + gin_debug_query_value_path +-------------------------------- + AND + + review_rating < 7 , entry 0 + + review_votes > 10 , entry 1 + + +(1 row) + +SELECT gin_debug_query_value_path('similar_product_ids . ? (# = "B0002W4TL2") . $'); + gin_debug_query_value_path +------------------------------------------------- + similar_product_ids.# = "B0002W4TL2" , entry 0 + + +(1 row) + +---table and index +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 0; + count +------- + 654 +(1 row) + +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 19; + count +------- + 13 +(1 row) + +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 < 19; + count +------- + 985 +(1 row) + +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 >= 19; + count +------- + 16 +(1 row) + +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 <= 19; + count +------- + 988 +(1 row) + +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 = 19; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 16 AND + (v->>'review_helpful_votes')::int4 < 20; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + count +------- + 654 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; + count +------- + 13 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; + count +------- + 985 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; + count +------- + 16 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; + count +------- + 988 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; + count +------- + 4 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; + count +------- + 54 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 't = *'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is string'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is array'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is object'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; + count +------- + 4 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; + count +------- + 5 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; + count +------- + 1017 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; + count +------- + 51 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; + count +------- + 40 +(1 row) + +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ '$'::jsquery; + count +------- + 1034 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; + count +------- + 79 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + count +------- + 3 +(1 row) + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; + v +------------------- + {"array": [2]} + {"array": [2, 3]} +(2 rows) + +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; + v +---------------------- + {"array": [2]} + {"array": [2, 3]} + {"array": [1, 2, 3]} + {"array": [2, 3, 4]} + {"array": [3, 4, 5]} +(5 rows) + +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; + v +---------------------- + {"array": [2, 3]} + {"array": [1, 2, 3]} + {"array": [2, 3, 4]} +(3 rows) + +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + v +------------------- + {"array": [2, 3]} +(1 row) + +create index t_idx on test_jsquery using gin (v jsonb_value_path_ops); +set enable_seqscan = off; +explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + QUERY PLAN +------------------------------------------------------------------------ + Aggregate + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"review_helpful_votes" > 0'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"review_helpful_votes" > 0'::jsquery) +(5 rows) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + count +------- + 654 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; + count +------- + 13 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; + count +------- + 985 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; + count +------- + 16 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; + count +------- + 988 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; + count +------- + 4 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; + count +------- + 54 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 't = *'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is string'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is array'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is object'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; + count +------- + 4 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; + count +------- + 5 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; + count +------- + 1017 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; + count +------- + 51 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ '$'::jsquery; + count +------- + 1034 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; + count +------- + 79 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + count +------- + 3 +(1 row) + +explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; + QUERY PLAN +--------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" <@ [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" <@ [2, 3]'::jsquery) +(6 rows) + +explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; + QUERY PLAN +--------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" && [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" && [2, 3]'::jsquery) +(6 rows) + +explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; + QUERY PLAN +--------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" @> [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" @> [2, 3]'::jsquery) +(6 rows) + +explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + QUERY PLAN +-------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" = [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" = [2, 3]'::jsquery) +(6 rows) + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; + v +------------------- + {"array": [2]} + {"array": [2, 3]} +(2 rows) + +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; + v +---------------------- + {"array": [2]} + {"array": [2, 3]} + {"array": [1, 2, 3]} + {"array": [2, 3, 4]} + {"array": [3, 4, 5]} +(5 rows) + +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; + v +---------------------- + {"array": [2, 3]} + {"array": [1, 2, 3]} + {"array": [2, 3, 4]} +(3 rows) + +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + v +------------------- + {"array": [2, 3]} +(1 row) + +drop index t_idx; +create index t_idx on test_jsquery using gin (v jsonb_path_value_ops); +set enable_seqscan = off; +explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + QUERY PLAN +------------------------------------------------------------------------ + Aggregate + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"review_helpful_votes" > 0'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"review_helpful_votes" > 0'::jsquery) +(5 rows) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + count +------- + 654 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; + count +------- + 13 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; + count +------- + 985 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; + count +------- + 16 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; + count +------- + 988 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; + count +------- + 8 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; + count +------- + 4 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; + count +------- + 54 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; + count +------- + 1 +(1 row) + +select count(*) from test_jsquery where v @@ 't = *'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is string'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is array'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't is object'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; + count +------- + 4 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; + count +------- + 5 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; + count +------- + 1017 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; + count +------- + 51 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; + count +------- + 1001 +(1 row) + +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; + count +------- + 7 +(1 row) + +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; + count +------- + 3 +(1 row) + +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; + count +------- + 2 +(1 row) + +select count(*) from test_jsquery where v @@ 't'::jsquery; + count +------- + 10 +(1 row) + +select count(*) from test_jsquery where v @@ '$'::jsquery; + count +------- + 1034 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; + count +------- + 950 +(1 row) + +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; + count +------- + 79 +(1 row) + +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + count +------- + 3 +(1 row) + +explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; + QUERY PLAN +--------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" <@ [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" <@ [2, 3]'::jsquery) +(6 rows) + +explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; + QUERY PLAN +--------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" && [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" && [2, 3]'::jsquery) +(6 rows) + +explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; + QUERY PLAN +--------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" @> [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" @> [2, 3]'::jsquery) +(6 rows) + +explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + QUERY PLAN +-------------------------------------------------------------- + Sort + Sort Key: v + -> Bitmap Heap Scan on test_jsquery + Recheck Cond: (v @@ '"array" = [2, 3]'::jsquery) + -> Bitmap Index Scan on t_idx + Index Cond: (v @@ '"array" = [2, 3]'::jsquery) +(6 rows) + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; + v +------------------- + {"array": [2]} + {"array": [2, 3]} +(2 rows) + +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; + v +---------------------- + {"array": [2]} + {"array": [2, 3]} + {"array": [1, 2, 3]} + {"array": [2, 3, 4]} + {"array": [3, 4, 5]} +(5 rows) + +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; + v +---------------------- + {"array": [2, 3]} + {"array": [1, 2, 3]} + {"array": [2, 3, 4]} +(3 rows) + +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + v +------------------- + {"array": [2, 3]} +(1 row) + +RESET enable_seqscan; diff --git a/jsonb_gin_ops.c b/jsonb_gin_ops.c index fe2a543..9a72c20 100644 --- a/jsonb_gin_ops.c +++ b/jsonb_gin_ops.c @@ -1,13 +1,14 @@ /*------------------------------------------------------------------------- * * jsonb_gin_ops.c - * Support GIN over jsonb with jsquery operation + * Support GIN over jsonb with jsquery operation * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Alexander Korotkov * * IDENTIFICATION - * contrib/jsquery/jsonb_gin_ops.c + * contrib/jsquery/jsonb_gin_ops.c * *------------------------------------------------------------------------- */ @@ -172,7 +173,6 @@ get_bloom_value(uint32 hash) static uint32 get_path_bloom(PathHashStack *stack) { - int i = 0; uint32 res = 0, val; while (stack) @@ -182,7 +182,6 @@ get_path_bloom(PathHashStack *stack) val = get_bloom_value(hash); res |= val; - i++; stack = stack->parent; } return res; @@ -258,51 +257,54 @@ make_gin_key(JsonbValue *v, uint32 hash) { GINKey *key; - if (v->type == jbvNull) + switch (v->type) { - key = (GINKey *)palloc(GINKEYLEN); - key->type = v->type; - SET_VARSIZE(key, GINKEYLEN); - } - else if (v->type == jbvBool) - { - key = (GINKey *)palloc(GINKEYLEN); - key->type = v->type | (v->val.boolean ? GINKeyTrue : 0); - SET_VARSIZE(key, GINKEYLEN); - } - else if (v->type == jbvArray) - { - key = (GINKey *)palloc(GINKEYLEN); - key->type = v->type; - if (v->val.array.nElems == 0) - key->type |= GINKeyEmptyArray; - SET_VARSIZE(key, GINKEYLEN); - } - else if (v->type == jbvObject) - { - key = (GINKey *)palloc(GINKEYLEN); - key->type = v->type; - SET_VARSIZE(key, GINKEYLEN); - } - else if (v->type == jbvNumeric) - { - key = (GINKey *)palloc(GINKeyLenNumeric(VARSIZE_ANY(v->val.numeric))); - key->type = v->type; - memcpy(GINKeyDataNumeric(key), v->val.numeric, VARSIZE_ANY(v->val.numeric)); - SET_VARSIZE(key, GINKeyLenNumeric(VARSIZE_ANY(v->val.numeric))); - } - else if (v->type == jbvString) - { - key = (GINKey *)palloc(GINKeyLenString); - key->type = v->type; - GINKeyDataString(key) = hash_any((unsigned char *)v->val.string.val, - v->val.string.len); - SET_VARSIZE(key, GINKeyLenString); - } - else - { - elog(ERROR, "GINKey must be scalar"); + case jbvNull: + case jbvObject: + { + key = (GINKey *)palloc(GINKEYLEN); + key->type = v->type; + SET_VARSIZE(key, GINKEYLEN); + break; + } + case jbvBool: + { + key = (GINKey *)palloc(GINKEYLEN); + key->type = v->type | (v->val.boolean ? GINKeyTrue : 0); + SET_VARSIZE(key, GINKEYLEN); + break; + } + case jbvArray: + { + key = (GINKey *)palloc(GINKEYLEN); + key->type = v->type; + if (v->val.array.nElems == 0) + key->type |= GINKeyEmptyArray; + + SET_VARSIZE(key, GINKEYLEN); + break; + } + case jbvNumeric: + { + key = (GINKey *) palloc0(GINKeyLenNumeric(VARSIZE_ANY(v->val.numeric))); + key->type = v->type; + memcpy(GINKeyDataNumeric(key), v->val.numeric, VARSIZE_ANY(v->val.numeric)); + SET_VARSIZE(key, GINKeyLenNumeric(VARSIZE_ANY(v->val.numeric))); + break; + } + case jbvString: + { + key = (GINKey *) palloc0(GINKeyLenString); + key->type = v->type; + GINKeyDataString(key) = hash_any((unsigned char *)v->val.string.val, + v->val.string.len); + SET_VARSIZE(key, GINKeyLenString); + break; + } + default: + elog(ERROR, "GINKey must be scalar"); } + key->hash = hash; return key; } @@ -462,7 +464,7 @@ make_value_path_entry_handler(ExtractedNode *node, Pointer extra) { Entries *e = (Entries *)extra; uint32 hash; - bool lossy, partialMatch; + bool lossy, partialMatch = false; GINKey *key; KeyExtra *keyExtra; int result; @@ -691,6 +693,8 @@ gin_extract_jsonb_value_path_internal(Jsonb *jb, int32 *nentries, uint32 **bloom stack->parent = tmp; break; case WJB_KEY: + if (!stack) /* should never happen */ + elog(ERROR, "error jsonb iteration"); stack->hash = 0; JsonbHashScalarValue(&v, &stack->hash); break; @@ -709,6 +713,8 @@ gin_extract_jsonb_value_path_internal(Jsonb *jb, int32 *nentries, uint32 **bloom break; case WJB_END_OBJECT: /* Pop the stack */ + if (!stack) /* should never happen */ + elog(ERROR, "error jsonb iteration"); tmp = stack->parent; pfree(stack); stack = tmp; @@ -728,7 +734,7 @@ gin_extract_jsonb_value_path_internal(Jsonb *jb, int32 *nentries, uint32 **bloom Datum gin_extract_jsonb_value_path(PG_FUNCTION_ARGS) { - Jsonb *jb = PG_GETARG_JSONB(0); + Jsonb *jb = PG_GETARG_JSONB_P(0); int32 *nentries = (int32 *) PG_GETARG_POINTER(1); PG_RETURN_POINTER(gin_extract_jsonb_value_path_internal(jb, nentries, NULL)); @@ -766,12 +772,12 @@ gin_extract_jsonb_query_value_path(PG_FUNCTION_ARGS) switch(strategy) { case JsonbContainsStrategyNumber: - jb = PG_GETARG_JSONB(0); + jb = PG_GETARG_JSONB_P(0); entries = gin_extract_jsonb_value_path_internal(jb, nentries, NULL); break; case JsonbNestedContainsStrategyNumber: - jb = PG_GETARG_JSONB(0); + jb = PG_GETARG_JSONB_P(0); entries = gin_extract_jsonb_value_path_internal(jb, nentries, &bloom); n = *nentries; @@ -863,7 +869,7 @@ gin_triconsistent_jsonb_value_path(PG_FUNCTION_ARGS) { GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0); StrategyNumber strategy = PG_GETARG_UINT16(1); - /* Jsonb *query = PG_GETARG_JSONB(2); */ + /* Jsonb *query = PG_GETARG_JSONB_P(2); */ int32 nkeys = PG_GETARG_INT32(3); Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); GinTernaryValue res = GIN_TRUE; @@ -1109,6 +1115,9 @@ gin_extract_jsonb_path_value_internal(Jsonb *jb, int32 *nentries) entries = (Datum *) repalloc(entries, sizeof(Datum) * total); } + if (!stack) /* should never happen */ + elog(ERROR, "error jsonb iteration"); + switch (r) { case WJB_BEGIN_ARRAY: @@ -1131,6 +1140,8 @@ gin_extract_jsonb_path_value_internal(Jsonb *jb, int32 *nentries) break; case WJB_KEY: /* Initialize hash from parent */ + if (!stack->parent) /* should never happen */ + elog(ERROR, "error jsonb iteration"); stack->hash = stack->parent->hash; JsonbHashScalarValue(&v, &stack->hash); break; @@ -1162,7 +1173,7 @@ gin_extract_jsonb_path_value_internal(Jsonb *jb, int32 *nentries) Datum gin_extract_jsonb_path_value(PG_FUNCTION_ARGS) { - Jsonb *jb = PG_GETARG_JSONB(0); + Jsonb *jb = PG_GETARG_JSONB_P(0); int32 *nentries = (int32 *) PG_GETARG_POINTER(1); PG_RETURN_POINTER(gin_extract_jsonb_path_value_internal(jb, nentries)); @@ -1175,6 +1186,7 @@ gin_debug_query_path_value(PG_FUNCTION_ARGS) Entries e = {0}; char *s; + jq = PG_GETARG_JSQUERY(0); s = debugJsQuery(jq, make_path_value_entry_handler, check_path_value_entry_handler, (Pointer)&e); @@ -1199,7 +1211,7 @@ gin_extract_jsonb_query_path_value(PG_FUNCTION_ARGS) switch(strategy) { case JsonbContainsStrategyNumber: - jb = PG_GETARG_JSONB(0); + jb = PG_GETARG_JSONB_P(0); entries = gin_extract_jsonb_path_value_internal(jb, nentries); break; @@ -1240,7 +1252,7 @@ gin_consistent_jsonb_path_value(PG_FUNCTION_ARGS) { bool *check = (bool *) PG_GETARG_POINTER(0); StrategyNumber strategy = PG_GETARG_UINT16(1); - /* Jsonb *query = PG_GETARG_JSONB(2); */ + /* Jsonb *query = PG_GETARG_JSONB_P(2); */ int32 nkeys = PG_GETARG_INT32(3); Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); bool *recheck = (bool *) PG_GETARG_POINTER(5); @@ -1281,7 +1293,7 @@ gin_triconsistent_jsonb_path_value(PG_FUNCTION_ARGS) { GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0); StrategyNumber strategy = PG_GETARG_UINT16(1); - /* Jsonb *query = PG_GETARG_JSONB(2); */ + /* Jsonb *query = PG_GETARG_JSONB_P(2); */ int32 nkeys = PG_GETARG_INT32(3); Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); GinTernaryValue res = GIN_TRUE; diff --git a/jsquery--1.0--1.1.sql b/jsquery--1.0--1.1.sql new file mode 100644 index 0000000..db16070 --- /dev/null +++ b/jsquery--1.0--1.1.sql @@ -0,0 +1,11 @@ +CREATE FUNCTION json_jsquery_filter(jsonb, jsquery) + RETURNS jsonb + AS 'MODULE_PATHNAME' + LANGUAGE C STRICT IMMUTABLE; + +CREATE OPERATOR ~~ ( + LEFTARG = jsonb, + RIGHTARG = jsquery, + PROCEDURE = json_jsquery_filter +); + diff --git a/jsquery--1.0.sql b/jsquery--1.1.sql similarity index 96% rename from jsquery--1.0.sql rename to jsquery--1.1.sql index 3bf1d9b..d10076a 100644 --- a/jsquery--1.0.sql +++ b/jsquery--1.1.sql @@ -48,6 +48,17 @@ CREATE OPERATOR @@ ( JOIN = contjoinsel ); +CREATE FUNCTION json_jsquery_filter(jsonb, jsquery) + RETURNS jsonb + AS 'MODULE_PATHNAME' + LANGUAGE C STRICT IMMUTABLE; + +CREATE OPERATOR ~~ ( + LEFTARG = jsonb, + RIGHTARG = jsquery, + PROCEDURE = json_jsquery_filter +); + CREATE FUNCTION jsquery_join_and(jsquery, jsquery) RETURNS jsquery AS 'MODULE_PATHNAME' @@ -147,7 +158,7 @@ CREATE OPERATOR = ( CREATE OPERATOR <> ( LEFTARG = jsquery, RIGHTARG = jsquery, - PROCEDURE = jsquery_eq, + PROCEDURE = jsquery_ne, COMMUTATOR = '<>', NEGATOR = '=', RESTRICT = neqsel, @@ -167,7 +178,7 @@ CREATE OPERATOR >= ( CREATE OPERATOR > ( LEFTARG = jsquery, RIGHTARG = jsquery, - PROCEDURE = jsquery_ge, + PROCEDURE = jsquery_gt, COMMUTATOR = '<', NEGATOR = '<=', RESTRICT = scalargtsel, diff --git a/jsquery.control b/jsquery.control index 94510c8..d95ba24 100644 --- a/jsquery.control +++ b/jsquery.control @@ -1,6 +1,6 @@ # jsquery extension comment = 'data type for jsonb inspection' -default_version = '1.0' +default_version = '1.1' module_pathname = '$libdir/jsquery' relocatable = true diff --git a/jsquery.h b/jsquery.h index d98dd6c..86226eb 100644 --- a/jsquery.h +++ b/jsquery.h @@ -4,6 +4,7 @@ * Definitions of jsquery datatype * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION @@ -25,7 +26,7 @@ typedef struct int32 vl_len_; /* varlena header (do not touch directly!) */ } JsQuery; -#define DatumGetJsQueryP(d) ((JsQuery*)DatumGetPointer(PG_DETOAST_DATUM(d))) +#define DatumGetJsQueryP(d) ((JsQuery*) PG_DETOAST_DATUM(d)) #define PG_GETARG_JSQUERY(x) DatumGetJsQueryP(PG_GETARG_DATUM(x)) #define PG_RETURN_JSQUERY(p) PG_RETURN_POINTER(p) @@ -57,12 +58,13 @@ typedef enum JsQueryItemType { jqiLength, jqiIn, jqiIs, - jqiIndexArray + jqiIndexArray, + jqiFilter } JsQueryItemType; /* * JsQueryHint is stored in the same byte as JsQueryItemType so - * JsQueryItemType should not use two high bits + * JsQueryItemType should not use three high bits */ typedef enum JsQueryHint { jsqIndexDefault = 0x00, @@ -84,7 +86,7 @@ typedef enum JsQueryHint { typedef struct JsQueryItem { JsQueryItemType type; JsQueryHint hint; - int32 nextPos; + uint32 nextPos; char *base; union { @@ -122,6 +124,7 @@ extern int32 jsqGetIsType(JsQueryItem *v); extern char * jsqGetString(JsQueryItem *v, int32 *len); extern void jsqIterateInit(JsQueryItem *v); extern bool jsqIterateArray(JsQueryItem *v, JsQueryItem *e); +extern void jsqIterateDestroy(JsQueryItem *v); void alignStringInfoInt(StringInfo buf); @@ -135,6 +138,7 @@ struct JsQueryParseItem { JsQueryItemType type; JsQueryHint hint; JsQueryParseItem *next; /* next in path */ + bool filter; union { struct { @@ -246,4 +250,12 @@ bool queryNeedRecheck(ExtractedNode *node); bool execRecursive(ExtractedNode *node, bool *check); bool execRecursiveTristate(ExtractedNode *node, GinTernaryValue *check); +#ifndef PG_RETURN_JSONB_P +#define PG_RETURN_JSONB_P(x) PG_RETURN_JSONB(x) +#endif + +#ifndef PG_GETARG_JSONB_P +#define PG_GETARG_JSONB_P(x) PG_GETARG_JSONB(x) +#endif + #endif diff --git a/jsquery_constr.c b/jsquery_constr.c index fcb9088..bfd0468 100644 --- a/jsquery_constr.c +++ b/jsquery_constr.c @@ -4,6 +4,7 @@ * Functions and operations to manipulate jsquery * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION @@ -137,6 +138,7 @@ copyJsQuery(StringInfo buf, JsQueryItem *jsq) case jqiAll: case jqiAllArray: case jqiAllKey: + case jqiFilter: break; default: elog(ERROR, "Unknown type: %d", jsq->type); diff --git a/jsquery_extract.c b/jsquery_extract.c index 7cacc18..4517706 100644 --- a/jsquery_extract.c +++ b/jsquery_extract.c @@ -4,6 +4,7 @@ * Functions and operations to support jsquery in indexes * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Alexander Korotkov * * IDENTIFICATION @@ -22,6 +23,7 @@ #include "jsquery.h" static ExtractedNode *recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path); +static ExtractedNode *makeAnyNode(bool not, bool indirect, PathItem *path); static int coundChildren(ExtractedNode *node, ExtractedNodeType type, bool first, bool *found); static void fillChildren(ExtractedNode *node, ExtractedNodeType type, bool first, ExtractedNode **items, int *i); static void flatternTree(ExtractedNode *node); @@ -48,18 +50,32 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) ExtractedNodeType type; JsQueryItem elem, e; + e.hint = false; check_stack_depth(); switch(jsq->type) { case jqiAnd: case jqiOr: - type = ((jsq->type == jqiAnd) == not) ? eOr : eAnd; + case jqiFilter: + type = ((jsq->type == jqiAnd || jsq->type == jqiFilter) == not) ? eOr : eAnd; - jsqGetLeftArg(jsq, &elem); - leftNode = recursiveExtract(&elem, not, false, path); - jsqGetRightArg(jsq, &elem); - rightNode = recursiveExtract(&elem, not, false, path); + if (jsq->type == jqiFilter) + { + jsqGetArg(jsq, &elem); + leftNode = recursiveExtract(&elem, not, false, path); + if (jsqGetNext(jsq, &elem)) + rightNode = recursiveExtract(&elem, not, false, path); + else + rightNode = makeAnyNode(not, false, path); + } + else + { + jsqGetLeftArg(jsq, &elem); + leftNode = recursiveExtract(&elem, not, false, path); + jsqGetRightArg(jsq, &elem); + rightNode = recursiveExtract(&elem, not, false, path); + } if (!leftNode || !rightNode) { @@ -94,7 +110,8 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) pathItem->type = iKey; pathItem->s = jsqGetString(jsq, &pathItem->len); pathItem->parent = path; - jsqGetNext(jsq, &elem); + if (!jsqGetNext(jsq, &elem)) + return makeAnyNode(not, indirect, pathItem); return recursiveExtract(&elem, not, indirect, pathItem); case jqiAny: case jqiAll: @@ -103,14 +120,16 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) pathItem = (PathItem *)palloc(sizeof(PathItem)); pathItem->type = iAny; pathItem->parent = path; - jsqGetNext(jsq, &elem); + if (!jsqGetNext(jsq, &elem)) + return makeAnyNode(not, indirect, pathItem); return recursiveExtract(&elem, not, true, pathItem); case jqiIndexArray: pathItem = (PathItem *)palloc(sizeof(PathItem)); pathItem->type = iIndexArray; pathItem->arrayIndex = jsq->arrayIndex; pathItem->parent = path; - jsqGetNext(jsq, &elem); + if (!jsqGetNext(jsq, &elem)) + return makeAnyNode(not, indirect, pathItem); return recursiveExtract(&elem, not, true, pathItem); case jqiAnyArray: case jqiAllArray: @@ -119,7 +138,8 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) pathItem = (PathItem *)palloc(sizeof(PathItem)); pathItem->type = iAnyArray; pathItem->parent = path; - jsqGetNext(jsq, &elem); + if (!jsqGetNext(jsq, &elem)) + return makeAnyNode(not, indirect, pathItem); return recursiveExtract(&elem, not, true, pathItem); case jqiAnyKey: case jqiAllKey: @@ -128,10 +148,12 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) pathItem = (PathItem *)palloc(sizeof(PathItem)); pathItem->type = iAnyKey; pathItem->parent = path; - jsqGetNext(jsq, &elem); + if (!jsqGetNext(jsq, &elem)) + return makeAnyNode(not, indirect, pathItem); return recursiveExtract(&elem, not, true, pathItem); case jqiCurrent: - jsqGetNext(jsq, &elem); + if (!jsqGetNext(jsq, &elem)) + return makeAnyNode(not, indirect, path); return recursiveExtract(&elem, not, indirect, path); case jqiEqual: if (not) @@ -157,6 +179,7 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) *result->exactValue = e; return result; } + /* fall through */ /* jqiEqual with jqiArray follows */ case jqiIn: case jqiOverlap: @@ -229,16 +252,18 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) if (jsq->type == jqiGreater || jsq->type == jqiGreaterOrEqual) { + result->bounds.leftBound = (JsQueryItem *)palloc(sizeof(JsQueryItem)); result->bounds.leftInclusive = (jsq->type == jqiGreaterOrEqual); result->bounds.rightBound = NULL; - result->bounds.leftBound = (JsQueryItem *)palloc(sizeof(JsQueryItem)); + result->bounds.rightInclusive = false; jsqGetArg(jsq, result->bounds.leftBound); } else { + result->bounds.rightBound = (JsQueryItem *)palloc(sizeof(JsQueryItem)); result->bounds.rightInclusive = (jsq->type == jqiLessOrEqual); result->bounds.leftBound = NULL; - result->bounds.rightBound = (JsQueryItem *)palloc(sizeof(JsQueryItem)); + result->bounds.leftInclusive = false; jsqGetArg(jsq, result->bounds.rightBound); } return result; @@ -261,6 +286,25 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path) return NULL; } +/* + * Make node for checking existence of path. + */ +static ExtractedNode * +makeAnyNode(bool not, bool indirect, PathItem *path) +{ + ExtractedNode *result; + + if (not) + return NULL; + + result = (ExtractedNode *) palloc(sizeof(ExtractedNode)); + result->type = eAny; + result->hint = false; + result->path = path; + result->indirect = indirect; + return result; +} + /* * Count number of children connected with nodes of same type. */ @@ -659,12 +703,13 @@ makeEntries(ExtractedNode *node, MakeEntryHandler handler, Pointer extra) for (i = 0; i < node->args.count; i++) { child = node->args.items[i]; - if (!child) continue; - if (child->sClass > node->sClass && !child->forceIndex) - { - Assert(node->type != eOr); + if (!child) + continue; + /* Skip non-selective AND children */ + if (child->sClass > node->sClass && + node->type == eAnd && + !child->forceIndex) continue; - } child = makeEntries(child, handler, extra); if (child) { @@ -731,6 +776,8 @@ setSelectivityClass(ExtractedNode *node, CheckEntryHandler checkHandler, if (!child) continue; + setSelectivityClass(child, checkHandler, extra); + if (!isLogicalNodeType(child->type)) { if (child->hint == jsqNoIndex || @@ -738,8 +785,6 @@ setSelectivityClass(ExtractedNode *node, CheckEntryHandler checkHandler, continue; } - setSelectivityClass(child, checkHandler, extra); - if (child->forceIndex) node->forceIndex = true; @@ -825,22 +870,18 @@ execRecursiveTristate(ExtractedNode *node, GinTernaryValue *check) res = GIN_TRUE; for (i = 0; i < node->args.count; i++) { - v = execRecursive(node->args.items[i], check); + v = execRecursiveTristate(node->args.items[i], check); if (v == GIN_FALSE) return GIN_FALSE; - else if (v == GIN_MAYBE) - res = GIN_MAYBE; } return res; case eOr: res = GIN_FALSE; for (i = 0; i < node->args.count; i++) { - v = execRecursive(node->args.items[i], check); + v = execRecursiveTristate(node->args.items[i], check); if (v == GIN_TRUE) return GIN_TRUE; - else if (v == GIN_MAYBE) - res = GIN_MAYBE; } return res; default: @@ -854,6 +895,11 @@ execRecursiveTristate(ExtractedNode *node, GinTernaryValue *check) static void debugPath(StringInfo buf, PathItem *path) { + if (!path) + { + appendStringInfoChar(buf, '$'); + return; + } if (path->parent) { debugPath(buf, path->parent); diff --git a/jsquery_gram.y b/jsquery_gram.y index 7db9a2d..fd05e7a 100644 --- a/jsquery_gram.y +++ b/jsquery_gram.y @@ -1,13 +1,14 @@ /*------------------------------------------------------------------------- * * jsquery_gram.y - * Grammar definitions for jsquery datatype + * Grammar definitions for jsquery datatype * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION - * contrib/jsquery/jsquery_gram.y + * contrib/jsquery/jsquery_gram.y * *------------------------------------------------------------------------- */ @@ -43,11 +44,11 @@ fprintf_to_ereport(const char *fmt, const char *msg) /* struct string is shared between scan and gram */ typedef struct string { - char *val; - int len; + char *val; + int len; int total; } string; -#include +#include "jsquery_gram.h" /* flex 2.5.4 doesn't bother with a decl for this */ int jsquery_yylex(YYSTYPE * yylval_param); @@ -70,7 +71,11 @@ makeIndexArray(string *s) { JsQueryParseItem* v = makeItemType(jqiIndexArray); +#if PG_VERSION_NUM >= 120000 + v->arrayIndex = pg_strtoint32(s->val); +#else v->arrayIndex = pg_atoi(s->val, 4, 0); +#endif return v; } @@ -188,6 +193,9 @@ makeItemList(List *list) { head = end = (JsQueryParseItem*)linitial(list); + while(end->next) + end = end->next; + foreach(cell, list) { JsQueryParseItem *c = (JsQueryParseItem*)lfirst(cell); @@ -197,6 +205,9 @@ makeItemList(List *list) { end->next = c; end = c; + + while(end->next) + end = end->next; } return head; @@ -212,7 +223,7 @@ makeItemList(List *list) { %parse-param {JsQueryParseItem **result} %union { - string str; + string str; List *elems; /* list of JsQueryParseItem */ JsQueryParseItem *value; @@ -225,26 +236,31 @@ makeItemList(List *list) { %token STRING_P NUMERIC_P INT_P -%type result scalar_value +%type result scalar_value %type path value_list -%type key key_any right_expr expr array numeric +%type key key_any right_expr expr array numeric %token HINT_P -%left OR_P -%left AND_P -%right NOT_P -%nonassoc IN_P IS_P +%left OR_P +%left AND_P +%right NOT_P +%nonassoc IN_P IS_P %nonassoc '(' ')' /* Grammar follows */ %% -result: - expr { *result = $1; } - | /* EMPTY */ { *result = NULL; } +result: + expr { + *result = $1; + (void) yynerrs; /* suppress compiler warning */ + } + | /* EMPTY */ { + *result = NULL; + yyerror(NULL, "No symbols read"); } ; array: @@ -271,8 +287,8 @@ scalar_value: ; value_list: - scalar_value { $$ = lappend(NIL, $1); } - | value_list ',' scalar_value { $$ = lappend($1, $3); } + scalar_value { $$ = lappend(NIL, $1); } + | value_list ',' scalar_value { $$ = lappend($1, $3); } ; numeric: @@ -289,20 +305,21 @@ right_expr: | '>' numeric { $$ = makeItemUnary(jqiGreater, $2); } | '<' '=' numeric { $$ = makeItemUnary(jqiLessOrEqual, $3); } | '>' '=' numeric { $$ = makeItemUnary(jqiGreaterOrEqual, $3); } - | '@' '>' array { $$ = makeItemUnary(jqiContains, $3); } - | '<' '@' array { $$ = makeItemUnary(jqiContained, $3); } + | '@' '>' array { $$ = makeItemUnary(jqiContains, $3); } + | '<' '@' array { $$ = makeItemUnary(jqiContained, $3); } | '&' '&' array { $$ = makeItemUnary(jqiOverlap, $3); } - | IS_P ARRAY_T { $$ = makeItemIs(jbvArray); } - | IS_P NUMERIC_T { $$ = makeItemIs(jbvNumeric); } - | IS_P OBJECT_T { $$ = makeItemIs(jbvObject); } - | IS_P STRING_T { $$ = makeItemIs(jbvString); } - | IS_P BOOLEAN_T { $$ = makeItemIs(jbvBool); } + | IS_P ARRAY_T { $$ = makeItemIs(jbvArray); } + | IS_P NUMERIC_T { $$ = makeItemIs(jbvNumeric); } + | IS_P OBJECT_T { $$ = makeItemIs(jbvObject); } + | IS_P STRING_T { $$ = makeItemIs(jbvString); } + | IS_P BOOLEAN_T { $$ = makeItemIs(jbvBool); } ; expr: - path right_expr { $$ = makeItemList(lappend($1, $2)); } + path { $$ = makeItemList($1); } + | path right_expr { $$ = makeItemList(lappend($1, $2)); } | path HINT_P right_expr { $3->hint = $2; $$ = makeItemList(lappend($1, $3)); } - | NOT_P expr { $$ = makeItemUnary(jqiNot, $2); } + | NOT_P expr { $$ = makeItemUnary(jqiNot, $2); } /* * In next two lines NOT_P is a path actually, not a an * logical expression. @@ -346,15 +363,17 @@ key: ; /* - * NOT keyword needs separate processing + * NOT keyword needs separate processing */ key_any: key { $$ = $$; } + | '?' '(' expr ')' { $$ = makeItemUnary(jqiFilter, $3); } | NOT_P { $$ = makeItemKey(&$1); } ; path: key { $$ = lappend(NIL, $1); } + | '?' '(' expr ')' { $$ = lappend(NIL, makeItemUnary(jqiFilter, $3)); } | path '.' key_any { $$ = lappend($1, $3); } | NOT_P '.' key_any { $$ = lappend(lappend(NIL, makeItemKey(&$1)), $3); } ; diff --git a/jsquery_io.c b/jsquery_io.c index a295244..8a4b9ac 100644 --- a/jsquery_io.c +++ b/jsquery_io.c @@ -1,13 +1,14 @@ /*------------------------------------------------------------------------- * * jsquery_io.c - * I/O functions for jsquery datatype + * I/O functions for jsquery datatype * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2016-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION - * contrib/jsquery/jsquery_io.c + * contrib/jsquery/jsquery_io.c * *------------------------------------------------------------------------- */ @@ -44,6 +45,7 @@ flattenJsQueryParseItem(StringInfo buf, JsQueryParseItem *item, bool onlyCurrent case jqiKey: if (onlyCurrentInPath) elog(ERROR,"Array length should be last in path"); + /* fall through */ case jqiString: appendBinaryStringInfo(buf, (char*)&item->string.len, sizeof(item->string.len)); appendBinaryStringInfo(buf, item->string.val, item->string.len); @@ -103,6 +105,7 @@ flattenJsQueryParseItem(StringInfo buf, JsQueryParseItem *item, bool onlyCurrent case jqiContained: case jqiOverlap: case jqiNot: + case jqiFilter: { int32 arg; @@ -116,6 +119,7 @@ flattenJsQueryParseItem(StringInfo buf, JsQueryParseItem *item, bool onlyCurrent case jqiIndexArray: appendBinaryStringInfo(buf, (char*)&item->arrayIndex, sizeof(item->arrayIndex)); + /* FALLTHROUGH */ /* keep svace quiet */ case jqiAny: case jqiAnyArray: case jqiAnyKey: @@ -154,21 +158,16 @@ jsquery_in(PG_FUNCTION_ARGS) StringInfoData buf; initStringInfo(&buf); - enlargeStringInfo(&buf, 4 * len /* estimation */); + enlargeStringInfo(&buf, 4 * len /* estimation */); appendStringInfoSpaces(&buf, VARHDRSZ); - if (jsquery != NULL) - { - flattenJsQueryParseItem(&buf, jsquery, false); - - res = (JsQuery*)buf.data; - SET_VARSIZE(res, buf.len); + flattenJsQueryParseItem(&buf, jsquery, false); - PG_RETURN_JSQUERY(res); - } + res = (JsQuery*)buf.data; + SET_VARSIZE(res, buf.len); - PG_RETURN_NULL(); + PG_RETURN_JSQUERY(res); } static void @@ -237,6 +236,7 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes case jqiKey: if (inKey) appendStringInfoChar(buf, '.'); + /* fall through */ /* follow next */ case jqiString: escape_json(buf, jsqGetString(v, NULL)); @@ -321,7 +321,8 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes appendStringInfoChar(buf, ')'); break; case jqiNot: - appendBinaryStringInfo(buf, "(NOT ", 5); + appendStringInfoChar(buf, '('); + appendBinaryStringInfo(buf, "NOT ", 4); jsqGetArg(v, &elem); printJsQueryItem(buf, &elem, false, true); appendStringInfoChar(buf, ')'); @@ -375,6 +376,14 @@ printJsQueryItem(StringInfo buf, JsQueryItem *v, bool inKey, bool printBracketes appendStringInfoChar(buf, '.'); appendStringInfo(buf, "#%u", v->arrayIndex); break; + case jqiFilter: + if (inKey) + appendStringInfoChar(buf, '.'); + appendBinaryStringInfo(buf, " ?(", 3); + jsqGetArg(v, &elem); + printJsQueryItem(buf, &elem, false, false); + appendBinaryStringInfo(buf, ") ", 2); + break; default: elog(ERROR, "Unknown JsQueryItem type: %d", v->type); } diff --git a/jsquery_op.c b/jsquery_op.c index 426257f..29cfe3d 100644 --- a/jsquery_op.c +++ b/jsquery_op.c @@ -1,13 +1,14 @@ /*------------------------------------------------------------------------- * * jsquery_op.c - * Functions and operations over jsquery/jsonb datatypes + * Functions and operations over jsquery/jsonb datatypes * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION - * contrib/jsquery/jsquery_op.c + * contrib/jsquery/jsquery_op.c * *------------------------------------------------------------------------- */ @@ -29,7 +30,48 @@ #include "jsquery.h" -static bool recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg); +typedef struct ResultAccum { + StringInfo buf; + bool missAppend; + JsonbParseState *jbArrayState; +} ResultAccum; + + +static bool recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg, + ResultAccum *ra); + +static void +appendResult(ResultAccum *ra, JsonbValue *jb) +{ + if (ra == NULL || ra->missAppend == true) + return; + + if (ra->jbArrayState == NULL) + pushJsonbValue(&ra->jbArrayState, WJB_BEGIN_ARRAY, NULL); + + pushJsonbValue(&ra->jbArrayState, WJB_ELEM, jb); +} + +static void +concatResult(ResultAccum *ra, JsonbParseState *a, JsonbParseState *b) +{ + Jsonb *value; + JsonbIterator *it; + int32 r; + JsonbValue v; + + Assert(a); + Assert(b); + + ra->jbArrayState = a; + + value = JsonbValueToJsonb(pushJsonbValue(&b, WJB_END_ARRAY, NULL)); + it = JsonbIteratorInit(&value->root); + + while((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE) + if (r == WJB_ELEM) + pushJsonbValue(&ra->jbArrayState, WJB_ELEM, &v); +} static int compareNumeric(Numeric a, Numeric b) @@ -67,7 +109,7 @@ JsonbType(JsonbValue *jb) } static bool -recursiveAny(JsQueryItem *jsq, JsonbValue *jb) +recursiveAny(JsQueryItem *jsq, JsonbValue *jb, ResultAccum *ra) { bool res = false; JsonbIterator *it; @@ -88,10 +130,14 @@ recursiveAny(JsQueryItem *jsq, JsonbValue *jb) if (r == WJB_VALUE || r == WJB_ELEM) { - res = recursiveExecute(jsq, &v, NULL); + /* + * we don't need actually store result, we may need to store whole + * object/array + */ + res = recursiveExecute(jsq, &v, NULL, ra); if (res == false && v.type == jbvBinary) - res = recursiveAny(jsq, &v); + res = recursiveAny(jsq, &v, ra); } } @@ -99,7 +145,7 @@ recursiveAny(JsQueryItem *jsq, JsonbValue *jb) } static bool -recursiveAll(JsQueryItem *jsq, JsonbValue *jb) +recursiveAll(JsQueryItem *jsq, JsonbValue *jb, ResultAccum *ra) { bool res = true; JsonbIterator *it; @@ -120,10 +166,14 @@ recursiveAll(JsQueryItem *jsq, JsonbValue *jb) if (r == WJB_VALUE || r == WJB_ELEM) { - if ((res = recursiveExecute(jsq, &v, NULL)) == true) + /* + * we don't need actually store result, we may need to store whole + * object/array + */ + if ((res = recursiveExecute(jsq, &v, NULL, ra)) == true) { if (v.type == jbvBinary) - res = recursiveAll(jsq, &v); + res = recursiveAll(jsq, &v, ra); } if (res == false) @@ -221,10 +271,11 @@ checkScalarIn(JsQueryItem *jsq, JsonbValue *jb) static bool executeArrayOp(JsQueryItem *jsq, int32 op, JsonbValue *jb) { - int32 r; + int32 r = 0; /* keep static analyzer quiet */ JsonbIterator *it; JsonbValue v; - JsQueryItem elem; + JsQueryItem elem; + bool res; if (JsonbType(jb) != jbvArray) return false; @@ -235,7 +286,7 @@ executeArrayOp(JsQueryItem *jsq, int32 op, JsonbValue *jb) { while(jsqIterateArray(jsq, &elem)) { - bool res = false; + res = false; it = JsonbIteratorInit(jb->val.binary.data); @@ -257,7 +308,7 @@ executeArrayOp(JsQueryItem *jsq, int32 op, JsonbValue *jb) { if (r == WJB_ELEM) { - bool res = false; + res = false; jsqIterateInit(jsq); while(jsqIterateArray(jsq, &elem)) @@ -270,6 +321,7 @@ executeArrayOp(JsQueryItem *jsq, int32 op, JsonbValue *jb) break; } } + jsqIterateDestroy(jsq); if (op == jqiContained && res == false) return false; @@ -397,7 +449,8 @@ executeExpr(JsQueryItem *jsq, int32 op, JsonbValue *jb, JsQueryItem *jsqLeftArg) } static bool -recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) +recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg, + ResultAccum *ra) { JsQueryItem elem; bool res = false; @@ -406,27 +459,59 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) switch(jsq->type) { case jqiAnd: - jsqGetLeftArg(jsq, &elem); - res = recursiveExecute(&elem, jb, jsqLeftArg); - if (res == true) { - jsqGetRightArg(jsq, &elem); - res = recursiveExecute(&elem, jb, jsqLeftArg); + JsonbParseState *saveJbArrayState = NULL; + + jsqGetLeftArg(jsq, &elem); + if (ra && ra->missAppend == false) + { + saveJbArrayState = ra->jbArrayState; + ra->jbArrayState = NULL; + } + + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); + if (res == true) + { + jsqGetRightArg(jsq, &elem); + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); + } + + if (ra && ra->missAppend == false) + { + if (res == true) + { + if (saveJbArrayState != NULL) + /* append args lists to current */ + concatResult(ra, saveJbArrayState, ra->jbArrayState); + } + else + ra->jbArrayState = saveJbArrayState; + } + + break; } - break; case jqiOr: jsqGetLeftArg(jsq, &elem); - res = recursiveExecute(&elem, jb, jsqLeftArg); + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); if (res == false) { jsqGetRightArg(jsq, &elem); - res = recursiveExecute(&elem, jb, jsqLeftArg); + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); } break; case jqiNot: - jsqGetArg(jsq, &elem); - res = !recursiveExecute(&elem, jb, jsqLeftArg); - break; + { + bool saveMissAppend = (ra) ? ra->missAppend : true; + + jsqGetArg(jsq, &elem); + if (ra) + ra->missAppend = true; + res = !recursiveExecute(&elem, jb, jsqLeftArg, ra); + if (ra) + ra->missAppend = saveMissAppend; + + break; + } case jqiKey: if (JsonbType(jb) == jbvObject) { JsonbValue *v, key; @@ -438,18 +523,27 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) if (v != NULL) { - jsqGetNext(jsq, &elem); - res = recursiveExecute(&elem, v, NULL); + if (jsqGetNext(jsq, &elem) == false) + { + appendResult(ra, v); + res = true; + } + else + res = recursiveExecute(&elem, v, NULL, ra); pfree(v); } } break; case jqiCurrent: - jsqGetNext(jsq, &elem); - if (JsonbType(jb) == jbvScalar) + if (jsqGetNext(jsq, &elem) == false) + { + appendResult(ra, jb); + res = true; + } + else if (JsonbType(jb) == jbvScalar) { JsonbIterator *it; - int32 r; + int32 r PG_USED_FOR_ASSERTS_ONLY; JsonbValue v; it = JsonbIteratorInit(jb->val.binary.data); @@ -462,26 +556,34 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) r = JsonbIteratorNext(&it, &v, true); Assert(r == WJB_ELEM); - res = recursiveExecute(&elem, &v, jsqLeftArg); + res = recursiveExecute(&elem, &v, jsqLeftArg, ra); } else { - res = recursiveExecute(&elem, jb, jsqLeftArg); + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); } break; case jqiAny: - jsqGetNext(jsq, &elem); - if (recursiveExecute(&elem, jb, NULL)) + if (jsqGetNext(jsq, &elem) == false) + { + res = true; + appendResult(ra, jb); + } + else if (recursiveExecute(&elem, jb, NULL, ra)) res = true; else if (jb->type == jbvBinary) - res = recursiveAny(&elem, jb); + res = recursiveAny(&elem, jb, ra); break; case jqiAll: - jsqGetNext(jsq, &elem); - if ((res = recursiveExecute(&elem, jb, NULL)) == true) + if (jsqGetNext(jsq, &elem) == false) + { + res = true; + appendResult(ra, jb); + } + if ((res = recursiveExecute(&elem, jb, NULL, ra)) == true) { if (jb->type == jbvBinary) - res = recursiveAll(&elem, jb); + res = recursiveAll(&elem, jb, ra); } break; case jqiAnyArray: @@ -491,10 +593,23 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) JsonbIterator *it; int32 r; JsonbValue v; + bool anyres = false; + bool hasNext; - jsqGetNext(jsq, &elem); + hasNext = jsqGetNext(jsq, &elem); it = JsonbIteratorInit(jb->val.binary.data); + if (hasNext == false) + { + res = true; + + while(ra && (r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE) + if (r == WJB_ELEM) + appendResult(ra, &v); + + break; + } + if (jsq->type == jqiAllArray) res = true; @@ -502,11 +617,13 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) { if (r == WJB_ELEM) { - res = recursiveExecute(&elem, &v, NULL); + res = recursiveExecute(&elem, &v, NULL, ra); if (jsq->type == jqiAnyArray) { - if (res == true) + anyres |= res; + if (res == true && + (ra == NULL || ra->missAppend == true)) break; } else if (jsq->type == jqiAllArray) @@ -516,6 +633,9 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) } } } + + if (jsq->type == jqiAnyArray) + res = anyres; } break; case jqiIndexArray: @@ -523,12 +643,19 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) { JsonbValue *v; - jsqGetNext(jsq, &elem); - v = getIthJsonbValueFromContainer(jb->val.binary.data, jsq->arrayIndex); - res = v && recursiveExecute(&elem, v, NULL); + if (v) + { + if (jsqGetNext(jsq, &elem) == false) + { + res = true; + appendResult(ra, v); + } + else + res = recursiveExecute(&elem, v, NULL, ra); + } } break; case jqiAnyKey: @@ -538,10 +665,23 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) JsonbIterator *it; int32 r; JsonbValue v; + bool anyres = false; + bool hasNext; - jsqGetNext(jsq, &elem); + hasNext = jsqGetNext(jsq, &elem); it = JsonbIteratorInit(jb->val.binary.data); + if (hasNext == false) + { + res = true; + + while(ra && (r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE) + if (r == WJB_VALUE) + appendResult(ra, &v); + + break; + } + if (jsq->type == jqiAllKey) res = true; @@ -549,11 +689,13 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) { if (r == WJB_VALUE) { - res = recursiveExecute(&elem, &v, NULL); + res = recursiveExecute(&elem, &v, NULL, ra); if (jsq->type == jqiAnyKey) { - if (res == true) + anyres |= res; + if (res == true && + (ra == NULL || ra->missAppend == true)) break; } else if (jsq->type == jqiAllKey) @@ -563,6 +705,9 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) } } } + + if (jsq->type == jqiAnyKey) + res = anyres; } break; case jqiEqual: @@ -579,13 +724,13 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) break; case jqiLength: jsqGetNext(jsq, &elem); - res = recursiveExecute(&elem, jb, jsq); + res = recursiveExecute(&elem, jb, jsq, ra); break; case jqiIs: if (JsonbType(jb) == jbvScalar) { JsonbIterator *it; - int32 r; + int32 r PG_USED_FOR_ASSERTS_ONLY; JsonbValue v; it = JsonbIteratorInit(jb->val.binary.data); @@ -605,6 +750,27 @@ recursiveExecute(JsQueryItem *jsq, JsonbValue *jb, JsQueryItem *jsqLeftArg) res = (jsqGetIsType(jsq) == JsonbType(jb)); } break; + case jqiFilter: + { + bool saveMissAppend = (ra) ? ra->missAppend : true; + + jsqGetArg(jsq, &elem); + if (ra) + ra->missAppend = true; + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); + if (ra) + ra->missAppend = saveMissAppend; + if (res) { + if (jsqGetNext(jsq, &elem) == false) + { + appendResult(ra, jb); + res = true; + } + else + res = recursiveExecute(&elem, jb, jsqLeftArg, ra); + } + } + break; default: elog(ERROR,"Wrong state: %d", jsq->type); } @@ -617,10 +783,10 @@ Datum jsquery_json_exec(PG_FUNCTION_ARGS) { JsQuery *jq = PG_GETARG_JSQUERY(0); - Jsonb *jb = PG_GETARG_JSONB(1); + Jsonb *jb = PG_GETARG_JSONB_P(1); bool res; JsonbValue jbv; - JsQueryItem jsq; + JsQueryItem jsq; jbv.type = jbvBinary; jbv.val.binary.data = &jb->root; @@ -628,7 +794,7 @@ jsquery_json_exec(PG_FUNCTION_ARGS) jsqInit(&jsq, jq); - res = recursiveExecute(&jsq, &jbv, NULL); + res = recursiveExecute(&jsq, &jbv, NULL, NULL); PG_FREE_IF_COPY(jq, 0); PG_FREE_IF_COPY(jb, 1); @@ -640,7 +806,7 @@ PG_FUNCTION_INFO_V1(json_jsquery_exec); Datum json_jsquery_exec(PG_FUNCTION_ARGS) { - Jsonb *jb = PG_GETARG_JSONB(0); + Jsonb *jb = PG_GETARG_JSONB_P(0); JsQuery *jq = PG_GETARG_JSQUERY(1); bool res; JsonbValue jbv; @@ -652,7 +818,7 @@ json_jsquery_exec(PG_FUNCTION_ARGS) jsqInit(&jsq, jq); - res = recursiveExecute(&jsq, &jbv, NULL); + res = recursiveExecute(&jsq, &jbv, NULL, NULL); PG_FREE_IF_COPY(jb, 0); PG_FREE_IF_COPY(jq, 1); @@ -660,6 +826,43 @@ json_jsquery_exec(PG_FUNCTION_ARGS) PG_RETURN_BOOL(res); } +PG_FUNCTION_INFO_V1(json_jsquery_filter); +Datum +json_jsquery_filter(PG_FUNCTION_ARGS) +{ + Jsonb *jb = PG_GETARG_JSONB_P(0); + JsQuery *jq = PG_GETARG_JSQUERY(1); + Jsonb *res = NULL; + JsonbValue jbv; + JsQueryItem jsq; + ResultAccum ra; + + jbv.type = jbvBinary; + jbv.val.binary.data = &jb->root; + jbv.val.binary.len = VARSIZE_ANY_EXHDR(jb); + + jsqInit(&jsq, jq); + memset(&ra, 0, sizeof(ra)); + + recursiveExecute(&jsq, &jbv, NULL, &ra); + + if (ra.jbArrayState) + { + res = JsonbValueToJsonb( + pushJsonbValue(&ra.jbArrayState, WJB_END_ARRAY, NULL) + ); + } + + PG_FREE_IF_COPY(jb, 0); + PG_FREE_IF_COPY(jq, 1); + + if (res) + PG_RETURN_JSONB_P(res); + + PG_RETURN_NULL(); +} + + static int compareJsQuery(JsQueryItem *v1, JsQueryItem *v2) { @@ -682,6 +885,7 @@ compareJsQuery(JsQueryItem *v1, JsQueryItem *v2) case jqiAll: case jqiAllArray: case jqiAllKey: + case jqiFilter: break; case jqiIndexArray: if (v1->arrayIndex != v2->arrayIndex) @@ -980,6 +1184,7 @@ hashJsQuery(JsQueryItem *v, pg_crc32 *crc) case jqiAll: case jqiAllArray: case jqiAllKey: + case jqiFilter: break; case jqiIndexArray: COMP_CRC32(*crc, &v->arrayIndex, sizeof(v->arrayIndex)); diff --git a/jsquery_scan.l b/jsquery_scan.l index e73bdd7..f145650 100644 --- a/jsquery_scan.l +++ b/jsquery_scan.l @@ -4,6 +4,7 @@ * Lexical parser for jsquery datatype * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION @@ -48,8 +49,8 @@ static void parseUnicode(char *s, int l); %x xNONQUOTED %x xCOMMENT -special [\%\$\.\[\]\(\)\|\&\!\=\<\>\@\#\,\*:] -any [^\%\$\.\[\]\(\)\|\&\!\=\<\>\@\#\,\* \t\n\r\f\\\"\/:] +special [\?\%\$\.\[\]\(\)\|\&\!\=\<\>\@\#\,\*:] +any [^\?\%\$\.\[\]\(\)\|\&\!\=\<\>\@\#\,\* \t\n\r\f\\\"\/:] blank [ \t\n\r\f] unicode \\u[0-9A-Fa-f]{4} @@ -205,7 +206,7 @@ yyerror(JsQueryParseItem **result, const char *message) (errcode(ERRCODE_SYNTAX_ERROR), errmsg("bad jsquery representation"), /* translator: %s is typically "syntax error" */ - errdetail("%s at end of input", message))); + errdetail("%s at the end of input", message))); } else { diff --git a/jsquery_support.c b/jsquery_support.c index ee63c8c..747776b 100644 --- a/jsquery_support.c +++ b/jsquery_support.c @@ -4,6 +4,7 @@ * Functions and operations to support jsquery * * Copyright (c) 2014, PostgreSQL Global Development Group + * Portions Copyright (c) 2017-2021, Postgres Professional * Author: Teodor Sigaev * * IDENTIFICATION @@ -33,10 +34,13 @@ alignStringInfoInt(StringInfo buf) { case 3: appendStringInfoCharMacro(buf, 0); + /* fall through */ case 2: appendStringInfoCharMacro(buf, 0); + /* fall through */ case 1: appendStringInfoCharMacro(buf, 0); + /* fall through */ default: break; } @@ -60,9 +64,9 @@ jsqInitByBuffer(JsQueryItem *v, char *base, int32 pos) switch(INTALIGN(pos) - pos) { - case 3: pos++; - case 2: pos++; - case 1: pos++; + case 3: pos++; /* fall through */ + case 2: pos++; /* fall through */ + case 1: pos++; /* fall through */ default: break; } @@ -86,6 +90,7 @@ jsqInitByBuffer(JsQueryItem *v, char *base, int32 pos) case jqiKey: case jqiString: read_int32(v->value.datalen, base, pos); + /* fall through */ /* follow next */ case jqiNumeric: case jqiBool: @@ -112,6 +117,7 @@ jsqInitByBuffer(JsQueryItem *v, char *base, int32 pos) case jqiOverlap: case jqiIn: case jqiNot: + case jqiFilter: read_int32(v->arg, base, pos); break; default: @@ -132,6 +138,7 @@ jsqGetArg(JsQueryItem *v, JsQueryItem *a) v->type == jqiContains || v->type == jqiContained || v->type == jqiOverlap || + v->type == jqiFilter || v->type == jqiIn || v->type == jqiNot ); @@ -154,6 +161,7 @@ jsqGetNext(JsQueryItem *v, JsQueryItem *a) v->type == jqiAllArray || v->type == jqiAllKey || v->type == jqiCurrent || + v->type == jqiFilter || v->type == jqiLength ); @@ -206,7 +214,7 @@ jsqGetNumeric(JsQueryItem *v) int32 jsqGetIsType(JsQueryItem *v) { - Assert(v->type = jqiIs); + Assert(v->type == jqiIs); return (int32)*v->value.data; } @@ -249,3 +257,11 @@ jsqIterateArray(JsQueryItem *v, JsQueryItem *e) } } +void +jsqIterateDestroy(JsQueryItem *v) +{ + Assert(v->type == jqiArray); + Assert(v->array.current <= v->array.nelems); + v->array.current++; +} + diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..10f4ee9 --- /dev/null +++ b/meson.build @@ -0,0 +1,53 @@ +# Copyright (c) 2025, Postgres Professional + +# Does not support the PGXS infrastructure at this time. Please, compile as part +# of the contrib source tree. + +jsquery_sources = files( + 'jsonb_gin_ops.c', + 'jsquery_constr.c', + 'jsquery_extract.c', + 'jsquery_io.c', + 'jsquery_op.c', + 'jsquery_support.c', +) + +jsquery_gram = custom_target('jsquerygram', + input: 'jsquery_gram.y', + kwargs: bison_kw, +) +generated_sources += jsquery_gram.to_list() +jsquery_sources += jsquery_gram + +run_command(flex, '--outfile=jsquery_scan.c', 'jsquery_scan.l', check: true) + +if host_system == 'windows' + jsquery_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ + '--NAME', 'jsquery', + '--FILEDESC', 'jsquery - language to query jsonb data type.',]) +endif + +jsquery = shared_module('jsquery', + jsquery_sources, + include_directories: include_directories('.'), + kwargs: contrib_mod_args, +) +contrib_targets += jsquery + +install_data( + 'jsquery.control', + 'jsquery--1.0--1.1.sql', + 'jsquery--1.1.sql', + kwargs: contrib_data_args, +) + +tests += { + 'name': 'jsquery', + 'sd': meson.current_source_dir(), + 'bd': meson.current_build_dir(), + 'regress': { + 'sql': [ + 'jsquery', + ], + }, +} diff --git a/sql/jsquery.sql b/sql/jsquery.sql index fb5319a..d183741 100644 --- a/sql/jsquery.sql +++ b/sql/jsquery.sql @@ -1,11 +1,13 @@ CREATE EXTENSION jsquery; set escape_string_warning=off; +set standard_conforming_strings=on; CREATE TABLE test_jsquery (v jsonb); \copy test_jsquery from 'data/test_jsquery.data' +select ''::jsquery; select 'asd.zzz = 13'::jsquery; select 'asd.zzz < 13'::jsquery; select 'asd(zzz < 13)'::jsquery; @@ -104,124 +106,124 @@ select 'is.not < 1'::jsquery; select 'a.b.#4 > 4'::jsquery; select 'a.b.#10203.* > 4'::jsquery; -select '{"a": {"b": null}}'::jsonb @@ 'a.b = 1'; -select '{"a": {"b": null}}'::jsonb @@ 'a.b = null'; -select '{"a": {"b": null}}'::jsonb @@ 'a.b = false'; -select '{"a": {"b": false}}'::jsonb @@ 'a.b = false'; -select '{"a": {"b": false}}'::jsonb @@ 'a.b = true'; -select '{"a": {"b": true}}'::jsonb @@ 'a.b = true'; - - -select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 1'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 1'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 1'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 1'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 1'; - -select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 2'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 2'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 2'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 2'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 2'; - -select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 0'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 0'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 0'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 0'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 0'; - -select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'; -select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'; -select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'; - -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 1 ]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1 ]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1 ]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1,2,3,4 ]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1,2,3,4 ]'; - -select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 4'; -select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 3'; - -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a = 4'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a = 4'; - -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 1 OR a=3)'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 OR a=1)'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=1)'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=2)' as "false"; -select '[{"a": 2, "b":3}, {"a": 3, "b": 1}]'::jsonb @@ '#(b = 1 and a=3)'; - -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a.a = 4'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a.a = 4'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.#.a.a = 4'; -select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.*.a.a = 4'; - -select '{"a": 1}'::jsonb @@ 'a in (0,1,2)'; -select '{"a": 1}'::jsonb @@ 'a in (0,2)'; - -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#=2'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 5 ]'; - -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a=*'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b=*'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.c=*'; - -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b = [1,2,3]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# = [1,2,3]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b && [1,2,3]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# && [1,2,3]'; - -select 'asd.# = 3'::jsquery & 'zzz = true' | 'xxx.# = zero'; -select !'asd.# = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'; -select !'asd.#3.f = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'; +select '{"a": {"b": null}}'::jsonb @@ 'a.b = 1'::jsquery; +select '{"a": {"b": null}}'::jsonb @@ 'a.b = null'::jsquery; +select '{"a": {"b": null}}'::jsonb @@ 'a.b = false'::jsquery; +select '{"a": {"b": false}}'::jsonb @@ 'a.b = false'::jsquery; +select '{"a": {"b": false}}'::jsonb @@ 'a.b = true'::jsquery; +select '{"a": {"b": true}}'::jsonb @@ 'a.b = true'::jsquery; + + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 1'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 1'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 1'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 1'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 1'::jsquery; + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 2'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 2'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 2'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 2'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 2'::jsquery; + +select '{"a": {"b": 1}}'::jsonb @@ 'a.b = 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b < 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b <= 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b >= 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.b > 0'::jsquery; + +select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ '*.b > 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'::jsquery; +select '{"a": {"b": 1}}'::jsonb @@ 'a.* > 0'::jsquery; + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 1 ]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1 ]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1 ]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b @> [ 1,2,3,4 ]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b <@ [ 1,2,3,4 ]'::jsquery; + +select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 4'::jsquery; +select '[{"a": 2}, {"a": 3}]'::jsonb @@ '*.a = 3'::jsquery; + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a = 4'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a = 4'::jsquery; + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 1 OR a=3)'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 OR a=1)'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=1)'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#(a = 3 and a=2)'::jsquery as "false"; +select '[{"a": 2, "b":3}, {"a": 3, "b": 1}]'::jsonb @@ '#(b = 1 and a=3)'::jsquery; + +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.a.a = 4'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.a.a = 4'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '*.#.a.a = 4'::jsquery; +select '[{"a": 2}, {"a": 3}, {"a": {"a":4}}]'::jsonb @@ '#.*.a.a = 4'::jsquery; + +select '{"a": 1}'::jsonb @@ 'a in (0,1,2)'::jsquery; +select '{"a": 1}'::jsonb @@ 'a in (0,2)'::jsquery; + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#=2'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 5 ]'::jsquery; + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a=*'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b=*'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.c=*'::jsquery; + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b = [1,2,3]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# = [1,2,3]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b && [1,2,3]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.# && [1,2,3]'::jsquery; + +select 'asd.# = 3'::jsquery & 'zzz = true' | 'xxx.# = zero'::jsquery; +select !'asd.# = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'::jsquery; +select !'asd.#3.f = 3'::jsquery & 'zzz = true' | !'xxx.# = zero'::jsquery; select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0]'::jsquery; select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0,1]'::jsquery; select '{"x":[0,1,1,2]}'::jsonb @@ 'x @> [1,0,3]'::jsquery; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 2 ]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b($ && [ 2 ])'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b && [ 2 ]'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b ($ && [ 2 ])'; - -select '[1,2,3]'::jsonb @@ '# && [2]'; -select '[1,2,3]'::jsonb @@ '#($ && [2])'; -select '[1,2,3]'::jsonb @@ '$ && [2]'; -select '[1,2,3]'::jsonb @@ '$ ($ && [2])'; -select '[1,2,3]'::jsonb @@ '$ = 2'; -select '[1,2,3]'::jsonb @@ '# = 2'; -select '[1,2,3]'::jsonb @@ '#.$ = 2'; -select '[1,2,3]'::jsonb @@ '#($ = 2)'; - -select '[3,4]'::jsonb @@ '#($ > 2 and $ < 5)'; -select '[3,4]'::jsonb @@ '# > 2 and # < 5'; -select '[1,6]'::jsonb @@ '#($ > 2 and $ < 5)'; -select '[1,6]'::jsonb @@ '# > 2 and # < 5'; - -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.b=3'; -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ 'a.%=3'; -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.%="hey"'; -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%="hey"'; -select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%=[5,6]'; - -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#1 = 2'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#2 = 2'; -select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#3 = 2'; -select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#1.x = 2'; -select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#2.x = 2'; -select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#3.x = 2'; - -select '"XXX"'::jsonb @@ '$="XXX"'; -select '"XXX"'::jsonb @@ '#.$="XXX"'; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b && [ 2 ]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ '*.b($ && [ 2 ])'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b && [ 2 ]'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.$.b ($ && [ 2 ])'::jsquery; + +select '[1,2,3]'::jsonb @@ '# && [2]'::jsquery; +select '[1,2,3]'::jsonb @@ '#($ && [2])'::jsquery; +select '[1,2,3]'::jsonb @@ '$ && [2]'::jsquery; +select '[1,2,3]'::jsonb @@ '$ ($ && [2])'::jsquery; +select '[1,2,3]'::jsonb @@ '$ = 2'::jsquery; +select '[1,2,3]'::jsonb @@ '# = 2'::jsquery; +select '[1,2,3]'::jsonb @@ '#.$ = 2'::jsquery; +select '[1,2,3]'::jsonb @@ '#($ = 2)'::jsquery; + +select '[3,4]'::jsonb @@ '#($ > 2 and $ < 5)'::jsquery; +select '[3,4]'::jsonb @@ '# > 2 and # < 5'::jsquery; +select '[1,6]'::jsonb @@ '#($ > 2 and $ < 5)'::jsquery; +select '[1,6]'::jsonb @@ '# > 2 and # < 5'::jsquery; + +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.b=3'::jsquery; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ 'a.%=3'::jsquery; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%.%="hey"'::jsquery; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%="hey"'::jsquery; +select '{"a": {"b": 3, "c": "hey"}, "x": [5,6]}'::jsonb @@ '%=[5,6]'::jsquery; + +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#1 = 2'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#2 = 2'::jsquery; +select '{"a": {"b": [1,2,3]}}'::jsonb @@ 'a.b.#3 = 2'::jsquery; +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#1.x = 2'::jsquery; +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#2.x = 2'::jsquery; +select '{"a": {"b": [{"x":1},{"x":2},{"x":3}]}}'::jsonb @@ 'a.b.#3.x = 2'::jsquery; + +select '"XXX"'::jsonb @@ '$="XXX"'::jsquery; +select '"XXX"'::jsonb @@ '#.$="XXX"'::jsquery; --Unicode select 'a\t = "dollar \u0024 character"'::jsquery; -select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar \u0024 character"'; -select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar $ character"'; -select '{ "a": "dollar $ character" }'::jsonb @@ '* = "dollar \u0024 character"'; +select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar \u0024 character"'::jsquery; +select '{ "a": "dollar \u0024 character" }'::jsonb @@ '* = "dollar $ character"'::jsquery; +select '{ "a": "dollar $ character" }'::jsonb @@ '* = "dollar \u0024 character"'::jsquery; select 'a\r = "\n\""'::jsquery; select 'a\r = "\u0000"'::jsquery; select 'a\r = \u0000'::jsquery; @@ -295,6 +297,38 @@ select '{"a":[1,2]}' @@ '*.@# in (2, 4)'::jsquery; select '{"a":[1,2]}' @@ '*.@# ($ = 4 or $ = 2)'::jsquery; select '{"a":[1,2]}' @@ '@# = 1'::jsquery; +--filter +select '?( not b>0). x'::jsquery; +select 'a.?(b>0 and x= 0 ) .c'::jsquery; +select 'a.$. ?(b>0 and x= 0 ) . c.k'::jsquery; +select 'a.$.? (b>0 and x.*= 0 ).c.k'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a < 0) (b=20)'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 0) (b=20)'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 1) (b=20)'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 2) (b=20)'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb @@ '#. ?(a > 3) (b=20)'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}]'::jsonb ~~ '#.a'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}]'::jsonb ~~ '#. ?(a > 1). b'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '# . ?(a > 1)'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '%'::jsquery; +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '%'::jsquery; +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '% . ? ( $ > 2 )'::jsquery; +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '% . ? ( $ > 2 ).$'::jsquery; +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( % > 2 )'::jsquery; +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( %: > 0 )'::jsquery; +select '{"a":1, "b":2, "c":3}'::jsonb ~~ '? ( %: > 2 )'::jsquery; +select '[{"a":1, "b":10}, {"a":2, "b":20}, {"a":3, "b":30}]'::jsonb ~~ '#'::jsquery; +select '[1,2,3]'::jsonb ~~ '#'::jsquery; +select '[1,2,3]'::jsonb ~~ '#. ?($ > 2)'::jsquery; +select '[1,2,3]'::jsonb ~~ '#. ?($ > 2).$'::jsquery; +select '[1,2,3]'::jsonb ~~ ' ?(#.$ > 2).$'::jsquery; +select '[1,2,3]'::jsonb ~~ ' ?(#:.$ > 2).$'::jsquery; +select '[1,2,3]'::jsonb ~~ ' ?(#:.$ > 0).$'::jsquery; +select '{"a": {"b": {"c": 1}}}'::jsonb ~~ '*.?(c >0)'::jsquery; +select '{"a": {"b": {"c": 1}}}'::jsonb ~~ '?(*.c >0)'::jsquery; +select '{"tags":[{"term":["NYC", "CYN"]}, {"term":["1NYC", "1CYN"]} ]}'::jsonb ~~ 'tags.#.term.#. ? ( $ = "NYC")'::jsquery; +select '{"tags":[{"term":["NYC", "CYN"]}, {"term":["1NYC", "1CYN"]} ]}'::jsonb ~~ 'tags.#.term. ? ( # = "NYC")'::jsquery; + --ALL select 'a.*: = 4'::jsquery; select '%: = 4'::jsquery; @@ -336,6 +370,30 @@ SELECT 'test.# IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,2 select '[]' @@ '(@# > 0 and #: = 16)'::jsquery; select '[16]' @@ '(@# > 0 and #: = 16)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.b or b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c or b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g or b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g or b.c'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.b and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.c and b.b'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb @@ 'a.g and b.d'::jsquery; + +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and b.c'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and (b.d or b.c)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and (b.c or b.d)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b and a.c and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.b or a.c) and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.e or a.c) and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ '(a.e or a.g) and b.d'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'a.b or (b.d or b.c)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d or (a.b or a.c)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d or (a.b and a.c)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.f or (a.b and a.c)'::jsquery; +select '{"a": {"b": 1, "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d and (a.b and a.c)'::jsquery; +select '{"a": {"b": [6,5,4], "c": 2}, "b": {"d":3}}'::jsonb ~~ 'b.d and (a.b and a.c)'::jsquery; + --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'); @@ -360,6 +418,9 @@ SELECT gin_debug_query_path_value('x is object'); SELECT gin_debug_query_path_value('#:(x=1) AND %:(y=1) AND *:(z=1)'); SELECT gin_debug_query_path_value('#:(NOT x=1) AND %:(NOT y=1) AND *:(NOT z=1)'); SELECT gin_debug_query_path_value('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT *:(NOT z=1)'); +SELECT gin_debug_query_path_value('$ = true'); +SELECT gin_debug_query_path_value('$ . ? (review_votes > 10) . review_rating < 7'); +SELECT gin_debug_query_path_value('similar_product_ids . ? (# = "B0002W4TL2") . $'); SELECT gin_debug_query_value_path('NOT NOT NOT x(y(NOT (a=1) and NOT (b=2)) OR NOT NOT (c=3)) and z = 5'); SELECT gin_debug_query_value_path('NOT #(x=1) and NOT *(y=1) and NOT %(z=1) '); @@ -386,6 +447,10 @@ SELECT gin_debug_query_value_path('#:(NOT x=1) AND %:(NOT y=1) AND *:(NOT z=1)') SELECT gin_debug_query_value_path('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT *:(NOT z=1)'); SELECT gin_debug_query_value_path('(@# > 0 and #: = 16)'); SELECT gin_debug_query_value_path('*.@# ($ = 4 or $ = 2)'); +SELECT gin_debug_query_value_path('tags.#.term. ? ( # = "NYC").x > 0'); +SELECT gin_debug_query_value_path('$ = true'); +SELECT gin_debug_query_value_path('$ . ? (review_votes > 10) . review_rating < 7'); +SELECT gin_debug_query_value_path('similar_product_ids . ? (# = "B0002W4TL2") . $'); ---table and index @@ -399,149 +464,164 @@ select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 16 (v->>'review_helpful_votes')::int4 < 20; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16' AND - v @@ 'review_helpful_votes < 20'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'; -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'; -select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '; -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'; -select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'; -select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'; -select count(*) from test_jsquery where v @@ 'customer_id = null'; -select count(*) from test_jsquery where v @@ 'review_votes = true'; -select count(*) from test_jsquery where v @@ 'product_group = false'; -select count(*) from test_jsquery where v @@ 't = *'; -select count(*) from test_jsquery where v @@ 't is boolean'; -select count(*) from test_jsquery where v @@ 't is string'; -select count(*) from test_jsquery where v @@ 't is numeric'; -select count(*) from test_jsquery where v @@ 't is array'; -select count(*) from test_jsquery where v @@ 't is object'; -select count(*) from test_jsquery where v @@ '$ is boolean'; -select count(*) from test_jsquery where v @@ '$ is string'; -select count(*) from test_jsquery where v @@ '$ is numeric'; -select count(*) from test_jsquery where v @@ '$ is array'; -select count(*) from test_jsquery where v @@ '$ is object'; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'; -select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'; -select count(*) from test_jsquery where v @@ '$ > 2'; -select count(*) from test_jsquery where v @@ '$ = false'; - -select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; -select v from test_jsquery where v @@ 'array && [2,3]' order by v; -select v from test_jsquery where v @@ 'array @> [2,3]' order by v; -select v from test_jsquery where v @@ 'array = [2,3]' order by v; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; +select count(*) from test_jsquery where v @@ 't = *'::jsquery; +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; +select count(*) from test_jsquery where v @@ 't is string'::jsquery; +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; +select count(*) from test_jsquery where v @@ 't is array'::jsquery; +select count(*) from test_jsquery where v @@ 't is object'::jsquery; +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; +select count(*) from test_jsquery where v @@ 't'::jsquery; +select count(*) from test_jsquery where v @@ '$'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; create index t_idx on test_jsquery using gin (v jsonb_value_path_ops); set enable_seqscan = off; -explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; - -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16' AND - v @@ 'review_helpful_votes < 20'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'; -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'; -select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '; -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'; -select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'; -select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'; -select count(*) from test_jsquery where v @@ 'customer_id = null'; -select count(*) from test_jsquery where v @@ 'review_votes = true'; -select count(*) from test_jsquery where v @@ 'product_group = false'; -select count(*) from test_jsquery where v @@ 't = *'; -select count(*) from test_jsquery where v @@ 't is boolean'; -select count(*) from test_jsquery where v @@ 't is string'; -select count(*) from test_jsquery where v @@ 't is numeric'; -select count(*) from test_jsquery where v @@ 't is array'; -select count(*) from test_jsquery where v @@ 't is object'; -select count(*) from test_jsquery where v @@ '$ is boolean'; -select count(*) from test_jsquery where v @@ '$ is string'; -select count(*) from test_jsquery where v @@ '$ is numeric'; -select count(*) from test_jsquery where v @@ '$ is array'; -select count(*) from test_jsquery where v @@ '$ is object'; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'; -select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'; -select count(*) from test_jsquery where v @@ '$ > 2'; -select count(*) from test_jsquery where v @@ '$ = false'; - -explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; -explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v; -explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order by v; -explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v; - -select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; -select v from test_jsquery where v @@ 'array && [2,3]' order by v; -select v from test_jsquery where v @@ 'array @> [2,3]' order by v; -select v from test_jsquery where v @@ 'array = [2,3]' order by v; +explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; +select count(*) from test_jsquery where v @@ 't = *'::jsquery; +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; +select count(*) from test_jsquery where v @@ 't is string'::jsquery; +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; +select count(*) from test_jsquery where v @@ 't is array'::jsquery; +select count(*) from test_jsquery where v @@ 't is object'::jsquery; +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; +select count(*) from test_jsquery where v @@ 't'::jsquery; +select count(*) from test_jsquery where v @@ '$'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + +explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; +explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; +explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; +explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; drop index t_idx; create index t_idx on test_jsquery using gin (v jsonb_path_value_ops); set enable_seqscan = off; -explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; - -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16' AND - v @@ 'review_helpful_votes < 20'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'; -select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'; -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'; -select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '; -select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'; -select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'; -select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'; -select count(*) from test_jsquery where v @@ 'customer_id = null'; -select count(*) from test_jsquery where v @@ 'review_votes = true'; -select count(*) from test_jsquery where v @@ 'product_group = false'; -select count(*) from test_jsquery where v @@ 't = *'; -select count(*) from test_jsquery where v @@ 't is boolean'; -select count(*) from test_jsquery where v @@ 't is string'; -select count(*) from test_jsquery where v @@ 't is numeric'; -select count(*) from test_jsquery where v @@ 't is array'; -select count(*) from test_jsquery where v @@ 't is object'; -select count(*) from test_jsquery where v @@ '$ is boolean'; -select count(*) from test_jsquery where v @@ '$ is string'; -select count(*) from test_jsquery where v @@ '$ is numeric'; -select count(*) from test_jsquery where v @@ '$ is array'; -select count(*) from test_jsquery where v @@ '$ is object'; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'; -select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'; -select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'; -select count(*) from test_jsquery where v @@ '$ > 2'; -select count(*) from test_jsquery where v @@ '$ = false'; - -explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; -explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v; -explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order by v; -explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v; - -select v from test_jsquery where v @@ 'array <@ [2,3]' order by v; -select v from test_jsquery where v @@ 'array && [2,3]' order by v; -select v from test_jsquery where v @@ 'array @> [2,3]' order by v; -select v from test_jsquery where v @@ 'array = [2,3]' order by v; +explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; + +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes < 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes >= 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes <= 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes = 19'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16'::jsquery AND + v @@ 'review_helpful_votes < 20'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes > 16 and review_helpful_votes < 20'::jsquery; +select count(*) from test_jsquery where v @@ 'review_helpful_votes ($ > 16 and $ < 20)'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"]'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids(# = "0440180295") '::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#($ = "0440180295") '::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids && ["0440180295"] and product_sales_rank > 300000'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids <@ ["B00000DG0U", "B00004SQXU", "B0001XAM18", "B00000FDBU", "B00000FDBV", "B000002H2H", "B000002H6C", "B000002H5E", "B000002H97", "B000002HMH"]'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids @> ["B000002H2H", "B000002H6C"]'::jsquery; +select count(*) from test_jsquery where v @@ 'customer_id = null'::jsquery; +select count(*) from test_jsquery where v @@ 'review_votes = true'::jsquery; +select count(*) from test_jsquery where v @@ 'product_group = false'::jsquery; +select count(*) from test_jsquery where v @@ 't = *'::jsquery; +select count(*) from test_jsquery where v @@ 't is boolean'::jsquery; +select count(*) from test_jsquery where v @@ 't is string'::jsquery; +select count(*) from test_jsquery where v @@ 't is numeric'::jsquery; +select count(*) from test_jsquery where v @@ 't is array'::jsquery; +select count(*) from test_jsquery where v @@ 't is object'::jsquery; +select count(*) from test_jsquery where v @@ '$ is boolean'::jsquery; +select count(*) from test_jsquery where v @@ '$ is string'::jsquery; +select count(*) from test_jsquery where v @@ '$ is numeric'::jsquery; +select count(*) from test_jsquery where v @@ '$ is array'::jsquery; +select count(*) from test_jsquery where v @@ '$ is object'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string'::jsquery; +select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")'::jsquery; +select count(*) from test_jsquery where v @@ '$ > 2'::jsquery; +select count(*) from test_jsquery where v @@ '$ = false'::jsquery; +select count(*) from test_jsquery where v @@ 't'::jsquery; +select count(*) from test_jsquery where v @@ '$'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids.#'::jsquery; +select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7'::jsquery; +select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $'::jsquery; + +explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; +explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; +explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; +explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; + +select v from test_jsquery where v @@ 'array <@ [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array && [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array @> [2,3]'::jsquery order by v; +select v from test_jsquery where v @@ 'array = [2,3]'::jsquery order by v; RESET enable_seqscan; diff --git a/travis/dep-ubuntu-llvm.sh b/travis/dep-ubuntu-llvm.sh new file mode 100755 index 0000000..e640d5b --- /dev/null +++ b/travis/dep-ubuntu-llvm.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +cat ./travis/llvm-snapshot.gpg.key | sudo apt-key add - +echo "deb http://apt.llvm.org/trusty/ llvm-toolchain-$(lsb_release -cs)-$LLVM_VER main" | sudo tee /etc/apt/sources.list.d/llvm.list diff --git a/travis/dep-ubuntu-postgres.sh b/travis/dep-ubuntu-postgres.sh new file mode 100755 index 0000000..41c7d34 --- /dev/null +++ b/travis/dep-ubuntu-postgres.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +cat ./travis/postgresql.gpg.key | sudo apt-key add - +echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main $PG_VER" | sudo tee /etc/apt/sources.list.d/pgdg.list diff --git a/travis/llvm-snapshot.gpg.key b/travis/llvm-snapshot.gpg.key new file mode 100644 index 0000000..aa6b105 --- /dev/null +++ b/travis/llvm-snapshot.gpg.key @@ -0,0 +1,52 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- +Version: GnuPG v1.4.12 (GNU/Linux) + +mQINBFE9lCwBEADi0WUAApM/mgHJRU8lVkkw0CHsZNpqaQDNaHefD6Rw3S4LxNmM +EZaOTkhP200XZM8lVdbfUW9xSjA3oPldc1HG26NjbqqCmWpdo2fb+r7VmU2dq3NM +R18ZlKixiLDE6OUfaXWKamZsXb6ITTYmgTO6orQWYrnW6ckYHSeaAkW0wkDAryl2 +B5v8aoFnQ1rFiVEMo4NGzw4UX+MelF7rxaaregmKVTPiqCOSPJ1McC1dHFN533FY +Wh/RVLKWo6npu+owtwYFQW+zyQhKzSIMvNujFRzhIxzxR9Gn87MoLAyfgKEzrbbT +DhqqNXTxS4UMUKCQaO93TzetX/EBrRpJj+vP640yio80h4Dr5pAd7+LnKwgpTDk1 +G88bBXJAcPZnTSKu9I2c6KY4iRNbvRz4i+ZdwwZtdW4nSdl2792L7Sl7Nc44uLL/ +ZqkKDXEBF6lsX5XpABwyK89S/SbHOytXv9o4puv+65Ac5/UShspQTMSKGZgvDauU +cs8kE1U9dPOqVNCYq9Nfwinkf6RxV1k1+gwtclxQuY7UpKXP0hNAXjAiA5KS5Crq +7aaJg9q2F4bub0mNU6n7UI6vXguF2n4SEtzPRk6RP+4TiT3bZUsmr+1ktogyOJCc +Ha8G5VdL+NBIYQthOcieYCBnTeIH7D3Sp6FYQTYtVbKFzmMK+36ERreL/wARAQAB +tD1TeWx2ZXN0cmUgTGVkcnUgLSBEZWJpYW4gTExWTSBwYWNrYWdlcyA8c3lsdmVz +dHJlQGRlYmlhbi5vcmc+iQI4BBMBAgAiBQJRPZQsAhsDBgsJCAcDAgYVCAIJCgsE +FgIDAQIeAQIXgAAKCRAVz00Yr090Ibx+EADArS/hvkDF8juWMXxh17CgR0WZlHCC +9CTBWkg5a0bNN/3bb97cPQt/vIKWjQtkQpav6/5JTVCSx2riL4FHYhH0iuo4iAPR +udC7Cvg8g7bSPrKO6tenQZNvQm+tUmBHgFiMBJi92AjZ/Qn1Shg7p9ITivFxpLyX +wpmnF1OKyI2Kof2rm4BFwfSWuf8Fvh7kDMRLHv+MlnK/7j/BNpKdozXxLcwoFBmn +l0WjpAH3OFF7Pvm1LJdf1DjWKH0Dc3sc6zxtmBR/KHHg6kK4BGQNnFKujcP7TVdv +gMYv84kun14pnwjZcqOtN3UJtcx22880DOQzinoMs3Q4w4o05oIF+sSgHViFpc3W +R0v+RllnH05vKZo+LDzc83DQVrdwliV12eHxrMQ8UYg88zCbF/cHHnlzZWAJgftg +hB08v1BKPgYRUzwJ6VdVqXYcZWEaUJmQAPuAALyZESw94hSo28FAn0/gzEc5uOYx +K+xG/lFwgAGYNb3uGM5m0P6LVTfdg6vDwwOeTNIExVk3KVFXeSQef2ZMkhwA7wya +KJptkb62wBHFE+o9TUdtMCY6qONxMMdwioRE5BYNwAsS1PnRD2+jtlI0DzvKHt7B +MWd8hnoUKhMeZ9TNmo+8CpsAtXZcBho0zPGz/R8NlJhAWpdAZ1CmcPo83EW86Yq7 +BxQUKnNHcwj2ebkCDQRRPZQsARAA4jxYmbTHwmMjqSizlMJYNuGOpIidEdx9zQ5g +zOr431/VfWq4S+VhMDhs15j9lyml0y4ok215VRFwrAREDg6UPMr7ajLmBQGau0Fc +bvZJ90l4NjXp5p0NEE/qOb9UEHT7EGkEhaZ1ekkWFTWCgsy7rRXfZLxB6sk7pzLC +DshyW3zjIakWAnpQ5j5obiDy708pReAuGB94NSyb1HoW/xGsGgvvCw4r0w3xPStw +F1PhmScE6NTBIfLliea3pl8vhKPlCh54Hk7I8QGjo1ETlRP4Qll1ZxHJ8u25f/ta +RES2Aw8Hi7j0EVcZ6MT9JWTI83yUcnUlZPZS2HyeWcUj+8nUC8W4N8An+aNps9l/ +21inIl2TbGo3Yn1JQLnA1YCoGwC34g8QZTJhElEQBN0X29ayWW6OdFx8MDvllbBV +ymmKq2lK1U55mQTfDli7S3vfGz9Gp/oQwZ8bQpOeUkc5hbZszYwP4RX+68xDPfn+ +M9udl+qW9wu+LyePbW6HX90LmkhNkkY2ZzUPRPDHZANU5btaPXc2H7edX4y4maQa +xenqD0lGh9LGz/mps4HEZtCI5CY8o0uCMF3lT0XfXhuLksr7Pxv57yue8LLTItOJ +d9Hmzp9G97SRYYeqU+8lyNXtU2PdrLLq7QHkzrsloG78lCpQcalHGACJzrlUWVP/ +fN3Ht3kAEQEAAYkCHwQYAQIACQUCUT2ULAIbDAAKCRAVz00Yr090IbhWEADbr50X +OEXMIMGRLe+YMjeMX9NG4jxs0jZaWHc/WrGR+CCSUb9r6aPXeLo+45949uEfdSsB +pbaEdNWxF5Vr1CSjuO5siIlgDjmT655voXo67xVpEN4HhMrxugDJfCa6z97P0+ML +PdDxim57uNqkam9XIq9hKQaurxMAECDPmlEXI4QT3eu5qw5/knMzDMZj4Vi6hovL +wvvAeLHO/jsyfIdNmhBGU2RWCEZ9uo/MeerPHtRPfg74g+9PPfP6nyHD2Wes6yGd +oVQwtPNAQD6Cj7EaA2xdZYLJ7/jW6yiPu98FFWP74FN2dlyEA2uVziLsfBrgpS4l +tVOlrO2YzkkqUGrybzbLpj6eeHx+Cd7wcjI8CalsqtL6cG8cUEjtWQUHyTbQWAgG +5VPEgIAVhJ6RTZ26i/G+4J8neKyRs4vz+57UGwY6zI4AB1ZcWGEE3Bf+CDEDgmnP +LSwbnHefK9IljT9XU98PelSryUO/5UPw7leE0akXKB4DtekToO226px1VnGp3Bov +1GBGvpHvL2WizEwdk+nfk8LtrLzej+9FtIcq3uIrYnsac47Pf7p0otcFeTJTjSq3 +krCaoG4Hx0zGQG2ZFpHrSrZTVy6lxvIdfi0beMgY6h78p6M9eYZHQHc02DjFkQXN +bXb5c6gCHESH5PXwPU4jQEE7Ib9J6sbk7ZT2Mw== +=j+4q +-----END PGP PUBLIC KEY BLOCK----- diff --git a/travis/pg-travis-test.sh b/travis/pg-travis-test.sh new file mode 100755 index 0000000..18a393f --- /dev/null +++ b/travis/pg-travis-test.sh @@ -0,0 +1,193 @@ +#!/bin/bash + +set -eux + +echo -en 'travis_fold:start:pg_install\\r' && echo 'PostgreSQL installation' + +sudo apt-get update + +# bug: http://www.postgresql.org/message-id/20130508192711.GA9243@msgid.df7cb.de +sudo update-alternatives --remove-all postmaster.1.gz + +# stop all existing instances (because of https://github.com/travis-ci/travis-cookbooks/pull/221) +sudo service postgresql stop +# ... and make sure they don't come back +echo 'exit 0' | sudo tee /etc/init.d/postgresql +sudo chmod a+x /etc/init.d/postgresql + +# install PostgreSQL +if [ $CHECK_TYPE = "valgrind" ]; then + # install required packages + apt_packages="build-essential libgd-dev valgrind lcov" + sudo apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y install -qq $apt_packages + + set -e + + pushd ~ + CUSTOM_PG_BIN=$PWD/pg_bin + CUSTOM_PG_SRC=$PWD/postgresql + + curl "https://ftp.postgresql.org/pub/source/v$PG_VER/postgresql-$PG_VER.tar.bz2" -o postgresql.tar.bz2 + mkdir $CUSTOM_PG_SRC + + tar \ + --extract \ + --file postgresql.tar.bz2 \ + --directory $CUSTOM_PG_SRC \ + --strip-components 1 + + cd $CUSTOM_PG_SRC + + # enable Valgrind support + sed -i.bak "s/\/* #define USE_VALGRIND *\//#define USE_VALGRIND/g" src/include/pg_config_manual.h + + # enable additional options + ./configure \ + CFLAGS='-Og -ggdb3 -fno-omit-frame-pointer' \ + --enable-cassert \ + --enable-coverage \ + --prefix=$CUSTOM_PG_BIN \ + --quiet + + # build & install PG + time make -s -j4 && make -s install + + # override default PostgreSQL instance + export PATH=$CUSTOM_PG_BIN/bin:$PATH + export LD_LIBRARY_PATH=$CUSTOM_PG_BIN/lib + + popd + set +e + prefix=$CUSTOM_PG_BIN +else + apt_packages="postgresql-$PG_VER postgresql-server-dev-$PG_VER postgresql-common build-essential libgd-dev" + sudo apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y install -qq $apt_packages + prefix=/usr/lib/postgresql/$PG_VER +fi + +# config path +pg_ctl_path=$prefix/bin/pg_ctl +initdb_path=$prefix/bin/initdb +config_path=$prefix/bin/pg_config + +# exit code +status=0 + +echo -en 'travis_fold:end:pg_install\\r' + +# perform code analysis if necessary +if [ $CHECK_TYPE = "static" ]; then + echo -en 'travis_fold:start:static_analysis\\r' && echo 'Static analysis' + + if [ "$CC" = "clang" ]; then + sudo apt-get -y install -qq clang-$LLVM_VER + + scan-build-$LLVM_VER --status-bugs \ + -disable-checker deadcode.DeadStores \ + make USE_PGXS=1 USE_ASSERT_CHECKING=1 PG_CONFIG=$config_path || status=$? + + elif [ "$CC" = "gcc" ]; then + sudo apt-get -y install -qq cppcheck + + cppcheck --template "{file} ({line}): {severity} ({id}): {message}" \ + --enable=warning,portability,performance \ + --suppress=redundantAssignment \ + --suppress=uselessAssignmentPtrArg \ + --suppress=incorrectStringBooleanError \ + --std=c89 *.c *.h 2> cppcheck.log + + if [ -s cppcheck.log ]; then + cat cppcheck.log + status=1 # error + fi + fi + + # don't forget to "make clean" + make clean USE_PGXS=1 PG_CONFIG=$config_path + echo -en 'travis_fold:end:static_analysis\\r' + exit $status +fi + +echo -en 'travis_fold:start:build_extension\\r' && echo 'Build extension' + +# build extension (using CFLAGS_SL for gcov) +if [ $CHECK_TYPE == "valgrind" ]; then + make USE_PGXS=1 USE_ASSERT_CHECKING=1 PG_CONFIG=$config_path + make install USE_PGXS=1 PG_CONFIG=$config_path +else + make USE_PGXS=1 USE_ASSERT_CHECKING=1 CC=$CC PG_CONFIG=$config_path CFLAGS_SL="$($config_path --cflags_sl) -coverage" + sudo make install USE_PGXS=1 PG_CONFIG=$config_path +fi + +echo -en 'travis_fold:end:build_extension\\r' + +echo -en 'travis_fold:start:run_tests\\r' && echo 'Run tests' + +# enable core dumps and specify their path +ulimit -c unlimited -S +echo '/tmp/%e-%s-%p.core' | sudo tee /proc/sys/kernel/core_pattern + +# set permission to write postgres locks +sudo chown $USER /var/run/postgresql/ + +# create cluster 'test' +CLUSTER_PATH=$(pwd)/test_cluster +$initdb_path -D $CLUSTER_PATH -U $USER -A trust + +# start cluster 'test' +echo "port = 55435" >> $CLUSTER_PATH/postgresql.conf +if [ $CHECK_TYPE = "valgrind" ]; then + PGCTLTIMEOUT=600 \ + valgrind --leak-check=no --gen-suppressions=all \ + --suppressions=$CUSTOM_PG_SRC/src/tools/valgrind.supp --time-stamp=yes \ + --log-file=/tmp/pid-%p.log --trace-children=yes \ + $pg_ctl_path -D $CLUSTER_PATH start -l postgres.log -w +else + $pg_ctl_path -D $CLUSTER_PATH start -l postgres.log -w +fi + +# run regression tests +PGPORT=55435 PGUSER=$USER PG_CONFIG=$config_path make installcheck USE_PGXS=1 || status=$? + +# stop cluster +$pg_ctl_path -D $CLUSTER_PATH stop -l postgres.log -w + +echo -en 'travis_fold:end:run_tests\\r' + +echo -en 'travis_fold:start:output\\r' && echo 'Check output' + +# show diff if it exists +if test -f regression.diffs; then cat regression.diffs; fi + +# show valgrind logs if needed +if [ $CHECK_TYPE = "valgrind" ]; then + for f in ` find /tmp -name pid-*.log ` ; do + if grep -q 'Command: [^ ]*/postgres' $f && grep -q 'ERROR SUMMARY: [1-9]' $f; then + echo "========= Contents of $f" + cat $f + status=1 + fi + done +fi + +# check core dumps if any +for corefile in $(find /tmp/ -name '*.core' 2>/dev/null) ; do + binary=$(gdb -quiet -core $corefile -batch -ex 'info auxv' | grep AT_EXECFN | perl -pe "s/^.*\"(.*)\"\$/\$1/g") + echo dumping $corefile for $binary + gdb --batch --quiet -ex "thread apply all bt full" -ex "quit" $binary $corefile +done + +echo -en 'travis_fold:end:output\\r' + +echo -en 'travis_fold:start:coverage\\r' && echo 'Coverage check' + +#generate *.gcov files +if [ $CC = "clang" ]; then + bash <(curl -s https://codecov.io/bash) -x "llvm-cov gcov" +else + bash <(curl -s https://codecov.io/bash) +fi + +echo -en 'travis_fold:end:coverage\\r' + +exit $status diff --git a/travis/postgresql.gpg.key b/travis/postgresql.gpg.key new file mode 100644 index 0000000..8480576 --- /dev/null +++ b/travis/postgresql.gpg.key @@ -0,0 +1,77 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- + +mQINBE6XR8IBEACVdDKT2HEH1IyHzXkb4nIWAY7echjRxo7MTcj4vbXAyBKOfjja +UrBEJWHN6fjKJXOYWXHLIYg0hOGeW9qcSiaa1/rYIbOzjfGfhE4x0Y+NJHS1db0V +G6GUj3qXaeyqIJGS2z7m0Thy4Lgr/LpZlZ78Nf1fliSzBlMo1sV7PpP/7zUO+aA4 +bKa8Rio3weMXQOZgclzgeSdqtwKnyKTQdXY5MkH1QXyFIk1nTfWwyqpJjHlgtwMi +c2cxjqG5nnV9rIYlTTjYG6RBglq0SmzF/raBnF4Lwjxq4qRqvRllBXdFu5+2pMfC +IZ10HPRdqDCTN60DUix+BTzBUT30NzaLhZbOMT5RvQtvTVgWpeIn20i2NrPWNCUh +hj490dKDLpK/v+A5/i8zPvN4c6MkDHi1FZfaoz3863dylUBR3Ip26oM0hHXf4/2U +A/oA4pCl2W0hc4aNtozjKHkVjRx5Q8/hVYu+39csFWxo6YSB/KgIEw+0W8DiTII3 +RQj/OlD68ZDmGLyQPiJvaEtY9fDrcSpI0Esm0i4sjkNbuuh0Cvwwwqo5EF1zfkVj +Tqz2REYQGMJGc5LUbIpk5sMHo1HWV038TWxlDRwtOdzw08zQA6BeWe9FOokRPeR2 +AqhyaJJwOZJodKZ76S+LDwFkTLzEKnYPCzkoRwLrEdNt1M7wQBThnC5z6wARAQAB +tBxQb3N0Z3JlU1FMIERlYmlhbiBSZXBvc2l0b3J5iQJOBBMBCAA4AhsDBQsJCAcD +BRUKCQgLBRYCAwEAAh4BAheAFiEEuXsK/KoaR/BE8kSgf8x9RqzMTPgFAlhtCD8A +CgkQf8x9RqzMTPgECxAAk8uL+dwveTv6eH21tIHcltt8U3Ofajdo+D/ayO53LiYO +xi27kdHD0zvFMUWXLGxQtWyeqqDRvDagfWglHucIcaLxoxNwL8+e+9hVFIEskQAY +kVToBCKMXTQDLarz8/J030Pmcv3ihbwB+jhnykMuyyNmht4kq0CNgnlcMCdVz0d3 +z/09puryIHJrD+A8y3TD4RM74snQuwc9u5bsckvRtRJKbP3GX5JaFZAqUyZNRJRJ +Tn2OQRBhCpxhlZ2afkAPFIq2aVnEt/Ie6tmeRCzsW3lOxEH2K7MQSfSu/kRz7ELf +Cz3NJHj7rMzC+76Rhsas60t9CjmvMuGONEpctijDWONLCuch3Pdj6XpC+MVxpgBy +2VUdkunb48YhXNW0jgFGM/BFRj+dMQOUbY8PjJjsmVV0joDruWATQG/M4C7O8iU0 +B7o6yVv4m8LDEN9CiR6r7H17m4xZseT3f+0QpMe7iQjz6XxTUFRQxXqzmNnloA1T +7VjwPqIIzkj/u0V8nICG/ktLzp1OsCFatWXh7LbU+hwYl6gsFH/mFDqVxJ3+DKQi +vyf1NatzEwl62foVjGUSpvh3ymtmtUQ4JUkNDsXiRBWczaiGSuzD9Qi0ONdkAX3b +ewqmN4TfE+XIpCPxxHXwGq9Rv1IFjOdCX0iG436GHyTLC1tTUIKF5xV4Y0+cXIOI +RgQQEQgABgUCTpdI7gAKCRDFr3dKWFELWqaPAKD1TtT5c3sZz92Fj97KYmqbNQZP ++ACfSC6+hfvlj4GxmUjp1aepoVTo3weJAhwEEAEIAAYFAk6XSQsACgkQTFprqxLS +p64F8Q//cCcutwrH50UoRFejg0EIZav6LUKejC6kpLeubbEtuaIH3r2zMblPGc4i ++eMQKo/PqyQrceRXeNNlqO6/exHozYi2meudxa6IudhwJIOn1MQykJbNMSC2sGUp +1W5M1N5EYgt4hy+qhlfnD66LR4G+9t5FscTJSy84SdiOuqgCOpQmPkVRm1HX5X1+ +dmnzMOCk5LHHQuiacV0qeGO7JcBCVEIDr+uhU1H2u5GPFNHm5u15n25tOxVivb94 +xg6NDjouECBH7cCVuW79YcExH/0X3/9G45rjdHlKPH1OIUJiiX47OTxdG3dAbB4Q +fnViRJhjehFscFvYWSqXo3pgWqUsEvv9qJac2ZEMSz9x2mj0ekWxuM6/hGWxJdB+ ++985rIelPmc7VRAXOjIxWknrXnPCZAMlPlDLu6+vZ5BhFX0Be3y38f7GNCxFkJzl +hWZ4Cj3WojMj+0DaC1eKTj3rJ7OJlt9S9xnO7OOPEUTGyzgNIDAyCiu8F4huLPaT +ape6RupxOMHZeoCVlqx3ouWctelB2oNXcxxiQ/8y+21aHfD4n/CiIFwDvIQjl7dg +mT3u5Lr6yxuosR3QJx1P6rP5ZrDTP9khT30t+HZCbvs5Pq+v/9m6XDmi+NlU7Zuh +Ehy97tL3uBDgoL4b/5BpFL5U9nruPlQzGq1P9jj40dxAaDAX/WKJAj0EEwEIACcC +GwMFCwkIBwMFFQoJCAsFFgIDAQACHgECF4AFAlB5KywFCQPDFt8ACgkQf8x9RqzM +TPhuCQ//QAjRSAOCQ02qmUAikT+mTB6baOAakkYq6uHbEO7qPZkv4E/M+HPIJ4wd +nBNeSQjfvdNcZBA/x0hr5EMcBneKKPDj4hJ0panOIRQmNSTThQw9OU351gm3YQct +AMPRUu1fTJAL/AuZUQf9ESmhyVtWNlH/56HBfYjE4iVeaRkkNLJyX3vkWdJSMwC/ +LO3Lw/0M3R8itDsm74F8w4xOdSQ52nSRFRh7PunFtREl+QzQ3EA/WB4AIj3VohIG +kWDfPFCzV3cyZQiEnjAe9gG5pHsXHUWQsDFZ12t784JgkGyO5wT26pzTiuApWM3k +/9V+o3HJSgH5hn7wuTi3TelEFwP1fNzI5iUUtZdtxbFOfWMnZAypEhaLmXNkg4zD +kH44r0ss9fR0DAgUav1a25UnbOn4PgIEQy2fgHKHwRpCy20d6oCSlmgyWsR40EPP +YvtGq49A2aK6ibXmdvvFT+Ts8Z+q2SkFpoYFX20mR2nsF0fbt1lfH65P64dukxeR +GteWIeNakDD40bAAOH8+OaoTGVBJ2ACJfLVNM53PEoftavAwUYMrR910qvwYfd/4 +6rh46g1Frr9SFMKYE9uvIJIgDsQB3QBp71houU4H55M5GD8XURYs+bfiQpJG1p7e +B8e5jZx1SagNWc4XwL2FzQ9svrkbg1Y+359buUiP7T6QXX2zY++JAj0EEwEIACcC +GwMFCwkIBwMFFQoJCAsFFgIDAQACHgECF4AFAlEqbZUFCQg2wEEACgkQf8x9RqzM +TPhFMQ//WxAfKMdpSIA9oIC/yPD/dJpY/+DyouOljpE6MucMy/ArBECjFTBwi/j9 +NYM4ynAk34IkhuNexc1i9/05f5RM6+riLCLgAOsADDbHD4miZzoSxiVr6GQ3YXMb +OGld9kV9Sy6mGNjcUov7iFcf5Hy5w3AjPfKuR9zXswyfzIU1YXObiiZT38l55pp/ +BSgvGVQsvbNjsff5CbEKXS7q3xW+WzN0QWF6YsfNVhFjRGj8hKtHvwKcA02wwjLe +LXVTm6915ZUKhZXUFc0vM4Pj4EgNswH8Ojw9AJaKWJIZmLyW+aP+wpu6YwVCicxB +Y59CzBO2pPJDfKFQzUtrErk9irXeuCCLesDyirxJhv8o0JAvmnMAKOLhNFUrSQ2m ++3EnF7zhfz70gHW+EG8X8mL/EN3/dUM09j6TVrjtw43RLxBzwMDeariFF9yC+5bL +tnGgxjsB9Ik6GV5v34/NEEGf1qBiAzFmDVFRZlrNDkq6gmpvGnA5hUWNr+y0i01L +jGyaLSWHYjgw2UEQOqcUtTFK9MNzbZze4mVaHMEz9/aMfX25R6qbiNqCChveIm8m +Yr5Ds2zdZx+G5bAKdzX7nx2IUAxFQJEE94VLSp3npAaTWv3sHr7dR8tSyUJ9poDw +gw4W9BIcnAM7zvFYbLF5FNggg/26njHCCN70sHt8zGxKQINMc6SJAj0EEwEIACcC +GwMFCwkIBwMFFQoJCAsFFgIDAQACHgECF4AFAlLpFRkFCQ6EJy0ACgkQf8x9RqzM +TPjOZA//Zp0e25pcvle7cLc0YuFr9pBv2JIkLzPm83nkcwKmxaWayUIG4Sv6pH6h +m8+S/CHQij/yFCX+o3ngMw2J9HBUvafZ4bnbI0RGJ70GsAwraQ0VlkIfg7GUw3Tz +voGYO42rZTru9S0K/6nFP6D1HUu+U+AsJONLeb6oypQgInfXQExPZyliUnHdipei +4WR1YFW6sjSkZT/5C3J1wkAvPl5lvOVthI9Zs6bZlJLZwusKxU0UM4Btgu1Sf3nn +JcHmzisixwS9PMHE+AgPWIGSec/N27a0KmTTvImV6K6nEjXJey0K2+EYJuIBsYUN +orOGBwDFIhfRk9qGlpgt0KRyguV+AP5qvgry95IrYtrOuE7307SidEbSnvO5ezNe +mE7gT9Z1tM7IMPfmoKph4BfpNoH7aXiQh1Wo+ChdP92hZUtQrY2Nm13cmkxYjQ4Z +gMWfYMC+DA/GooSgZM5i6hYqyyfAuUD9kwRN6BqTbuAUAp+hCWYeN4D88sLYpFh3 +paDYNKJ+Gf7Yyi6gThcV956RUFDH3ys5Dk0vDL9NiWwdebWfRFbzoRM3dyGP889a +OyLzS3mh6nHzZrNGhW73kslSQek8tjKrB+56hXOnb4HaElTZGDvD5wmrrhN94kby +Gtz3cydIohvNO9d90+29h0eGEDYti7j7maHkBKUAwlcPvMg5m3Y= +=DA1T +-----END PGP PUBLIC KEY BLOCK----- pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy