/ Check-in [396b9d99]
Login

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

Overview
Comment:Bummer. Checked this in as a branch off of the wrong branch.... Was: Add the companion "carray_asc" table-valued function to the carray extension.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | mistake
Files: files | file ages | folders
SHA1: 396b9d99ae43cc5c9ce18509754aac001eae0ecf
User & Date: drh 2017-02-15 16:04:23
Original Comment: Add the companion "carray_asc" table-valued function to the carray extension.
Context
2017-02-15
16:11
Add the companion "carray_asc" table-valued function to the carray extension. check-in: a2b4f60b user: drh tags: carray_asc
16:04
Bummer. Checked this in as a branch off of the wrong branch.... Was: Add the companion "carray_asc" table-valued function to the carray extension. Closed-Leaf check-in: 396b9d99 user: drh tags: mistake
15:11
Remove the CLANG_VERSION macro, since we have learned that version numbers in clang are "marketing" and are inconsistent and unreliable. Builds using clang will still use the GCC_VERSION macro since clang works hard to be gcc compatible. check-in: 8d3f485d user: drh tags: branch-3.17
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to ext/misc/carray.c.

    21     21   ** has been cast to an integer.
    22     22   **
    23     23   ** There is an optional third parameter to determine the datatype of
    24     24   ** the C-language array.  Allowed values of the third parameter are
    25     25   ** 'int32', 'int64', 'double', 'char*'.  Example:
    26     26   **
    27     27   **      SELECT * FROM carray($ptr,10,'char*');
           28  +**
           29  +** There is a second table-valued funnction named "carrray_asc" that works
           30  +** exactly like carray except that it requires the values in the $ptr array
           31  +** to be in ascending order.  Queries involving ORDER BY clauses can sometimes
           32  +** be a little faster with carray_asc compared to plain carray.  However, if
           33  +** the application provides carray_asc($ptr) with a $ptr array in which the
           34  +** elements are not in ascending order, then incorrect query results might
           35  +** result.
    28     36   **
    29     37   ** HOW IT WORKS
    30     38   **
    31     39   ** The carray "function" is really a virtual table with the
    32     40   ** following schema:
    33     41   **
    34     42   **     CREATE TABLE carray(
................................................................................
    60     68   #define CARRAY_TEXT     3
    61     69   
    62     70   /*
    63     71   ** Names of types
    64     72   */
    65     73   static const char *azType[] = { "int32", "int64", "double", "char*" };
    66     74   
           75  +/* carray_vtab is a subclass of sqlite3_vtab containing carray-specific
           76  +** extensions.
           77  +*/
           78  +typedef struct carray_vtab carray_vtab;
           79  +struct carray_vtab {
           80  +  sqlite3_vtab base;         /* Base class - must be first */
           81  +  unsigned int mFlags;       /* Operational flags */
           82  +};
           83  +
           84  +/*
           85  +** Possible values for carray_vtab.mFlags
           86  +*/
           87  +#define CARRAY_ASC    0x0001    /* Values are always in ascending order */
           88  +
    67     89   
    68     90   /* carray_cursor is a subclass of sqlite3_vtab_cursor which will
    69     91   ** serve as the underlying representation of a cursor that scans
    70     92   ** over rows of the result
    71     93   */
    72     94   typedef struct carray_cursor carray_cursor;
    73     95   struct carray_cursor {
................................................................................
    94    116   static int carrayConnect(
    95    117     sqlite3 *db,
    96    118     void *pAux,
    97    119     int argc, const char *const*argv,
    98    120     sqlite3_vtab **ppVtab,
    99    121     char **pzErr
   100    122   ){
   101         -  sqlite3_vtab *pNew;
          123  +  carray_vtab *pNew;
   102    124     int rc;
   103    125   
   104    126   /* Column numbers */
   105    127   #define CARRAY_COLUMN_VALUE   0
   106    128   #define CARRAY_COLUMN_POINTER 1
   107    129   #define CARRAY_COLUMN_COUNT   2
   108    130   #define CARRAY_COLUMN_CTYPE   3
   109    131   
   110    132     rc = sqlite3_declare_vtab(db,
   111    133        "CREATE TABLE x(value,pointer hidden,count hidden,ctype hidden)");
   112    134     if( rc==SQLITE_OK ){
   113         -    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
          135  +    pNew = sqlite3_malloc( sizeof(*pNew) );
          136  +    *ppVtab = (sqlite3_vtab*)pNew;
   114    137       if( pNew==0 ) return SQLITE_NOMEM;
   115    138       memset(pNew, 0, sizeof(*pNew));
          139  +    if( pAux ) pNew->mFlags = *(unsigned int*)pAux;
   116    140     }
   117    141     return rc;
   118    142   }
   119    143   
   120    144   /*
   121    145   ** This method is the destructor for carray_cursor objects.
   122    146   */
................................................................................
   301    325       pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1;
   302    326       pIdxInfo->aConstraintUsage[ptrIdx].omit = 1;
   303    327       pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2;
   304    328       pIdxInfo->aConstraintUsage[cntIdx].omit = 1;
   305    329       pIdxInfo->estimatedCost = (double)1;
   306    330       pIdxInfo->estimatedRows = 100;
   307    331       pIdxInfo->idxNum = 2;
          332  +    if( pIdxInfo->nOrderBy==1
          333  +     && pIdxInfo->aOrderBy[0].iColumn==0
          334  +     && pIdxInfo->aOrderBy[0].desc==0
          335  +     && (((carray_vtab*)tab)->mFlags & CARRAY_ASC)!=0
          336  +    ){
          337  +      pIdxInfo->orderByConsumed = 1;
          338  +    }
   308    339       if( ctypeIdx>=0 ){
   309    340         pIdxInfo->aConstraintUsage[ctypeIdx].argvIndex = 3;
   310    341         pIdxInfo->aConstraintUsage[ctypeIdx].omit = 1;
   311    342         pIdxInfo->idxNum = 3;
   312    343       }
   313    344     }else{
   314    345       pIdxInfo->estimatedCost = (double)2147483647;
................................................................................
   352    383   #endif
   353    384   int sqlite3_carray_init(
   354    385     sqlite3 *db, 
   355    386     char **pzErrMsg, 
   356    387     const sqlite3_api_routines *pApi
   357    388   ){
   358    389     int rc = SQLITE_OK;
          390  +  static const unsigned int mAscFlags = CARRAY_ASC;
   359    391     SQLITE_EXTENSION_INIT2(pApi);
   360    392   #ifndef SQLITE_OMIT_VIRTUALTABLE
   361    393     rc = sqlite3_create_module(db, "carray", &carrayModule, 0);
          394  +  if( rc==SQLITE_OK ){
          395  +    rc = sqlite3_create_module(db, "carray_asc", &carrayModule,
          396  +                               (void*)&mAscFlags);
          397  +  }
   362    398   #endif
   363    399     return rc;
   364    400   }