os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/minmax2.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
# 2007 July 17
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 file is testing SELECT statements that contain
sl@0
    13
# aggregate min() and max() functions and which are handled as
sl@0
    14
# as a special case.  This file makes sure that the min/max
sl@0
    15
# optimization works right in the presence of descending
sl@0
    16
# indices.  Ticket #2514.
sl@0
    17
#
sl@0
    18
# $Id: minmax2.test,v 1.2 2008/01/05 17:39:30 danielk1977 Exp $
sl@0
    19
sl@0
    20
set testdir [file dirname $argv0]
sl@0
    21
source $testdir/tester.tcl
sl@0
    22
sl@0
    23
do_test minmax2-1.0 {
sl@0
    24
  execsql {
sl@0
    25
    PRAGMA legacy_file_format=0;
sl@0
    26
    BEGIN;
sl@0
    27
    CREATE TABLE t1(x, y);
sl@0
    28
    INSERT INTO t1 VALUES(1,1);
sl@0
    29
    INSERT INTO t1 VALUES(2,2);
sl@0
    30
    INSERT INTO t1 VALUES(3,2);
sl@0
    31
    INSERT INTO t1 VALUES(4,3);
sl@0
    32
    INSERT INTO t1 VALUES(5,3);
sl@0
    33
    INSERT INTO t1 VALUES(6,3);
sl@0
    34
    INSERT INTO t1 VALUES(7,3);
sl@0
    35
    INSERT INTO t1 VALUES(8,4);
sl@0
    36
    INSERT INTO t1 VALUES(9,4);
sl@0
    37
    INSERT INTO t1 VALUES(10,4);
sl@0
    38
    INSERT INTO t1 VALUES(11,4);
sl@0
    39
    INSERT INTO t1 VALUES(12,4);
sl@0
    40
    INSERT INTO t1 VALUES(13,4);
sl@0
    41
    INSERT INTO t1 VALUES(14,4);
sl@0
    42
    INSERT INTO t1 VALUES(15,4);
sl@0
    43
    INSERT INTO t1 VALUES(16,5);
sl@0
    44
    INSERT INTO t1 VALUES(17,5);
sl@0
    45
    INSERT INTO t1 VALUES(18,5);
sl@0
    46
    INSERT INTO t1 VALUES(19,5);
sl@0
    47
    INSERT INTO t1 VALUES(20,5);
sl@0
    48
    COMMIT;
sl@0
    49
    SELECT DISTINCT y FROM t1 ORDER BY y;
sl@0
    50
  }
sl@0
    51
} {1 2 3 4 5}
sl@0
    52
sl@0
    53
do_test minmax2-1.1 {
sl@0
    54
  set sqlite_search_count 0
sl@0
    55
  execsql {SELECT min(x) FROM t1}
sl@0
    56
} {1}
sl@0
    57
do_test minmax2-1.2 {
sl@0
    58
  set sqlite_search_count
sl@0
    59
} {19}
sl@0
    60
do_test minmax2-1.3 {
sl@0
    61
  set sqlite_search_count 0
sl@0
    62
  execsql {SELECT max(x) FROM t1}
sl@0
    63
} {20}
sl@0
    64
do_test minmax2-1.4 {
sl@0
    65
  set sqlite_search_count
sl@0
    66
} {19}
sl@0
    67
do_test minmax2-1.5 {
sl@0
    68
  execsql {CREATE INDEX t1i1 ON t1(x DESC)}
sl@0
    69
  set sqlite_search_count 0
sl@0
    70
  execsql {SELECT min(x) FROM t1}
sl@0
    71
} {1}
sl@0
    72
do_test minmax2-1.6 {
sl@0
    73
  set sqlite_search_count
sl@0
    74
} {1}
sl@0
    75
do_test minmax2-1.7 {
sl@0
    76
  set sqlite_search_count 0
sl@0
    77
  execsql {SELECT max(x) FROM t1}
sl@0
    78
} {20}
sl@0
    79
do_test minmax2-1.8 {
sl@0
    80
  set sqlite_search_count
sl@0
    81
} {0}
sl@0
    82
do_test minmax2-1.9 {
sl@0
    83
  set sqlite_search_count 0
sl@0
    84
  execsql {SELECT max(y) FROM t1}
sl@0
    85
} {5}
sl@0
    86
do_test minmax2-1.10 {
sl@0
    87
  set sqlite_search_count
sl@0
    88
} {19}
sl@0
    89
