Skip to content

Add support for CORRESPONDING option in set operations#25260

Merged
ebyhr merged 2 commits into
trinodb:masterfrom
ebyhr:ebi/core-union-corresponding
Apr 23, 2025
Merged

Add support for CORRESPONDING option in set operations#25260
ebyhr merged 2 commits into
trinodb:masterfrom
ebyhr:ebi/core-union-corresponding

Conversation

@ebyhr
Copy link
Copy Markdown
Member

@ebyhr ebyhr commented Mar 9, 2025

Description

Add support for CORRESPONDING option in set operations.
The option matches columns by name instead of by position:

SELECT * FROM (VALUES (1, 'alice')) AS t(id, name)
UNION ALL CORRESPONDING
SELECT * FROM (VALUES ('bob', 2)) AS t(name, id);
 id | name
----+-------
  1 | alice
  2 | bob
(2 rows)

The option is defined in SQL spec

<query expression body> ::=
    <query term> 
  | <query expression body>  UNION [ ALL | DISTINCT ]
      [ <corresponding spec>  ] <query term> 
  | <query expression body>  EXCEPT [ ALL | DISTINCT ]
      [ <corresponding spec>  ] <query term> 
<query term> ::=
    <query primary> 
  | <query term>  INTERSECT [ ALL | DISTINCT ]
      [ <corresponding spec>  ] <query primary> 
<corresponding spec> ::=
  CORRESPONDING [ BY <left paren>  <corresponding column list>  <right paren>  ]

P.508-509 in ISO_IEC_9075-2(E)_Foundation explains this option.

Release notes

## General
* Add support for the `CORRESPONDING` option in set operations. ({issue}`issuenumber`)

@cla-bot cla-bot Bot added the cla-signed label Mar 9, 2025
@github-actions github-actions Bot added the docs label Mar 9, 2025
@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch 2 times, most recently from 3bb218f to 46271ec Compare March 9, 2025 22:51
Comment thread core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/planner/RelationPlanner.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/planner/RelationPlanner.java Outdated
@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch from 46271ec to 2e81b33 Compare March 10, 2025 23:40
@ebyhr
Copy link
Copy Markdown
Member Author

ebyhr commented Mar 10, 2025

Changing to draft as I missed logic to handle different number of columns between left and right relations.

@ebyhr ebyhr marked this pull request as draft March 10, 2025 23:58
Comment thread core/trino-parser/src/main/java/io/trino/sql/tree/Except.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/planner/RelationPlanner.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/analyzer/RelationType.java Outdated
@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch 2 times, most recently from 2d07dd1 to f239bc6 Compare March 20, 2025 01:39
@ebyhr ebyhr marked this pull request as ready for review March 20, 2025 01:41
@ebyhr ebyhr requested a review from martint March 20, 2025 01:41
@github-actions
Copy link
Copy Markdown

This pull request has gone a while without any activity. Ask for help on #core-dev on Trino slack.

@github-actions github-actions Bot added the stale label Apr 14, 2025
@ebyhr
Copy link
Copy Markdown
Member Author

ebyhr commented Apr 14, 2025

@martint Gentle reminder.

@github-actions github-actions Bot removed the stale label Apr 15, 2025
Comment thread core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java Outdated
Comment thread core/trino-grammar/src/main/antlr4/io/trino/grammar/sql/SqlBase.g4 Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java Outdated
@ebyhr ebyhr requested a review from martint April 17, 2025 22:38
@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch from 52b71bb to 13d2159 Compare April 17, 2025 22:52
@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch from 13d2159 to 935d79c Compare April 23, 2025 01:14
Comment thread docs/src/main/sphinx/sql/select.md Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add examples for cases where the columns in the left and right table don't match exactly but have at least one common column.

@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch from 935d79c to d6cfd3d Compare April 23, 2025 04:35
@ebyhr ebyhr merged commit 8fb0263 into trinodb:master Apr 23, 2025
97 checks passed
@ebyhr ebyhr deleted the ebi/core-union-corresponding branch April 23, 2025 05:26
@github-actions github-actions Bot added this to the 475 milestone Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants