Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixes for the sqlite_stat3 related ANALYZE functionality.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f93e75f2b1c10b0b1a06a4ebab7285c25049e0a6
User & Date: dan 2013-06-24 19:03:43.324
Context
2013-06-24
20:06
Fix another bug in sqlite_stat3 related code. check-in: bad9060b5b user: dan tags: trunk
19:03
Fixes for the sqlite_stat3 related ANALYZE functionality. check-in: f93e75f2b1 user: dan tags: trunk
2013-06-22
19:57
Fixes for SQLITE4_ENABLE_STAT3 builds. check-in: d5d0e93a57 user: dan tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/analyze.c.
297
298
299
300
301
302
303

304
305
306
307
308
309
310
    }
  }
  if( !doInsert ) return;
  if( p->nSample==p->mxSample ){
    assert( p->nSample - iMin - 1 >= 0 );
    memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
    pSample = &p->a[p->nSample-1];

  }else{
    pSample = &p->a[p->nSample++];
  }

  pKey = sqlite4_value_blob(argv[3], &nKey);
  if( nKey>pSample->nAlloc ){
    sqlite4 *db = sqlite4_context_db_handle(context);







>







297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    }
  }
  if( !doInsert ) return;
  if( p->nSample==p->mxSample ){
    assert( p->nSample - iMin - 1 >= 0 );
    memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
    pSample = &p->a[p->nSample-1];
    memset(pSample, 0, sizeof(struct Stat3Sample));
  }else{
    pSample = &p->a[p->nSample++];
  }

  pKey = sqlite4_value_blob(argv[3], &nKey);
  if( nKey>pSample->nAlloc ){
    sqlite4 *db = sqlite4_context_db_handle(context);
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
  if( !zSql ){
    return SQLITE4_NOMEM;
  }
  rc = sqlite4_prepare(db, zSql, -1, &pStmt, 0);
  sqlite4DbFree(db, zSql);
  if( rc ) return rc;

  while( sqlite4_step(pStmt)==SQLITE4_ROW && 0 ){
    char *zIndex;   /* Index name */
    Index *pIdx;    /* Pointer to the index object */
    int i;          /* Loop counter */
    tRowcnt sumEq;  /* Sum of the nEq values */
    const u8 *aVal;
    int nVal;








|







930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
  if( !zSql ){
    return SQLITE4_NOMEM;
  }
  rc = sqlite4_prepare(db, zSql, -1, &pStmt, 0);
  sqlite4DbFree(db, zSql);
  if( rc ) return rc;

  while( sqlite4_step(pStmt)==SQLITE4_ROW ){
    char *zIndex;   /* Index name */
    Index *pIdx;    /* Pointer to the index object */
    int i;          /* Loop counter */
    tRowcnt sumEq;  /* Sum of the nEq values */
    const u8 *aVal;
    int nVal;

Changes to src/main.c.
1565
1566
1567
1568
1569
1570
1571















1572
1573
1574
1575
1576
1577
1578
      sqlite4_stmt *pStmt = va_arg(ap, sqlite4_stmt*);
      const char **pzRet = va_arg(ap, const char**);
      *pzRet = sqlite4VdbeExplanation((Vdbe*)pStmt);
      break;
    }
#endif
















  }
  va_end(ap);
#endif /* SQLITE4_OMIT_BUILTIN_TEST */
  return rc;
}

/*







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
      sqlite4_stmt *pStmt = va_arg(ap, sqlite4_stmt*);
      const char **pzRet = va_arg(ap, const char**);
      *pzRet = sqlite4VdbeExplanation((Vdbe*)pStmt);
      break;
    }
#endif

    case SQLITE4_TESTCTRL_PRNG_GET: {
      sqlite4_env *pEnv = va_arg(ap, sqlite4_env *);
      sqlite4_int64 *piOut = va_arg(ap, sqlite4_int64 *);
      if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;
      *piOut = (sqlite4_int64)(((u64)pEnv->prngX << 32) + pEnv->prngY);
      break;
    }
    case SQLITE4_TESTCTRL_PRNG_SET: {
      sqlite4_env *pEnv = va_arg(ap, sqlite4_env *);
      u64 iVal = (u64)va_arg(ap, sqlite4_int64);
      if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;
      pEnv->prngX = (iVal>>32);
      pEnv->prngY = (iVal & 0xFFFFFFFF);
      break;
    }
  }
  va_end(ap);
#endif /* SQLITE4_OMIT_BUILTIN_TEST */
  return rc;
}

