Skip to content

Commit 538ac13

Browse files
committed
Add JSON(B) examples for json ->> int and json -> int
1 parent 8672c6e commit 538ac13

File tree

1 file changed

+117
-12
lines changed

1 file changed

+117
-12
lines changed

NATIVE_POSTGRES_JSON_COMPARED_TO_EQL.md

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Native Postgres JSON(B) Compared to EQL
22

3-
EQL supports a subset of functionality supported by the native Postgres JSON(B) functions and operators. The following examples compare natiive Postres JSON(B) functions and operators to the related functionality in EQL.
3+
EQL supports a subset of functionality supported by the native Postgres JSON(B) functions and operators. The following examples compare native Postres JSON(B) functions and operators to the related functionality in EQL.
44

55
## `json ->> text``text` and `json -> text``jsonb`/`json`
66

@@ -74,7 +74,7 @@ WHERE cs_ste_vec_term_v1(examples.encrypted_jsonb, $1) > cs_ste_vec_term_v1($2)
7474
"q": "ejson_path"
7575
}
7676

77-
// `$2` is the EQL plaintext payload for the ORE term to compare against:
77+
// `$2` is the EQL plaintext payload for the ORE term to compare against (in this case, the ORE term for the integer `123`):
7878
{
7979
"k": "pt",
8080
"p": "123",
@@ -87,10 +87,6 @@ WHERE cs_ste_vec_term_v1(examples.encrypted_jsonb, $1) > cs_ste_vec_term_v1($2)
8787
}
8888
```
8989

90-
#### Containment example
91-
92-
TODO: do we want containment examples for these, too?
93-
9490
## `json #>> text[]``text` and `json #> text[]``jsonb`/`json`
9591

9692
### Native Postgres JSON(B)
@@ -169,7 +165,7 @@ WHERE cs_ste_vec_term_v1(examples.encrypted_jsonb, $1) > cs_ste_vec_term_v1($2)
169165
"q": "ejson_path"
170166
}
171167

172-
// `$2` is the EQL plaintext payload for the ORE term to compare against:
168+
// `$2` is the EQL plaintext payload for the ORE term to compare against (in this case, the ORE term for the integer `123`):
173169
{
174170
"k": "pt",
175171
"p": "123",
@@ -182,10 +178,6 @@ WHERE cs_ste_vec_term_v1(examples.encrypted_jsonb, $1) > cs_ste_vec_term_v1($2)
182178
}
183179
```
184180

185-
#### Containment example
186-
187-
TODO: do we want containment examples for these, too?
188-
189181
## `json_array_elements`, `jsonb_array_elements`, `json_array_elements_text`, and `jsonb_array_elements_text`
190182

191183
### Native Postgres JSON(B)
@@ -294,7 +286,7 @@ WHERE EXISTS (
294286
"q": "ejson_path"
295287
}
296288

297-
// `$2` is the EQL plaintext payload for the ORE term to compare against:
289+
// `$2` is the EQL plaintext payload for the ORE term to compare against (in this case, the ORE term for the integer `2`):
298290
{
299291
"k": "pt",
300292
"p": "2",
@@ -306,3 +298,116 @@ WHERE EXISTS (
306298
"q": "ste_vec"
307299
}
308300
```
301+
302+
## `json ->> int``text` and `json -> int``jsonb`/`json`
303+
304+
### Native Postgres JSON(B)
305+
306+
```sql
307+
-- `->` (returns JSON(B))
308+
SELECT plaintext_jsonb->0 FROM examples;
309+
310+
-- `->>` (returns text)
311+
SELECT plaintext_jsonb->>0 FROM examples;
312+
```
313+
314+
### EQL
315+
316+
EQL JSONB functions accept an eJSONPath as an argument (instead of using `->`/`->>`) for lookups.
317+
318+
#### Decryption example
319+
320+
EQL currently doesn't support returning a specific array element for decryption, but `cs_ste_vec_value_v1` can be used to return an array to the client to process.
321+
322+
The query...
323+
324+
```sql
325+
SELECT cs_ste_vec_value_v1(encrypted_jsonb, $1) AS val FROM examples;
326+
```
327+
328+
With the params...
329+
330+
```javascript
331+
// Assume that examples.encrypted_jsonb has JSON objects with
332+
// the shape:
333+
{
334+
"field_a": [1, 2, 3]
335+
}
336+
337+
// `$1` is the EQL plaintext payload for the eJSONPath `$.field_a`:
338+
{
339+
"k": "pt",
340+
"p": "$.field_a",
341+
"i": {
342+
"t": "examples",
343+
"c": "encrypted_jsonb"
344+
},
345+
"v": 1,
346+
"q": "ejson_path"
347+
}
348+
```
349+
350+
Would return the EQL plaintext payload with an array (`[1, 2, 3]` for example):
351+
352+
```javascript
353+
// Example result for a single row
354+
{
355+
"k": "pt",
356+
"p": "[1, 2, 3]",
357+
"i": {
358+
"t": "examples",
359+
"c": "encrypted_jsonb"
360+
},
361+
"v": 1,
362+
"q": null
363+
}
364+
```
365+
366+
#### Comparison example
367+
368+
`cs_ste_vec_terms_v1` can be used with the native Postgres array access operator to get a term for comparison by array index.
369+
370+
The eJSONPath used with `cs_ste_vec_terms_v1` needs to end with `[*]` (`$.some_array_field[*]` for example).
371+
372+
> [!IMPORTANT]
373+
> Array access with `cs_ste_vec_terms_v1` only works when the given eJSONPath only matches a single array.
374+
> Accessing array elements from `cs_ste_vec_terms_v1` when the eJSONPath matches multiple arrays (for example, when there are nested arrays or multiple arrays at the same depth) can return unexpected results.
375+
376+
The following query compares the first item in the array at the eJSONPath in $1 to the value in $2.
377+
378+
```sql
379+
SELECT * FROM examples
380+
WHERE (cs_ste_vec_terms_v1(examples.encrypted_jsonb, $1))[1] > cs_ste_vec_term_v1($2)
381+
```
382+
383+
```javascript
384+
// Assume that examples.encrypted_jsonb has JSON objects with
385+
// the shape:
386+
{
387+
"field_a": [4, 5, 6]
388+
}
389+
390+
// `$1` is the EQL plaintext payload for the eJSONPath `$.field_a[*]`:
391+
{
392+
"k": "pt",
393+
"p": "$.field_a[*]",
394+
"i": {
395+
"t": "examples",
396+
"c": "encrypted_jsonb"
397+
},
398+
"v": 1,
399+
"q": "ejson_path"
400+
}
401+
402+
// `$2` is the EQL plaintext payload for the ORE term to compare against (in this case, the ORE term for the integer `3`):
403+
{
404+
"k": "pt",
405+
"p": "3",
406+
"i": {
407+
"t": "examples",
408+
"c": "encrypted_jsonb"
409+
},
410+
"v": 1,
411+
"q": "ste_vec"
412+
}
413+
```

0 commit comments

Comments
 (0)