[MDEV-9740] Window functions: catch invalid window frame specs Created: 2016-03-16  Updated: 2016-04-14  Resolved: 2016-03-17

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - Window functions
Fix Version/s: N/A

Type: Task Priority: Major
Reporter: Sergei Petrunia Assignee: Sergei Petrunia
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
PartOf
is part of MDEV-6115 window functions as in the SQL standard Closed

 Description   

Window functions: catch invalid window frame specs

The standard specifies a lot of restrictions around frame definitions. We catch
some, check

ERROR HY000: Unacceptable combination of window frame bound specifications

in win.result, but not all. Things like

"rows between 1.5 preceding and 'foo' following"

are still allowed and cause assertion failures.



 Comments   
Comment by Sergei Petrunia [ 2016-03-17 ]

The standard, section 6.10, <window function>.
syntax rules says:

5) OF shall not contain an outer reference or a <query expression>.

select rank() over (partition by a order by (select max(a) from t2 ) from ten;

  • PostgreSQL, Oracle, MS SQL: allow this.

7) If <ntile function>, <lead or lag function>, RANK or DENSE_RANK is specified, then the window ordering clause WOC of WDX shall be present.

select rank() over (partition by a) from ten;

  • MariaDB: query works
  • PostgreSQL: query works
  • Oracle: ORA-30485: missing ORDER BY expression in the window specification
  • MS SQL: The function 'rank' must have an OVER clause with ORDER BY.

7) b) The window framing clause of WDX shall not be present.

select rank() over (order by a rows between 1 preceding and 1 following) from ten;

MariaDB: ok (framing ignored)
PG: Ok (framing ignored)
Oracle: ORA-00907: missing right parenthesis
MS SQL: The function 'rank' may not have a window frame.

ORA-30486: invalid window aggregation group in the window specification

11 a) iii) WDEFOC shall contain a single <sort key> SK.

  create table t1 (a int, b int, c int);
  insert into t1 values (1,1,1);
  insert into t1 values (2,2,2);
  select count(*) over (order by a,b range between unbounded preceding and
  current row) from t1;

PG, MS-SQL: ok
Oracle: ORA-30486: invalid window aggregation group in the window specification
MariaDB: crash.

The declared type of SK shall be numeric, datetime, or interval

  create table t1 (a varchar(32), b int, c int);
  insert into t1 values ('aaa',1,1);
  insert into t1 values ('bbbb',2,2);
  select count(*) over (order by a range between unbounded preceding and
  current row) from t1;

PG, MS-SQL: OK
Oracle: OK
Oracle, with "1 PRECEDING": "ORA-00902: invalid datatype"
MariaDB: crash.

If RANGE is specified, then ... i) 2) A) I) If V1P is negative or the null value, then an
exception condition is raised: data exception — invalid preceding or following size in window function.

select count(*) over (rows between -1 preceding and current row ) from t1;

MariaDB: handled by the parser. One gets "Integer is required for ROWS-type frame" error.
PG: ERROR: frame starting offset must not be negative
MS-SQL: Incorrect syntax near 'rows'.
Oracle: No error

Comment by Sergei Petrunia [ 2016-03-17 ]

Pushed into the feature tree

Generated at Thu Feb 08 07:36:58 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.