AQL ternary operator evaluates an untaken operand when it contains an array expansion with a subquery
Component, Query & Data
Affected feature: AQL, ternary operator
The documentation of the ternary operator states that "you may use subqueries as operands" and that "from
v3.12.1 onward, short-circuiting is applied", so the operand the condition does not select must not be
evaluated. That holds for most operand shapes, but not when the operand contains an array expansion
([* FILTER ...], [* RETURN ...], [* LIMIT ...]) whose inner expression is a subquery. The subquery is
then evaluated whichever branch is taken, and a query that should return "ok" fails instead.
AQL query that reproduces the problem:
RETURN NOOPT(true) ? 'ok' : [1, 2][* FILTER (FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))[0]]Dataset: none, the queries are self-contained.
Size of your Dataset on disk: n/a
Replication Factor & Number of Shards: n/a
Steps to reproduce:
- Start a single server, any of 3.12.4 to 3.12.12.
- Run the query above. It returns HTTP 500,
AQL: boom [node #5: CalculationNode] .... - Run the same query with an operand that is a plain subquery instead:
RETURN NOOPT(true) ? 'ok' : (FOR _y IN 1..1 RETURN ASSERT(false, 'boom')). It returns["ok"], as short-circuiting requires.
Problem:
The array-expansion operand is evaluated although its branch is not selected. All three positions are affected, and all three expansion keywords:
| Operand | untaken false branch | untaken true branch | shortcut x ? : y |
|---|---|---|---|
(FOR _y IN 1..1 RETURN ASSERT(false, 'boom')) |
["ok"] |
["ok"] |
["ok"] |
[(FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))] |
["ok"] |
["ok"] |
["ok"] |
{v: (FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))} |
["ok"] |
["ok"] |
["ok"] |
LENGTH((FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))) |
["ok"] |
["ok"] |
["ok"] |
(FOR _y IN 1..1 RETURN (FOR _z IN 1..1 RETURN ASSERT(false, 'boom'))) |
["ok"] |
["ok"] |
["ok"] |
[1, 2][* FILTER (FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))[0]] |
500 AQL: boom |
500 | 500 |
[1, 2][* RETURN (FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))[0]] |
500 | 500 | 500 |
[1, 2][* LIMIT (FOR _y IN 1..1 RETURN ASSERT(false, 'boom'))[0]] |
500 | 500 | 500 |
The error message names a CalculationNode, which suggests the subquery of the expansion is hoisted out of
the conditional expression during planning and so is no longer under the ternary's control.
Expected result: ["ok"], as for the other five operand shapes.
Actual result: HTTP 500, AQL: boom [node #5: CalculationNode] [node #10: LimitNode] [node #12: ...].
ASSERT makes the evaluation visible; the same hoisting means an expensive or side-effecting subquery in an
untaken operand is paid for as well.
Related reports
#12490, #14463, and #21449 cover the broader behavior before subquery short-circuiting was introduced in 3.12.1. Those reports were closed after that change. Plain subqueries now short-circuit correctly, as the control cases above show, but the array-expansion forms still fail on 3.12.12. This appears to be an expression shape that the 3.12.1 fix does not cover.
Source: arangodb/arangodb