14aeff62eSdanielk1977# 2007 May 12 24aeff62eSdanielk1977# 34aeff62eSdanielk1977# The author disclaims copyright to this source code. In place of 44aeff62eSdanielk1977# a legal notice, here is a blessing: 54aeff62eSdanielk1977# 64aeff62eSdanielk1977# May you do good and not evil. 74aeff62eSdanielk1977# May you find forgiveness for yourself and forgive others. 84aeff62eSdanielk1977# May you share freely, never taking more than you give. 94aeff62eSdanielk1977# 104aeff62eSdanielk1977#*********************************************************************** 114aeff62eSdanielk1977# This file tests a special case in the b-tree code that can be 124aeff62eSdanielk1977# hit by the "IN" operator (or EXISTS, NOT IN, etc.). 134aeff62eSdanielk1977# 14*e8f52c50Sdrh# $Id: in2.test,v 1.3 2008/07/12 14:52:20 drh Exp $ 154aeff62eSdanielk1977 164aeff62eSdanielk1977set testdir [file dirname $argv0] 174aeff62eSdanielk1977source $testdir/tester.tcl 184aeff62eSdanielk1977 194aeff62eSdanielk1977do_test in2-1 { 204aeff62eSdanielk1977 execsql { 214aeff62eSdanielk1977 CREATE TABLE a(i INTEGER PRIMARY KEY, a); 224aeff62eSdanielk1977 } 234aeff62eSdanielk1977} {} 244aeff62eSdanielk1977 2539c9604aSdanielk1977set ::N 2000 2639c9604aSdanielk1977 274aeff62eSdanielk1977do_test in2-2 { 284aeff62eSdanielk1977 db transaction { 2939c9604aSdanielk1977 for {set ::ii 0} {$::ii < $::N} {incr ::ii} { 304aeff62eSdanielk1977 execsql {INSERT INTO a VALUES($::ii, $::ii)} 314aeff62eSdanielk1977 } 3239c9604aSdanielk1977 execsql {INSERT INTO a VALUES(4000, '')} 334aeff62eSdanielk1977 3439c9604aSdanielk1977 for {set ::ii 0} {$::ii < $::N} {incr ::ii} { 3539c9604aSdanielk1977 set ::t [format "x%04d" $ii] 3639c9604aSdanielk1977 execsql {INSERT INTO a VALUES(NULL, $::t)} 374aeff62eSdanielk1977 } 384aeff62eSdanielk1977 } 394aeff62eSdanielk1977} {} 404aeff62eSdanielk1977 414aeff62eSdanielk1977# Each iteration of this loop builds a slightly different b-tree to 424aeff62eSdanielk1977# evaluate the "IN (...)" operator in the SQL statement. The contents 434aeff62eSdanielk1977# of the b-tree are (in sorted order): 444aeff62eSdanielk1977# 454aeff62eSdanielk1977# $::ii integers. 464aeff62eSdanielk1977# a string of zero length. 4739c9604aSdanielk1977# $::N short strings. 484aeff62eSdanielk1977# 494aeff62eSdanielk1977# Records are inserted in sorted order. 504aeff62eSdanielk1977# 514aeff62eSdanielk1977# The string of zero-length is stored in a b-tree cell with 3 bytes 524aeff62eSdanielk1977# of payload. Moving this cell from a leaf node to a internal node 534aeff62eSdanielk1977# during b-tree balancing was causing an assertion failure. 544aeff62eSdanielk1977# 554aeff62eSdanielk1977# This bug only applied to b-trees generated to evaluate IN (..) 564aeff62eSdanielk1977# clauses, as it is impossible for persistent b-trees (SQL tables + 574aeff62eSdanielk1977# indices) to contain cells smaller than 4 bytes. 584aeff62eSdanielk1977# 5939c9604aSdanielk1977for {set ::ii 3} {$::ii < $::N} {incr ::ii} { 604aeff62eSdanielk1977 do_test in2-$::ii { 614aeff62eSdanielk1977 execsql { 6239c9604aSdanielk1977 SELECT 1 IN (SELECT a FROM a WHERE (i < $::ii) OR (i >= $::N)) 634aeff62eSdanielk1977 } 644aeff62eSdanielk1977 } {1} 654aeff62eSdanielk1977} 664aeff62eSdanielk1977 674aeff62eSdanielk1977finish_test 68