Skip to content

row_number() doesn't respect order by clause if partition by clause is missing and order is same as in base query #2705

@bziobrowski

Description

@bziobrowski

Describe the bug

No response

To reproduce

create table rntest as ( select x, x%10 from long_sequence(100));

select *, row_number() over (order by x desc ) as rn from rntest order by x desc  ;

 select *, row_number() over (order by column ) as rn from rntest order by column  ;

  x  | column | rn
-----+--------+-----
 100 |      0 | 100
  90 |      0 |  90
  80 |      0 |  80
  70 |      0 |  70
  60 |      0 |  60
  50 |      0 |  50
  40 |      0 |  40
  30 |      0 |  30
  20 |      0 |  20
  10 |      0 |  10


select *, row_number() over (order by column, x ) as rn from rntest order by column, x   ;

  x  | column | rn
-----+--------+-----
  10 |      0 |  10
  20 |      0 |  20
  30 |      0 |  30
  40 |      0 |  40
  50 |      0 |  50
  60 |      0 |  60
  70 |      0 |  70
  80 |      0 |  80
  90 |      0 |  90
 100 |      0 | 100

in PostgreSQL :

create table rntest as ( select x, x%10 column from generate_series(1,100,1) as s(x));

select *, row_number() over (order by "column" ) as rn from rntest order by "column"  ;

 x  | column | rn
-----+--------+-----
  40 |      0 |   1
  20 |      0 |   2
  50 |      0 |   3
  80 |      0 |   4
  70 |      0 |   5
  30 |      0 |   6
  90 |      0 |   7
  60 |      0 |   8
 100 |      0 |   9
  10 |      0 |  10
  71 |      1 |  11

select *, row_number() over (order by "column", x ) as rn from rntest order by "column", x   ;
  x  | column | rn
-----+--------+----
  10 |      0 |  1
  20 |      0 |  2
  30 |      0 |  3
  40 |      0 |  4
  50 |      0 |  5
  60 |      0 |  6
  70 |      0 |  7
  80 |      0 |  8
  90 |      0 |  9
 100 |      0 | 10
   1 |      1 | 11

Expected Behavior

No response

Environment

- **QuestDB version**:6.5.5-SNAPSHOT/master
- **OS**:
- **Browser**:

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIncorrect or unexpected behaviorSQLIssues or changes relating to SQL execution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions