xref: /sqlite-3.40.0/test/autoindex5.test (revision d092ed43)
1*d092ed43Sdrh# 2014-10-24
2*d092ed43Sdrh#
3*d092ed43Sdrh# The author disclaims copyright to this source code.  In place of
4*d092ed43Sdrh# a legal notice, here is a blessing:
5*d092ed43Sdrh#
6*d092ed43Sdrh#    May you do good and not evil.
7*d092ed43Sdrh#    May you find forgiveness for yourself and forgive others.
8*d092ed43Sdrh#    May you share freely, never taking more than you give.
9*d092ed43Sdrh#
10*d092ed43Sdrh#*************************************************************************
11*d092ed43Sdrh#
12*d092ed43Sdrh# This file implements regression tests for SQLite library.  The
13*d092ed43Sdrh# focus of this script is testing automatic index creation logic,
14*d092ed43Sdrh# and specifically ensuring that automatic indexes can be used with
15*d092ed43Sdrh# co-routine subqueries.
16*d092ed43Sdrh#
17*d092ed43Sdrh
18*d092ed43Sdrhset testdir [file dirname $argv0]
19*d092ed43Sdrhsource $testdir/tester.tcl
20*d092ed43Sdrh
21*d092ed43Sdrh# Schema is from the Debian security database
22*d092ed43Sdrh#
23*d092ed43Sdrhdo_execsql_test autoindex5-1.0 {
24*d092ed43Sdrh  CREATE TABLE source_package_status
25*d092ed43Sdrh          (bug_name TEXT NOT NULL,
26*d092ed43Sdrh           package INTEGER NOT NULL,
27*d092ed43Sdrh           vulnerable INTEGER NOT NULL,
28*d092ed43Sdrh           urgency TEXT NOT NULL,
29*d092ed43Sdrh           PRIMARY KEY (bug_name, package));
30*d092ed43Sdrh  CREATE INDEX source_package_status_package
31*d092ed43Sdrh              ON source_package_status(package);
32*d092ed43Sdrh
33*d092ed43Sdrh  CREATE TABLE source_packages
34*d092ed43Sdrh              (name TEXT NOT NULL,
35*d092ed43Sdrh              release TEXT NOT NULL,
36*d092ed43Sdrh              subrelease TEXT NOT NULL,
37*d092ed43Sdrh              archive TEXT NOT NULL,
38*d092ed43Sdrh              version TEXT NOT NULL,
39*d092ed43Sdrh              version_id INTEGER NOT NULL DEFAULT 0,
40*d092ed43Sdrh              PRIMARY KEY (name, release, subrelease, archive));
41*d092ed43Sdrh
42*d092ed43Sdrh  CREATE TABLE bugs
43*d092ed43Sdrh          (name TEXT NOT NULL PRIMARY KEY,
44*d092ed43Sdrh           cve_status TEXT NOT NULL
45*d092ed43Sdrh               CHECK (cve_status IN
46*d092ed43Sdrh                      ('', 'CANDIDATE', 'ASSIGNED', 'RESERVED', 'REJECTED')),
47*d092ed43Sdrh           not_for_us INTEGER NOT NULL CHECK (not_for_us IN (0, 1)),
48*d092ed43Sdrh           description TEXT NOT NULL,
49*d092ed43Sdrh           release_date TEXT NOT NULL,
50*d092ed43Sdrh           source_file TEXT NOT NULL,
51*d092ed43Sdrh           source_line INTEGER NOT NULL);
52*d092ed43Sdrh
53*d092ed43Sdrh  CREATE TABLE package_notes
54*d092ed43Sdrh          (id INTEGER NOT NULL PRIMARY KEY,
55*d092ed43Sdrh           bug_name TEXT NOT NULL,
56*d092ed43Sdrh           package TEXT NOT NULL,
57*d092ed43Sdrh           fixed_version TEXT
58*d092ed43Sdrh               CHECK (fixed_version IS NULL OR fixed_version <> ''),
59*d092ed43Sdrh           fixed_version_id INTEGER NOT NULL DEFAULT 0,
60*d092ed43Sdrh           release TEXT NOT NULL,
61*d092ed43Sdrh           package_kind TEXT NOT NULL DEFAULT 'unknown',
62*d092ed43Sdrh           urgency TEXT NOT NULL,
63*d092ed43Sdrh           bug_origin TEXT NOT NULL DEFAULT '');
64*d092ed43Sdrh  CREATE INDEX package_notes_package
65*d092ed43Sdrh              ON package_notes(package);
66*d092ed43Sdrh  CREATE UNIQUE INDEX package_notes_bug
67*d092ed43Sdrh              ON package_notes(bug_name, package, release);
68*d092ed43Sdrh
69*d092ed43Sdrh  CREATE TABLE debian_bugs
70*d092ed43Sdrh          (bug INTEGER NOT NULL,
71*d092ed43Sdrh           note INTEGER NOT NULL,
72*d092ed43Sdrh           PRIMARY KEY (bug, note));
73*d092ed43Sdrh
74*d092ed43Sdrh
75*d092ed43Sdrh  CREATE VIEW debian_cve AS
76*d092ed43Sdrh              SELECT DISTINCT debian_bugs.bug, st.bug_name
77*d092ed43Sdrh              FROM package_notes, debian_bugs, source_package_status AS st
78*d092ed43Sdrh              WHERE package_notes.bug_name = st.bug_name
79*d092ed43Sdrh              AND debian_bugs.note = package_notes.id
80*d092ed43Sdrh              ORDER BY debian_bugs.bug;
81*d092ed43Sdrh} {}
82*d092ed43Sdrh
83*d092ed43Sdrh# The following query should use an automatic index for the view
84*d092ed43Sdrh# in FROM clause of the subquery of the second result column.
85*d092ed43Sdrh#
86*d092ed43Sdrhdo_execsql_test autoindex5-1.1 {
87*d092ed43Sdrh  EXPLAIN QUERY PLAN
88*d092ed43Sdrh  SELECT
89*d092ed43Sdrh    st.bug_name,
90*d092ed43Sdrh    (SELECT ALL debian_cve.bug FROM debian_cve
91*d092ed43Sdrh      WHERE debian_cve.bug_name = st.bug_name
92*d092ed43Sdrh      ORDER BY debian_cve.bug),
93*d092ed43Sdrh    sp.release
94*d092ed43Sdrh  FROM
95*d092ed43Sdrh     source_package_status AS st,
96*d092ed43Sdrh     source_packages AS sp,
97*d092ed43Sdrh     bugs
98*d092ed43Sdrh  WHERE
99*d092ed43Sdrh     sp.rowid = st.package
100*d092ed43Sdrh     AND st.bug_name = bugs.name
101*d092ed43Sdrh     AND ( st.bug_name LIKE 'CVE-%' OR st.bug_name LIKE 'TEMP-%' )
102*d092ed43Sdrh     AND ( sp.release = 'sid' OR sp.release = 'stretch' OR sp.release = 'jessie'
103*d092ed43Sdrh            OR sp.release = 'wheezy' OR sp.release = 'squeeze' )
104*d092ed43Sdrh  ORDER BY sp.name, st.bug_name, sp.release, sp.subrelease;
105*d092ed43Sdrh} {/SEARCH SUBQUERY 2 USING AUTOMATIC COVERING INDEX .bug_name=/}
106*d092ed43Sdrh
107*d092ed43Sdrh
108*d092ed43Sdrhfinish_test
109