[MDEV-10141] Add support for INTERSECT (and common parts for EXCEPT) Created: 2016-05-27 Updated: 2018-08-31 Resolved: 2017-03-13 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer, Parser |
| Fix Version/s: | 10.3.0 |
| Type: | Task | Priority: | Major |
| Reporter: | Michael Widenius | Assignee: | Oleksandr Byelkin |
| Resolution: | Fixed | Votes: | 2 |
| Labels: | 10.3-beta, Compatibility | ||
| Issue Links: |
|
||||||||||||||||||||
| Epic Link: | Oracle Compatibility | ||||||||||||||||||||
| Sprint: | 10.2.4-2, 10.2.4-4, 10.1.20 | ||||||||||||||||||||
| Description |
|
The INTERSECT/EXCEPT clause has this general form: select_statement INTERSECT select_statement select_statement is any SELECT statement without an ORDER BY, LIMIT, or FOR UPDATE clause. The INTERSECT operator computes the set intersection of the rows returned by the involved SELECT statements. A row is in the intersection of two result sets if it appears in both result sets. The result of INTERSECT does not contain any duplicate rows unless the ALL option is specified. With ALL, a row that has m duplicates in the left table and n duplicates in the right table will appear min(m,n) times in the result set. Steps to implement INTERSECT/EXCEPT 1. Functionality 1.1. Basic (UNION LIKE) functionality with ORDER BY, GROUP BY, LIMIT etc. (no ALL, no recursive CTE) Main idea: First query works like with UNION (distinct) select_union (inherited from select_result) results interceptor and collect data in a temporary table with an unique index. Second (and so on) has its own result interceptor
UNION/EXCEPT/INTERSECT has different priority and they can be put on one level of UNIT/SELECT tree (SELECT_LEX/SELECT_LEX_UNIT) only in case if execution corresponds order. For beginner I think better to divide levels. Later (as optimization) possible to "join" levels in case sequential execution order and eliminate so called "fake_select" if results can be passed without processing. 1.2. ALL clause In basic case can we build non unique index and work with all records which fit the same way as in 1.1. 1.3. Recursive CTE support 2.1 Conversion to nested JOIN In many cases (when there is no grouping operations) the operation can be put in one SELECT with joining all records (better when there is unique indices from both sides) 2.2 Optimize unique index There is cases when we know that there is some unique set of field (also can work for UNION but need the same unique set from the other end) In conversation with Sergei Petrunia we got idea of "oppening" brackets with not standart operations: S1 intersect (S2 union S3) can be unrolled to: S1 except (S2 except S2) : ... The question is: is it possible all brackets "open" in such way? |
| Comments |
| Comment by Michael Widenius [ 2017-03-26 ] |
|
This is pushed into bb-10.2-compatibility and will soon be in the 10.3 tree |
| Comment by Oleksandr Byelkin [ 2017-07-02 ] |
|
ALL is not standard option. |