/*
Changes to src/sqlite.h.in.
3572
3573
3574
3575
3576
3577
3578


3579
3580
3581
3582
3583
3584
3585
3586
#define SQLITE4_TESTCTRL_ASSERT                   3
#define SQLITE4_TESTCTRL_ALWAYS                   4
#define SQLITE4_TESTCTRL_RESERVE                  5
#define SQLITE4_TESTCTRL_OPTIMIZATIONS            6
#define SQLITE4_TESTCTRL_ISKEYWORD                7
#define SQLITE4_TESTCTRL_LOCALTIME_FAULT          8
#define SQLITE4_TESTCTRL_EXPLAIN_STMT             9


#define SQLITE4_TESTCTRL_LAST                     9

/*
** CAPIREF: SQLite Runtime Status
**
** ^This interface is used to retrieve runtime status information
** about the performance of SQLite, and optionally to reset various
** highwater marks.  ^The first argument is an integer code for







>
>
|







3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
#define SQLITE4_TESTCTRL_ASSERT                   3
#define SQLITE4_TESTCTRL_ALWAYS                   4
#define SQLITE4_TESTCTRL_RESERVE                  5
#define SQLITE4_TESTCTRL_OPTIMIZATIONS            6
#define SQLITE4_TESTCTRL_ISKEYWORD                7
#define SQLITE4_TESTCTRL_LOCALTIME_FAULT          8
#define SQLITE4_TESTCTRL_EXPLAIN_STMT             9
#define SQLITE4_TESTCTRL_PRNG_GET                 10  /* pEnv, *pi64 */
#define SQLITE4_TESTCTRL_PRNG_SET                 11  /* pEnv, i64   */
#define SQLITE4_TESTCTRL_LAST                     11

