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

Implement window functions #103

Closed
MichaelScofield opened this issue Jul 20, 2022 · 4 comments
Closed

Implement window functions #103

MichaelScofield opened this issue Jul 20, 2022 · 4 comments
Assignees

Comments

@MichaelScofield
Copy link
Collaborator

MichaelScofield commented Jul 20, 2022

A lot of useful window functions are not implemented now.

Check how DataFusion supports defining these "User Defined Window Function" and implement them.

@MichaelScofield MichaelScofield self-assigned this Sep 8, 2022
@MichaelScofield
Copy link
Collaborator Author

related: apache/datafusion#4553

@killme2008
Copy link
Contributor

Looks like we already support the time window functions? @MichaelScofield

@MichaelScofield
Copy link
Collaborator Author

@killme2008 last time I checked, window function in datafusion was lack of the filter clause support (see https://www.sqlite.org/windowfunctions.html). Will check it now.

@MichaelScofield
Copy link
Collaborator Author

@killme2008 Unfortunately, datafusion still lack the support for "filter clause on window function". However, the filter could be partially substituted by plain "where" clause. See below the experiment I ran:

First create table and insert data:

CREATE TABLE hello(a bigint, b varchar(10), primary key(b));
INSERT INTO hello VALUES(1, 'one'), (1, 'two'), (1, 'three'), (2, 'four'), (3, 'five'), (3, 'six');

Run query with filter:

SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a);

and using plain "where" to do filter:

SELECT a, b, sum(a) OVER win AS sum FROM hello where a != 1 WINDOW win AS (ORDER BY a);

Please note the difference between the output. Some rows are missing if using plain "where" to do the filter but the window function calculation is correct:

sqlite

sqlite> SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a);
1|one|
1|two|
1|three|
2|four|2
3|five|8
3|six|8
sqlite> SELECT a, b, sum(a) OVER win AS sum FROM hello where a != 1 WINDOW win AS (ORDER BY a);
2|four|2
3|five|8
3|six|8

pg

luofucong=# SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a);
 a |   b   | sum
---+-------+-----
 1 | one   |
 1 | two   |
 1 | three |
 2 | four  |   2
 3 | five  |   8
 3 | six   |   8
(6 rows)

luofucong=# SELECT a, b, sum(a) OVER win AS sum FROM hello where a != 1 WINDOW win AS (ORDER BY a);
 a |  b   | sum
---+------+-----
 2 | four |   2
 3 | five |   8
 3 | six  |   8
(3 rows)

mysql

mysql> SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a)' at line 1
mysql> SELECT a, b, sum(a) OVER win AS sum FROM hello where a != 1 WINDOW win AS (ORDER BY a);
+------+------+------+
| a    | b    | sum  |
+------+------+------+
|    2 | four |    2 |
|    3 | five |    8 |
|    3 | six  |    8 |
+------+------+------+
3 rows in set (0.00 sec)

greptimedb

mysql> SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a);
ERROR 1815 (HY000): Failed to execute query: SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a), source: Failed to parse SQL, source: SQL statement is not supported: SELECT a, b, sum(a) filter ( where a != 1 ) OVER win AS sum FROM hello WINDOW win AS (ORDER BY a), keyword: (
mysql> SELECT a, b, sum(a) OVER win AS sum FROM hello where a != 1 WINDOW win AS (ORDER BY a);
+------+------+------+
| a    | b    | sum  |
+------+------+------+
|    2 | four |    2 |
|    3 | five |    8 |
|    3 | six  |    8 |
+------+------+------+
3 rows in set (0.02 sec)

Since the datafusion does support other window function features but this "filter clause", I'm claiming this issue is done.

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

No branches or pull requests

2 participants