Skip to content

Conversation

@jussisaurio
Copy link
Collaborator

@jussisaurio jussisaurio commented May 20, 2025

Adds support for UNION ALL and introduces Plan::CompoundSelect so that it can be extended to support UNION/EXCEPT/INTERSECT as well

do_execsql_test_on_specific_db {:memory:} select-union-all-1 {
  CREATE TABLE t1(x INTEGER);
  CREATE TABLE t2(x INTEGER); 
  CREATE TABLE t3(x INTEGER);

  INSERT INTO t1 VALUES(1),(2),(3);
  INSERT INTO t2 VALUES(4),(5),(6);
  INSERT INTO t3 VALUES(7),(8),(9);

  SELECT x FROM t1
  UNION ALL 
  SELECT x FROM t2
  UNION ALL
  SELECT x FROM t3;
} {1
2
3
4
5
6
7
8
9}

do_execsql_test_on_specific_db {:memory:} select-union-all-with-filters {
  CREATE TABLE t4(x INTEGER);
  CREATE TABLE t5(x INTEGER);
  CREATE TABLE t6(x INTEGER);

  INSERT INTO t4 VALUES(1),(2),(3),(4);
  INSERT INTO t5 VALUES(5),(6),(7),(8);
  INSERT INTO t6 VALUES(9),(10),(11),(12);

  SELECT x FROM t4 WHERE x > 2
  UNION ALL
  SELECT x FROM t5 WHERE x < 7 
  UNION ALL
  SELECT x FROM t6 WHERE x = 10;
} {3
4
5
6
10}

Supports LIMIT. Currently does not support WITH(), OFFSET or ORDER BY and explicitly returns a parse error if those are present.

@jussisaurio jussisaurio merged commit 02e7726 into main May 24, 2025
41 checks passed
@jussisaurio jussisaurio deleted the union-all branch May 24, 2025 10:42
jussisaurio added a commit that referenced this pull request May 25, 2025
```sql

limbo> select * from products where id between 1 and 3
UNION ALL
select * from products where id between 2 and 4;
┌───┬─────────┬──────┐
│ 1 │ hat     │ 79.0 │
├───┼─────────┼──────┤
│ 2 │ cap     │ 82.0 │
├───┼─────────┼──────┤
│ 3 │ shirt   │ 18.0 │
├───┼─────────┼──────┤
│ 2 │ cap     │ 82.0 │
├───┼─────────┼──────┤
│ 3 │ shirt   │ 18.0 │
├───┼─────────┼──────┤
│ 4 │ sweater │ 25.0 │
└───┴─────────┴──────┘
limbo> select * from products where id between 1 and 3
UNION
select * from products where id between 2 and 4;
┌───┬─────────┬──────┐
│ 1 │ hat     │ 79.0 │
├───┼─────────┼──────┤
│ 2 │ cap     │ 82.0 │
├───┼─────────┼──────┤
│ 3 │ shirt   │ 18.0 │
├───┼─────────┼──────┤
│ 4 │ sweater │ 25.0 │
└───┴─────────┴──────┘
limbo>
```
Similarly as UNION ALL (#1541 ), supports LIMIT but not OFFSET or ORDER
BY.
Augments `compound_select_fuzz()` to work with both UNION and UNION ALL

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants