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 function chaining and the dot syntax for function calls #6725

Merged
merged 4 commits into from Mar 15, 2023

Conversation

Mytherin
Copy link
Collaborator

This PR adds support for function chaining using the dot operator (see #6717).

For example:

Strings
CREATE TABLE varchars(str VARCHAR);
INSERT INTO varchars VALUES ('Hello World');
SELECT str.lower().split(' ')[1] AS res FROM varchars;
┌─────────┐
│   res   │
│ varchar │
├─────────┤
│ hello   │
└─────────┘
Lists
CREATE TABLE lists(ints INT[]);
INSERT INTO lists VALUES ([1, 5, 7, 9]), ([999, 1, 3, 7]), ([999, 4, 999]), ([999]);
SELECT ints.filter(x -> x < 999).apply(x -> x * 3).aggregate('sum') AS res FROM lists;
┌────────┐
│  res   │
│ int128 │
├────────┤
│     66 │
│     33 │
│     12 │
│   NULL │
└────────┘

The way it works is that whenever a function is called using the dot syntax, the left-side becomes the first parameter of the function. For example, str.lower() is equivalent to lower(str). This allows more convenient chaining of multiple functions without having to deeply nest function calls.

@douenergy
Copy link
Contributor

Dr. @Mytherin you are a ducking magician 🦆🎩

@Mytherin Mytherin merged commit 1d7a295 into duckdb:master Mar 15, 2023
80 checks passed
@Mytherin Mytherin deleted the chainedfunctions branch April 24, 2023 13:34
@Alex-Monahan Alex-Monahan added Needs Documentation Use for issues or PRs that require changes in the documentation Friendly SQL labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Friendly SQL Needs Documentation Use for issues or PRs that require changes in the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants