Skip to content

Select.json(JSONObject) silently re-associates a nested WHERE, so a = 2 and (b = 1 or b = 3) runs as (a = 2 and b = 1) or b = 3 #8167

Closed as duplicate

Description

@ruispereira

Summary

Select.json(JSONObject) does not build the tree the JSON describes. parseJsonCondition recurses into a
nested right operand and replays its conditions through setLogic, which re-applies operator precedence and
throws the JSON's own grouping away. A caller-supplied parenthesised where-tree therefore runs as a different
predicate, silently and with no error: a = 2 and (b = 1 or b = 3) executes as (a = 2 and b = 1) or b = 3.

The JSON array is the only way to hand the native Select API an explicitly grouped condition (the fluent
builder appends leaves left to right and has no grouping call), so this is the one input shape that can express
the grouping and the one that silently loses it.

Where (HEAD 9a88be7)

engine/src/main/java/com/arcadedb/query/select/Select.java:243-295. Both operands are recursed into with the
same call the top level uses, so a nested array's leaves are appended to the SAME builder state as their
parent's:

  private void parseJsonCondition(final JSONArray condition) {
    ...
    final Object parsedLeft = condition.get(0);
    if (parsedLeft instanceof JSONArray array)
      parseJsonCondition(array);
    ...
    if (parsedOperator.logicOperator)
      setLogic(parsedOperator);
    else
      setOperator(parsedOperator);

    final Object parsedRight = condition.get(2);
    if (parsedRight instanceof JSONArray array)
      parseJsonCondition(array);

setLogic is the fluent builder's precedence machine, Select.java:348-392. It decides where a condition
lands from precedence alone:

      } else if (lastTreeElement.getParent().operator.precedence < newLogicOperator.precedence) {
        // AND+ OPERATOR
        final SelectTreeNode newNode = new SelectTreeNode(newTreeElement, newLogicOperator, null);
        lastTreeElement.getParent().setRight(newNode);
        lastTreeElement = newTreeElement;
      } else {
        // OR+ OPERATOR: THE NEW OPERATOR DOES NOT BIND MORE TIGHTLY THAN THE ONE ALREADY THERE, SO THE CURRENT
        // SUBTREE IS PUSHED DOWN AND BECOMES THE LEFT CHILD OF A NODE CARRYING THE NEW OPERATOR

so an or arriving from inside a nested array is indistinguishable from an or typed at the top level: it
pushes the whole tree built so far down as its left child. The grouping the caller wrote is gone before the
first row is read.

Repro

Native Select API against 12 documents, a in 0..2 crossed with b in 0..3, engine built at this HEAD. The
compiled tree is read back with SelectCompiled.json() and each predicate is also run through SQL, both as
written and as re-associated:

  dataset: 12 documents, a in 0..2 x b in 0..3

  json in   : [[":a","=",2],"and",[[":b","=",1],"or",[":b","=",3]]]
  compiled  : [[[":a","=",2],"and",[":b","=",1]],"or",[":b","=",3]]
  native rows = 4   | sql 'a = 2 and (b = 1 or b = 3)' = 2   | sql '(a = 2 and b = 1) or b = 3' = 4

  json in   : [[":a","=",1],"or",[[":a","=",2],"and",[[":b","=",1],"or",[":b","=",3]]]]
  compiled  : [[":a","=",1],"or",[[[":a","=",2],"and",[":b","=",1]],"or",[":b","=",3]]]
  native rows = 7   | sql 'a = 1 or (a = 2 and (b = 1 or b = 3))' = 6   | sql '(a = 1 or (a = 2 and b = 1)) or b = 3' = 7

  json in   : [[":a","=",1],"or",[[":b","=",1],"or",[":a","=",2]]]
  compiled  : [[[":a","=",1],"or",[":b","=",1]],"or",[":a","=",2]]
  native rows = 9   | sql 'a = 1 or (b = 1 or a = 2)' = 9   | sql '(a = 1 or b = 1) or a = 2' = 9

The native answer matches the re-associated predicate on every line, never the one the JSON spells. No
exception and no warning is raised. The third shape gives the same count either way only because or is
associative, so a nested or under an or is the one case where the re-association is harmless.

Two consequences beyond the row counts:

  • SelectCompiled.json() and Select.json(JSONObject) are not inverse for a nested tree: feeding the first
    two compiled lines back in reproduces them, but the original input is not recoverable, and the second
    round trip of any grouped tree is not the select the caller wrote. That is the invariant Select.json(JSONObject) always throws ArrayStoreException for a fromBuckets select, so the JSON round-trip is broken #6817 established
    for this pair.
  • The comment on Issue8048SelectIndexCursorUnderNestedOrTest.rightNestedOrSiblingIsOnlyIndexedWhenBothOfItsSidesAre
    (Issue8048SelectIndexCursorUnderNestedOrTest.java:112, "THE FLUENT BUILDER CANNOT PRODUCE A RIGHT-NESTED
    or, BUT Select.json() CAN") describes something that does not happen: that JSON compiles to
    (a = 1 or b = 1) or a = 2, the left-nested shape, as the third line above shows. The test itself stays
    valid, since it still exercises the || to && change through the left-nested sibling, but it is not
    testing the tree its comment names.

Suggested fix

Parse a nested array into its own subtree and attach it as a single operand, rather than replaying its leaves
through setLogic. Concretely, give parseJsonCondition a return value (the root of the subtree it parsed)
and have the caller graft it in place of the operand, so a nested array is opaque to the precedence rules,
which is what the parentheses in the equivalent SQL do.

Scope / confidence

Reproduced, output above. Pre-existing, and not from the #8047 / #8048 / #8094 work in this window: those
changes touched the operator-null and not refusals and the grandparent read in setLogic, while the shapes
above carry three conditions and never reach the branch #8047 fixed. Affects any caller of
Select.json(JSONObject) whose where nests an array on the right of a logic operator with different
precedence; a flat or left-associated tree is unaffected, which is why SelectCompiled.json() output round
trips.

Activity

  1. added
    engine
    severity:major
    on Sep 22, 2026
  2. added this to the 26.10.1 milestone on Sep 22, 2026
  3. github-actions commented on Sep 22, 2026

    @github-actions
    Contributor

    Merged into #8180, which now tracks this together with the other related issues in engine. Closing here as a duplicate - reopen this issue if the grouping is wrong.

  4. added 3 commits that reference this issue on Sep 22, 2026
  5. lvca commented on Sep 22, 2026

    @lvca
    Member

    Fixed in #8198, released in 26.10.1, tracked under #8180.

    A nested condition passed to Select.json(JSONObject) was re-flattened through the builder's operator-precedence rules, so the grouping was discarded: a = 2 and (b = 1 or b = 3) ran as (a = 2 and b = 1) or b = 3, returning different rows with no error. A nested array is now parsed as its own subtree - the same thing parentheses do in SQL - so the native answer matches the equivalent SQL query.

    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugengineseverity:major

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions