8000 Pivot statement unsupported? · Issue #57 · duckdb/duckdb-ui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Pivot statement unsupported? #57

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

Open
ericemc3 opened this issue Mar 15, 2025 · 10 comments
Open

Pivot statement unsupported? #57

ericemc3 opened this issue Mar 15, 2025 · 10 comments
Labels
bug Something isn't working extension Pertaining to the UI extension code, which is in this repository.

Comments

@ericemc3
Copy link
CREATE TABLE cities (
    country VARCHAR, name VARCHAR, year INTEGER, population INTEGER
);
INSERT INTO cities VALUES
    ('NL', 'Amsterdam', 2000, 1005),
    ('NL', 'Amsterdam', 2010, 1065),
    ('NL', 'Amsterdam', 2020, 1158),
    ('US', 'Seattle', 2000, 564),
    ('US', 'Seattle', 2010, 608),
    ('US', 'Seattle', 2020, 738),
    ('US', 'New York City', 2000, 8015),
    ('US', 'New York City', 2010, 8175),
    ('US', 'New York City', 2020, 8772);

Second cell:

PIVOT cities
ON year
USING first(population);

=> Invalid Input Error: Cannot prepare multiple statements at once!

@jraymakers
Copy link
Collaborator

Ah, yes, this is a known problem in the interaction between now PIVOT is implemented and the PendingQuery C++ API call we use to run queries. PIVOT gets expanded into multiple statements, which PendingQuery can't handle.

We can probably address this by using Query, at least in this case. We'll have to think a bit about the best way to do that, without negatively affecting other queries (since PendingQuery provides some advantages, such as prompt cancellation).

@jraymakers jraymakers added the bug Something isn't working label Mar 16, 2025
@pugpapa
Copy link
pugpapa commented Mar 18, 2025

Is there a workaround currently?

@jraymakers
Copy link
Collaborator

There's no workaround that I'm aware of.

@olsgaard
Copy link

Is there a workaround currently?

Standard SQL PIVOT syntax can still be used in the UI, but it is very clumsy compared to the simplified syntax.

-- example using Simplified PIVOT syntax
-- https://duckdb.org/docs/stable/sql/statements/pivot.html#pivot-on-and-using

PIVOT cities
ON year
USING sum(population);

Becomes

-- example using SQL Standard PIVOT Syntax
-- https://duckdb.org/docs/stable/sql/statements/pivot.html#examples

SELECT *
FROM cities
PIVOT (
    sum(population)
    FOR
        year IN (2000, 2010, 2020)
    GROUP BY country, name
);

I don't know how to get around having to know all the possible years in data when writing the query.

@jraymakers
Copy link
Collaborator

Right, one of the main features of DuckDB's PIVOT is its ability to generate the necessary columns. Unfortunately it is exactly this feature that results in multiple statements being run behind the scenes, which leads to the issue.

Some kind of change will be needed to either DuckDB or the UI extension (or both) for this to work.

@ericemc3
Copy link
Author
ericemc3 commented May 12, 2025

Right, one of the main features of DuckDB's PIVOT is its ability to generate the necessary columns. Unfortunately it is exactly this feature that results in multiple statements being run behind the scenes, which leads to the issue.

Some kind of change will be needed to either DuckDB or the UI extension (or both) for this to work.

Thnak you! Hopefully, i guess this is a subject that you have been discussing with the DuckDB team to find a solution? Meanwhile, it is a little bit difficult to advocate UI as a generic interface for DuckDB without Pivot/Unpivot support

@olsgaard
Copy link

If this is something that is not easily fixed, can I propose adding a hardcoded error message for cells that use duckdb pivot as a temporary measure?

As an end-user, it is hard to understand that Invalid Input Error: Cannot prepare multiple statements at once! is not actually a syntax error that the user needs to fix, but a current limitation on the UI.

@jraymakers
Copy link
Collaborator

This is definitely something we're looking into, but it's not trivial to fix so it may take some time. Unfortunately even detecting when this will happen isn't straightforward, so displaying a better error message isn't easy, either.

We'll try to understand how long this might take to address, which will help inform whether investing in producing a better error message is worthwhile.

@jraymakers
Copy link
Collaborator

I learned something recently (how the Python client addresses this problem) that points towards a solution that wouldn't involve changes to DuckDB core. That increases the chances this can be fixed in the near future (time permitting).

(Details: The Python client uses ExtractStatements to split input text into parsed statements before running each through PendingQuery. I believe this handles the case of PIVOT, which looks like a single statement but generates multiple behind the scenes (which is the cause of this issue); ExtractStatements should return multiple parsed statement objects in this case. This pattern shouldn't be hard to implement in the UI extension.)

@nosklo
Copy link
nosklo commented May 27, 2025

Is there a workaround currently?
...
I don't know how to get around having to know all the possible years in data when writing the query.

Here's the statement it generates behind the hood:

CREATE TEMPORARY TYPE __pivot_enum_0_0 AS ENUM (
    SELECT DISTINCT
        year::VARCHAR
    FROM cities
    ORDER BY
        year
    );

PIVOT cities
ON year IN __pivot_enum_0_0
USING sum(population);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working extension Pertaining to the UI extension code, which is in this repository.
Projects
None yet
Development

No branches or pull requests

5 participants
0