Skip to content

Add Python-style list-comprehension syntax support to SQL #4926

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

Merged
merged 2 commits into from
Oct 9, 2022

Conversation

Mytherin
Copy link
Collaborator

@Mytherin Mytherin commented Oct 9, 2022

The following syntax is now supported:

SELECT [x + 1 for x in [1, 2, 3]] AS l;
┌───────────┐
│     l     │
├───────────┤
│ [2, 3, 4] │
└───────────┘
SELECT [x + 1 for x in [1, 2, 3] if x >= 2] AS l;
┌────────┐
│   l    │
├────────┤
│ [3, 4] │
└────────┘

Under the hood this just translates to the list_apply and list_filter functions as implemented in #3694. Any functions or expressions can be used for both the filter and the main expression. For example:

CREATE TABLE store_inventory(store_id INTEGER, fruits VARCHAR[]);
INSERT INTO store_inventory VALUES (1, ['Apple', 'Banana', 'Mango']);
INSERT INTO store_inventory VALUES (2, ['Mango', 'Pineapple']);
SELECT [concat(store_id, '-', fruit) for fruit in fruits if lower(fruit) IN ('mango', 'banana')] AS filtered_fruit
FROM store_inventory;
┌─────────────────────┐
│   filtered_fruit    │
├─────────────────────┤
│ [1-Banana, 1-Mango] │
│ [2-Mango]           │
└─────────────────────┘

@Alex-Monahan
Copy link
Contributor

This is awesome! We might need an "Even friendlier SQL with DuckDB" blog post at this rate!!

@Mytherin Mytherin merged commit 17b8fd4 into duckdb:master Oct 9, 2022
@hannes
Copy link
Member

hannes commented Oct 10, 2022

We can also just add an Update section to the blog post for now

@l1t1

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants