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

Window Function BEWEEN clause behaviour is inconsistent with Mysql When frame_end is less than or equal to frame_start #12353

Closed
DQinYuan opened this issue Sep 24, 2019 · 2 comments · Fixed by #12394
Assignees
Labels
type/bug The issue is confirmed as a bug.

Comments

@DQinYuan
Copy link
Contributor

Bug Report

Similar with issue#11011,but not the same. This issue still exist after issue#11011 fixed.

  1. What did you do?
    If possible, provide a recipe for reproducing the error.
create table temp (val int);

insert into temp values(1),(1),(2),(2);

SELECT val, SUM(val) OVER w AS 'sum' FROM temp WINDOW w AS (ROWS BETWEEN  CURRENT ROW AND  0 PRECEDING);

SELECT val, SUM(val) OVER w AS 'sum' FROM temp WINDOW w AS (ROWS BETWEEN  CURRENT ROW AND  1 PRECEDING);
  1. What did you expect to see?

In mysql 8.0.16, it will return error when frame_end is less than or equal to frame_start

mysql> create table temp (val int);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into temp values(1),(1),(2),(2);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT val, SUM(val) OVER w AS 'sum' FROM temp WINDOW w AS (ROWS BETWEEN  CURRENT ROW AND  0 PRECEDING);
ERROR 3586 (HY000): Window 'w': frame start or end is negative, NULL or of non-integral type
mysql> SELECT val, SUM(val) OVER w AS 'sum' FROM temp WINDOW w AS (ROWS BETWEEN  CURRENT ROW AND  1 PRECEDING);
ERROR 3586 (HY000): Window 'w': frame start or end is negative, NULL or of non-integral type
  1. What did you see instead?

In tidb,

  • When frame_end == frame_start,it will become frames with one row
  • When frame_end < frame_start,it will all return NULL
mysql> create table temp (val int);
Query OK, 0 rows affected (0.17 sec)

mysql> insert into temp values(1),(1),(2),(2);
Query OK, 4 rows affected (0.06 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT val, SUM(val) OVER w AS 'sum' FROM temp WINDOW w AS (ROWS BETWEEN  CURRENT ROW AND  0 PRECEDING);
+------+------+
| val  | sum  |
+------+------+
|    1 |    1 |
|    1 |    1 |
|    2 |    2 |
|    2 |    2 |
+------+------+
4 rows in set (0.00 sec)

mysql> SELECT val, SUM(val) OVER w AS 'sum' FROM temp WINDOW w AS (ROWS BETWEEN  CURRENT ROW AND  1 PRECEDING);
+------+------+
| val  | sum  |
+------+------+
|    1 | NULL |
|    1 | NULL |
|    2 | NULL |
|    2 | NULL |
+------+------+
4 rows in set (0.00 sec)
  1. What version of TiDB are you using (tidb-server -V or run select tidb_version(); on TiDB)?
mysql> select tidb_version();
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                                                       |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v3.0.3
Git Commit Hash: 836982c617fbaa42d74616eb9ba9ed4cae46c46b
Git Branch: HEAD
UTC Build Time: 2019-08-30 02:42:42
GoVersion: go version go1.12 linux/amd64
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
@DQinYuan DQinYuan added the type/bug The issue is confirmed as a bug. label Sep 24, 2019
@SunRunAway
Copy link
Contributor

@lzmhhh123 PTAL

@lzmhhh123
Copy link
Contributor

@DQinYuan Thanks for your report. I'll fix in several days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants