moodle/moodle · error · RuntimeException

Invalid comparison: $cmp

Error message

Invalid comparison: $cmp

What it means

TreeInterpreter::relativeCmp implements the JMESPath ordering operators >, >=, <, <=. If any other comparison operator string reaches it, it throws RuntimeException 'Invalid comparison: $cmp'. Since the parser only emits valid operators, this indicates a corrupted/foreign AST rather than user input error.

Solutions

  1. Re-parse the expression with the current library so operators are encoded correctly
  2. Remove or rewrite the malformed comparison node in any hand-built AST (use '==' in expression text, which the parser routes to the equality node)
  3. Check that '=' or '!=' did not end up in the operator field; equality is a separate node type
  4. Align library versions if the AST came from a cache produced by another jmespath.php release

Example fix

// before: hand-built comparison node
['type' => 'comparator', 'value' => '==', 'children' => [...]] // dispatched to relativeCmp
// after: let the parser build it
$ast = (new Parser())->parse('a >= `1`'); // '==' stays in the equality node, not relativeCmp
Defensive patterns

Strategy: validation

Validate before calling

if (isset($node['value']) && !in_array($node['value'], ['>', '>=', '<', '<='], true)) {
    throw new \InvalidArgumentException('Unsupported comparator in AST');
}

Type guard

function isSupportedComparator($op): bool {
    return in_array($op, ['>', '>=', '<', '<='], true);
}

Try / catch

try {
    $result = \JmesPath\search($expr, $data);
} catch (\RuntimeException $e) {
    if (str_starts_with($e->getMessage(), 'Invalid comparison')) {
        throw new \InvalidArgumentException("Bad JMESPath expression: $expr", 0, $e);
    } else { throw $e; }
}

Prevention

When it happens

Trigger: Evaluating an AST comparison node whose operator value is not one of '>', '>=', '<', '<=' — e.g. '=' or '!=' (equality is handled elsewhere) injected via a hand-built or deserialized-from-another-version AST.

Common situations: Users porting filter expressions with '==' semantics expecting it to hit relativeCmp, custom AST mutation, or ASTs cached across library versions where operator encoding changed.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/393eb02e7ca3eff1. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/jmespath/src/TreeInterpreter.php:232

                throw new \RuntimeException("Unknown node type: {$node['type']}");
        }
    }

    /**
     * @return bool
     */
    private static function relativeCmp($left, $right, $cmp)
    {
        if (!(is_int($left) || is_float($left)) || !(is_int($right) || is_float($right))) {
            return false;
        }

        switch ($cmp) {
            case '>': return $left > $right;
            case '>=': return $left >= $right;
            case '<': return $left < $right;
            case '<=': return $left <= $right;
            default: throw new \RuntimeException("Invalid comparison: $cmp");
        }
    }
}

View on GitHub (pinned to 48ea8c3322)