#23397·arangodb

AQL `remove-redundant-calculations` treats `(a ? b : c) ? d : e` and `a ? b : (c ? d : e)` as the same calculation

Author: the-ShallowCreated Oct 1, 2026Updated Oct 1, 2026

Description

When one query computes both a left-nested and a right-nested ternary over the same operands, the optimizer replaces one with the other and returns a wrong value. AstNode::stringify() renders a ternary without parentheses, so both expressions stringify to a?b:c?d:e, and remove-redundant-calculations compares calculations by that string. Each expression is correct on its own.

Reproduction

aql
FOR x IN [{a: 1, b: 0, c: 1, d: "D", e: "E"}]
  LET p = (x.a ? x.b : x.c) ? x.d : x.e
  LET r = x.a ? x.b : (x.c ? x.d : x.e)
  RETURN [p, r]
// [ "E", "E" ]   expected [ "E", 0 ]

With OPTIONS { optimizer: { rules: ["-remove-redundant-calculations"] } } the result is ["E", 0].