Skip to content

Conversation

@SkyFan2002
Copy link
Member

I hereby agree to the terms of the CLA available at: https://databend.rs/dev/policies/cla/

Summary

Syntax

The basic syntax of pivot and unpivot is currently supported:

pivot:

SELECT select_list FROM table_name PIVOT (aggregate_function(column_name) FOR column_name IN (value1, value2, ...))
[GROUP BY column_name, ...]

group by is optional, by default this is all columns not mentioned in the above clauses

unpivot:

SELECT * FROM table_name UNPIVOT(column_name FOR column_name IN (column_name..))

Implement

Like duckdb, pivoting and unpivoting are implemented entirely as rewrites into SQL queries. Specifically, it is implemented by rewriting SelectStmt in the bind stage.

Now unpivot works fine, pivot results are the same as equivalent SQL statements, but due to agg_if implementation seems to have bugs, pivot results are not correct.

For example,the following two statements are equivalent:

query IIIII
SELECT *
  FROM monthly_sales
    PIVOT(SUM(amount) FOR MONTH IN ('JAN', 'FEB', 'MAR', 'APR'))
  ORDER BY EMPID;
----
1	10400	8000	11000	18000
2	39500	90700	12000	5300

query IIIII
SELECT EMPID,
    SUM_IF(AMOUNT,MONTH = 'JAN') AS JAN,
    SUM_IF(AMOUNT,MONTH = 'FEB') AS FEB,
    SUM_IF(AMOUNT,MONTH = 'MAR') AS MAR,
    SUM_IF(AMOUNT,MONTH = 'APR') AS APR
    FROM monthly_sales
    GROUP BY EMPID;
----
1	10400	8000	11000	18000
2	39500	90700	12000	5300

Their results of them are indeed the same, but they are not correct:

1 10400 NULL NULL NULL                                                                                              +
2 39500 NULL NULL NULL

Closes #10498

@vercel
Copy link

vercel bot commented Mar 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated
databend ⬜️ Ignored (Inspect) Visit Preview Mar 22, 2023 at 2:01AM (UTC)

@mergify mergify bot added the pr-feature this PR introduces a new feature to the codebase label Mar 20, 2023
# Conflicts:
#	src/query/ast/src/parser/token.rs
@sundy-li sundy-li merged commit 5dc4c8b into databendlabs:main Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature this PR introduces a new feature to the codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: add pivot/unpivot function like duckdb

3 participants