SQLite

Check-in [97fb5a72]
Login

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

Overview
Comment:In the rtree extension, actively suppress extra tokens on the column names, and be pedantic about column affinities. Ticket [367a86e28859f1ff]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 97fb5a72f91a44d579466e67c33aa862384c4d1c95c2010cf4ae9dbd172bbcfb
User & Date: drh 2019-12-06 13:10:29
Context
2019-12-06
19:07
Early detection and reporting of too few columns in an rtree CREATE VIRTUAL TABLE statement. (check-in: ef73107f user: drh tags: trunk)
13:10
In the rtree extension, actively suppress extra tokens on the column names, and be pedantic about column affinities. Ticket [367a86e28859f1ff] (check-in: 97fb5a72 user: drh tags: trunk)
11:48
The check-in [b7810062ec2489e1] was not quite right in that it allowed an oversized shift operation (which is UB in C) on some obscure inputs. OSSFuzz found the problem for us overnight. (check-in: 62f2235a user: drh tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ext/rtree/rtree.c.

58
59
60
61
62
63
64

65
66
67
68
69
70
71

#ifndef SQLITE_CORE
  #include "sqlite3ext.h"
  SQLITE_EXTENSION_INIT1
#else
  #include "sqlite3.h"
#endif


#ifndef SQLITE_AMALGAMATION
#include "sqlite3rtree.h"
typedef sqlite3_int64 i64;
typedef sqlite3_uint64 u64;
typedef unsigned char u8;
typedef unsigned short u16;







>







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

#ifndef SQLITE_CORE
  #include "sqlite3ext.h"
  SQLITE_EXTENSION_INIT1
#else
  #include "sqlite3.h"
#endif
int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */

#ifndef SQLITE_AMALGAMATION
#include "sqlite3rtree.h"
typedef sqlite3_int64 i64;
typedef sqlite3_uint64 u64;
typedef unsigned char u8;
typedef unsigned short u16;
3656
3657
3658
3659
3660
3661
3662








3663
3664
3665
3666
3667
3668
3669
                               pRtree->zName);
    }
  }

  sqlite3_free(zSql);
  return rc;
}









/* 
** This function is the implementation of both the xConnect and xCreate
** methods of the r-tree virtual table.
**
**   argv[0]   -> module name
**   argv[1]   -> database name







>
>
>
>
>
>
>
>







3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
                               pRtree->zName);
    }
  }

  sqlite3_free(zSql);
  return rc;
}

/*
** Return the length of a token
*/
static int rtreeTokenLength(const char *z){
  int dummy = 0;
  return sqlite3GetToken((const unsigned char*)z,&dummy);
}

/* 
** This function is the implementation of both the xConnect and xCreate
** methods of the r-tree virtual table.
**
**   argv[0]   -> module name
**   argv[1]   -> database name
3722
3723
3724
3725
3726
3727
3728
3729

3730

3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745


  /* Create/Connect to the underlying relational database schema. If
  ** that is successful, call sqlite3_declare_vtab() to configure
  ** the r-tree table schema.
  */
  pSql = sqlite3_str_new(db);
  sqlite3_str_appendf(pSql, "CREATE TABLE x(%s INT", argv[3]);

  for(ii=4; ii<argc; ii++){

    if( argv[ii][0]=='+' ){
      pRtree->nAux++;
      sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
    }else if( pRtree->nAux>0 ){
      break;
    }else{
      pRtree->nDim2++;
      sqlite3_str_appendf(pSql, ",%s", argv[ii]);
    }
  }
  sqlite3_str_appendf(pSql, ");");
  zSql = sqlite3_str_finish(pSql);
  if( !zSql ){
    rc = SQLITE_NOMEM;
  }else if( ii<argc ){







|
>

>
|

|




|







3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756


  /* Create/Connect to the underlying relational database schema. If
  ** that is successful, call sqlite3_declare_vtab() to configure
  ** the r-tree table schema.
  */
  pSql = sqlite3_str_new(db);
  sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT", 
                      rtreeTokenLength(argv[3]), argv[3]);
  for(ii=4; ii<argc; ii++){
    const char *zArg = argv[ii];
    if( zArg[0]=='+' ){
      pRtree->nAux++;
      sqlite3_str_appendf(pSql, ",%s", zArg+1);
    }else if( pRtree->nAux>0 ){
      break;
    }else{
      pRtree->nDim2++;
      sqlite3_str_appendf(pSql, ",%.*s NUM", rtreeTokenLength(zArg), zArg);
    }
  }
  sqlite3_str_appendf(pSql, ");");
  zSql = sqlite3_str_finish(pSql);
  if( !zSql ){
    rc = SQLITE_NOMEM;
  }else if( ii<argc ){

Changes to ext/rtree/rtree1.test.

700
701
702
703
704
705
706









707
708
709
  REINDEX t1;
  REINDEX t2;
} {}

do_execsql_test 17.2 {
  REINDEX;
} {}










expand_all_sql db
finish_test







>
>
>
>
>
>
>
>
>



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  REINDEX t1;
  REINDEX t2;
} {}

do_execsql_test 17.2 {
  REINDEX;
} {}

reset_db
do_execsql_test 18.0 {
  CREATE VIRTUAL TABLE rt0 USING rtree(c0, c1, c2);
  INSERT INTO rt0(c0,c1,c2) VALUES(9,2,3);
  SELECT c0 FROM rt0 WHERE rt0.c1 > '-1'; 
  SELECT rt0.c1 > '-1' FROM rt0;
} {9 1}


expand_all_sql db
finish_test

Changes to ext/rtree/rtree2.test.

29
30
31
32
33
34
35

36
37
38
39
40
41
42
43
44
45
46
47
48

if {[info exists G(isquick)] && $G(isquick)} {
  set ::NROW 100
  set ::NSELECT 10
}

foreach module {rtree_i32 rtree} {

  for {set nDim 1} {$nDim <= 5} {incr nDim} {
  
    do_test rtree2-$module.$nDim.1 {
      set cols [list]
      foreach c [list c0 c1 c2 c3 c4 c5 c6 c7 c8 c9] {
        lappend cols "$c REAL"
      }
      set cols [join [lrange $cols 0 [expr {$nDim*2-1}]] ", "]
      execsql " 
        CREATE VIRTUAL TABLE t1 USING ${module}(ii, $cols);
        CREATE TABLE t2 (ii, $cols);
      "
    } {}







>





|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

if {[info exists G(isquick)] && $G(isquick)} {
  set ::NROW 100
  set ::NSELECT 10
}

foreach module {rtree_i32 rtree} {
  if {$module=="rtree_i32"} {set etype INT} {set etype REAL}
  for {set nDim 1} {$nDim <= 5} {incr nDim} {
  
    do_test rtree2-$module.$nDim.1 {
      set cols [list]
      foreach c [list c0 c1 c2 c3 c4 c5 c6 c7 c8 c9] {
        lappend cols "$c $etype"
      }
      set cols [join [lrange $cols 0 [expr {$nDim*2-1}]] ", "]
      execsql " 
        CREATE VIRTUAL TABLE t1 USING ${module}(ii, $cols);
        CREATE TABLE t2 (ii, $cols);
      "
    } {}