/ Check-in [14f15663]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add tests for ORDER BY clauses to e_select.test.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 14f156632729d1a44ef234f39ff9df32db6b308a
User & Date: dan 2010-09-15 19:02:15
Context
2010-09-15
23:41
Updates to the sqlite3_progress_handler() documentation. check-in: 78f659ee user: drh tags: trunk
19:02
Add tests for ORDER BY clauses to e_select.test. check-in: 14f15663 user: dan tags: trunk
17:54
Added the sqlite3_soft_heap_limit64() interface. Deprecate the older sqlite3_soft_heap_limit() interface. check-in: 82268a2c user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to test/e_select.test.

2005
2006
2007
2008
2009
2010
2011

































































































































































































































































































2012

  7  "(2,3) EXCEPT    (2)   EXCEPT    (3)"   {}
} {
  set select [string map {( {SELECT x FROM t1 WHERE x IN (}} $select]
  do_execsql_test e_select-7.12.$tn $select [list {*}$res]
}


































































































































































































































































































finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301

  7  "(2,3) EXCEPT    (2)   EXCEPT    (3)"   {}
} {
  set select [string map {( {SELECT x FROM t1 WHERE x IN (}} $select]
  do_execsql_test e_select-7.12.$tn $select [list {*}$res]
}


#-------------------------------------------------------------------------
# ORDER BY clauses
#

drop_all_tables
do_execsql_test e_select-8.1.0 {
  CREATE TABLE d1(x, y, z);

  INSERT INTO d1 VALUES(1, 2, 3);
  INSERT INTO d1 VALUES(2, 5, -1);
  INSERT INTO d1 VALUES(1, 2, 8);
  INSERT INTO d1 VALUES(1, 2, 7);
  INSERT INTO d1 VALUES(2, 4, 93);
  INSERT INTO d1 VALUES(1, 2, -20);
  INSERT INTO d1 VALUES(1, 4, 93);
  INSERT INTO d1 VALUES(1, 5, -1);

  CREATE TABLE d2(a, b);
  INSERT INTO d2 VALUES('gently', 'failings');
  INSERT INTO d2 VALUES('commercials', 'bathrobe');
  INSERT INTO d2 VALUES('iterate', 'sexton');
  INSERT INTO d2 VALUES('babied', 'charitableness');
  INSERT INTO d2 VALUES('solemnness', 'annexed');
  INSERT INTO d2 VALUES('rejoicing', 'liabilities');
  INSERT INTO d2 VALUES('pragmatist', 'guarded');
  INSERT INTO d2 VALUES('barked', 'interrupted');
  INSERT INTO d2 VALUES('reemphasizes', 'reply');
  INSERT INTO d2 VALUES('lad', 'relenting');
} {}

# EVIDENCE-OF: R-44988-41064 Rows are first sorted based on the results
# of evaluating the left-most expression in the ORDER BY list, then ties
# are broken by evaluating the second left-most expression and so on.
#
foreach {tn select res} {
  1  "SELECT * FROM d1 ORDER BY x, y, z" {
     1 2 -20    1 2 3    1 2 7    1 2 8    
     1 4  93    1 5 -1   2 4 93   2 5 -1
  }
} {
  do_execsql_test e_select-8.1.$tn $select [list {*}$res]
}

# EVIDENCE-OF: R-06617-54588 Each ORDER BY expression may be optionally
# followed by one of the keywords ASC (smaller values are returned
# first) or DESC (larger values are returned first).
#
#   Test cases e_select-8.2.* test the above.
#
# EVIDENCE-OF: R-18705-33393 If neither ASC or DESC are specified, rows
# are sorted in ascending (smaller values first) order by default.
#
#   Test cases e_select-8.3.* test the above. All 8.3 test cases are
#   copies of 8.2 test cases with the explicit "ASC" removed.
#
foreach {tn select res} {
  2.1  "SELECT * FROM d1 ORDER BY x ASC, y ASC, z ASC" {
     1 2 -20    1 2 3    1 2 7    1 2 8    
     1 4  93    1 5 -1   2 4 93   2 5 -1
  }
  2.2  "SELECT * FROM d1 ORDER BY x DESC, y DESC, z DESC" {
     2 5 -1     2 4 93   1 5 -1   1 4  93    
     1 2 8      1 2 7    1 2 3    1 2 -20    
  }
  2.3 "SELECT * FROM d1 ORDER BY x DESC, y ASC, z DESC" {
     2 4 93   2 5 -1     1 2 8      1 2 7    
     1 2 3    1 2 -20    1 4  93    1 5 -1   
  }
  2.4  "SELECT * FROM d1 ORDER BY x DESC, y ASC, z ASC" {
     2 4 93   2 5 -1     1 2 -20    1 2 3    
     1 2 7    1 2 8      1 4  93    1 5 -1   
  }

  3.1  "SELECT * FROM d1 ORDER BY x, y, z" {
     1 2 -20    1 2 3    1 2 7    1 2 8    
     1 4  93    1 5 -1   2 4 93   2 5 -1
  }
  3.3  "SELECT * FROM d1 ORDER BY x DESC, y, z DESC" {
     2 4 93   2 5 -1     1 2 8      1 2 7    
     1 2 3    1 2 -20    1 4  93    1 5 -1   
  }
  3.4 "SELECT * FROM d1 ORDER BY x DESC, y, z" {
     2 4 93   2 5 -1     1 2 -20    1 2 3    
     1 2 7    1 2 8      1 4  93    1 5 -1   
  }
} {
  do_execsql_test e_select-8.$tn $select [list {*}$res]
}

# EVIDENCE-OF: R-29779-04281 If the ORDER BY expression is a constant
# integer K then the expression is considered an alias for the K-th
# column of the result set (columns are numbered from left to right
# starting with 1).
#
foreach {tn select res} {
  1  "SELECT * FROM d1 ORDER BY 1 ASC, 2 ASC, 3 ASC" {
     1 2 -20    1 2 3    1 2 7    1 2 8    
     1 4  93    1 5 -1   2 4 93   2 5 -1
  }
  2  "SELECT * FROM d1 ORDER BY 1 DESC, 2 DESC, 3 DESC" {
     2 5 -1     2 4 93   1 5 -1   1 4  93    
     1 2 8      1 2 7    1 2 3    1 2 -20    
  }
  3 "SELECT * FROM d1 ORDER BY 1 DESC, 2 ASC, 3 DESC" {
     2 4 93   2 5 -1     1 2 8      1 2 7    
     1 2 3    1 2 -20    1 4  93    1 5 -1   
  }
  4  "SELECT * FROM d1 ORDER BY 1 DESC, 2 ASC, 3 ASC" {
     2 4 93   2 5 -1     1 2 -20    1 2 3    
     1 2 7    1 2 8      1 4  93    1 5 -1   
  }
  5  "SELECT * FROM d1 ORDER BY 1, 2, 3" {
     1 2 -20    1 2 3    1 2 7    1 2 8    
     1 4  93    1 5 -1   2 4 93   2 5 -1
  }
  6  "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3 DESC" {
     2 4 93   2 5 -1     1 2 8      1 2 7    
     1 2 3    1 2 -20    1 4  93    1 5 -1   
  }
  7  "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3" {
     2 4 93   2 5 -1     1 2 -20    1 2 3    
     1 2 7    1 2 8      1 4  93    1 5 -1   
  }
  8  "SELECT z, x FROM d1 ORDER BY 2" {
     3 1     8 1    7 1   -20 1 
     93 1   -1 1   -1 2   93 2
  }
  9  "SELECT z, x FROM d1 ORDER BY 1" {
     -20 1  -1 2   -1 1   3 1     
     7 1     8 1   93 2   93 1   
  }
} {
  do_execsql_test e_select-8.4.$tn $select [list {*}$res]
}

# EVIDENCE-OF: R-63286-51977 If the ORDER BY expression is an identifier
# that corresponds to the alias of one of the output columns, then the
# expression is considered an alias for that column.
#
foreach {tn select res} {
  1   "SELECT z+1 AS abc FROM d1 ORDER BY abc" {
    -19 0 0 4 8 9 94 94
  }
  2   "SELECT z+1 AS abc FROM d1 ORDER BY abc DESC" {
    94 94 9 8 4 0 0 -19
  }
  3  "SELECT z AS x, x AS z FROM d1 ORDER BY z" {
    3 1    8 1    7 1    -20 1    93 1    -1 1    -1 2    93 2
  }
  4  "SELECT z AS x, x AS z FROM d1 ORDER BY x" {
    -20 1    -1 2    -1 1    3 1    7 1    8 1    93 2    93 1
  }
} {
  do_execsql_test e_select-8.5.$tn $select [list {*}$res]
}

# EVIDENCE-OF: R-27923-38747 Otherwise, if the ORDER BY expression is
# any other expression, it is evaluated and the the returned value used
# to order the output rows.
#
# EVIDENCE-OF: R-03421-57988 If the SELECT statement is a simple SELECT,
# then an ORDER BY may contain any arbitrary expressions.
#
foreach {tn select res} {
  1   "SELECT * FROM d1 ORDER BY x+y+z" {
    1 2 -20    1 5 -1    1 2 3    2 5 -1 
    1 2 7      1 2 8     1 4 93   2 4 93
  }
  2   "SELECT * FROM d1 ORDER BY x*z" {
    1 2 -20    2 5 -1    1 5 -1    1 2 3 
    1 2 7      1 2 8     1 4 93    2 4 93
  }
  3   "SELECT * FROM d1 ORDER BY y*z" {
    1 2 -20    2 5 -1    1 5 -1    1 2 3 
    1 2 7      1 2 8     2 4 93    1 4 93
  }
} {
  do_execsql_test e_select-8.6.$tn $select [list {*}$res]
}

# EVIDENCE-OF: R-28853-08147 However, if the SELECT is a compound
# SELECT, then ORDER BY expressions that are not aliases to output
# columns must be exactly the same as an expression used as an output
# column.
#
foreach {tn select violation} {
  1   "SELECT x FROM d1 UNION ALL SELECT a FROM d2 ORDER BY x*z"        1st
  2   "SELECT x,z FROM d1 UNION ALL SELECT a,b FROM d2 ORDER BY x, x/z" 2nd
} {
  set err "$violation ORDER BY term does not match any column in the result set"
  do_catchsql_test e_select-8.7.1.$tn $select [list 1 $err]
}
foreach {tn select res} {
  1   "SELECT x*z FROM d1 UNION ALL SELECT a FROM d2 ORDER BY x*z" {
    -20 -2 -1 3 7 8 93 186 babied barked commercials gently 
    iterate lad pragmatist reemphasizes rejoicing solemnness
  }
  2   "SELECT x, x/z FROM d1 UNION ALL SELECT a,b FROM d2 ORDER BY x, x/z" {
    1 -1 1 0 1 0 1 0 1 0 1 0 2 -2 2 0 
    babied charitableness barked interrupted commercials bathrobe gently
    failings iterate sexton lad relenting pragmatist guarded reemphasizes reply
    rejoicing liabilities solemnness annexed
  }
} {
  do_execsql_test e_select-8.7.2.$tn $select [list {*}$res]
}

do_execsql_test e_select-8.8.0 {
  CREATE TABLE d3(a);
  INSERT INTO d3 VALUES('text');
  INSERT INTO d3 VALUES(14.1);
  INSERT INTO d3 VALUES(13);
  INSERT INTO d3 VALUES(X'78787878');
  INSERT INTO d3 VALUES(15);
  INSERT INTO d3 VALUES(12.9);
  INSERT INTO d3 VALUES(null);

  CREATE TABLE d4(x COLLATE nocase);
  INSERT INTO d4 VALUES('abc');
  INSERT INTO d4 VALUES('ghi');
  INSERT INTO d4 VALUES('DEF');
  INSERT INTO d4 VALUES('JKL');
} {}

# EVIDENCE-OF: R-10883-17697 For the purposes of sorting rows, values
# are compared in the same way as for comparison expressions.
#
#   The following tests verify that values of different types are sorted
#   correctly, and that mixed real and integer values are compared properly.
#
do_execsql_test e_select-8.8.1 {
  SELECT a FROM d3 ORDER BY a
} {{} 12.9 13 14.1 15 text xxxx}
do_execsql_test e_select-8.8.2 {
  SELECT a FROM d3 ORDER BY a DESC
} {xxxx text 15 14.1 13 12.9 {}}


# EVIDENCE-OF: R-64199-22471 If the ORDER BY expression is assigned a
# collation sequence using the postfix COLLATE operator, then the
# specified collation sequence is used.
#
do_execsql_test e_select-8.9.1 {
  SELECT x FROM d4 ORDER BY 1 COLLATE binary
} {DEF JKL abc ghi}
do_execsql_test e_select-8.9.2 {
  SELECT x COLLATE binary FROM d4 ORDER BY 1 COLLATE nocase
} {abc DEF ghi JKL}

# EVIDENCE-OF: R-09398-26102 Otherwise, if the ORDER BY expression is 
# an alias to an expression that has been assigned a collation sequence 
# using the postfix COLLATE operator, then the collation sequence 
# assigned to the aliased expression is used.
#
#   In the test 8.10.2, the only result-column expression has no alias. So the
#   ORDER BY expression is not a reference to it and therefore does not inherit
#   the collation sequence. In test 8.10.3, "x" is the alias (as well as the
#   column name), so the ORDER BY expression is interpreted as an alias and the
#   collation sequence attached to the result column is used for sorting.
#
do_execsql_test e_select-8.10.1 {
  SELECT x COLLATE binary FROM d4 ORDER BY 1
} {DEF JKL abc ghi}
do_execsql_test e_select-8.10.2 {
  SELECT x COLLATE binary FROM d4 ORDER BY x
} {abc DEF ghi JKL}
do_execsql_test e_select-8.10.3 {
  SELECT x COLLATE binary AS x FROM d4 ORDER BY x
} {DEF JKL abc ghi}

# EVIDENCE-OF: R-27301-09658 Otherwise, if the ORDER BY expression is a
# column or an alias of an expression that is a column, then the default
# collation sequence for the column is used.
#
do_execsql_test e_select-8.11.1 {
  SELECT x AS y FROM d4 ORDER BY y
} {abc DEF ghi JKL}
do_execsql_test e_select-8.11.2 {
  SELECT x||'' FROM d4 ORDER BY x
} {abc DEF ghi JKL}

# EVIDENCE-OF: R-49925-55905 Otherwise, the BINARY collation sequence is
# used.
#
do_execsql_test e_select-8.12.1 {
  SELECT x FROM d4 ORDER BY x||''
} {DEF JKL abc ghi}

finish_test