|
| 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