xref: /sqlite-3.40.0/test/amatch1.test (revision 3b6cb3ce)
192054fefSdrh# 2013-09-30
292054fefSdrh#
392054fefSdrh# The author disclaims copyright to this source code.  In place of
492054fefSdrh# a legal notice, here is a blessing:
592054fefSdrh#
692054fefSdrh#    May you do good and not evil.
792054fefSdrh#    May you find forgiveness for yourself and forgive others.
892054fefSdrh#    May you share freely, never taking more than you give.
992054fefSdrh#
1092054fefSdrh#*************************************************************************
1192054fefSdrh# This file implements regression tests for "approximate_match" virtual
1292054fefSdrh# table that is in the "amatch.c" extension.
1392054fefSdrh#
1492054fefSdrh#
1592054fefSdrh
1692054fefSdrhset testdir [file dirname $argv0]
1792054fefSdrhsource $testdir/tester.tcl
1892054fefSdrh
1992054fefSdrh# If SQLITE_ENABLE_FTS4 is defined, omit this file.
2092054fefSdrhifcapable !fts3 {
2192054fefSdrh  finish_test
2292054fefSdrh  return
2392054fefSdrh}
2492054fefSdrh
2592054fefSdrh# Create the fts_kjv_genesis procedure which fills and FTS3/4 table with
2692054fefSdrh# the complete text of the Book of Genesis.
2792054fefSdrh#
2892054fefSdrhsource $testdir/genesis.tcl
2992054fefSdrh
3092054fefSdrh
3192054fefSdrh
3292054fefSdrhdo_test amatch1-1.0 {
3392054fefSdrh  db eval {
3492054fefSdrh    CREATE VIRTUAL TABLE t1 USING fts4(words); --, tokenize porter);
3592054fefSdrh  }
3692054fefSdrh  fts_kjv_genesis
3792054fefSdrh  db eval {
38*3b6cb3ceSdrh    INSERT INTO t1(t1) VALUES('optimize');
3992054fefSdrh    CREATE VIRTUAL TABLE temp.t1aux USING fts4aux(main, t1);
4092054fefSdrh    SELECT term FROM t1aux WHERE col=0 ORDER BY 1 LIMIT 5
4192054fefSdrh  }
4292054fefSdrh} {a abated abel abelmizraim abidah}
4392054fefSdrhdo_test amatch1-1.1 {
4492054fefSdrh  db eval {
4592054fefSdrh    SELECT term FROM t1aux WHERE term>'b' AND col=0 ORDER BY 1 LIMIT 5
4692054fefSdrh  }
4792054fefSdrh} {baalhanan babel back backward bad}
4892054fefSdrhdo_test amatch1-1.2 {
4992054fefSdrh  db eval {
5092054fefSdrh    SELECT term FROM t1aux WHERE term>'b' AND col=0 LIMIT 5
5192054fefSdrh  }
5292054fefSdrh} {baalhanan babel back backward bad}
5392054fefSdrh
5492054fefSdrh# Load the amatch extension
5592054fefSdrhload_static_extension db amatch
5692054fefSdrh
57*3b6cb3ceSdrhdo_execsql_test amatch1-2.0 {
5892054fefSdrh    CREATE TABLE costs(iLang, cFrom, cTo, Cost);
5992054fefSdrh    INSERT INTO costs VALUES(0, '', '?', 100);
6092054fefSdrh    INSERT INTO costs VALUES(0, '?', '', 100);
6192054fefSdrh    INSERT INTO costs VALUES(0, '?', '?', 150);
62*3b6cb3ceSdrh    CREATE TABLE vocab(w TEXT UNIQUE);
63*3b6cb3ceSdrh    INSERT OR IGNORE INTO vocab SELECT term FROM t1aux;
6492054fefSdrh    CREATE VIRTUAL TABLE t2 USING approximate_match(
6592054fefSdrh      vocabulary_table=t1aux,
6692054fefSdrh      vocabulary_word=term,
6792054fefSdrh      edit_distances=costs
6892054fefSdrh    );
69*3b6cb3ceSdrh    CREATE VIRTUAL TABLE t3 USING approximate_match(
70*3b6cb3ceSdrh      vocabulary_table=vocab,
71*3b6cb3ceSdrh      vocabulary_word=w,
72*3b6cb3ceSdrh      edit_distances=costs
73*3b6cb3ceSdrh    );
74*3b6cb3ceSdrh    CREATE VIRTUAL TABLE t4 USING approximate_match(
75*3b6cb3ceSdrh        vocabulary_table=vtemp,
76*3b6cb3ceSdrh        vocabulary_word=w,
77*3b6cb3ceSdrh        edit_distances=costs
78*3b6cb3ceSdrh      );
79*3b6cb3ceSdrh} {}
80*3b6cb3ceSdrhputs "Query against fts4aux: [time {
81*3b6cb3ceSdrh  do_execsql_test amatch1-2.1 {
8292054fefSdrh      SELECT word, distance FROM t2
8392054fefSdrh       WHERE word MATCH 'josxph' AND distance<300;
84*3b6cb3ceSdrh  } {joseph 150}} 1]"
85*3b6cb3ceSdrhputs "Query against ordinary table: [time {
86*3b6cb3ceSdrh  do_execsql_test amatch1-2.2 {
87*3b6cb3ceSdrh      SELECT word, distance FROM t3
88*3b6cb3ceSdrh       WHERE word MATCH 'josxph' AND distance<300;
89*3b6cb3ceSdrh  } {joseph 150}} 1]"
90*3b6cb3ceSdrhputs "Temp table initialized from fts4aux: [time {
91*3b6cb3ceSdrh  do_execsql_test amatch1-2.3a {
92*3b6cb3ceSdrh      CREATE TEMP TABLE vtemp(w TEXT UNIQUE);
93*3b6cb3ceSdrh      INSERT OR IGNORE INTO vtemp SELECT term FROM t1aux;
94*3b6cb3ceSdrh  } {}} 1]"
95*3b6cb3ceSdrhputs "Query against temp table: [time {
96*3b6cb3ceSdrh  do_execsql_test amatch1-2.3b {
97*3b6cb3ceSdrh      SELECT word, distance FROM t4
98*3b6cb3ceSdrh       WHERE word MATCH 'josxph' AND distance<300;
99*3b6cb3ceSdrh  } {joseph 150}} 1]"
100*3b6cb3ceSdrhdo_execsql_test amatch1-2.11 {
101*3b6cb3ceSdrh    SELECT word, distance FROM t2
102*3b6cb3ceSdrh     WHERE word MATCH 'joxxph' AND distance<=300;
103*3b6cb3ceSdrh} {joseph 300}
104*3b6cb3ceSdrhdo_execsql_test amatch1-2.12 {
105*3b6cb3ceSdrh    SELECT word, distance FROM t3
106*3b6cb3ceSdrh     WHERE word MATCH 'joxxph' AND distance<=300;
107*3b6cb3ceSdrh} {joseph 300}
108*3b6cb3ceSdrhdo_execsql_test amatch1-2.21 {
109*3b6cb3ceSdrh    SELECT word, distance FROM t2
110*3b6cb3ceSdrh     WHERE word MATCH 'joxxph' AND distance<300;
111*3b6cb3ceSdrh} {}
112*3b6cb3ceSdrhdo_execsql_test amatch1-2.22 {
113*3b6cb3ceSdrh    SELECT word, distance FROM t3
114*3b6cb3ceSdrh     WHERE word MATCH 'joxxph' AND distance<300;
115*3b6cb3ceSdrh} {}
11692054fefSdrh
11792054fefSdrhfinish_test
118