1*8c9bcb23Sdan# 2019-09-21 2*8c9bcb23Sdan# 3*8c9bcb23Sdan# The author disclaims copyright to this source code. In place of 4*8c9bcb23Sdan# a legal notice, here is a blessing: 5*8c9bcb23Sdan# 6*8c9bcb23Sdan# May you do good and not evil. 7*8c9bcb23Sdan# May you find forgiveness for yourself and forgive others. 8*8c9bcb23Sdan# May you share freely, never taking more than you give. 9*8c9bcb23Sdan# 10*8c9bcb23Sdan#*********************************************************************** 11*8c9bcb23Sdan# This file implements regression tests for SQLite library. 12*8c9bcb23Sdan# 13*8c9bcb23Sdan# Specifically, it tests cases where the expressions in a GROUP BY 14*8c9bcb23Sdan# clause are the same as those in the ORDER BY clause. 15*8c9bcb23Sdan# 16*8c9bcb23Sdan 17*8c9bcb23Sdanset testdir [file dirname $argv0] 18*8c9bcb23Sdansource $testdir/tester.tcl 19*8c9bcb23Sdanset ::testprefix orderbyA 20*8c9bcb23Sdan 21*8c9bcb23Sdanproc do_sortcount_test {tn sql cnt res} { 22*8c9bcb23Sdan set eqp [execsql "EXPLAIN QUERY PLAN $sql"] 23*8c9bcb23Sdan set rcnt [regexp -all {USE TEMP} $eqp] 24*8c9bcb23Sdan uplevel [list do_test $tn.1 [list set {} $rcnt] $cnt] 25*8c9bcb23Sdan uplevel [list do_execsql_test $tn.2 $sql $res] 26*8c9bcb23Sdan} 27*8c9bcb23Sdan 28*8c9bcb23Sdando_execsql_test 1.0 { 29*8c9bcb23Sdan CREATE TABLE t1(a, b, c); 30*8c9bcb23Sdan INSERT INTO t1 VALUES('one', 1, 11); 31*8c9bcb23Sdan INSERT INTO t1 VALUES('three', 7, 11); 32*8c9bcb23Sdan INSERT INTO t1 VALUES('one', 2, 11); 33*8c9bcb23Sdan INSERT INTO t1 VALUES('one', 3, 11); 34*8c9bcb23Sdan INSERT INTO t1 VALUES('two', 4, 11); 35*8c9bcb23Sdan INSERT INTO t1 VALUES('two', 6, 11); 36*8c9bcb23Sdan INSERT INTO t1 VALUES('three', 8, 11); 37*8c9bcb23Sdan INSERT INTO t1 VALUES('two', 5, 11); 38*8c9bcb23Sdan INSERT INTO t1 VALUES('three', 9, 11); 39*8c9bcb23Sdan} 40*8c9bcb23Sdan 41*8c9bcb23Sdanforeach {tn idx} { 42*8c9bcb23Sdan 1 {} 43*8c9bcb23Sdan 2 {CREATE INDEX i1 ON t1(a)} 44*8c9bcb23Sdan 3 {CREATE INDEX i1 ON t1(a DESC)} 45*8c9bcb23Sdan} { 46*8c9bcb23Sdan execsql { DROP INDEX IF EXISTS i1 } 47*8c9bcb23Sdan execsql $idx 48*8c9bcb23Sdan 49*8c9bcb23Sdan # $match is the number of temp-table sorts we expect if the GROUP BY 50*8c9bcb23Sdan # can use the same sort order as the ORDER BY. $nomatch is the number 51*8c9bcb23Sdan # of expected sorts if the GROUP BY and ORDER BY are not compatible. 52*8c9bcb23Sdan set match 1 53*8c9bcb23Sdan set nomatch 2 54*8c9bcb23Sdan if {$tn>=2} { 55*8c9bcb23Sdan set match 0 56*8c9bcb23Sdan set nomatch 1 57*8c9bcb23Sdan } 58*8c9bcb23Sdan 59*8c9bcb23Sdan do_sortcount_test 1.$tn.1.1 { 60*8c9bcb23Sdan SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a 61*8c9bcb23Sdan } $match {one 6 three 24 two 15} 62*8c9bcb23Sdan do_sortcount_test 1.$tn.1.2 { 63*8c9bcb23Sdan SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a DESC 64*8c9bcb23Sdan } $match {two 15 three 24 one 6} 65*8c9bcb23Sdan 66*8c9bcb23Sdan do_sortcount_test 1.$tn.2.1 { 67*8c9bcb23Sdan SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a||'' 68*8c9bcb23Sdan } $nomatch {one 6 three 24 two 15} 69*8c9bcb23Sdan do_sortcount_test 1.$tn.2.2 { 70*8c9bcb23Sdan SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a||'' DESC 71*8c9bcb23Sdan } $nomatch {two 15 three 24 one 6} 72*8c9bcb23Sdan 73*8c9bcb23Sdan do_sortcount_test 1.$tn.3.1 { 74*8c9bcb23Sdan SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a NULLS LAST 75*8c9bcb23Sdan } $nomatch {one 6 three 24 two 15} 76*8c9bcb23Sdan do_sortcount_test 1.$tn.3.2 { 77*8c9bcb23Sdan SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a DESC NULLS FIRST 78*8c9bcb23Sdan } $nomatch {two 15 three 24 one 6} 79*8c9bcb23Sdan} 80*8c9bcb23Sdan 81*8c9bcb23Sdan#------------------------------------------------------------------------- 82*8c9bcb23Sdando_execsql_test 2.0 { 83*8c9bcb23Sdan CREATE TABLE t2(a, b, c); 84*8c9bcb23Sdan INSERT INTO t2 VALUES(1, 'one', 1); 85*8c9bcb23Sdan INSERT INTO t2 VALUES(1, 'two', 2); 86*8c9bcb23Sdan INSERT INTO t2 VALUES(1, 'one', 3); 87*8c9bcb23Sdan INSERT INTO t2 VALUES(1, 'two', 4); 88*8c9bcb23Sdan INSERT INTO t2 VALUES(1, 'one', 5); 89*8c9bcb23Sdan INSERT INTO t2 VALUES(1, 'two', 6); 90*8c9bcb23Sdan 91*8c9bcb23Sdan INSERT INTO t2 VALUES(2, 'one', 7); 92*8c9bcb23Sdan INSERT INTO t2 VALUES(2, 'two', 8); 93*8c9bcb23Sdan INSERT INTO t2 VALUES(2, 'one', 9); 94*8c9bcb23Sdan INSERT INTO t2 VALUES(2, 'two', 10); 95*8c9bcb23Sdan INSERT INTO t2 VALUES(2, 'one', 11); 96*8c9bcb23Sdan INSERT INTO t2 VALUES(2, 'two', 12); 97*8c9bcb23Sdan 98*8c9bcb23Sdan INSERT INTO t2 VALUES(NULL, 'one', 13); 99*8c9bcb23Sdan INSERT INTO t2 VALUES(NULL, 'two', 14); 100*8c9bcb23Sdan INSERT INTO t2 VALUES(NULL, 'one', 15); 101*8c9bcb23Sdan INSERT INTO t2 VALUES(NULL, 'two', 16); 102*8c9bcb23Sdan INSERT INTO t2 VALUES(NULL, 'one', 17); 103*8c9bcb23Sdan INSERT INTO t2 VALUES(NULL, 'two', 18); 104*8c9bcb23Sdan} 105*8c9bcb23Sdan 106*8c9bcb23Sdanforeach {tn idx} { 107*8c9bcb23Sdan 1 {} 108*8c9bcb23Sdan 109*8c9bcb23Sdan 2 { CREATE INDEX i2 ON t2(a, b) } 110*8c9bcb23Sdan 3 { CREATE INDEX i2 ON t2(a DESC, b DESC) } 111*8c9bcb23Sdan 112*8c9bcb23Sdan 4 { CREATE INDEX i2 ON t2(a, b DESC) } 113*8c9bcb23Sdan 5 { CREATE INDEX i2 ON t2(a DESC, b) } 114*8c9bcb23Sdan} { 115*8c9bcb23Sdan execsql { DROP INDEX IF EXISTS i2 } 116*8c9bcb23Sdan execsql $idx 117*8c9bcb23Sdan 118*8c9bcb23Sdan 119*8c9bcb23Sdan set nSort [expr ($tn==2 || $tn==3) ? 0 : 1] 120*8c9bcb23Sdan do_sortcount_test 2.$tn.1.1 { 121*8c9bcb23Sdan SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a, b; 122*8c9bcb23Sdan } $nSort {{} one 45 {} two 48 1 one 9 1 two 12 2 one 27 2 two 30} 123*8c9bcb23Sdan do_sortcount_test 2.$tn.1.2 { 124*8c9bcb23Sdan SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a DESC, b DESC; 125*8c9bcb23Sdan } $nSort {2 two 30 2 one 27 1 two 12 1 one 9 {} two 48 {} one 45} 126*8c9bcb23Sdan 127*8c9bcb23Sdan set nSort [expr ($tn==4 || $tn==5) ? 0 : 1] 128*8c9bcb23Sdan do_sortcount_test 2.$tn.2.1 { 129*8c9bcb23Sdan SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a, b DESC; 130*8c9bcb23Sdan } $nSort { {} two 48 {} one 45 1 two 12 1 one 9 2 two 30 2 one 27 } 131*8c9bcb23Sdan do_sortcount_test 2.$tn.2.2 { 132*8c9bcb23Sdan SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a DESC, b; 133*8c9bcb23Sdan } $nSort { 2 one 27 2 two 30 1 one 9 1 two 12 {} one 45 {} two 48 } 134*8c9bcb23Sdan 135*8c9bcb23Sdan # ORDER BY can never piggyback on the GROUP BY sort if it uses 136*8c9bcb23Sdan # non-standard NULLS behaviour. 137*8c9bcb23Sdan set nSort [expr $tn==1 ? 2 : 1] 138*8c9bcb23Sdan do_sortcount_test 2.$tn.3.1 { 139*8c9bcb23Sdan SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a, b DESC NULLS FIRST; 140*8c9bcb23Sdan } $nSort { {} two 48 {} one 45 1 two 12 1 one 9 2 two 30 2 one 27 } 141*8c9bcb23Sdan do_sortcount_test 2.$tn.3.2 { 142*8c9bcb23Sdan SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a DESC, b NULLS LAST; 143*8c9bcb23Sdan } $nSort { 2 one 27 2 two 30 1 one 9 1 two 12 {} one 45 {} two 48 } 144*8c9bcb23Sdan} 145*8c9bcb23Sdan 146*8c9bcb23Sdan 147*8c9bcb23Sdanfinish_test 148