Skip to content

Commit 5174701

Browse files
authored
Standardize on pipe-expression handling of null values. (#114)
* Standardize on pipe-expression handling of `null` values. * [jep-019] Evaluation of Pipe Expressions
1 parent 4f3ef02 commit 5174701

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

GRAMMAR.ABNF

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,20 @@ sub-expression = expression "." ( identifier / multi-select-list / multi-sele
8585
;; ```
8686

8787
pipe-expression = expression "|" expression ;; # Pipe Expressions
88-
;; A pipe expression combines two expressions, separated by the `|` character. It is similar to a sub-expression with two
89-
;; important distinctions:
88+
;; A pipe expression combines two expressions, separated by the `|` character.
89+
;; It is similar to a sub-expression with a few important distinctions:
9090
;;
9191
;; 1. Any expression can be used on the right hand side. A sub-expression restricts the type of expression that can be
9292
;; used on the right hand side.
9393
;; 1. A pipe-expression stops projections on the left hand side for propagating to the right hand side. If the left
9494
;; expression creates a projection, it does not apply to the right hand side.
95+
;; 1. Contrary to a sub-expression, a pipe-expression **does not stop evaluation** if the left-hand-side evaluates to `null`.
96+
;;
97+
;; In pseudocode:
98+
;; ```
99+
;; left-evaluation = search(left-expression, original-json-document)
100+
;; result = search(right-expression, left-evaluation)
101+
;; ```
95102
;;
96103
;; For example, given the following data:
97104
;;

jep-019-pipe-expressions-evaluation

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Evaluation of Pipe Expressions
2+
3+
|||
4+
|---|---
5+
| **JEP** | 19
6+
| **Author** | Maxime Labelle
7+
| **Created**| 29-October-2022
8+
| **SemVer** | MINOR
9+
| **Status**| Draft
10+
11+
## Abstract
12+
13+
This JEP introduces changes that outline the exact intended behaviour of evaluating a `pipe-expression`. It also clarifies an ambiguity when evaluating a `sub-expression` where the left-hand-side evaluates to `null`.
14+
15+
## Motivation
16+
17+
### Sub Expression Evaluation
18+
19+
The specification for [`sub-expression`](https://jmespath.org/specification.html#subexpressions) outlines how it should be evaluated using pseudocode:
20+
21+
```
22+
left-evaluation = search(left-expression, original-json-document)
23+
result = search(right-expression, left-evaluation)
24+
```
25+
26+
This is slightly ambiguous, as many compliance tests expect the result to be `null` when the left-hand-side evaluates to  `null`. So, the real pseudocode shoud in fact be:
27+
28+
```
29+
left-evaluation = search(left-expression, original-json-document)
30+
if left-evaluation is `null` then result = `null`
31+
else result = search(right-expression, left-evaluation)
32+
```
33+
34+
### Pipe Expression Evaluation
35+
36+
In contrast, however, it seems intuitive for `pipe-expression` to evaluate as was originally outlined by the first pseudocode fragment referred to a above.
37+
38+
```
39+
left-evaluation = search(left-expression, original-json-document)
40+
result = search(right-expression, left-evaluation)
41+
```
42+
43+
Which means that the evaluation should still happens if the left-hand-side is `null`.
44+
45+
## Specification
46+
47+
### Sub Expressions
48+
49+
The paragraph on sub expressions in the specification will be updated as follows, with changes outlined in **bold**:
50+
51+
_A subexpression is evaluted as follows:_
52+
53+
- _Evaluate the expression on the left with the original JSON document._
54+
- _**If the result of the left expression evaluation is `null`, return `null`.**_
55+
- _**Otherwise, e**~~E~~valuate the expression on the right with the result of the left expression evaluation._
56+
57+
_In pseudocode:_
58+
59+
```
60+
left-evaluation = search(left-expression, original-json-document)
61+
If result is `null` the result = `null`
62+
else result = search(right-expression, left-evaluation)
63+
```
64+
65+
### Pipe Expressions
66+
67+
Likewise the paragraph on pipe expressions in the specification will be updated as follows:
68+
69+
_A pipe expression combines two expressions, separated by the | character. It is similar to a sub-expression with ~~two~~**the following** important distinctions:_
70+
71+
1. _Any expression can be used on the right hand side. A `sub-expression` restricts the type of expression that can be used on the right hand side._
72+
2. _A `pipe-expression` stops projections on the left hand side ~~for~~ **from** propagating to the right hand side. If the left expression creates a projection, it does not apply to the right hand side._
73+
3. _**A `pipe-expression` evaluates the right expression unconditionally. A `sub-expression` shortcuts evaluation if the result of the left expression evaluation is `null`**_
74+
75+
_**In pseudocode:**_
76+
77+
```
78+
left-evaluation = search(left-expression, original-json-document)
79+
result = search(right-expression, left-evaluation)
80+
```
81+
82+
### Example
83+
84+
| Expression | Result
85+
|---|----
86+
| `` `null` . [@] `` | `null`
87+
| `` `null` `| [@] `` | `[ null ]`
88+
89+
## Compliance
90+
91+
The `pipe.json` compliance test file will be updated with the example from this JEP.

0 commit comments

Comments
 (0)