/*
** CAPIREF: SQLite Runtime Status
**
** ^This interface is used to retrieve runtime status information
** about the performance of SQLite, and optionally to reset various
** highwater marks.  ^The first argument is an integer code for
Changes to src/where.c.
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
  for(i=0; i<pIdx->nSample; i++){
    int res;
    int n = pBuf->n;
    if( n>aSample[i].nVal ) n = aSample[i].nVal;

    res = memcmp(pBuf->p, aSample[i].aVal, n);
    if( res==0 ) res = pBuf->n - aSample[i].nVal;
    if( res>=0 ){
      isEq = (res==0);
      break;
    }
  }

  /* At this point, aSample[i] is the first sample that is greater than
  ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less







|







2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
  for(i=0; i<pIdx->nSample; i++){
    int res;
    int n = pBuf->n;
    if( n>aSample[i].nVal ) n = aSample[i].nVal;

    res = memcmp(pBuf->p, aSample[i].aVal, n);
    if( res==0 ) res = pBuf->n - aSample[i].nVal;
    if( res<=0 ){
      isEq = (res==0);
      break;
    }
  }

  /* At this point, aSample[i] is the first sample that is greater than
  ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
    sqlite4_buffer_init(&buf, db->pEnv->pMM);
    rc = whereSampleKeyinfo(pParse, p, &keyinfo);

    if( rc==SQLITE4_OK && pLower ){
      Expr *pExpr = pLower->pExpr->pRight;
      rc = valueFromExpr(pParse, &keyinfo, pExpr, aff, &buf);
      assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
      if( rc==SQLITE4_OK
       && whereKeyStats(pParse, p, &buf, 0, a)==SQLITE4_OK
      ){
        iLower = a[0];
        if( pLower->eOperator==WO_GT ) iLower += a[1];
      }
      sqlite4_buffer_set(&buf, 0, 0);
    }
    if( rc==SQLITE4_OK && pUpper ){
      Expr *pExpr = pUpper->pExpr->pRight;
      rc = valueFromExpr(pParse, &keyinfo, pExpr, aff, &buf);
      assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
      if( rc==SQLITE4_OK
       && whereKeyStats(pParse, p, &buf, 1, a)==SQLITE4_OK
      ){
        iUpper = a[0];
        if( pUpper->eOperator==WO_LE ) iUpper += a[1];
      }
    }
    sqlite4_buffer_clear(&buf);







|











|







2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
    sqlite4_buffer_init(&buf, db->pEnv->pMM);
    rc = whereSampleKeyinfo(pParse, p, &keyinfo);

    if( rc==SQLITE4_OK && pLower ){
      Expr *pExpr = pLower->pExpr->pRight;
      rc = valueFromExpr(pParse, &keyinfo, pExpr, aff, &buf);
      assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
      if( rc==SQLITE4_OK && buf.n
       && whereKeyStats(pParse, p, &buf, 0, a)==SQLITE4_OK
      ){
        iLower = a[0];
        if( pLower->eOperator==WO_GT ) iLower += a[1];
      }
      sqlite4_buffer_set(&buf, 0, 0);
    }
    if( rc==SQLITE4_OK && pUpper ){
      Expr *pExpr = pUpper->pExpr->pRight;
      rc = valueFromExpr(pParse, &keyinfo, pExpr, aff, &buf);
      assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
      if( rc==SQLITE4_OK && buf.n
       && whereKeyStats(pParse, p, &buf, 1, a)==SQLITE4_OK
      ){
        iUpper = a[0];
        if( pUpper->eOperator==WO_LE ) iUpper += a[1];
      }
    }
    sqlite4_buffer_clear(&buf);
Changes to test/analyze3.test.
17
18
19
20
21
22
23

24
25
26
27
28
29
30
set testdir [file dirname $argv0]
source $testdir/tester.tcl

ifcapable !stat3 {
  finish_test
  return
}


#----------------------------------------------------------------------
# Test Organization:
#
# analyze3-1.*: Test that the values of bound parameters are considered 
#               in the same way as constants when planning queries that
#               use range constraints.







>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
set testdir [file dirname $argv0]
source $testdir/tester.tcl

ifcapable !stat3 {
  finish_test
  return
}


#----------------------------------------------------------------------
# Test Organization:
#
# analyze3-1.*: Test that the values of bound parameters are considered 
#               in the same way as constants when planning queries that
#               use range constraints.
49
50
51
52
53
54
55
56
57



58
59











60





61
62
63
64
65
66
67
db function var getvar

proc eqp {sql {db db}} {
  uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
}

proc sf_execsql {sql {db db}} {
  set ::sqlite_search_count 0
  set r [uplevel [list execsql $sql $db]]




  concat $::sqlite_search_count [$db status step] $r











}






#-------------------------------------------------------------------------
#
# analyze3-1.1.1: 
#   Create a table with two columns. Populate the first column (affinity 
#   INTEGER) with integer values from 100 to 1100. Create an index on this 
#   column. ANALYZE the table.







|
|
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
db function var getvar

proc eqp {sql {db db}} {
  uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
}

proc sf_execsql {sql {db db}} {
  kvwrap reset
  set res [uplevel execsql [list $sql]]
  #puts "sql={$sql} seek=[kvwrap seek] step=[kvwrap step]"
  set count [expr [kvwrap step] + [kvwrap seek]]
  concat $count [$db status step] $res
}

# This proc calls sqlite4_step() on the statement passed as the only
# argument. It ignores the results of this call and immediately resets
# the statement. The return value is 1 if the authorization callback
# was invoked one or more times, or 0 otherwise. This is used to test
# that statements are recompiled as required.
#
proc test_for_recompile {stmt} {
  set ::auth_callbacks 0
  sqlite4_step $stmt
  sqlite4_reset $stmt
  return [expr $::auth_callbacks>0]
}
proc auth_callback {args} {
  incr ::auth_callbacks
  return SQLITE4_OK
}
db auth auth_callback

#-------------------------------------------------------------------------
#
# analyze3-1.1.1: 
#   Create a table with two columns. Populate the first column (affinity 
#   INTEGER) with integer values from 100 to 1100. Create an index on this 
#   column. ANALYZE the table.
77
78
79
80
81
82
83








84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#   literal values to constrain the range scan.
#
#   These tests also check that the compiler code considers column 
#   affinities when estimating the number of rows scanned by the "use 
#   index strategy".
#
do_test analyze3-1.1.1 {








  execsql {
    BEGIN;
    CREATE TABLE t1(x INTEGER, y);
    CREATE INDEX i1 ON t1(x);
  }
  for {set i 0} {$i < 1000} {incr i} {
    execsql { INSERT INTO t1 VALUES($i+100, $i) }
  }
  execsql {
    COMMIT;
    ANALYZE;
  }
} {}

do_eqp_test analyze3-1.1.2 {
  SELECT sum(y) FROM t1 WHERE x>200 AND x<300
} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~179 rows)}}
do_eqp_test analyze3-1.1.3 {
  SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 
} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~959 rows)}}

do_test analyze3-1.1.4 {
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
} {199 0 14850}
do_test analyze3-1.1.5 {
  set l [string range "200" 0 end]
  set u [string range "300" 0 end]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.1.6 {
  set l [expr int(200)]
  set u [expr int(300)]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.1.7 {
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
} {2000 0 499500}
do_test analyze3-1.1.8 {
  set l [string range "0" 0 end]
  set u [string range "1100" 0 end]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {2000 0 499500}
do_test analyze3-1.1.9 {
  set l [expr int(0)]
  set u [expr int(1100)]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {2000 0 499500}


# The following tests are similar to the block above. The difference is
# that the indexed column has TEXT affinity in this case. In the tests
# above the affinity is INTEGER.
#
do_test analyze3-1.2.1 {

  execsql {
    BEGIN;
      CREATE TABLE t2(x TEXT, y);
      INSERT INTO t2 SELECT * FROM t1;
      CREATE INDEX i2 ON t2(x);
    COMMIT;
    ANALYZE;
  }
} {}
do_eqp_test analyze3-1.2.2 {
  SELECT sum(y) FROM t2 WHERE x>1 AND x<2
} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~196 rows)}}
do_eqp_test analyze3-1.2.3 {
  SELECT sum(y) FROM t2 WHERE x>0 AND x<99
} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~968 rows)}}
do_test analyze3-1.2.4 {
  sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
} {161 0 4760}
do_test analyze3-1.2.5 {
  set l [string range "12" 0 end]
  set u [string range "20" 0 end]
  sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}







>
>
>
>
>
>
>
>








|
<
|
<




|


|
<















|




|




|







>











|


|







97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

121

122
123
124
125
126
127
128
129

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#   literal values to constrain the range scan.
#
#   These tests also check that the compiler code considers column 
#   affinities when estimating the number of rows scanned by the "use 
#   index strategy".
#
do_test analyze3-1.1.1 {
  # The precise output of EXPLAIN QUERY PLAN are normally non-deterministic 
  # (as the estimated number of rows may vary slightly based on arbitrary 
  # decisions made while sampling the index for the sqlite_stat3 table).
  # Or, more accurately, the output depends on the built-in PRNG. So set
  # the PRNG to a known state before running ANALYZE in order to make
  # the results deterministic.
  #
  prng_state_set 0
  execsql {
    BEGIN;
    CREATE TABLE t1(x INTEGER, y);
    CREATE INDEX i1 ON t1(x);
  }
  for {set i 0} {$i < 1000} {incr i} {
    execsql { INSERT INTO t1 VALUES($i+100, $i) }
  }
  execsql COMMIT

  execsql ANALYZE

} {}

do_eqp_test analyze3-1.1.2 {
  SELECT sum(y) FROM t1 WHERE x>200 AND x<300
} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~160 rows)}}
do_eqp_test analyze3-1.1.3 {
  SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 
} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~985 rows)}}

do_test analyze3-1.1.4 {
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
} {199 0 14850}
do_test analyze3-1.1.5 {
  set l [string range "200" 0 end]
  set u [string range "300" 0 end]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.1.6 {
  set l [expr int(200)]
  set u [expr int(300)]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.1.7 {
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
} {2001 0 499500}
do_test analyze3-1.1.8 {
  set l [string range "0" 0 end]
  set u [string range "1100" 0 end]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {2001 0 499500}
do_test analyze3-1.1.9 {
  set l [expr int(0)]
  set u [expr int(1100)]
  sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
} {2001 0 499500}


# The following tests are similar to the block above. The difference is
# that the indexed column has TEXT affinity in this case. In the tests
# above the affinity is INTEGER.
#
do_test analyze3-1.2.1 {
  prng_state_set 0
  execsql {
    BEGIN;
      CREATE TABLE t2(x TEXT, y);
      INSERT INTO t2 SELECT * FROM t1;
      CREATE INDEX i2 ON t2(x);
    COMMIT;
    ANALYZE;
  }
} {}
do_eqp_test analyze3-1.2.2 {
  SELECT sum(y) FROM t2 WHERE x>1 AND x<2
} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~193 rows)}}
do_eqp_test analyze3-1.2.3 {
  SELECT sum(y) FROM t2 WHERE x>0 AND x<99
} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~972 rows)}}
do_test analyze3-1.2.4 {
  sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
} {161 0 4760}
do_test analyze3-1.2.5 {
  set l [string range "12" 0 end]
  set u [string range "20" 0 end]
  sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
178
179
180
181
182
183
184

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
} {1981 0 integer integer 490555}

# Same tests a third time. This time, column x has INTEGER affinity and
# is not the leftmost column of the table. This triggered a bug causing
# SQLite to use sub-optimal query plans in 3.6.18 and earlier.
#
do_test analyze3-1.3.1 {

  execsql {
    BEGIN;
      CREATE TABLE t3(y TEXT, x INTEGER);
      INSERT INTO t3 SELECT y, x FROM t1;
      CREATE INDEX i3 ON t3(x);
    COMMIT;
    ANALYZE;
  }
} {}
do_eqp_test analyze3-1.3.2 {
  SELECT sum(y) FROM t3 WHERE x>200 AND x<300
} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~156 rows)}}
do_eqp_test analyze3-1.3.3 {
  SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~989 rows)}}

do_test analyze3-1.3.4 {
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
} {199 0 14850}
do_test analyze3-1.3.5 {
  set l [string range "200" 0 end]
  set u [string range "300" 0 end]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.3.6 {
  set l [expr int(200)]
  set u [expr int(300)]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.3.7 {
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
} {2000 0 499500}
do_test analyze3-1.3.8 {
  set l [string range "0" 0 end]
  set u [string range "1100" 0 end]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {2000 0 499500}
do_test analyze3-1.3.9 {
  set l [expr int(0)]
  set u [expr int(1100)]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {2000 0 499500}

#-------------------------------------------------------------------------
# Test that the values of bound SQL variables may be used for the LIKE
# optimization.
#
drop_all_tables
do_test analyze3-2.1 {







>











|


|
















|




|




|







204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
} {1981 0 integer integer 490555}

# Same tests a third time. This time, column x has INTEGER affinity and
# is not the leftmost column of the table. This triggered a bug causing
# SQLite to use sub-optimal query plans in 3.6.18 and earlier.
#
do_test analyze3-1.3.1 {
  prng_state_set 0
  execsql {
    BEGIN;
      CREATE TABLE t3(y TEXT, x INTEGER);
      INSERT INTO t3 SELECT y, x FROM t1;
      CREATE INDEX i3 ON t3(x);
    COMMIT;
    ANALYZE;
  }
} {}
do_eqp_test analyze3-1.3.2 {
  SELECT sum(y) FROM t3 WHERE x>200 AND x<300
} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~107 rows)}}
do_eqp_test analyze3-1.3.3 {
  SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~972 rows)}}

do_test analyze3-1.3.4 {
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
} {199 0 14850}
do_test analyze3-1.3.5 {
  set l [string range "200" 0 end]
  set u [string range "300" 0 end]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.3.6 {
  set l [expr int(200)]
  set u [expr int(300)]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {199 0 14850}
do_test analyze3-1.3.7 {
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
} {2001 0 499500}
do_test analyze3-1.3.8 {
  set l [string range "0" 0 end]
  set u [string range "1100" 0 end]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {2001 0 499500}
do_test analyze3-1.3.9 {
  set l [expr int(0)]
  set u [expr int(1100)]
  sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
} {2001 0 499500}

#-------------------------------------------------------------------------
# Test that the values of bound SQL variables may be used for the LIKE
# optimization.
#
drop_all_tables
do_test analyze3-2.1 {
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?) (~31250 rows)}}
do_eqp_test analyze3-2.3 {
  SELECT count(a) FROM t1 WHERE b LIKE '%a'
} {0 0 0 {SCAN TABLE t1 (~500000 rows)}}

do_test analyze3-2.4 {
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
} {101 0 100}
do_test analyze3-2.5 {
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
} {999 999 100}

do_test analyze3-2.4 {
  set like "a%"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {101 0 100}
do_test analyze3-2.5 {
  set like "%a"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {999 999 100}
do_test analyze3-2.6 {
  set like "a"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {101 0 0}
do_test analyze3-2.7 {
  set like "ab"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {11 0 0}
do_test analyze3-2.8 {
  set like "abc"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {2 0 1}
do_test analyze3-2.9 {
  set like "a_c"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {101 0 10}


#-------------------------------------------------------------------------
# This block of tests checks that statements are correctly marked as
# expired when the values bound to any parameters that may affect the 
# query plan are modified.
#
drop_all_tables
db auth auth
proc auth {args} {
  set ::auth 1
  return SQLITE4_OK
}

do_test analyze3-3.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(a, b, c);
    CREATE INDEX i1 ON t1(b);
  }
  for {set i 0} {$i < 100} {incr i} {
    execsql { INSERT INTO t1 VALUES($i, $i, $i) }
  }
  execsql COMMIT
  execsql ANALYZE
} {}

do_test analyze3-3.2.1 {
  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE b>?" -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.2.2 {
  sqlite4_bind_text $S 1 "abc" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.2.4 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.2.5 {
  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.2.6 {
  sqlite4_bind_text $S 1 "abc" 3
  sqlite4_expired $S
} {0}
do_test analyze3-3.2.7 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.4.1 {
  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.4.2 {
  sqlite4_bind_text $S 1 "abc" 3
  sqlite4_expired $S
} {0}
do_test analyze3-3.4.3 {
  sqlite4_bind_text $S 2 "def" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.4.4 {
  sqlite4_bind_text $S 2 "ghi" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.4.5 {
  sqlite4_expired $S
} {1}
do_test analyze3-3.4.6 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.5.1 {
  set S [sqlite4_prepare_v2 db {
    SELECT * FROM t1 WHERE a IN (
      ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
      ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
      ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
    ) AND b>?32;
  } -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.5.2 {
  sqlite4_bind_text $S 31 "abc" 3
  sqlite4_expired $S
} {0}
do_test analyze3-3.5.3 {
  sqlite4_bind_text $S 32 "def" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.5.5 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.6.1 {
  set S [sqlite4_prepare_v2 db {
    SELECT * FROM t1 WHERE a IN (
      ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
      ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
      ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
    ) AND b>?33;
  } -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.6.2 {
  sqlite4_bind_text $S 32 "abc" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.6.3 {
  sqlite4_bind_text $S 33 "def" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.6.5 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.7.1 {
  set S [sqlite4_prepare_v2 db {
    SELECT * FROM t1 WHERE a IN (
      ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
      ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
      ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
    ) AND b>?10;
  } -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.7.2 {
  sqlite4_bind_text $S 32 "abc" 3
  sqlite4_expired $S
} {0}
do_test analyze3-3.7.3 {
  sqlite4_bind_text $S 33 "def" 3
  sqlite4_expired $S
} {0}
do_test analyze3-3.7.4 {
  sqlite4_bind_text $S 10 "def" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.7.6 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.8.1 {
  execsql {
    CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
    CREATE INDEX i4 ON t4(y);
  }
} {}
do_test analyze3-3.8.2 {
  set S [sqlite4_prepare_v2 db {
    SELECT * FROM t4 WHERE x != ? AND y LIKE ?
  } -1 dummy]
  sqlite4_expired $S
} {0}
do_test analyze3-3.8.3 {
  sqlite4_bind_text $S 1 "abc" 3
  sqlite4_expired $S
} {0}
do_test analyze3-3.8.4 {
  sqlite4_bind_text $S 2 "def" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.7 {
  sqlite4_bind_text $S 2 "ghi%" 4
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.8 {
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.9 {
  sqlite4_bind_text $S 2 "ghi%def" 7
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.10 {
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.11 {
  sqlite4_bind_text $S 2 "%ab" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.12 {
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.12 {
  sqlite4_bind_text $S 2 "%de" 3
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.13 {
  sqlite4_expired $S
} {1}
do_test analyze3-3.8.14 {
  sqlite4_finalize $S
} {SQLITE4_OK}

#-------------------------------------------------------------------------
# These tests check that errors encountered while repreparing an SQL
# statement within sqlite4Reprepare() are handled correctly.
#

# Check a schema error.
#
do_test analyze3-4.1.1 {
  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  sqlite4_step $S
} {SQLITE4_DONE}
do_test analyze3-4.1.2 {
  sqlite4_reset $S
  sqlite4_bind_text $S 2 "abc" 3
  execsql { DROP TABLE t1 }
  sqlite4_step $S







|


|




|



|



|



|



|



|








|
<
<
<
<















|
|



|




<

|
|



|






|
|



|



|



<
<
|
<






|






|



|



|






|






|



|



|






|






|



|



|



|












|


|



|



|



|


|
|


|


|
|


|


|
|


|


|
|












|







278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322




323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373


374

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?) (~31250 rows)}}
do_eqp_test analyze3-2.3 {
  SELECT count(a) FROM t1 WHERE b LIKE '%a'
} {0 0 0 {SCAN TABLE t1 (~500000 rows)}}

do_test analyze3-2.4 {
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
} {201 0 100}
do_test analyze3-2.5 {
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
} {1001 999 100}

do_test analyze3-2.4 {
  set like "a%"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {201 0 100}
do_test analyze3-2.5 {
  set like "%a"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {1001 999 100}
do_test analyze3-2.6 {
  set like "a"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {201 0 0}
do_test analyze3-2.7 {
  set like "ab"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {21 0 0}
do_test analyze3-2.8 {
  set like "abc"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {3 0 1}
do_test analyze3-2.9 {
  set like "a_c"
  sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
} {201 0 10}


#-------------------------------------------------------------------------
# This block of tests checks that statements are correctly marked as
# expired when the values bound to any parameters that may affect the 
# query plan are modified.
#
drop_all_tables
db auth auth_callback





do_test analyze3-3.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(a, b, c);
    CREATE INDEX i1 ON t1(b);
  }
  for {set i 0} {$i < 100} {incr i} {
    execsql { INSERT INTO t1 VALUES($i, $i, $i) }
  }
  execsql COMMIT
  execsql ANALYZE
} {}

do_test analyze3-3.2.1 {
  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE b>?" -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.2.2 {
  sqlite4_bind_text $S 1 "abc" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.2.4 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.2.5 {
  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE b=?" -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.2.6 {
  sqlite4_bind_text $S 1 "abc" 3
  test_for_recompile $S
} {0}
do_test analyze3-3.2.7 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.4.1 {
  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.4.2 {
  sqlite4_bind_text $S 1 "abc" 3
  test_for_recompile $S
} {0}
do_test analyze3-3.4.3 {
  sqlite4_bind_text $S 2 "def" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.4.4 {
  sqlite4_bind_text $S 2 "ghi" 3


  test_for_recompile $S

} {1}
do_test analyze3-3.4.6 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.5.1 {
  set S [sqlite4_prepare db {
    SELECT * FROM t1 WHERE a IN (
      ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
      ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
      ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
    ) AND b>?32;
  } -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.5.2 {
  sqlite4_bind_text $S 31 "abc" 3
  test_for_recompile $S
} {0}
do_test analyze3-3.5.3 {
  sqlite4_bind_text $S 32 "def" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.5.5 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.6.1 {
  set S [sqlite4_prepare db {
    SELECT * FROM t1 WHERE a IN (
      ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
      ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
      ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
    ) AND b>?33;
  } -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.6.2 {
  sqlite4_bind_text $S 32 "abc" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.6.3 {
  sqlite4_bind_text $S 33 "def" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.6.5 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.7.1 {
  set S [sqlite4_prepare db {
    SELECT * FROM t1 WHERE a IN (
      ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
      ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
      ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
    ) AND b>?10;
  } -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.7.2 {
  sqlite4_bind_text $S 32 "abc" 3
  test_for_recompile $S
} {0}
do_test analyze3-3.7.3 {
  sqlite4_bind_text $S 33 "def" 3
  test_for_recompile $S
} {0}
do_test analyze3-3.7.4 {
  sqlite4_bind_text $S 10 "def" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.7.6 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-3.8.1 {
  execsql {
    CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
    CREATE INDEX i4 ON t4(y);
  }
} {}
do_test analyze3-3.8.2 {
  set S [sqlite4_prepare db {
    SELECT * FROM t4 WHERE x != ? AND y LIKE ?
  } -1 dummy]
  test_for_recompile $S
} {0}
do_test analyze3-3.8.3 {
  sqlite4_bind_text $S 1 "abc" 3
  test_for_recompile $S
} {0}
do_test analyze3-3.8.4 {
  sqlite4_bind_text $S 2 "def" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.8.7 {
  sqlite4_bind_text $S 2 "ghi%" 4
  test_for_recompile $S
} {1}
do_test analyze3-3.8.8 {
  test_for_recompile $S
} {0}
do_test analyze3-3.8.9 {
  sqlite4_bind_text $S 2 "ghi%def" 7
  test_for_recompile $S
} {1}
do_test analyze3-3.8.10 {
  test_for_recompile $S
} {0}
do_test analyze3-3.8.11 {
  sqlite4_bind_text $S 2 "%ab" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.8.12 {
  test_for_recompile $S
} {0}
do_test analyze3-3.8.12 {
  sqlite4_bind_text $S 2 "%de" 3
  test_for_recompile $S
} {1}
do_test analyze3-3.8.13 {
  test_for_recompile $S
} {0}
do_test analyze3-3.8.14 {
  sqlite4_finalize $S
} {SQLITE4_OK}

#-------------------------------------------------------------------------
# These tests check that errors encountered while repreparing an SQL
# statement within sqlite4Reprepare() are handled correctly.
#

# Check a schema error.
#
do_test analyze3-4.1.1 {
  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  sqlite4_step $S
} {SQLITE4_DONE}
do_test analyze3-4.1.2 {
  sqlite4_reset $S
  sqlite4_bind_text $S 2 "abc" 3
  execsql { DROP TABLE t1 }
  sqlite4_step $S
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    CREATE INDEX i1 ON t1(b);
  }
  for {set i 0} {$i < 100} {incr i} {
    execsql { INSERT INTO t1 VALUES($i, $i, $i) }
  }
  execsql COMMIT
  execsql ANALYZE
  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  sqlite4_step $S
} {SQLITE4_DONE}
db auth auth
proc auth {args} {
  if {[lindex $args 0] == "SQLITE4_READ"} {return SQLITE4_DENY}
  return SQLITE4_OK
}







|







529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
    CREATE INDEX i1 ON t1(b);
  }
  for {set i 0} {$i < 100} {incr i} {
    execsql { INSERT INTO t1 VALUES($i, $i, $i) }
  }
  execsql COMMIT
  execsql ANALYZE
  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  sqlite4_step $S
} {SQLITE4_DONE}
db auth auth
proc auth {args} {
  if {[lindex $args 0] == "SQLITE4_READ"} {return SQLITE4_DENY}
  return SQLITE4_OK
}
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

# Check the effect of an authorization error that occurs in a re-prepare
# performed by sqlite4_step() is the same as one that occurs within
# sqlite4Reprepare().
#
do_test analyze3-4.3.1 {
  db auth {}
  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  execsql { CREATE TABLE t2(d, e, f) }
  db auth auth
  sqlite4_step $S
} {SQLITE4_AUTH}
do_test analyze3-4.3.2 {
  sqlite4_finalize $S
} {SQLITE4_AUTH}
db auth {}

#-------------------------------------------------------------------------
# Test that modifying bound variables using the clear_bindings() or
# transfer_bindings() APIs works.
#
#   analyze3-5.1.*: sqlite4_clear_bindings()
#   analyze3-5.2.*: sqlite4_transfer_bindings()
#
do_test analyze3-5.1.1 {
  drop_all_tables
  execsql {
    CREATE TABLE t1(x TEXT COLLATE NOCASE);
    CREATE INDEX i1 ON t1(x);
    INSERT INTO t1 VALUES('aaa');
    INSERT INTO t1 VALUES('abb');
    INSERT INTO t1 VALUES('acc');
    INSERT INTO t1 VALUES('baa');
    INSERT INTO t1 VALUES('bbb');
    INSERT INTO t1 VALUES('bcc');
  }

  set S [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
  sqlite4_bind_text $S 1 "a%" 2
  set R [list]
  while { "SQLITE4_ROW" == [sqlite4_step $S] } {
    lappend R [sqlite4_column_text $S 0]
  }
  concat [sqlite4_reset $S] $R
} {SQLITE4_OK aaa abb acc}
do_test analyze3-5.1.2 {
  sqlite4_clear_bindings $S
  set R [list]
  while { "SQLITE4_ROW" == [sqlite4_step $S] } {
    lappend R [sqlite4_column_text $S 0]
  }
  concat [sqlite4_reset $S] $R
} {SQLITE4_OK}
do_test analyze3-5.1.3 {
  sqlite4_finalize $S
} {SQLITE4_OK}

do_test analyze3-5.1.1 {
  set S1 [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
  sqlite4_bind_text $S1 1 "b%" 2
  set R [list]
  while { "SQLITE4_ROW" == [sqlite4_step $S1] } {
    lappend R [sqlite4_column_text $S1 0]
  }
  concat [sqlite4_reset $S1] $R
} {SQLITE4_OK baa bbb bcc}

do_test analyze3-5.1.2 {
  set S2 [sqlite4_prepare_v2 db "SELECT * FROM t1 WHERE x = ?" -1 dummy]
  sqlite4_bind_text $S2 1 "a%" 2
  sqlite4_transfer_bindings $S2 $S1
  set R [list]
  while { "SQLITE4_ROW" == [sqlite4_step $S1] } {
    lappend R [sqlite4_column_text $S1 0]
  }
  concat [sqlite4_reset $S1] $R
} {SQLITE4_OK aaa abb acc}
do_test analyze3-5.1.3 {
  sqlite4_finalize $S2
  sqlite4_finalize $S1
} {SQLITE4_OK}

finish_test







|










|
<
<
<
<














|



















<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570




571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

























605

# Check the effect of an authorization error that occurs in a re-prepare
# performed by sqlite4_step() is the same as one that occurs within
# sqlite4Reprepare().
#
do_test analyze3-4.3.1 {
  db auth {}
  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  execsql { CREATE TABLE t2(d, e, f) }
  db auth auth
  sqlite4_step $S
} {SQLITE4_AUTH}
do_test analyze3-4.3.2 {
  sqlite4_finalize $S
} {SQLITE4_AUTH}
db auth {}

#-------------------------------------------------------------------------
# Test that modifying bound variables using the clear_bindings() works.




#
do_test analyze3-5.1.1 {
  drop_all_tables
  execsql {
    CREATE TABLE t1(x TEXT COLLATE NOCASE);
    CREATE INDEX i1 ON t1(x);
    INSERT INTO t1 VALUES('aaa');
    INSERT INTO t1 VALUES('abb');
    INSERT INTO t1 VALUES('acc');
    INSERT INTO t1 VALUES('baa');
    INSERT INTO t1 VALUES('bbb');
    INSERT INTO t1 VALUES('bcc');
  }

  set S [sqlite4_prepare db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
  sqlite4_bind_text $S 1 "a%" 2
  set R [list]
  while { "SQLITE4_ROW" == [sqlite4_step $S] } {
    lappend R [sqlite4_column_text $S 0]
  }
  concat [sqlite4_reset $S] $R
} {SQLITE4_OK aaa abb acc}
do_test analyze3-5.1.2 {
  sqlite4_clear_bindings $S
  set R [list]
  while { "SQLITE4_ROW" == [sqlite4_step $S] } {
    lappend R [sqlite4_column_text $S 0]
  }
  concat [sqlite4_reset $S] $R
} {SQLITE4_OK}
do_test analyze3-5.1.3 {
  sqlite4_finalize $S
} {SQLITE4_OK}


























finish_test
Changes to test/ckpt1.test.
44
45
46
47
48
49
50

51
52
53
54
55
56
57

#-------------------------------------------------------------------------
# The point of this test is to add a large number of blocks to the 
# free-block list and check that this doesn't seem to cause any
# obvious problems.
#
do_test 3.0 {

  db close
  forcedelete test.db
  sqlite4 db file:test.db?lsm_block_size=65536
  execsql { 
    CREATE TABLE t1(a PRIMARY KEY, b);
    CREATE INDEX i1 ON t1(b);
  }







>







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#-------------------------------------------------------------------------
# The point of this test is to add a large number of blocks to the 
# free-block list and check that this doesn't seem to cause any
# obvious problems.
#
do_test 3.0 {
  prng_state_set 11003242342353200
  db close
  forcedelete test.db
  sqlite4 db file:test.db?lsm_block_size=65536
  execsql { 
    CREATE TABLE t1(a PRIMARY KEY, b);
    CREATE INDEX i1 ON t1(b);
  }
Changes to test/permutations.test.
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  lsm1.test lsm2.test lsm3.test lsm4.test lsm5.test
  csr1.test
  ckpt1.test
  mc1.test
  fts5expr1.test fts5query1.test fts5rnd1.test fts5create.test
  fts5snippet.test

  analyze.test
  auth.test auth2.test auth3.test auth4.test
  aggerror.test
  attach.test
  autoindex1.test
  badutf.test
  between.test
  bigrow.test







|







137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  lsm1.test lsm2.test lsm3.test lsm4.test lsm5.test
  csr1.test
  ckpt1.test
  mc1.test
  fts5expr1.test fts5query1.test fts5rnd1.test fts5create.test
  fts5snippet.test

  analyze.test analyze3.test
  auth.test auth2.test auth3.test auth4.test
  aggerror.test
  attach.test
  autoindex1.test
  badutf.test
  between.test
  bigrow.test
Changes to test/test_main.c.
3846
3847
3848
3849
3850
3851
3852


































3853
3854
3855
3856
3857
3858
3859
  ** printExplainQueryPlan() immediately.
  */
  fflush(stdout);
  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  return TCL_OK;
}
#endif /* SQLITE4_OMIT_EXPLAIN */



