sl@0
    90
do_test minmax2-2.0 {
sl@0
    91
  execsql {
sl@0
    92
    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
sl@0
    93
    INSERT INTO t2 SELECT * FROM t1;
sl@0
    94
  }
sl@0
    95
  set sqlite_search_count 0
sl@0
    96
  execsql {SELECT min(a) FROM t2}
sl@0
    97
} {1}
sl@0
    98
do_test minmax2-2.1 {
sl@0
    99
  set sqlite_search_count
sl@0
   100
} {0}
sl@0
   101
do_test minmax2-2.2 {
sl@0
   102
  set sqlite_search_count 0
sl@0
   103
  execsql {SELECT max(a) FROM t2}
sl@0
   104
} {20}
sl@0
   105
do_test minmax2-2.3 {
sl@0
   106
  set sqlite_search_count
sl@0
   107
} {0}
sl@0
   108
sl@0
   109
do_test minmax2-3.0 {
sl@0
   110
  ifcapable subquery {
sl@0
   111
    execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
sl@0
   112
  } else {
sl@0
   113
    db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
sl@0
   114
    execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
sl@0
   115
  }
sl@0
   116
  set sqlite_search_count 0
sl@0
   117
  execsql {SELECT max(a) FROM t2}
sl@0
   118
} {21}
sl@0
   119
do_test minmax2-3.1 {
sl@0
   120
  set sqlite_search_count
sl@0
   121
} {0}
sl@0
   122
do_test minmax2-3.2 {
sl@0
   123
  ifcapable subquery {
sl@0
   124
    execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
sl@0
   125
  } else {
sl@0
   126
    db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
sl@0
   127
    execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
sl@0
   128
  }
sl@0
   129
  set sqlite_search_count 0
sl@0
   130
  ifcapable subquery {
sl@0
   131
    execsql { SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2) }
sl@0
   132
  } else {
sl@0
   133
    execsql { SELECT b FROM t2 WHERE a=max_a_t2() }
sl@0
   134
  }
sl@0
   135
} {999}
sl@0
   136
do_test minmax2-3.3 {
sl@0
   137
  set sqlite_search_count
sl@0
   138
} {0}
sl@0
   139
sl@0
   140
