Skip to content

Commit

Permalink
Fix a crash that could occur if a sub-select that uses both DISTINCT …
Browse files Browse the repository at this point in the history
…and window functions also used an ORDER BY that is the same as its select list.
  • Loading branch information
danielk-1977 committed Nov 22, 2019
1 parent bee1be1 commit f681d84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -6064,6 +6064,7 @@ int sqlite3Select(
*/
if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
&& sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
&& p->pWin==0
){
p->selFlags &= ~SF_Distinct;
pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
Expand Down
13 changes: 13 additions & 0 deletions test/window1.test
Original file line number Diff line number Diff line change
Expand Up @@ -1243,4 +1243,17 @@ do_catchsql_test 32.10 {
ALTER TABLE a0 RENAME TO S;
} {1 {error in view a: 1st ORDER BY term does not match any column in the result set}}

reset_db
do_execsql_test 33.1 {
CREATE TABLE t1(aa, bb);
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(5, 6);
CREATE TABLE t2(x);
INSERT INTO t2 VALUES(1);
}
do_execsql_test 33.2 {
SELECT (SELECT DISTINCT sum(aa) OVER() FROM t1 ORDER BY 1), x FROM t2
ORDER BY 1;
} {6 1}

finish_test

0 comments on commit f681d84

Please sign in to comment.