os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/descidx1.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2005 December 21
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#*************************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this script is descending indices.
sl@0
    13
#
sl@0
    14
# $Id: descidx1.test,v 1.10 2008/03/19 00:21:31 drh Exp $
sl@0
    15
#
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
db eval {PRAGMA legacy_file_format=OFF}
sl@0
    21
sl@0
    22
# This procedure sets the value of the file-format in file 'test.db'
sl@0
    23
# to $newval. Also, the schema cookie is incremented.
sl@0
    24
# 
sl@0
    25
proc set_file_format {newval} {
sl@0
    26
  hexio_write test.db 44 [hexio_render_int32 $newval]
sl@0
    27
  set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
sl@0
    28
  incr schemacookie
sl@0
    29
  hexio_write test.db 40 [hexio_render_int32 $schemacookie]
sl@0
    30
  return {}
sl@0
    31
}
sl@0
    32
sl@0
    33
# This procedure returns the value of the file-format in file 'test.db'.
sl@0
    34
# 
sl@0
    35
proc get_file_format {{fname test.db}} {
sl@0
    36
  return [hexio_get_int [hexio_read $fname 44 4]]
sl@0
    37
}
sl@0
    38
sl@0
    39
sl@0
    40
# Verify that the file format starts as 4.
sl@0
    41
#
sl@0
    42
do_test descidx1-1.1 {
sl@0
    43
  execsql {
sl@0
    44
    CREATE TABLE t1(a,b);
sl@0
    45
    CREATE INDEX i1 ON t1(b ASC);
sl@0
    46
  }
sl@0
    47
  get_file_format
sl@0
    48
} {4}
sl@0
    49
do_test descidx1-1.2 {
sl@0
    50
  execsql {
sl@0
    51
    CREATE INDEX i2 ON t1(a DESC);
sl@0
    52
  }
sl@0
    53
  get_file_format
sl@0
    54
} {4}
sl@0
    55
sl@0
    56
# Put some information in the table and verify that the descending
sl@0
    57
# index actually works.
sl@0
    58
#
sl@0
    59
do_test descidx1-2.1 {
sl@0
    60
  execsql {
sl@0
    61
    INSERT INTO t1 VALUES(1,1);
sl@0
    62
    INSERT INTO t1 VALUES(2,2);
sl@0
    63
    INSERT INTO t1 SELECT a+2, a+2 FROM t1;
sl@0
    64
    INSERT INTO t1 SELECT a+4, a+4 FROM t1;
sl@0
    65
    SELECT b FROM t1 WHERE a>3 AND a<7;
sl@0
    66
  }
sl@0
    67
} {6 5 4}
sl@0
    68
do_test descidx1-2.2 {
sl@0
    69
  execsql {
sl@0
    70
    SELECT a FROM t1 WHERE b>3 AND b<7;
sl@0
    71
  }
sl@0
    72
} {4 5 6}
sl@0
    73
do_test descidx1-2.3 {
sl@0
    74
  execsql {
sl@0
    75
    SELECT b FROM t1 WHERE a>=3 AND a<7;
sl@0
    76
  }
sl@0
    77
} {6 5 4 3}
sl@0
    78
do_test descidx1-2.4 {
sl@0
    79
  execsql {
sl@0
    80
    SELECT b FROM t1 WHERE a>3 AND a<=7;
sl@0
    81
  }
sl@0
    82
} {7 6 5 4}
sl@0
    83
do_test descidx1-2.5 {
sl@0
    84
  execsql {
sl@0
    85
    SELECT b FROM t1 WHERE a>=3 AND a<=7;
sl@0
    86
  }
sl@0
    87
} {7 6 5 4 3}
sl@0
    88
do_test descidx1-2.6 {
sl@0
    89
  execsql {
sl@0
    90
    SELECT a FROM t1 WHERE b>=3 AND b<=7;
sl@0
    91
  }
sl@0
    92
} {3 4 5 6 7}
sl@0
    93
sl@0
    94
# This procedure executes the SQL.  Then it checks to see if the OP_Sort
sl@0
    95
# opcode was executed.  If an OP_Sort did occur, then "sort" is appended
sl@0
    96
# to the result.  If no OP_Sort happened, then "nosort" is appended.
sl@0
    97
#
sl@0
    98
# This procedure is used to check to make sure sorting is or is not
sl@0
    99
# occurring as expected.
sl@0
   100
#
sl@0
   101
proc cksort {sql} {
sl@0
   102
  set ::sqlite_sort_count 0
sl@0
   103
  set data [execsql $sql]
sl@0
   104
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
   105
  lappend data $x
sl@0
   106
  return $data
sl@0
   107
}
sl@0
   108