ifcapable {compound && subquery} {
sl@0
   141
  do_test minmax2-4.1 {
sl@0
   142
    execsql {
sl@0
   143
      SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
sl@0
   144
        (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
sl@0
   145
    }
sl@0
   146
  } {1 20}
sl@0
   147
  do_test minmax2-4.2 {
sl@0
   148
    execsql {
sl@0
   149
      SELECT y, coalesce(sum(x),0) FROM
sl@0
   150
        (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
sl@0
   151
      GROUP BY y ORDER BY y;
sl@0
   152
    }
sl@0
   153
  } {1 1 2 5 3 22 4 92 5 90 6 0}
sl@0
   154
  do_test minmax2-4.3 {
sl@0
   155
    execsql {
sl@0
   156
      SELECT y, count(x), count(*) FROM
sl@0
   157
        (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
sl@0
   158
      GROUP BY y ORDER BY y;
sl@0
   159
    }
sl@0
   160
  } {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
sl@0
   161
} ;# ifcapable compound
sl@0
   162
sl@0
   163
# Make sure the min(x) and max(x) optimizations work on empty tables
sl@0
   164
# including empty tables with indices. Ticket #296.
sl@0
   165
#
sl@0
   166
do_test minmax2-5.1 {
sl@0
   167
  execsql {
sl@0
   168
    CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
sl@0
   169
    SELECT coalesce(min(x),999) FROM t3;
sl@0
   170
  }
sl@0
   171
} {999}
sl@0
   172
do_test minmax2-5.2 {
sl@0
   173
  execsql {
sl@0
   174
    SELECT coalesce(min(rowid),999) FROM t3;
sl@0
   175
  }
sl@0
   176
} {999}
sl@0
   177
do_test minmax2-5.3 {
sl@0
   178
  execsql {
sl@0
   179
    SELECT coalesce(max(x),999) FROM t3;
sl@0
   180
  }
sl@0
   181
} {999}
sl@0
   182
do_test minmax2-5.4 {
sl@0
   183
  execsql {
sl@0
   184
    SELECT coalesce(max(rowid),999) FROM t3;
sl@0
   185
  }
sl@0
   186
} {999}
sl@0
   187
do_test minmax2-5.5 {
sl@0
   188
  execsql {
sl@0
   189
    SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
sl@0
   190
  }
sl@0
   191
} {999}
sl@0
   192
sl@0
   193
# Make sure the min(x) and max(x) optimizations work when there
sl@0
   194
# is a LIMIT clause.  Ticket #396.
sl@0
   195
#
sl@0
   196
do_test minmax2-6.1 {
sl@0
   197
  execsql {
sl@0
   198
    SELECT min(a) FROM t2 LIMIT 1
sl@0
   199
  }
sl@0
   200
} {1}
sl@0
   201
do_test minmax2-6.2 {
sl@0
   202
  execsql {
sl@0
   203
    SELECT max(a) FROM t2 LIMIT 3
sl@0
   204
  }
sl@0
   205
} {22}
sl@0
   206
do_test minmax2-6.3 {
sl@0
   207
  execsql {
sl@0
   208
    SELECT min(a) FROM t2 LIMIT 0,100
sl@0
   209
  }
sl@0
   210
} {1}
sl@0
   211
do_test minmax2-6.4 {
sl@0
   212
  execsql {
sl@0
   213
    SELECT max(a) FROM t2 LIMIT 1,100
sl@0
   214
  }
sl@0
   215
} {}
sl@0
   216
do_test minmax2-6.5 {
sl@0
   217
  execsql {
sl@0
   218
    SELECT min(x) FROM t3 LIMIT 1
sl@0
   219
  }
sl@0
   220
} {{}}
sl@0
   221
do_test minmax2-6.6 {
sl@0
   222
  execsql {
sl@0
   223
    SELECT max(x) FROM t3 LIMIT 0
sl@0
   224
  }
sl@0
   225
} {}
sl@0
   226
do_test minmax2-6.7 {
sl@0
   227
  execsql {
sl@0
   228
    SELECT max(a) FROM t2 LIMIT 0
sl@0
   229
  }
sl@0
   230
} {}
sl@0
   231
sl@0
   232
# Make sure the max(x) and min(x) optimizations work for nested
sl@0
   233
# queries.  Ticket #587.
sl@0
   234
#
sl@0
   235
do_test minmax2-7.1 {
sl@0
   236
  execsql {
sl@0
   237
    SELECT max(x) FROM t1;
sl@0
   238
  }
sl@0
   239
} 20
sl@0
   240
ifcapable subquery {
sl@0
   241
  do_test minmax2-7.2 {
sl@0
   242
    execsql {
sl@0
   243
      SELECT * FROM (SELECT max(x) FROM t1);
sl@0
   244
    }
sl@0
   245
  } 20
sl@0
   246
}
sl@0
   247
do_test minmax2-7.3 {
sl@0
   248
  execsql {
sl@0
   249
    SELECT min(x) FROM t1;
sl@0
   250
  }
sl@0
   251
} 1
sl@0
   252
ifcapable subquery {
sl@0
   253
  do_test minmax2-7.4 {
sl@0
   254
    execsql {
sl@0
   255
      SELECT * FROM (SELECT min(x) FROM t1);
sl@0
   256
    }
sl@0
   257
  } 1
sl@0
   258
}
sl@0
   259
sl@0
   260
# Make sure min(x) and max(x) work correctly when the datatype is
sl@0
   261
# TEXT instead of NUMERIC.  Ticket #623.
sl@0
   262
#
sl@0
   263
do_test minmax2-8.1 {
sl@0
   264
  execsql {
sl@0
   265
    CREATE TABLE t4(a TEXT);
sl@0
   266
    INSERT INTO t4 VALUES('1234');
sl@0
   267
    INSERT INTO t4 VALUES('234');
sl@0
   268
    INSERT INTO t4 VALUES('34');
sl@0
   269
    SELECT min(a), max(a) FROM t4;
sl@0
   270
  }
sl@0
   271
} {1234 34}
sl@0
   272
do_test minmax2-8.2 {
sl@0
   273
  execsql {
sl@0
   274
    CREATE TABLE t5(a INTEGER);
sl@0
   275
    INSERT INTO t5 VALUES('1234');
sl@0
   276
    INSERT INTO t5 VALUES('234');
sl@0
   277
    INSERT INTO t5 VALUES('34');
sl@0
   278
    SELECT min(a), max(a) FROM t5;
sl@0
   279
  }
sl@0
   280
} {34 1234}
sl@0
   281
sl@0
   282
# Ticket #658:  Test the min()/max() optimization when the FROM clause
sl@0
   283
# is a subquery.
sl@0
   284
#
sl@0
   285
ifcapable {compound && subquery} {
sl@0
   286
  do_test minmax2-9.1 {
sl@0
   287
    execsql {
sl@0
   288
      SELECT max(rowid) FROM (
sl@0
   289
        SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
sl@0
   290
      )
sl@0
   291
    }
sl@0
   292
  } {1}
sl@0
   293
  do_test minmax2-9.2 {
sl@0
   294
    execsql {
sl@0
   295
      SELECT max(rowid) FROM (
sl@0
   296
        SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
sl@0
   297
      )
sl@0
   298
    }
sl@0
   299
  } {{}}
sl@0
   300
} ;# ifcapable compound&&subquery
sl@0
   301
sl@0
   302
# If there is a NULL in an aggregate max() or min(), ignore it.  An
sl@0
   303
# aggregate min() or max() will only return NULL if all values are NULL.
sl@0
   304
#
sl@0
   305
do_test minmax2-10.1 {
sl@0
   306
  execsql {
sl@0
   307
    CREATE TABLE t6(x);
sl@0
   308
    INSERT INTO t6 VALUES(1);
sl@0
   309
    INSERT INTO t6 VALUES(2);
sl@0
   310
    INSERT INTO t6 VALUES(NULL);
sl@0
   311
    SELECT coalesce(min(x),-1) FROM t6;
sl@0
   312
  }
sl@0
   313
} {1}
sl@0
   314
do_test minmax2-10.2 {
sl@0
   315
  execsql {
sl@0
   316
    SELECT max(x) FROM t6;
sl@0
   317
  }
sl@0
   318
} {2}
sl@0
   319
do_test minmax2-10.3 {
sl@0
   320
  execsql {
sl@0
   321
    CREATE INDEX i6 ON t6(x DESC);
sl@0
   322
    SELECT coalesce(min(x),-1) FROM t6;
sl@0
   323
  }
sl@0
   324
} {1}
sl@0
   325
do_test minmax2-10.4 {
sl@0
   326
  execsql {
sl@0
   327
    SELECT max(x) FROM t6;
sl@0
   328
  }
sl@0
   329
} {2}
sl@0
   330
do_test minmax2-10.5 {
sl@0
   331
  execsql {
sl@0
   332
    DELETE FROM t6 WHERE x NOT NULL;
sl@0
   333
    SELECT count(*) FROM t6;
sl@0
   334
  }
sl@0
   335
} 1
sl@0
   336
do_test minmax2-10.6 {
sl@0
   337
  execsql {
sl@0
   338
    SELECT count(x) FROM t6;
sl@0
   339
  }
sl@0
   340
} 0
sl@0
   341
ifcapable subquery {
sl@0
   342
  do_test minmax2-10.7 {
sl@0
   343
    execsql {
sl@0
   344
      SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
sl@0
   345
    }
sl@0
   346
  } {{} {}}
sl@0
   347
}
sl@0
   348
do_test minmax2-10.8 {
sl@0
   349
  execsql {
sl@0
   350
    SELECT min(x), max(x) FROM t6;
sl@0
   351
  }
sl@0
   352
} {{} {}}
sl@0
   353
do_test minmax2-10.9 {
sl@0
   354
  execsql {
sl@0
   355
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   356
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   357
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   358
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   359
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   360
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   361
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   362
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   363
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   364
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   365
    SELECT count(*) FROM t6;
sl@0
   366
  }
sl@0
   367
} 1024
sl@0
   368
do_test minmax2-10.10 {
sl@0
   369
  execsql {
sl@0
   370
    SELECT count(x) FROM t6;
sl@0
   371
  }
sl@0
   372
} 0
sl@0
   373
ifcapable subquery {
sl@0
   374
  do_test minmax2-10.11 {
sl@0
   375
    execsql {
sl@0
   376
      SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
sl@0
   377
    }
sl@0
   378
  } {{} {}}
sl@0
   379
}
sl@0
   380
do_test minmax2-10.12 {
sl@0
   381
  execsql {
sl@0
   382
    SELECT min(x), max(x) FROM t6;
sl@0
   383
  }
sl@0
   384
} {{} {}}
sl@0
   385
sl@0
   386
sl@0
   387
finish_test