/*
** sqlite4_test_control VERB ARGS...
*/
static int test_test_control(
  void * clientData,
  Tcl_Interp *interp,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
  ** printExplainQueryPlan() immediately.
  */
  fflush(stdout);
  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  return TCL_OK;
}
#endif /* SQLITE4_OMIT_EXPLAIN */

static int test_prng_state_get(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_int64 iVal;
  if( objc!=0 ){
    Tcl_WrongNumArgs(interp, 1, objv, "");
    return TCL_ERROR;
  }
  sqlite4_test_control(SQLITE4_TESTCTRL_PRNG_SET, (sqlite4_env *)0, &iVal);
  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
  return TCL_OK;
}

static int test_prng_state_set(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_int64 iVal;
  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "VALUE");
    return TCL_ERROR;
  }
  if( Tcl_GetWideIntFromObj(interp, objv[1], &iVal) ){
    return TCL_ERROR;
  }
  sqlite4_test_control(SQLITE4_TESTCTRL_PRNG_SET, (sqlite4_env *)0, iVal);
  return TCL_OK;
}

/*
** sqlite4_test_control VERB ARGS...
*/
static int test_test_control(
  void * clientData,
  Tcl_Interp *interp,
4207
4208
4209
4210
4211
4212
4213



4214
4215
4216
4217
4218
4219
4220
     { "tcl_variable_type",       tcl_variable_type, 0       },
     { "sqlite4_libversion_number", test_libversion_number, 0  },
     { "test_sqlite4_log",         test_sqlite4_log, 0  },
#ifndef SQLITE4_OMIT_EXPLAIN
     { "print_explain_query_plan", test_print_eqp, 0  },
#endif
     { "sqlite4_test_control", test_test_control },



  };
  static int bitmask_size = sizeof(Bitmask)*8;
  int i;
  extern int sqlite4_opentemp_count;
  extern int sqlite4_like_count;
  extern int sqlite4_xferopt_count;
  extern int sqlite4_pager_readdb_count;







>
>
>







4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
     { "tcl_variable_type",       tcl_variable_type, 0       },
     { "sqlite4_libversion_number", test_libversion_number, 0  },
     { "test_sqlite4_log",         test_sqlite4_log, 0  },
#ifndef SQLITE4_OMIT_EXPLAIN
     { "print_explain_query_plan", test_print_eqp, 0  },
#endif
     { "sqlite4_test_control", test_test_control },

     { "prng_state_get", test_prng_state_get },
     { "prng_state_set", test_prng_state_set },
  };
  static int bitmask_size = sizeof(Bitmask)*8;
  int i;
  extern int sqlite4_opentemp_count;
  extern int sqlite4_like_count;
  extern int sqlite4_xferopt_count;
  extern int sqlite4_pager_readdb_count;