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

Support DuckDB's "simplified" UNPIVOT syntax #4542

Closed
Bilbottom opened this issue Dec 22, 2024 · 0 comments · Fixed by #4545
Closed

Support DuckDB's "simplified" UNPIVOT syntax #4542

Bilbottom opened this issue Dec 22, 2024 · 0 comments · Fixed by #4545
Assignees

Comments

@Bilbottom
Copy link

Summary

DuckDB has implemented a "simplified" syntax for PIVOT and UNPIVOT statements (in addition to the standard SQL syntax).

The PIVOT statement can be parsed by SQLGlot, but not the UNPIVOT statements.

Corresponding DuckDB documentation for UNPIVOT:

MWE

I tried parsing each example on the docs linked above using the latest version of SQLGlot (main), and all six of them threw an error for different reasons -- but running them directly in DuckDB works fine, so the queries are valid

# sqlglot @ git+https://github.com/tobymao/sqlglot@07d05da95c7d3882a7032dade3cbeefbd96628b7
import sqlglot

statements = [
    # https://duckdb.org/docs/sql/statements/unpivot#unpivot-manually
    """
    UNPIVOT monthly_sales
    ON jan, feb, mar, apr, may, jun
    INTO
        NAME month
        VALUE sales;
    """,
    # https://duckdb.org/docs/sql/statements/unpivot#unpivot-dynamically-using-columns-expression
    """
    UNPIVOT monthly_sales
    ON COLUMNS(* EXCLUDE (empid, dept))
    INTO
        NAME month
        VALUE sales;
    """,
    # https://duckdb.org/docs/sql/statements/unpivot#unpivot-into-multiple-value-columns
    """
    UNPIVOT monthly_sales
    ON (jan, feb, mar) AS q1, (apr, may, jun) AS q2
    INTO
        NAME quarter
        VALUE month_1_sales, month_2_sales, month_3_sales;
    """,
    # https://duckdb.org/docs/sql/statements/unpivot#using-unpivot-within-a-select-statement (1)
    """
    WITH unpivot_alias AS (
        UNPIVOT monthly_sales
        ON COLUMNS(* EXCLUDE (empid, dept))
        INTO
            NAME month
            VALUE sales
    )
    SELECT * FROM unpivot_alias;
    """,
    # https://duckdb.org/docs/sql/statements/unpivot#using-unpivot-within-a-select-statement (2)
    """
    SELECT *
    FROM (
        UNPIVOT monthly_sales
        ON COLUMNS(* EXCLUDE (empid, dept))
        INTO
            NAME month
            VALUE sales
    ) unpivot_alias;
    """,
    # https://duckdb.org/docs/sql/statements/unpivot#expressions-within-unpivot-statements
    """
    UNPIVOT (SELECT 42 AS col1, 'woot' AS col2)
    ON
        (col1 * 2)::VARCHAR,
        col2;
    """,
]

for i, sql in enumerate(statements):
    try:
        sqlglot.parse(sql, read="duckdb", dialect="duckdb")
        print(f"statement {i} parse succeeded")
    except Exception as err:
        print(f"statement {i} parse failed: {err}")
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 a pull request may close this issue.

2 participants