Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CORRESPONDING option in set operations #25260

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ebyhr
Copy link
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>  ]

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
@ebyhr ebyhr force-pushed the ebi/core-union-corresponding branch from 46271ec to 2e81b33 Compare March 10, 2025 23:40
@ebyhr
Copy link
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
@@ -28,9 +28,9 @@ public class Except
private final Relation left;
private final Relation right;

public Except(NodeLocation location, Relation left, Relation right, boolean distinct)
public Except(NodeLocation location, Relation left, Relation right, boolean distinct, boolean corresponding)
Copy link
Member

Choose a reason for hiding this comment

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

Using a boolean is not going to be sufficient for modeling the list of columns in the CORRESPONDING clause. It will require a bigger change later.

Add support for passing in the list of columns in the grammar and AST, even if, for the first version, the analyzer rejects any queries where the list is provided.

RelationPlan plan = process(child, null);

if (node.isCorresponding() && child.equals(rightRelation)) {
Copy link
Member

Choose a reason for hiding this comment

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

The planner should not be doing any mappings based on names. That should have been established during analysis, and any necessary supporting data structure recorded in the Analysis object. E.g., a list of fields to select in the order in which they should be considered + the corresponding relation types.

/**
* Gets the field at the specified name.
*/
public Optional<Field> getFieldByName(String name)
Copy link
Member

Choose a reason for hiding this comment

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

How does this handle standard SQL identifier canonicalization and matching?

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