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

[FriendlySQL] Unpacked COLUMNS() Expression #11872

Draft
wants to merge 15 commits into
base: feature
Choose a base branch
from

Conversation

Tishj
Copy link
Contributor

@Tishj Tishj commented Apr 30, 2024

This PR adds the *COLUMNS(...) expression that changes the behavior of the existing COLUMNS(...) expression.
Instead of expanding vertically, instead the list expands horizontally.

To explain this in more detail, here's some SQL:

COLUMNS behavior

select COALESCE(COLUMNS(*)) from (select NULL, 2, 3) t(a, b, c)

When the COLUMNS expression is expanded, the resulting query becomes:

select COALESCE(a) a, COALESCE(b) b, COALESCE(c) c from (select NULL, 2, 3) t(a, b, c)

The result of this query is:

┌───────┬───────┬───────┐
│   a   │   b   │   c   │
│ int32 │ int32 │ int32 │
├───────┼───────┼───────┤
│       │     2 │     3 │
└───────┴───────┴───────┘

*COLUMNS behavior

select COALESCE(*COLUMNS(*)) from (select NULL, 2, 3) t(a, b, c)

When the *COLUMNS expression is expanded, the resulting query becomes:

select COALESCE(a, b, c) from (select NULL, 2, 3) t(a, b, c)

The result of this query is:

┌─────────────────────────┐
│ COALESCE(t.a, t.b, t.c) │
│          int32          │
├─────────────────────────┤
│                       2 │
└─────────────────────────┘

@Mytherin Mytherin changed the base branch from main to feature May 3, 2024 14:43
Copy link
Collaborator

@Mytherin Mytherin left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looks good - some comments:


# IN (...)
query I
select 2 in (*COLUMNS(*)) from (select 1, 2, 3) t(a, b, c);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add some more tests?

  • Combining COLUMNS(*) with *COLUMNS(*)
  • Multiple *COLUMNS(*) in the same statement?
  • *COLUMNS(*) inside the lambda of a *COLUMNS(*) statement?
  • Can we test this with a varargs function, e.g. CONCAT?
  • Can we try *COLUMNS(*) + 42?

Copy link
Contributor Author

@Tishj Tishj May 5, 2024

Choose a reason for hiding this comment

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

I noticed that COLUMNS(COLUMNS(COLUMNS(*))) was allowed, because it wasn't checking for "columns" in the transformer, that now properly errors

This was caused by the check that enables COLUMNS(*), COLUMNS is a StarExpression and * is a StarExpression

When a StarExpression (*) appears in COLUMNS we just merge them.
But that also allowed COLUMNS(COLUMNS(COLUMNS(*)))

test/sql/parser/test_columns_unpacked.test Show resolved Hide resolved
test/sql/parser/test_columns_unpacked.test Show resolved Hide resolved
@duckdb-draftbot duckdb-draftbot marked this pull request as draft May 22, 2024 08:03
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.

None yet

2 participants