@@ -84,28 +84,27 @@ Simple expression is specified as `path binary_operator value` or
84
84
than 5;
85
85
* ` similar_product_ids.# = "0684824396" ` – array "similar\_ product\_ ids"
86
86
contains string "0684824396".
87
- * ` *.color = "red" ` – there is object somewhere which key "color" has value
88
- "red".
87
+ * ` *.color = "red" ` – there is object somewhere which key "color" has value "red".
89
88
* ` foo = * ` – key "foo" exists in object.
90
89
91
- Path selects set of JSON values to be checked using given operators. In
92
- the simplest case path is justan key name. In general path is key names and
93
- placeholders combined by dot signs. Path can use following placeholders:
90
+ Path selectsa set of JSON values to be checked using given operators. In
91
+ the simplest case path is justa key name. In general path is key names and
92
+ placeholders combined by dot signs. Path can usethe following placeholders:
94
93
95
- * ` # ` – any index of array;
96
- * ` #N ` – N-th index of array;
97
- * ` % ` – any key of object;
94
+ * ` # ` – any index ofan array;
95
+ * ` #N ` – N-th index ofan array;
96
+ * ` % ` – any key ofan object;
98
97
* ` * ` – any sequence of array indexes and object keys;
99
- * ` @# ` – length of array or object,could be only used as last component of
100
- path;
101
- * ` $ ` – the whole JSON document as single value,could be only the whole path.
98
+ * ` @# ` – length of array or object,may onlybe used as the last component of
99
+ a path;
100
+ * ` $ ` – the whole JSON document as single value,may only be the whole path.
102
101
103
102
Expression is true when operator is true against at least one value selected
104
103
by path.
105
104
106
105
Key names could be given either with or without double quotes. Key names
107
- without double quotesshouldn't contain spaces, start with number orconcur
108
- with jsquery keyword.
106
+ without double quotesmay not contain spaces, start witha number ormatch
107
+ a jsquery keyword.
109
108
110
109
The supported binary operators are:
111
110
@@ -121,78 +120,77 @@ The supported unary operators are:
121
120
* Check for type operators:` IS ARRAY ` ,` IS NUMERIC ` ,` IS OBJECT ` ,` IS STRING `
122
121
and` IS BOOLEAN ` .
123
122
124
- Expressionscould be complex. Complex expression is a set of expressions
123
+ Expressionscan be complex. Complex expression is a set of expressions
125
124
combined by logical operators (` AND ` ,` OR ` ,` NOT ` ) and grouped using braces.
126
125
127
- Examples of complex expressions are given below.
126
+ Examples of complex expressions:
128
127
129
128
* ` a = 1 AND (b = 2 OR c = 3) AND NOT d = 1 `
130
129
* ` x.% = true OR x.# = true `
131
130
132
- Prefix expressions are expressions given in the form path (subexpression).
133
- In this case path selects JSON values to be checked using given subexpression.
131
+ Prefix expressions are expressions given in the form` path (subexpression) ` .
132
+ In this case path selects JSON values to be checked usingthe given subexpression.
134
133
Check results are aggregated in the same way as in simple expressions.
135
134
136
135
* ` #(a = 1 AND b = 2) ` – exists element of array which a key is 1 and b key is 2
137
136
* ` %($ >= 10 AND $ <= 20) ` – exists object key which values is between 10 and 20
138
137
139
- Path alsocould contain following special placeholders with "every" semantics:
138
+ Pathcan also contain the following special placeholders with "every" semantics:
140
139
141
- * ` #: ` – everyindexes of array;
142
- * ` %: ` – every key of object;
140
+ * ` #: ` – everyindex of an array;
141
+ * ` %: ` – every key ofan object;
143
142
* ` *: ` – every sequence of array indexes and object keys.
144
143
145
144
Consider following example.
146
145
147
146
%.#:($ >= 0 AND $ <= 1)
148
147
149
- This example could be read as following: there is at least one keywhich value
150
- is array of numerics between 0 and 1.
148
+ This example could be read as following: there is at least one keywhose value
149
+ isan array of numerics between 0 and 1.
151
150
152
- We can rewrite this example in the following form with extra braces.
151
+ We can rewrite this example in the following form with extra braces:
153
152
154
153
%(#:($ >= 0 AND $ <= 1))
155
154
156
- The first placeholder` % ` checks that expression in braces is true for at least
157
- one value in object. The second placeholder` #: ` checks valueto be array and
158
- all its elements satisfy expressions in braces.
155
+ The first placeholder` % ` checks thatthe expression in braces is true for at least
156
+ one value inthe object. The second placeholder` #: ` checksif the valueis an array
157
+ and that all its elements satisfy the expressions in braces.
159
158
160
- We can rewrite this example without` #: ` placeholder as follows.
159
+ We can rewrite this example withoutthe ` #: ` placeholder as follows:
161
160
162
161
%(NOT #(NOT ($ >= 0 AND $ <= 1)) AND $ IS ARRAY)
163
162
164
- In this example we transform assertion that every element of array satisfy some
165
- condition to assertion that thereis noone element whichdoesn 't satisfy the
166
- same condition.
163
+ In this example we transformthe assertion that every element of array satisfy some
164
+ condition toan assertion that thereare noelements whichdon 't satisfy the same
165
+ condition.
167
166
168
- Some examples of using paths are given below.
167
+ Some examples of using paths:
169
168
170
169
* ` numbers.#: IS NUMERIC ` – every element of "numbers" array is numeric.
171
170
* ` *:($ IS OBJECT OR $ IS BOOLEAN) ` – JSON is a structure of nested objects
172
171
with booleans as leaf values.
173
- * ` #:.%:($ >= 0 AND $ <= 1) ` – each element of array is object containing
172
+ * ` #:.%:($ >= 0 AND $ <= 1) ` – each element of array isan object containing
174
173
only numeric values between 0 and 1.
175
- * ` documents.#:.% = * ` – "documents" is array of objects containing at least
174
+ * ` documents.#:.% = * ` – "documents" isan array of objects containing at least
176
175
one key.
177
176
* ` %.#: ($ IS STRING) ` – JSON object contains at least one array of strings.
178
- * ` #.% = true ` – at least one array element isobjects which contains at least
177
+ * ` #.% = true ` – at least one array element isan object which contains at least
179
178
one "true" value.
180
179
181
- Usage of path operators and braces need some explanation. When same path
182
- operators are used multiple times they may refer different values while you can
183
- refer same value multiple time by using braces and` $ ` operator. See following
184
- examples.
180
+ The use of path operators and braces need some further explanation. When the same path
181
+ operators are used multiple times, they may refer to different values. If you want them
182
+ to always refer to the same value, you must use braces and the` $ ` operator. For example:
185
183
186
- * ` # < 10 AND # > 20 ` –exists element less than 10and exists another element
187
- greater than 20.
188
- * ` #($ < 10 AND $ > 20) ` –exists element which both less than 10 and greater
189
- than 20 (impossible).
190
- * ` #($ >= 10 AND $ <= 20) ` –exists element between 10 and 20.
191
- * ` # >= 10 AND # <= 20 ` –exists elementgreat or equal to 10 andexists
192
- another element less or equal to 20. Query can be satisfied by array with
193
- no elements between 10 and 20, for instance[ 0,30] .
184
+ * ` # < 10 AND # > 20 ` –an element less than 10 exists, and another element
185
+ greater than 20 exists .
186
+ * ` #($ < 10 AND $ > 20) ` –an element which is both less than 10 and greater
187
+ than 20exists (impossible).
188
+ * ` #($ >= 10 AND $ <= 20) ` –an element between 10 and 20 exists .
189
+ * ` # >= 10 AND # <= 20 ` –an elementgreater or equal to 10exists, andanother
190
+ element less or equal to 20 exists. Please note that this query also can be
191
+ satisfied by an array with no elements between 10 and 20, for instance[ 0,30] .
194
192
195
- Same rules apply whenyou search inside objects andbranchy structures.
193
+ Same rules apply whensearching inside objects andbranch structures.
196
194
197
195
Type checking operators and "every" placeholders are useful for document
198
196
schema validation. JsQuery matchig operator` @@ ` is immutable and can be used
@@ -208,9 +206,9 @@ CREATE TABLE js (
208
206
points.#:(x IS NUMERIC AND y IS NUMERIC)' ::jsquery));
209
207
```
210
208
211
- In this example check constraint validates that in "data" jsonb column:
212
- value of "name" key is string, value of "similar_ids" key is array of numerics,
213
- value of "points" key is array of objects which contain numeric values in
209
+ In this examplethe check constraint validates that inthe "data" jsonb column
210
+ the value ofthe "name" key isa string,the value ofthe "similar_ids" key is an array of numerics,
211
+ and the value ofthe "points" key is an array of objects which contain numeric values in
214
212
"x" and "y" keys.
215
213
216
214
See our
@@ -227,11 +225,11 @@ provide different kinds of query optimization.
227
225
* jsonb\_ value\_ path\_ ops
228
226
229
227
In each of two GIN opclasses jsonb documents are decomposed into entries. Each
230
- entry is associated with particular value andit's path.Difference between
228
+ entry is associated witha particular value andits path.The difference between
231
229
opclasses is in the entry representation, comparison and usage for search
232
230
optimization.
233
231
234
- For example, jsonb document
232
+ For example,the jsonb document
235
233
` {"a": [{"b": "xyz", "c": true}, 10], "d": {"e": [7, false]}} `
236
234
would be decomposed into following entries:
237
235
@@ -241,57 +239,57 @@ would be decomposed into following entries:
241
239
* "d"."e".#.7
242
240
* "d"."e".#.false
243
241
244
- Since JsQuery doesn't supportsearch in particular array index, we consider
242
+ Since JsQuery doesn't supportsearching in a particular array index, we consider
245
243
all array elements to be equivalent. Thus, each array element is marked with
246
- same` # ` sign inthe path.
244
+ the same` # ` sign inits path.
247
245
248
246
Major problem in the entries representation is its size. In the given example
249
- key "a" is presented three times. In the large branchy documents with long
250
- keyssize of naive entries representation becomesunreasonable. Both opclasses
251
- address this issue but ina slightly differentway .
247
+ the key "a" is presented three times. In large branchy documents with long
248
+ keyssizes of naive entries, the representation becomesunreasonably large.
249
+ Both opclasses address this issue, but in slightly differentways .
252
250
253
251
###jsonb\_ path\_ value\_ ops
254
252
255
253
jsonb\_ path\_ value\_ ops represents entry as pair of path hash and value.
256
- Following pseudocode illustrates it.
254
+ Following pseudocode illustrates it:
257
255
258
256
(hash(path_item_1.path_item_2. ... .path_item_n); value)
259
257
260
- In comparisonof entries path hash is the higher part of entry and value is
261
- its lower part. This determines the features of this opclass. Since path
262
- is hashed and it is higher part of entry we need to know the full path to
263
- the value in order to use it forsearch . However, once path is specified
258
+ When comparison entries, the path hash is the higher part of entry and the value is
259
+ the lower part. This determines the features of this opclass. Since the path
260
+ is hashed and it's the higher part ofthe entry, we need to know the full path to
261
+ a value in order to usethe it forsearching . However, once the path is specified
264
262
we can use both exact and range searches very efficiently.
265
263
266
264
###jsonb\_ value\_ path\_ ops
267
265
268
- jsonb\_ value\_ path\_ ops represents entry as pair of value and bloom filter
269
- ofpath.
266
+ jsonb\_ value\_ path\_ ops represents entry as pair ofthe value and a bloom filter
267
+ ofpaths:
270
268
271
269
(value; bloom(path_item_1) | bloom(path_item_2) | ... | bloom(path_item_n))
272
270
273
271
In comparison of entries value is the higher part of entry and bloom filter of
274
272
path is its lower part. This determines the features of this opclass. Since
275
- value is the higher part of entry we canperform only exact value search
276
- efficiently. Range value search is possible as well but we would have to
277
- filter all the the different paths where matching values occur. Bloom filter
278
- over path items allows indexusage for conditions containing` % ` and` * ` in
273
+ the value is the higher part ofan entry, we can only perform exact value search
274
+ effectively. A searchover a range of values is possible as well, but we have to
275
+ filter all the the different paths where matching values occur.The Bloom filter
276
+ over path items allowsthe indexto be used for conditions containing` % ` and` * ` in
279
277
their paths.
280
278
281
279
###Query optimization
282
280
283
- JsQuery opclasses perform complex query optimization.Thus it 's valuable for
281
+ JsQuery opclasses perform complex query optimization.It 's valuable for a
284
282
developer or administrator to see the result of such optimization.
285
- Unfortunately, opclasses aren't allowed todo any custom outputto the
286
- EXPLAIN. That's why JsQuery providesfollowing functionswhich allows to see
287
- how particular opclass optimizes given query.
283
+ Unfortunately, opclasses aren't allowed toput any custom outputin an
284
+ EXPLAIN. That's why JsQuery providesthese functionsto let you see
285
+ how particular opclass optimizes given query:
288
286
289
287
* gin\_ debug\_ query\_ path\_ value(jsquery) – for jsonb\_ path\_ value\_ ops
290
288
* gin\_ debug\_ query\_ value\_ path(jsquery) – for jsonb\_ value\_ path\_ ops
291
289
292
- Result of these functions is a textual representation of query tree which
293
- leafs are GIN search entries. Following examples show different results of
294
- query optimization by different opclasses.
290
+ The result of these functions is a textual representation ofthe query tree
291
+ where leaves are GIN search entries. Following examples show different results of
292
+ query optimization by different opclasses:
295
293
296
294
# SELECT gin_debug_query_path_value('x = 1 AND (*.y = 1 OR y = 2)');
297
295
gin_debug_query_path_value
@@ -309,29 +307,29 @@ query optimization by different opclasses.
309
307
310
308
Unfortunately, jsonb have no statistics yet. That's why JsQuery optimizer has
311
309
to do imperative decision while selecting conditions to be evaluated using
312
- index. This decision is made byassumtion that some condition types are less
313
- selective than others.Optimizer divides conditions into following selectivity
314
- class (listedby descending of selectivity).
310
+ index. This decision is made byassuming that some condition types are less
311
+ selective than others.The optimizer divides conditions into following selectivity
312
+ classes (listedin descendingorder of selectivity):
315
313
316
314
1 . Equality (x = c)
317
315
2 . Range (c1 < x < c2)
318
316
3 . Inequality (x > c)
319
317
4 . Is (x is type)
320
318
5 . Any (x =\* )
321
319
322
- Optimizer evades index evaluation of less selective conditions when possible.
320
+ The optimizer avoids index evaluation of less selective conditions when possible.
323
321
For example, in the` x = 1 AND y > 0 ` query` x = 1 ` is assumed to be more
324
- selective than` y > 0 ` . That's why index isn't used for evaluation of` y > 0 ` .
322
+ selective than` y > 0 ` . That's whythe index isn't used for evaluation of` y > 0 ` .
325
323
326
324
# SELECT gin_debug_query_path_value('x = 1 AND y > 0');
327
325
gin_debug_query_path_value
328
326
----------------------------
329
327
x = 1 , entry 0 +
330
328
331
- With lack of statistics decisions made by optimizer can be inaccurate. That's
332
- why JsQuery supports hints.Comments ` /*-- index */ ` and ` /*-- noindex */ `
333
- placed in the conditionsforces optimizer to useand not use index
334
- correspondingly.
329
+ Withthe lack of statistics, decisions made by optimizer can be inaccurate. That's
330
+ why JsQuery supports hints.The comments ` /*-- index */ ` or ` /*-- noindex */ `
331
+ placed in the conditionsforce the optimizer to useor not use an index
332
+ correspondingly:
335
333
336
334
SELECT gin_debug_query_path_value('x = 1 AND y /*-- index */ > 0');
337
335
gin_debug_query_path_value
@@ -348,11 +346,11 @@ correspondingly.
348
346
Contribution
349
347
------------
350
348
351
- Please, notice, that JsQuery is still under development and while it's
352
- stable and tested, it maycontains some bugs. Don't hesitate toraise
349
+ Please note that JsQuery is still under development. While it's
350
+ stable and tested, it maycontain some bugs. Don't hesitate tocreate
353
351
[ issues at github] ( https://github.com/postgrespro/jsquery/issues ) with your
354
352
bug reports.
355
353
356
- If you're lacking of some functionality in JsQuery andfeeling power to
357
- implement it then you're welcome to make pull requests.
354
+ Ifthere's some functionality you'd like to see added to JsQuery andyou feel
355
+ like you can implement it, then you're welcome to make pull requests.
358
356