sl@0
   109
# Test sorting using a descending index.
sl@0
   110
#
sl@0
   111
do_test descidx1-3.1 {
sl@0
   112
  cksort {SELECT a FROM t1 ORDER BY a}
sl@0
   113
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   114
do_test descidx1-3.2 {
sl@0
   115
  cksort {SELECT a FROM t1 ORDER BY a ASC}
sl@0
   116
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   117
do_test descidx1-3.3 {
sl@0
   118
  cksort {SELECT a FROM t1 ORDER BY a DESC}
sl@0
   119
} {8 7 6 5 4 3 2 1 nosort}
sl@0
   120
do_test descidx1-3.4 {
sl@0
   121
  cksort {SELECT b FROM t1 ORDER BY a}
sl@0
   122
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   123
do_test descidx1-3.5 {
sl@0
   124
  cksort {SELECT b FROM t1 ORDER BY a ASC}
sl@0
   125
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   126
do_test descidx1-3.6 {
sl@0
   127
  cksort {SELECT b FROM t1 ORDER BY a DESC}
sl@0
   128
} {8 7 6 5 4 3 2 1 nosort}
sl@0
   129
do_test descidx1-3.7 {
sl@0
   130
  cksort {SELECT a FROM t1 ORDER BY b}
sl@0
   131
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   132
do_test descidx1-3.8 {
sl@0
   133
  cksort {SELECT a FROM t1 ORDER BY b ASC}
sl@0
   134
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   135
do_test descidx1-3.9 {
sl@0
   136
  cksort {SELECT a FROM t1 ORDER BY b DESC}
sl@0
   137
} {8 7 6 5 4 3 2 1 nosort}
sl@0
   138
do_test descidx1-3.10 {
sl@0
   139
  cksort {SELECT b FROM t1 ORDER BY b}
sl@0
   140
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   141
do_test descidx1-3.11 {
sl@0
   142
  cksort {SELECT b FROM t1 ORDER BY b ASC}
sl@0
   143
} {1 2 3 4 5 6 7 8 nosort}
sl@0
   144
do_test descidx1-3.12 {
sl@0
   145
  cksort {SELECT b FROM t1 ORDER BY b DESC}
sl@0
   146
} {8 7 6 5 4 3 2 1 nosort}
sl@0
   147
sl@0
   148
do_test descidx1-3.21 {
sl@0
   149
  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a}
sl@0
   150
} {4 5 6 7 nosort}
sl@0
   151
do_test descidx1-3.22 {
sl@0
   152
  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
sl@0
   153
} {4 5 6 7 nosort}
sl@0
   154
do_test descidx1-3.23 {
sl@0
   155
  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
sl@0
   156
} {7 6 5 4 nosort}
sl@0
   157
do_test descidx1-3.24 {
sl@0
   158
  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a}
sl@0
   159
} {4 5 6 7 nosort}
sl@0
   160
do_test descidx1-3.25 {
sl@0
   161
  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
sl@0
   162
} {4 5 6 7 nosort}
sl@0
   163
do_test descidx1-3.26 {
sl@0
   164
  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
sl@0
   165
} {7 6 5 4 nosort}
sl@0
   166
sl@0
   167
# Create a table with indices that are descending on some terms and
sl@0
   168
# ascending on others.
sl@0
   169
#
sl@0
   170
ifcapable bloblit {
sl@0
   171
  do_test descidx1-4.1 {
sl@0
   172
    execsql {
sl@0
   173
      CREATE TABLE t2(a INT, b TEXT, c BLOB, d REAL);
sl@0
   174
      CREATE INDEX i3 ON t2(a ASC, b DESC, c ASC);
sl@0
   175
      CREATE INDEX i4 ON t2(b DESC, a ASC, d DESC);
sl@0
   176
      INSERT INTO t2 VALUES(1,'one',x'31',1.0);
sl@0
   177
      INSERT INTO t2 VALUES(2,'two',x'3232',2.0);
sl@0
   178
      INSERT INTO t2 VALUES(3,'three',x'333333',3.0);
sl@0
   179
      INSERT INTO t2 VALUES(4,'four',x'34343434',4.0);
sl@0
   180
      INSERT INTO t2 VALUES(5,'five',x'3535353535',5.0);
sl@0
   181
      INSERT INTO t2 VALUES(6,'six',x'363636363636',6.0);
sl@0
   182
      INSERT INTO t2 VALUES(2,'two',x'323232',2.1);
sl@0
   183
      INSERT INTO t2 VALUES(2,'zwei',x'3232',2.2);
sl@0
   184
      INSERT INTO t2 VALUES(2,NULL,NULL,2.3);
sl@0
   185
      SELECT count(*) FROM t2;
sl@0
   186
    }
sl@0
   187
  } {9}
sl@0
   188
  do_test descidx1-4.2 {
sl@0
   189
    execsql {
sl@0
   190
      SELECT d FROM t2 ORDER BY a;
sl@0
   191
    }
sl@0
   192
  } {1.0 2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
sl@0
   193
  do_test descidx1-4.3 {
sl@0
   194
    execsql {
sl@0
   195
      SELECT d FROM t2 WHERE a>=2;
sl@0
   196
    }
sl@0
   197
  } {2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
sl@0
   198
  do_test descidx1-4.4 {
sl@0
   199
    execsql {
sl@0
   200
      SELECT d FROM t2 WHERE a>2;
sl@0
   201
    }
sl@0
   202
  } {3.0 4.0 5.0 6.0}
sl@0
   203
  do_test descidx1-4.5 {
sl@0
   204
    execsql {
sl@0
   205
      SELECT d FROM t2 WHERE a=2 AND b>'two';
sl@0
   206
    }
sl@0
   207
  } {2.2}
sl@0
   208
  do_test descidx1-4.6 {
sl@0
   209
    execsql {
sl@0
   210
      SELECT d FROM t2 WHERE a=2 AND b>='two';
sl@0
   211
    }
sl@0
   212
  } {2.2 2.0 2.1}
sl@0
   213
  do_test descidx1-4.7 {
sl@0
   214
    execsql {
sl@0
   215
      SELECT d FROM t2 WHERE a=2 AND b<'two';
sl@0
   216
    }
sl@0
   217
  } {}
sl@0
   218
  do_test descidx1-4.8 {
sl@0
   219
    execsql {
sl@0
   220
      SELECT d FROM t2 WHERE a=2 AND b<='two';
sl@0
   221
    }
sl@0
   222
  } {2.0 2.1}
sl@0
   223
}
sl@0
   224
sl@0
   225
do_test descidx1-5.1 {
sl@0
   226
  execsql {
sl@0
   227
    CREATE TABLE t3(a,b,c,d);
sl@0
   228
    CREATE INDEX t3i1 ON t3(a DESC, b ASC, c DESC, d ASC);
sl@0
   229
    INSERT INTO t3 VALUES(0,0,0,0);
sl@0
   230
    INSERT INTO t3 VALUES(0,0,0,1);
sl@0
   231
    INSERT INTO t3 VALUES(0,0,1,0);
sl@0
   232
    INSERT INTO t3 VALUES(0,0,1,1);
sl@0
   233
    INSERT INTO t3 VALUES(0,1,0,0);
sl@0
   234
    INSERT INTO t3 VALUES(0,1,0,1);
sl@0
   235
    INSERT INTO t3 VALUES(0,1,1,0);
sl@0
   236
    INSERT INTO t3 VALUES(0,1,1,1);
sl@0
   237
    INSERT INTO t3 VALUES(1,0,0,0);
sl@0
   238
    INSERT INTO t3 VALUES(1,0,0,1);
sl@0
   239
    INSERT INTO t3 VALUES(1,0,1,0);
sl@0
   240
    INSERT INTO t3 VALUES(1,0,1,1);
sl@0
   241
    INSERT INTO t3 VALUES(1,1,0,0);
sl@0
   242
    INSERT INTO t3 VALUES(1,1,0,1);
sl@0
   243
    INSERT INTO t3 VALUES(1,1,1,0);
sl@0
   244
    INSERT INTO t3 VALUES(1,1,1,1);
sl@0
   245
    SELECT count(*) FROM t3;
sl@0
   246
  }
sl@0
   247
} {16}
sl@0
   248
do_test descidx1-5.2 {
sl@0
   249
  cksort {
sl@0
   250
    SELECT a||b||c||d FROM t3 ORDER BY a,b,c,d;
sl@0
   251
  }
sl@0
   252
} {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 sort}
sl@0
   253
do_test descidx1-5.3 {
sl@0
   254
  cksort {
sl@0
   255
    SELECT a||b||c||d FROM t3 ORDER BY a DESC, b ASC, c DESC, d ASC;
sl@0
   256
  }
sl@0
   257
} {1010 1011 1000 1001 1110 1111 1100 1101 0010 0011 0000 0001 0110 0111 0100 0101 nosort}
sl@0
   258
do_test descidx1-5.4 {
sl@0
   259
  cksort {
sl@0
   260
    SELECT a||b||c||d FROM t3 ORDER BY a ASC, b DESC, c ASC, d DESC;
sl@0
   261
  }
sl@0
   262
} {0101 0100 0111 0110 0001 0000 0011 0010 1101 1100 1111 1110 1001 1000 1011 1010 nosort}
sl@0
   263
do_test descidx1-5.5 {
sl@0
   264
  cksort {
sl@0
   265
    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b ASC, c DESC
sl@0
   266
  }
sl@0
   267
} {101 100 111 110 001 000 011 010 nosort}
sl@0
   268
do_test descidx1-5.6 {
sl@0
   269
  cksort {
sl@0
   270
    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c ASC
sl@0
   271
  }
sl@0
   272
} {010 011 000 001 110 111 100 101 nosort}
sl@0
   273
do_test descidx1-5.7 {
sl@0
   274
  cksort {
sl@0
   275
    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c DESC
sl@0
   276
  }
sl@0
   277
} {011 010 001 000 111 110 101 100 sort}
sl@0
   278
do_test descidx1-5.8 {
sl@0
   279
  cksort {
sl@0
   280
    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b ASC, c ASC
sl@0
   281
  }
sl@0
   282
} {000 001 010 011 100 101 110 111 sort}
sl@0
   283
do_test descidx1-5.9 {
sl@0
   284
  cksort {
sl@0
   285
    SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b DESC, c ASC
sl@0
   286
  }
sl@0
   287
} {110 111 100 101 010 011 000 001 sort}
sl@0
   288
sl@0
   289
# Test the legacy_file_format pragma here because we have access to
sl@0
   290
# the get_file_format command.
sl@0
   291
#
sl@0
   292
ifcapable legacyformat {
sl@0
   293
  do_test descidx1-6.1 {
sl@0
   294
    db close
sl@0
   295
    file delete -force test.db test.db-journal
sl@0
   296
    sqlite3 db test.db
sl@0
   297
    execsql {PRAGMA legacy_file_format}
sl@0
   298
  } {1}
sl@0
   299
} else {
sl@0
   300
  do_test descidx1-6.1 {
sl@0
   301
    db close
sl@0
   302
    file delete -force test.db test.db-journal
sl@0
   303
    sqlite3 db test.db
sl@0
   304
    execsql {PRAGMA legacy_file_format}
sl@0
   305
  } {0}
sl@0
   306
}
sl@0
   307
do_test descidx1-6.2 {
sl@0
   308
  execsql {PRAGMA legacy_file_format=YES}
sl@0
   309
  execsql {PRAGMA legacy_file_format}
sl@0
   310
} {1}
sl@0
   311
do_test descidx1-6.3 {
sl@0
   312
  execsql {
sl@0
   313
    CREATE TABLE t1(a,b,c);
sl@0
   314
  }
sl@0
   315
  get_file_format
sl@0
   316
} {1}
sl@0
   317
ifcapable vacuum {
sl@0
   318
  # Verify that the file format is preserved across a vacuum.
sl@0
   319
  do_test descidx1-6.3.1 {
sl@0
   320
    execsql {VACUUM}
sl@0
   321
    get_file_format
sl@0
   322
  } {1}
sl@0
   323
}
sl@0
   324
do_test descidx1-6.4 {
sl@0
   325
  db close
sl@0
   326
  file delete -force test.db test.db-journal
sl@0
   327
  sqlite3 db test.db
sl@0
   328
  execsql {PRAGMA legacy_file_format=NO}
sl@0
   329
  execsql {PRAGMA legacy_file_format}
sl@0
   330
} {0}
sl@0
   331
do_test descidx1-6.5 {
sl@0
   332
  execsql {
sl@0
   333
    CREATE TABLE t1(a,b,c);
sl@0
   334
    CREATE INDEX i1 ON t1(a ASC, b DESC, c ASC);
sl@0
   335
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   336
    INSERT INTO t1 VALUES(1,1,0);
sl@0
   337
    INSERT INTO t1 VALUES(1,2,1);
sl@0
   338
    INSERT INTO t1 VALUES(1,3,4);
sl@0
   339
  }
sl@0
   340
  get_file_format
sl@0
   341
} {4}
sl@0
   342
ifcapable vacuum {
sl@0
   343
  # Verify that the file format is preserved across a vacuum.
sl@0
   344
  do_test descidx1-6.6 {
sl@0
   345
    execsql {VACUUM}
sl@0
   346
    get_file_format
sl@0
   347
  } {4}
sl@0
   348
  do_test descidx1-6.7 {
sl@0
   349
    execsql {
sl@0
   350
      PRAGMA legacy_file_format=ON;
sl@0
   351
      VACUUM;
sl@0
   352
    }
sl@0
   353
    get_file_format
sl@0
   354
  } {4}
sl@0
   355
} 
sl@0
   356
sl@0
   357
sl@0
   358
sl@0
   359
finish_test