Skip to content

Evaluate set operations left to right with INTERSECT binding tighter, with a dialect keeping the old order #202

Open

Description

@agershun

What happens today

After #2712 the parser nests a chain of UNION, UNION ALL, INTERSECT and EXCEPT to the right, so A op1 B op2 C runs as A op1 (B op2 C).

const e1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}], e2 = [{a: 1}, {a: 5}], e3 = [{a: 3}, {a: 5}];
alasql('SELECT a FROM ? EXCEPT SELECT a FROM ? EXCEPT SELECT a FROM ?', [e1, e2, e3]);
// [{a: 2}, {a: 3}, {a: 4}]    SQL gives [{a: 2}, {a: 4}]

What to change

  1. In the grammar rules Body and Term in src/alasqlparser.jison, build UNION and EXCEPT chains to the left and give INTERSECT its own, tighter level.

    Body := Term { (UNION | EXCEPT) [ALL] Term }      -- left-associative
    Term := Primary { INTERSECT [ALL] Primary }
    
  2. Add the rule setOperationOrder to Rules that differ between dialects become named rules chosen when a statement is compiled #3170 with the values standard (the new default) and v4. With v4 the parser action builds today's right-nested tree. Keep the choice in the parser action, so the evaluation code of Compare the rows of set operations and DISTINCT with one key function #3167 does not change.

  3. Add setOperationOrder: 'v4' to alasql/dialect/v4.

  4. Regenerate src/alasqlparser.js with the pinned jison.

What stays the same

  • Queries with one set operation, chains of only UNION or only UNION ALL, and parenthesised groups.
  • With alasql/dialect/v4 every chain gives the 4.x result.
  • In the default, chains of two or more different operators change.

Done when

  • A new test file named after this issue has these cases, with e4 = {2, 5}, and the same queries with alasql/dialect/v4 giving the 4.x rows.

    Query Today Expected
    e1 UNION e2 EXCEPT e3 {1,2,3,4} {1,2,4}
    e1 EXCEPT e2 UNION e3 {2,4} {2,3,4,5}
    e2 UNION ALL e3 EXCEPT e4 {1,3,5} {1,3}
    e1 EXCEPT e2 EXCEPT e3 EXCEPT e4 {2,3,4} {4}
  • It also covers INTERSECT after UNION and after EXCEPT, and parenthesised groups on both sides.

  • The set operation matrix in the dialect test against SQLite covers chains of three with mixed operators. The only expected differences are where SQLite's equal precedence differs from the standard.

  • The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.

Depends on

#2712, #3167, #3170

Activity

  1. agershun commented on May 19, 2015

    @agershun
    MemberAuthor
    The INTERSECT operator has higher precedence than the UNION and EXCEPT operators. Therefore, in the absence of parentheses, INTERSECT operations are always evaluated first.
  2. added this to the milestone on May 21, 2015
  3. mathiasrw commented on Jul 27, 2015

    @mathiasrw
    Member

    Any news on this?

  4. changed the title [-]Fix multiple EXCEPT problem[/-] [+]Evaluate set operations left to right with INTERSECT binding tighter, with a dialect keeping the old order[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions