SQLite

Check-in [658b84d7]
Login

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

Overview
Comment:Fix VDBE opcodes OP_SeekLT and OP_SeekLE so that they work on intkey tables with non-numeric text values. Fix for [9cf6c9bb].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 658b84d7f4a0886591c5aab30ed9e31c4a0f56db303eb863f24833ca37085d14
User & Date: dan 2019-05-08 11:52:13
Original Comment: Fix VDBE opcodes OP_SeekLT and OP_SeekLE so that they work on intkey tables with non-numeric text values.
References
2019-05-14
20:25
Fix a problem with the fix for [9cf6c9bb51] (commit [658b84d7]) that could cause a cursor to be left in an invalid state following a (rowid < text-value) search. (check-in: bc7d2c16 user: dan tags: trunk)
2019-05-09
11:19
Fix a problem in the new code introduced by [658b84d7] causing corruption and other errors to be ignored. (check-in: 7ccf2e7d user: dan tags: trunk)
2019-05-08
11:54 Ticket [9cf6c9bb] "<" or "<=" comparison of rowid and non-numeric text value sometimes gets the wrong answer. status still Closed with 3 other changes (artifact: 8cbc4833 user: dan)
Context
2019-05-08
17:27
Provide the SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA compile-time option to omit the case_sensitive_like pragma. This change, in combination with documentation changes, is the current solution to ticket [a340eef47b0cad5]. (check-in: eabe7f2d user: drh tags: trunk)
11:52
Fix VDBE opcodes OP_SeekLT and OP_SeekLE so that they work on intkey tables with non-numeric text values. Fix for [9cf6c9bb]. (check-in: 658b84d7 user: dan tags: trunk)
11:42
Fix VDBE opcodes OP_SeekLT and OP_SeekLE so that they work on intkey tables with non-numeric text values. (Closed-Leaf check-in: a870c196 user: dan tags: tkt-9cf6c9bb)
04:33
Remove an ALWAYS() that was previously added by check-in [a0819086] but which turns out can sometimes be false. (check-in: ad8fc5d8 user: drh tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/vdbe.c.

4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018



4019

4020
4021
4022
4023
4024
4025
4026
    }
    iKey = sqlite3VdbeIntValue(pIn3);

    /* If the P3 value could not be converted into an integer without
    ** loss of information, then special processing is required... */
    if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
      if( (pIn3->flags & MEM_Real)==0 ){
        /* If the P3 value cannot be converted into any kind of a number,
        ** then the seek is not possible, so jump to P2 */
        VdbeBranchTaken(1,2); goto jump_to_p2;
        break;



      }


      /* If the approximation iKey is larger than the actual real search
      ** term, substitute >= for > and < for <=. e.g. if the search term
      ** is 4.9 and the integer approximation 5:
      **
      **        (x >  4.9)    ->     (x >= 5)
      **        (x <= 4.9)    ->     (x <  5)







|
<
|
|
>
>
>
|
>







4008
4009
4010
4011
4012
4013
4014
4015

4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
    }
    iKey = sqlite3VdbeIntValue(pIn3);

    /* If the P3 value could not be converted into an integer without
    ** loss of information, then special processing is required... */
    if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
      if( (pIn3->flags & MEM_Real)==0 ){
        if( (pIn3->flags & MEM_Null) || oc>=OP_SeekGE ){

          VdbeBranchTaken(1,2); goto jump_to_p2;
          break;
        }else{
          sqlite3BtreeLast(pC->uc.pCursor, &res);
          goto seek_not_found;
        }
      }else

      /* If the approximation iKey is larger than the actual real search
      ** term, substitute >= for > and < for <=. e.g. if the search term
      ** is 4.9 and the integer approximation 5:
      **
      **        (x >  4.9)    ->     (x >= 5)
      **        (x <= 4.9)    ->     (x <  5)
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
      ** term, substitute <= for < and > for >=.  */
      else if( pIn3->u.r>(double)iKey ){
        assert( OP_SeekLE==(OP_SeekLT+1) );
        assert( OP_SeekGT==(OP_SeekGE+1) );
        assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
        if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
      }
    } 
    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
    pC->movetoTarget = iKey;  /* Used by OP_Delete */
    if( rc!=SQLITE_OK ){
      goto abort_due_to_error;
    }
  }else{
    /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and







|







4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
      ** term, substitute <= for < and > for >=.  */
      else if( pIn3->u.r>(double)iKey ){
        assert( OP_SeekLE==(OP_SeekLT+1) );
        assert( OP_SeekGT==(OP_SeekGE+1) );
        assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
        if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
      }
    }
    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
    pC->movetoTarget = iKey;  /* Used by OP_Delete */
    if( rc!=SQLITE_OK ){
      goto abort_due_to_error;
    }
  }else{
    /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and

Changes to test/rowid.test.

655
656
657
658
659
660
661


























662
663
664
665
666
667
668
do_test rowid-11.3 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
do_test rowid-11.4 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}



























# Test the automatic generation of rowids when the table already contains
# a rowid with the maximum value.
#
# Once the maximum rowid is taken, rowids are normally chosen at
# random.  By by reseting the random number generator, we can cause
# the rowid guessing loop to collide with prior rowids, and test the
# loop out to its limit of 100 iterations.  After 100 collisions, the







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







655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
do_test rowid-11.3 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
do_test rowid-11.4 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}

do_test rowid-11.asc.1 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc' ORDER BY 1 ASC}
} {}
do_test rowid-11.asc.2 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc' ORDER BY 1 ASC}
} {}
do_test rowid-11.asc.3 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc' ORDER BY 1 ASC}
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
do_test rowid-11.asc.4 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc' ORDER BY 1 ASC}
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}

do_test rowid-11.desc.1 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc' ORDER BY 1 DESC}
} {}
do_test rowid-11.desc.2 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc' ORDER BY 1 DESC}
} {}
do_test rowid-11.desc.3 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc' ORDER BY 1 DESC}
} {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1}
do_test rowid-11.desc.4 {
  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc' ORDER BY 1 DESC}
} {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1}

# Test the automatic generation of rowids when the table already contains
# a rowid with the maximum value.
#
# Once the maximum rowid is taken, rowids are normally chosen at
# random.  By by reseting the random number generator, we can cause
# the rowid guessing loop to collide with prior rowids, and test the
# loop out to its limit of 100 iterations.  After 100 collisions, the
714
715
716
717
718
719
720
721




















722
db function addrow rowid_addrow_func
do_execsql_test rowid-13.1 {
  CREATE TABLE t13(x);
  INSERT INTO t13(rowid,x) VALUES(1234,5);
  SELECT rowid, x, addrow(rowid+1000), '|' FROM t13 LIMIT 3;
  SELECT last_insert_rowid();
} {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234}





















finish_test








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

740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
db function addrow rowid_addrow_func
do_execsql_test rowid-13.1 {
  CREATE TABLE t13(x);
  INSERT INTO t13(rowid,x) VALUES(1234,5);
  SELECT rowid, x, addrow(rowid+1000), '|' FROM t13 LIMIT 3;
  SELECT last_insert_rowid();
} {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234}

#-------------------------------------------------------------------------
do_execsql_test rowid-14.0 {
  CREATE TABLE t14(x INTEGER PRIMARY KEY);
  INSERT INTO t14(x) VALUES (100);
}
do_execsql_test rowid-14.1 {
  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid ASC;
} {100}
do_execsql_test rowid-14.2 {
  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid DESC;
} {100}

do_execsql_test rowid-14.3 {
  DELETE FROM t14;
  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid ASC;
} {}
do_execsql_test rowid-14.4 {
  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid DESC;
} {}

finish_test