xref: /sqlite-3.40.0/test/index6.test (revision 8a9789b6)
11fe0537eSdrh# 2013-07-31
21fe0537eSdrh#
31fe0537eSdrh# The author disclaims copyright to this source code.  In place of
41fe0537eSdrh# a legal notice, here is a blessing:
51fe0537eSdrh#
61fe0537eSdrh#    May you do good and not evil.
71fe0537eSdrh#    May you find forgiveness for yourself and forgive others.
81fe0537eSdrh#    May you share freely, never taking more than you give.
91fe0537eSdrh#
101fe0537eSdrh#***********************************************************************
111fe0537eSdrh#
121fe0537eSdrh# Test cases for partial indices
131fe0537eSdrh#
141fe0537eSdrh
151fe0537eSdrh
161fe0537eSdrhset testdir [file dirname $argv0]
171fe0537eSdrhsource $testdir/tester.tcl
181fe0537eSdrh
191fe0537eSdrhload_static_extension db wholenumber;
201fe0537eSdrhdo_test index6-1.1 {
211fe0537eSdrh  execsql {
221fe0537eSdrh    CREATE TABLE t1(a,b);
231fe0537eSdrh    CREATE INDEX t1a ON t1(a) WHERE a IS NOT NULL;
241fe0537eSdrh    CREATE INDEX t1b ON t1(b) WHERE b>10;
251fe0537eSdrh    CREATE VIRTUAL TABLE nums USING wholenumber;
261fe0537eSdrh    INSERT INTO t1(a,b)
271fe0537eSdrh       SELECT CASE WHEN value%3!=0 THEN value END, value
281fe0537eSdrh         FROM nums WHERE value<=20;
291fe0537eSdrh    SELECT count(a), count(b) FROM t1;
301fe0537eSdrh  }
311fe0537eSdrh} {14 20}
321fe0537eSdrh
333780be11Sdrhdo_test index6-1.2 {
343780be11Sdrh  catchsql {
353780be11Sdrh    CREATE INDEX bad1 ON t1(a,b) WHERE c IS NOT NULL;
363780be11Sdrh  }
373780be11Sdrh} {1 {no such column: c}}
383780be11Sdrhdo_test index6-1.3 {
393780be11Sdrh  catchsql {
403780be11Sdrh    CREATE INDEX bad1 ON t1(a,b) WHERE EXISTS(SELECT * FROM t1);
413780be11Sdrh  }
423780be11Sdrh} {1 {subqueries prohibited in partial index WHERE clauses}}
433780be11Sdrhdo_test index6-1.4 {
443780be11Sdrh  catchsql {
453780be11Sdrh    CREATE INDEX bad1 ON t1(a,b) WHERE a!=?1;
463780be11Sdrh  }
473780be11Sdrh} {1 {parameters prohibited in partial index WHERE clauses}}
483780be11Sdrhdo_test index6-1.5 {
493780be11Sdrh  catchsql {
503780be11Sdrh    CREATE INDEX bad1 ON t1(a,b) WHERE a!=random();
513780be11Sdrh  }
523780be11Sdrh} {1 {functions prohibited in partial index WHERE clauses}}
533780be11Sdrhdo_test index6-1.6 {
543780be11Sdrh  catchsql {
553780be11Sdrh    CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%';
563780be11Sdrh  }
573780be11Sdrh} {1 {functions prohibited in partial index WHERE clauses}}
583780be11Sdrh
59*8a9789b6Sdrhdo_test index6-1.10 {
60*8a9789b6Sdrh  execsql {
61*8a9789b6Sdrh    ANALYZE;
62*8a9789b6Sdrh    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
63*8a9789b6Sdrh    PRAGMA integrity_check;
64*8a9789b6Sdrh  }
65*8a9789b6Sdrh} {t1a {14 1} t1b {10 1} ok}
66*8a9789b6Sdrh
67*8a9789b6Sdrhdo_test index6-1.11 {
68*8a9789b6Sdrh  execsql {
69*8a9789b6Sdrh    UPDATE t1 SET a=b;
70*8a9789b6Sdrh    ANALYZE;
71*8a9789b6Sdrh    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
72*8a9789b6Sdrh    PRAGMA integrity_check;
73*8a9789b6Sdrh  }
74*8a9789b6Sdrh} {t1a {20 1} t1b {10 1} ok}
75*8a9789b6Sdrh
76*8a9789b6Sdrhdo_test index6-1.11 {
77*8a9789b6Sdrh  execsql {
78*8a9789b6Sdrh    UPDATE t1 SET a=NULL WHERE b%3!=0;
79*8a9789b6Sdrh    UPDATE t1 SET b=b+100;
80*8a9789b6Sdrh    ANALYZE;
81*8a9789b6Sdrh    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
82*8a9789b6Sdrh    PRAGMA integrity_check;
83*8a9789b6Sdrh  }
84*8a9789b6Sdrh} {t1a {6 1} t1b {20 1} ok}
85*8a9789b6Sdrh
86*8a9789b6Sdrhdo_test index6-1.12 {
87*8a9789b6Sdrh  execsql {
88*8a9789b6Sdrh    UPDATE t1 SET a=CASE WHEN b%3!=0 THEN b END;
89*8a9789b6Sdrh    UPDATE t1 SET b=b-100;
90*8a9789b6Sdrh    ANALYZE;
91*8a9789b6Sdrh    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
92*8a9789b6Sdrh    PRAGMA integrity_check;
93*8a9789b6Sdrh  }
94*8a9789b6Sdrh} {t1a {13 1} t1b {10 1} ok}
95*8a9789b6Sdrh
96*8a9789b6Sdrhdo_test index6-1.13 {
97*8a9789b6Sdrh  execsql {
98*8a9789b6Sdrh    DELETE FROM t1 WHERE b BETWEEN 8 AND 12;
99*8a9789b6Sdrh    ANALYZE;
100*8a9789b6Sdrh    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
101*8a9789b6Sdrh    PRAGMA integrity_check;
102*8a9789b6Sdrh  }
103*8a9789b6Sdrh} {t1a {10 1} t1b {8 1} ok}
104*8a9789b6Sdrh
105*8a9789b6Sdrhdo_test index6-1.14 {
106*8a9789b6Sdrh  execsql {
107*8a9789b6Sdrh    REINDEX;
108*8a9789b6Sdrh    ANALYZE;
109*8a9789b6Sdrh    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
110*8a9789b6Sdrh    PRAGMA integrity_check;
111*8a9789b6Sdrh  }
112*8a9789b6Sdrh} {t1a {10 1} t1b {8 1} ok}
1133780be11Sdrh
1141fe0537eSdrhfinish_test
115