15579d59fSdrh# 2015-08-26 25579d59fSdrh# 35579d59fSdrh# The author disclaims copyright to this source code. In place of 45579d59fSdrh# a legal notice, here is a blessing: 55579d59fSdrh# 65579d59fSdrh# May you do good and not evil. 75579d59fSdrh# May you find forgiveness for yourself and forgive others. 85579d59fSdrh# May you share freely, never taking more than you give. 95579d59fSdrh# 105579d59fSdrh#*********************************************************************** 115579d59fSdrh# This file implements regression tests for SQLite library. 125579d59fSdrh# 135579d59fSdrh# This file seeks to verify that expressions (and especially functions) 145579d59fSdrh# that are in both the ORDER BY clause and the result set are only 155579d59fSdrh# evaluated once. 165579d59fSdrh# 175579d59fSdrh 185579d59fSdrhset testdir [file dirname $argv0] 195579d59fSdrhsource $testdir/tester.tcl 205579d59fSdrhset ::testprefix orderby9 215579d59fSdrh 225579d59fSdrh 235579d59fSdrhdo_execsql_test setup { 245579d59fSdrh -- create a table with many entries 255579d59fSdrh CREATE TABLE t1(x); 265579d59fSdrh WITH RECURSIVE 275579d59fSdrh c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) 285579d59fSdrh INSERT INTO t1 SELECT x FROM c; 295579d59fSdrh} 30*3a84411fSdrh 31*3a84411fSdrh# Some versions of TCL are unable to [lsort -int] for 32*3a84411fSdrh# 64-bit integers. So we write our own comparison 33*3a84411fSdrh# routine. 34*3a84411fSdrhproc bigintcompare {a b} { 35*3a84411fSdrh set x [expr {$a-$b}] 36*3a84411fSdrh if {$x<0} {return -1} 37*3a84411fSdrh if {$x>0} {return +1} 38*3a84411fSdrh return 0 39*3a84411fSdrh} 405579d59fSdrhdo_test 1.0 { 415579d59fSdrh set l1 {} 425579d59fSdrh # If random() is only evaluated once and then reused for each row, then 435579d59fSdrh # the output should appear in sorted order. If random() is evaluated 445579d59fSdrh # separately for the result set and the ORDER BY clause, then the output 455579d59fSdrh # order will be random. 465579d59fSdrh db eval {SELECT random() AS y FROM t1 ORDER BY 1;} {lappend l1 $y} 47*3a84411fSdrh expr {$l1==[lsort -command bigintcompare $l1]} 485579d59fSdrh} {1} 495579d59fSdrh 505579d59fSdrhdo_test 1.1 { 515579d59fSdrh set l1 {} 525579d59fSdrh db eval {SELECT random() AS y FROM t1 ORDER BY random();} {lappend l1 $y} 53*3a84411fSdrh expr {$l1==[lsort -command bigintcompare $l1]} 545579d59fSdrh} {1} 555579d59fSdrh 565579d59fSdrhdo_test 1.2 { 575579d59fSdrh set l1 {} 585579d59fSdrh db eval {SELECT random() AS y FROM t1 ORDER BY +random();} {lappend l1 $y} 59*3a84411fSdrh expr {$l1==[lsort -command bigintcompare $l1]} 605579d59fSdrh} {0} 615579d59fSdrh 625579d59fSdrhfinish_test 63