/ Check-in [ef6936e5]
Login

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

Overview
Comment:Initialize the global built-in function table at start-time instead of at compile-time. This is less prone to malfunction when compile-time parameters very. (CVS 5583)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ef6936e50adb9ebea39c890167403fff01bbb5ed
User & Date: drh 2008-08-21 18:49:28
Context
2008-08-21
19:28
Remove references to OP_MoveTo in comments of vdbe.c - that opcode no longer exists. Ticket #3327. (CVS 5584) check-in: 30077ece user: drh tags: trunk
18:49
Initialize the global built-in function table at start-time instead of at compile-time. This is less prone to malfunction when compile-time parameters very. (CVS 5583) check-in: ef6936e5 user: drh tags: trunk
15:54
Fix soft-heap-limit related test suite failures. (CVS 5582) check-in: 2091d9a5 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   153    153   # You should not have to change anything below this line
   154    154   ###############################################################################
   155    155   
   156    156   # Object files for the SQLite library (non-amalgamation).
   157    157   #
   158    158   OBJS0 = alter.lo analyze.lo attach.lo auth.lo bitvec.lo btmutex.lo \
   159    159           btree.lo build.lo callback.lo complete.lo date.lo \
   160         -        delete.lo expr.lo fault.lo func2.lo global.lo \
          160  +        delete.lo expr.lo fault.lo func.lo global.lo \
   161    161           hash.lo journal.lo insert.lo loadext.lo \
   162    162           main.lo malloc.lo mem1.lo mem2.lo mem3.lo mem4.lo mem5.lo mem6.lo \
   163    163           mutex.lo mutex_os2.lo mutex_unix.lo mutex_w32.lo \
   164    164           opcodes.lo os.lo os_unix.lo os_win.lo os_os2.lo \
   165    165           pager.lo parse.lo pcache.lo pragma.lo prepare.lo printf.lo random.lo \
   166    166           resolve.lo select.lo status.lo \
   167    167           table.lo tokenize.lo trigger.lo update.lo \
................................................................................
   194    194     $(TOP)/src/build.c \
   195    195     $(TOP)/src/callback.c \
   196    196     $(TOP)/src/complete.c \
   197    197     $(TOP)/src/date.c \
   198    198     $(TOP)/src/delete.c \
   199    199     $(TOP)/src/expr.c \
   200    200     $(TOP)/src/fault.c \
          201  +  $(TOP)/src/func.c \
   201    202     $(TOP)/src/global.c \
   202    203     $(TOP)/src/hash.c \
   203    204     $(TOP)/src/hash.h \
   204    205     $(TOP)/src/hwtime.h \
   205    206     $(TOP)/src/insert.c \
   206    207     $(TOP)/src/journal.c \
   207    208     $(TOP)/src/legacy.c \
................................................................................
   261    262     $(TOP)/src/vtab.c \
   262    263     $(TOP)/src/walker.c \
   263    264     $(TOP)/src/where.c
   264    265   
   265    266   # Generated source code files
   266    267   #
   267    268   SRC += \
   268         -  func2.c \
   269    269     keywordhash.h \
   270    270     opcodes.c \
   271    271     opcodes.h \
   272    272     parse.c \
   273    273     parse.h \
   274    274     config.h \
   275    275     sqlite3.h
................................................................................
   517    517   
   518    518   expr.lo:	$(TOP)/src/expr.c $(HDR)
   519    519   	$(LTCOMPILE) -c $(TOP)/src/expr.c
   520    520   
   521    521   fault.lo:	$(TOP)/src/fault.c $(HDR)
   522    522   	$(LTCOMPILE) -c $(TOP)/src/fault.c
   523    523   
   524         -func2.lo:	func2.c $(HDR)
   525         -	$(LTCOMPILE) -c func2.c
          524  +func.lo:	$(TOP)/src/func.c $(HDR)
          525  +	$(LTCOMPILE) -c $(TOP)/src/func.c
   526    526   
   527    527   global.lo:	$(TOP)/src/global.c $(HDR)
   528    528   	$(LTCOMPILE) -c $(TOP)/src/global.c
   529    529   
   530    530   hash.lo:	$(TOP)/src/hash.c $(HDR)
   531    531   	$(LTCOMPILE) -c $(TOP)/src/hash.c
   532    532   
................................................................................
   612    612   
   613    613   parse.c:	$(TOP)/src/parse.y lemon$(BEXE) $(TOP)/addopcodes.awk
   614    614   	cp $(TOP)/src/parse.y .
   615    615   	./lemon$(BEXE) $(OPTS) $(OPT_FEATURE_FLAGS) parse.y
   616    616   	mv parse.h parse.h.temp
   617    617   	$(NAWK) -f $(TOP)/addopcodes.awk parse.h.temp >parse.h
   618    618   
   619         -func2.c: $(TOP)/src/func.c $(HDR)
   620         -	$(BCC) -o mkfunction$(BEXE) $(OPTS) $(OPT_FEATURE_FLAGS) $(TOP)/tool/mkfunction.c -I$(TOP)/src -I.
   621         -	cat $(TOP)/src/func.c > func2.c
   622         -	./mkfunction$(BEXE) >> func2.c
   623         -
   624    619   pragma.lo:	$(TOP)/src/pragma.c $(HDR)
   625    620   	$(LTCOMPILE) -c $(TOP)/src/pragma.c
   626    621   
   627    622   prepare.lo:	$(TOP)/src/prepare.c $(HDR)
   628    623   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/prepare.c
   629    624   
   630    625   printf.lo:	$(TOP)/src/printf.c $(HDR)

Changes to main.mk.

    47     47   TCCX = $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP) -I$(TOP)/ext/rtree
    48     48   
    49     49   # Object files for the SQLite library.
    50     50   #
    51     51   LIBOBJ+= alter.o analyze.o attach.o auth.o \
    52     52            bitvec.o btmutex.o btree.o build.o \
    53     53            callback.o complete.o date.o delete.o \
    54         -         expr.o fault.o func2.o global.o hash.o \
           54  +         expr.o fault.o func.o global.o hash.o \
    55     55            icu.o insert.o journal.o legacy.o loadext.o \
    56     56            main.o malloc.o mem1.o mem2.o mem3.o mem4.o mem5.o mem6.o \
    57     57            mutex.o mutex_os2.o mutex_unix.o mutex_w32.o \
    58     58            opcodes.o os.o os_os2.o os_unix.o os_win.o \
    59     59            pager.o parse.o pcache.o pragma.o prepare.o printf.o \
    60     60            random.o resolve.o rtree.o select.o status.o \
    61     61            table.o tokenize.o trigger.o \
................................................................................
    79     79     $(TOP)/src/build.c \
    80     80     $(TOP)/src/callback.c \
    81     81     $(TOP)/src/complete.c \
    82     82     $(TOP)/src/date.c \
    83     83     $(TOP)/src/delete.c \
    84     84     $(TOP)/src/expr.c \
    85     85     $(TOP)/src/fault.c \
           86  +  $(TOP)/src/func.c \
    86     87     $(TOP)/src/global.c \
    87     88     $(TOP)/src/hash.c \
    88     89     $(TOP)/src/hash.h \
    89     90     $(TOP)/src/hwtime.h \
    90     91     $(TOP)/src/insert.c \
    91     92     $(TOP)/src/journal.c \
    92     93     $(TOP)/src/legacy.c \
................................................................................
   138    139     $(TOP)/src/vdbe.c \
   139    140     $(TOP)/src/vdbe.h \
   140    141     $(TOP)/src/vdbeapi.c \
   141    142     $(TOP)/src/vdbeaux.c \
   142    143     $(TOP)/src/vdbeblob.c \
   143    144     $(TOP)/src/vdbefifo.c \
   144    145     $(TOP)/src/vdbemem.c \
   145         -  $(TOP)/src/pcache.c \
   146    146     $(TOP)/src/vdbeInt.h \
   147    147     $(TOP)/src/vtab.c \
   148    148     $(TOP)/src/walker.c \
   149    149     $(TOP)/src/where.c
   150    150   
   151    151   # Source code for extensions
   152    152   #
................................................................................
   189    189   #
   190    190   SRC += \
   191    191     keywordhash.h \
   192    192     opcodes.c \
   193    193     opcodes.h \
   194    194     parse.c \
   195    195     parse.h \
   196         -  func2.c \
   197    196     sqlite3.h
   198    197   
   199    198   
   200    199   # Source code to the test files.
   201    200   #
   202    201   TESTSRC = \
   203    202     $(TOP)/src/test1.c \
................................................................................
   227    226     $(TOP)/src/test_thread.c \
   228    227   
   229    228   #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
   230    229   #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c
   231    230   
   232    231   TESTSRC2 = \
   233    232     $(TOP)/src/attach.c $(TOP)/src/btree.c $(TOP)/src/build.c $(TOP)/src/date.c  \
   234         -  $(TOP)/src/expr.c func2.c $(TOP)/src/insert.c $(TOP)/src/os.c      \
          233  +  $(TOP)/src/expr.c $(TOP)/src/func.c $(TOP)/src/insert.c $(TOP)/src/os.c      \
   235    234     $(TOP)/src/os_os2.c $(TOP)/src/os_unix.c $(TOP)/src/os_win.c                 \
   236    235     $(TOP)/src/pager.c $(TOP)/src/pragma.c $(TOP)/src/prepare.c                  \
   237    236     $(TOP)/src/printf.c $(TOP)/src/random.c $(TOP)/src/pcache.c                  \
   238    237     $(TOP)/src/select.c $(TOP)/src/tokenize.c                                    \
   239    238     $(TOP)/src/utf.c $(TOP)/src/util.c $(TOP)/src/vdbeapi.c $(TOP)/src/vdbeaux.c \
   240    239     $(TOP)/src/vdbe.c $(TOP)/src/vdbemem.c $(TOP)/src/where.c parse.c
   241    240   
................................................................................
   345    344   
   346    345   # Rules to build opcodes.c and opcodes.h
   347    346   #
   348    347   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   349    348   	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
   350    349   
   351    350   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   352         -	cat parse.h $(TOP)/src/vdbe.c |$(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   353         -
   354         -func2.c: $(TOP)/src/func.c $(HDR)
   355         -	$(BCC) -o mkfunction $(OPTS) $(TOP)/tool/mkfunction.c -I$(TOP)/src -I.
   356         -	cat $(TOP)/src/func.c > func2.c
   357         -	./mkfunction >> func2.c
          351  +	cat parse.h $(TOP)/src/vdbe.c | \
          352  +		$(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   358    353   
   359    354   # Rules to build parse.c and parse.h - the outputs of lemon.
   360    355   #
   361    356   parse.h:	parse.c
   362    357   
   363    358   parse.c:	$(TOP)/src/parse.y lemon $(TOP)/addopcodes.awk
   364    359   	cp $(TOP)/src/parse.y .
................................................................................
   493    488   clean:	
   494    489   	rm -f *.o sqlite3 libsqlite3.a sqlite3.h opcodes.*
   495    490   	rm -f lemon lempar.c parse.* sqlite*.tar.gz mkkeywordhash keywordhash.h
   496    491   	rm -f $(PUBLISH)
   497    492   	rm -f *.da *.bb *.bbg gmon.out
   498    493   	rm -rf tsrc target_source
   499    494   	rm -f testloadext.dll libtestloadext.so
   500         -	rm -f sqlite3.c fts?amal.c tclsqlite3.c func2.c
          495  +	rm -f sqlite3.c fts?amal.c tclsqlite3.c

Changes to src/callback.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains functions used to access the internal hash tables
    14     14   ** of user defined functions and collation sequences.
    15     15   **
    16         -** $Id: callback.c,v 1.27 2008/08/20 14:49:24 danielk1977 Exp $
           16  +** $Id: callback.c,v 1.28 2008/08/21 18:49:28 drh Exp $
    17     17   */
    18     18   
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** Invoke the 'collation needed' callback to request a collation sequence
    23     23   ** in the database text encoding of name zName, length nName.
................................................................................
   256    256                (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
   257    257         match += 1;
   258    258       }
   259    259     }
   260    260     return match;
   261    261   }
   262    262   
          263  +/*
          264  +** Search a FuncDefHash for a function with the given name.  Return
          265  +** a pointer to the matching FuncDef if found, or 0 if there is no match.
          266  +*/
          267  +static FuncDef *functionSearch(
          268  +  FuncDefHash *pHash,  /* Hash table to search */
          269  +  int h,               /* Hash of the name */
          270  +  const char *zFunc,   /* Name of function */
          271  +  int nFunc            /* Number of bytes in zFunc */
          272  +){
          273  +  FuncDef *p;
          274  +  for(p=pHash->a[h]; p; p=p->pHash){
          275  +    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
          276  +      return p;
          277  +    }
          278  +  }
          279  +  return 0;
          280  +}
          281  +
          282  +/*
          283  +** Insert a new FuncDef into a FuncDefHash hash table.
          284  +*/
          285  +void sqlite3FuncDefInsert(
          286  +  FuncDefHash *pHash,  /* The hash table into which to insert */
          287  +  FuncDef *pDef        /* The function definition to insert */
          288  +){
          289  +  FuncDef *pOther;
          290  +  int nName = strlen(pDef->zName);
          291  +  u8 c1 = (u8)pDef->zName[0];
          292  +  int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
          293  +  pOther = functionSearch(pHash, h, pDef->zName, nName);
          294  +  if( pOther ){
          295  +    pDef->pNext = pOther->pNext;
          296  +    pOther->pNext = pDef;
          297  +  }else{
          298  +    pDef->pNext = 0;
          299  +    pDef->pHash = pHash->a[h];
          300  +    pHash->a[h] = pDef;
          301  +  }
          302  +}
          303  +  
          304  +  
          305  +
   263    306   /*
   264    307   ** Locate a user function given a name, a number of arguments and a flag
   265    308   ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
   266    309   ** pointer to the FuncDef structure that defines that function, or return
   267    310   ** NULL if the function does not exist.
   268    311   **
   269    312   ** If the createFlag argument is true, then a new (blank) FuncDef
................................................................................
   285    328     const char *zName, /* Name of the function.  Not null-terminated */
   286    329     int nName,         /* Number of characters in the name */
   287    330     int nArg,          /* Number of arguments.  -1 means any number */
   288    331     u8 enc,            /* Preferred text encoding */
   289    332     int createFlag     /* Create new entry if true and does not otherwise exist */
   290    333   ){
   291    334     FuncDef *p;         /* Iterator variable */
   292         -  FuncDef *pFirst;    /* First function with this name */
   293    335     FuncDef *pBest = 0; /* Best match found so far */
   294         -  int bestmatch = 0;  
          336  +  int bestScore = 0;  /* Score of best match */
          337  +  int h;              /* Hash value */
   295    338   
   296    339   
   297    340     assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
   298    341     if( nArg<-1 ) nArg = -1;
          342  +  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
          343  +
   299    344   
   300         -  pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
   301         -  for(p=pFirst; p; p=p->pNext){
   302         -    int match = matchQuality(p, nArg, enc);
   303         -    if( match>bestmatch ){
          345  +  p = functionSearch(&db->aFunc, h, zName, nName);
          346  +  while( p ){
          347  +    int score = matchQuality(p, nArg, enc);
          348  +    if( score>bestScore ){
   304    349         pBest = p;
   305         -      bestmatch = match;
          350  +      bestScore = score;
   306    351       }
          352  +    p = p->pNext;
   307    353     }
   308    354   
   309    355     /* If the createFlag parameter is false and no match was found amongst
   310    356     ** the custom functions stored in sqlite3.aFunc, try to find a built-in
   311    357     ** function to use.
   312    358     */ 
   313    359     if( !createFlag && !pBest ){
   314         -    FuncDef *aFunc;
   315         -    int nFunc;
   316         -    int i;
   317         -    nFunc = sqlite3GetBuiltinFunction(zName, nName, &aFunc);
   318         -    for(i=0; i<nFunc; i++){
   319         -      int match = matchQuality(&aFunc[i], nArg, enc);
   320         -      if( match>bestmatch ){
   321         -        pBest = &aFunc[i];
   322         -        bestmatch = match;
          360  +    p = functionSearch(&sqlite3FuncBuiltins, h, zName, nName);
          361  +    while( p ){
          362  +      int score = matchQuality(p, nArg, enc);
          363  +      if( score>bestScore ){
          364  +        pBest = p;
          365  +        bestScore = score;
   323    366         }
          367  +      p = p->pNext;
   324    368       }
   325    369     }
   326    370   
   327    371     /* If the createFlag parameter is true, and the seach did not reveal an
   328    372     ** exact match for the name, number of arguments and encoding, then add a
   329    373     ** new entry to the hash table and return it.
   330    374     */
   331         -  if( createFlag && bestmatch<6 && 
          375  +  if( createFlag && bestScore<6 && 
   332    376         (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
   333    377       pBest->zName = (char *)&pBest[1];
   334    378       pBest->nArg = nArg;
   335         -    pBest->pNext = pFirst;
   336    379       pBest->iPrefEnc = enc;
   337    380       memcpy(pBest->zName, zName, nName);
   338    381       pBest->zName[nName] = 0;
   339         -    if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
   340         -      db->mallocFailed = 1;
   341         -      sqlite3DbFree(db, pBest);
   342         -      return 0;
   343         -    }
          382  +    sqlite3FuncDefInsert(&db->aFunc, pBest);
   344    383     }
   345    384   
   346    385     if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
   347    386       return pBest;
   348    387     }
   349    388     return 0;
   350    389   }

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.197 2008/08/20 14:49:24 danielk1977 Exp $
           19  +** $Id: func.c,v 1.198 2008/08/21 18:49:28 drh Exp $
    20     20   */
    21     21   
    22     22   #ifndef CREATE_BUILTIN_HASHTABLE
    23     23   
    24     24   #include "sqliteInt.h"
    25     25   #include <ctype.h>
    26     26   #include <stdlib.h>
................................................................................
  1418   1418     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1419   1419   #else
  1420   1420     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
  1421   1421     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
  1422   1422   #endif
  1423   1423   };
  1424   1424   
         1425  +/*
         1426  +** Build up the global built-in function table at initialization
         1427  +** time.
         1428  +*/
         1429  +void sqlite3RegisterGlobalFunctions(void){
         1430  +  int i;
         1431  +  for(i=0; i<ArraySize(aBuiltinFunc); i++){
         1432  +    sqlite3FuncDefInsert(&sqlite3FuncBuiltins, &aBuiltinFunc[i]);
         1433  +  }
         1434  +}

Changes to src/global.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains definitions of global variables and contants.
    14     14   **
    15         -** $Id: global.c,v 1.4 2008/07/28 19:34:53 drh Exp $
           15  +** $Id: global.c,v 1.5 2008/08/21 18:49:28 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /* An array to map all upper-case characters into their corresponding
    21     21   ** lower-case character. 
    22     22   **
................................................................................
    71     71      1,                /* bCoreMutex */
    72     72      1,                /* bFullMutex */
    73     73      0x7ffffffe,       /* mxStrlen */
    74     74      100,              /* szLookaside */
    75     75      500,              /* nLookaside */
    76     76      /* Other fields all default to zero */
    77     77   };
           78  +
           79  +
           80  +/*
           81  +** Hash table for global functions - functions common to all
           82  +** database connections.  After initialization, this table is
           83  +** read-only.
           84  +*/
           85  +FuncDefHash sqlite3FuncBuiltins;

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.491 2008/08/20 16:35:10 drh Exp $
           17  +** $Id: main.c,v 1.492 2008/08/21 18:49:28 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   #ifdef SQLITE_ENABLE_FTS3
    23     23   # include "fts3.h"
    24     24   #endif
................................................................................
   143    143     ** sqlite3_initialize().  The recursive calls normally come through
   144    144     ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
   145    145     ** recursive calls might also be possible.
   146    146     */
   147    147     sqlite3_mutex_enter(sqlite3Config.pInitMutex);
   148    148     if( sqlite3Config.isInit==0 && inProgress==0 ){
   149    149       inProgress = 1;
          150  +    memset(&sqlite3FuncBuiltins, 0, sizeof(sqlite3FuncBuiltins));
          151  +    sqlite3RegisterGlobalFunctions();
   150    152       rc = sqlite3_os_init();
   151    153       if( rc==SQLITE_OK ){
   152    154         rc = sqlite3PcacheInitialize();
   153    155         sqlite3PCacheBufferSetup(sqlite3Config.pPage, sqlite3Config.szPage, 
   154    156             sqlite3Config.nPage);
   155    157       }
   156    158       inProgress = 0;
................................................................................
   562    564           pDb->pSchema = 0;
   563    565         }
   564    566       }
   565    567     }
   566    568     sqlite3ResetInternalSchema(db, 0);
   567    569     assert( db->nDb<=2 );
   568    570     assert( db->aDb==db->aDbStatic );
   569         -  for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
   570         -    FuncDef *pFunc, *pNext;
   571         -    for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
   572         -      pNext = pFunc->pNext;
   573         -      sqlite3DbFree(db, pFunc);
          571  +  for(j=0; j<ArraySize(db->aFunc.a); j++){
          572  +    FuncDef *pNext, *pHash, *p;
          573  +    for(p=db->aFunc.a[j]; p; p=pHash){
          574  +      pHash = p->pHash;
          575  +      while( p ){
          576  +        pNext = p->pNext;
          577  +        sqlite3DbFree(db, p);
          578  +        p = pNext;
          579  +      }
   574    580       }
   575    581     }
   576         -
   577    582     for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
   578    583       CollSeq *pColl = (CollSeq *)sqliteHashData(i);
   579    584       /* Invoke any destructors registered for collation sequence user data. */
   580    585       for(j=0; j<3; j++){
   581    586         if( pColl[j].xDel ){
   582    587           pColl[j].xDel(pColl[j].pUser);
   583    588         }
................................................................................
   592    597         pMod->xDestroy(pMod->pAux);
   593    598       }
   594    599       sqlite3DbFree(db, pMod);
   595    600     }
   596    601     sqlite3HashClear(&db->aModule);
   597    602   #endif
   598    603   
   599         -  sqlite3HashClear(&db->aFunc);
   600    604     sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
   601    605     if( db->pErr ){
   602    606       sqlite3ValueFree(db->pErr);
   603    607     }
   604    608     sqlite3CloseExtensions(db);
   605    609   
   606    610     db->magic = SQLITE_MAGIC_ERROR;
................................................................................
  1367   1371   #endif
  1368   1372   #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
  1369   1373   # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
  1370   1374   #endif
  1371   1375   #if SQLITE_MAX_VARIABLE_NUMBER<1
  1372   1376   # error SQLITE_MAX_VARIABLE_NUMBER must be at least 1
  1373   1377   #endif
         1378  +#if SQLITE_MAX_COLUMN>32767
         1379  +# error SQLITE_MAX_COLUMN must not exceed 32767
         1380  +#endif
  1374   1381   
  1375   1382   
  1376   1383   /*
  1377   1384   ** Change the value of a limit.  Report the old value.
  1378   1385   ** If an invalid limit index is supplied, report -1.
  1379   1386   ** Make no changes but still report the old value if the
  1380   1387   ** new limit is negative.
................................................................................
  1462   1469   #if SQLITE_DEFAULT_FILE_FORMAT<4
  1463   1470                    | SQLITE_LegacyFileFmt
  1464   1471   #endif
  1465   1472   #ifdef SQLITE_ENABLE_LOAD_EXTENSION
  1466   1473                    | SQLITE_LoadExtension
  1467   1474   #endif
  1468   1475         ;
  1469         -  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
  1470   1476     sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
  1471   1477   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1472   1478     sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);
  1473   1479   #endif
  1474   1480   
  1475   1481     db->pVfs = sqlite3_vfs_find(zVfs);
  1476   1482     if( !db->pVfs ){

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.757 2008/08/20 16:35:10 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.758 2008/08/21 18:49:28 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Include the configuration header output by 'configure' if we're using the
    21     21   ** autoconf-based build
................................................................................
   362    362   #endif
   363    363   typedef sqlite_int64 i64;          /* 8-byte signed integer */
   364    364   typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
   365    365   typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
   366    366   typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
   367    367   typedef INT16_TYPE i16;            /* 2-byte signed integer */
   368    368   typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
   369         -typedef UINT8_TYPE i8;             /* 1-byte signed integer */
          369  +typedef INT8_TYPE i8;              /* 1-byte signed integer */
   370    370   
   371    371   /*
   372    372   ** Macros to determine whether the machine is big or little endian,
   373    373   ** evaluated at runtime.
   374    374   */
   375    375   #ifdef SQLITE_AMALGAMATION
   376    376   const int sqlite3one;
................................................................................
   451    451   typedef struct Column Column;
   452    452   typedef struct Db Db;
   453    453   typedef struct Schema Schema;
   454    454   typedef struct Expr Expr;
   455    455   typedef struct ExprList ExprList;
   456    456   typedef struct FKey FKey;
   457    457   typedef struct FuncDef FuncDef;
          458  +typedef struct FuncDefHash FuncDefHash;
   458    459   typedef struct IdList IdList;
   459    460   typedef struct Index Index;
   460    461   typedef struct KeyClass KeyClass;
   461    462   typedef struct KeyInfo KeyInfo;
   462    463   typedef struct Lookaside Lookaside;
   463    464   typedef struct LookasideSlot LookasideSlot;
   464    465   typedef struct Module Module;
................................................................................
   588    589     void *pStart;           /* First byte of available memory space */
   589    590     void *pEnd;             /* First byte past end of available space */
   590    591   };
   591    592   struct LookasideSlot {
   592    593     LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
   593    594   };
   594    595   
          596  +/*
          597  +** A hash table for function definitions.
          598  +**
          599  +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
          600  +** Collisions are on the FuncDef.pHash chain.
          601  +*/
          602  +struct FuncDefHash {
          603  +  FuncDef *a[23];       /* Hash table for functions */
          604  +};
          605  +
   595    606   /*
   596    607   ** Each database is an instance of the following structure.
   597    608   **
   598    609   ** The sqlite.lastRowid records the last insert rowid generated by an
   599    610   ** insert statement.  Inserts on views do not affect its value.  Each
   600    611   ** trigger has its own context, so that lastRowid can be updated inside
   601    612   ** triggers as usual.  The previous value will be restored once the trigger
................................................................................
   684    695   #endif
   685    696   #ifndef SQLITE_OMIT_VIRTUALTABLE
   686    697     Hash aModule;                 /* populated by sqlite3_create_module() */
   687    698     Table *pVTab;                 /* vtab with active Connect/Create method */
   688    699     sqlite3_vtab **aVTrans;       /* Virtual tables with open transactions */
   689    700     int nVTrans;                  /* Allocated size of aVTrans */
   690    701   #endif
   691         -  Hash aFunc;                   /* All functions that can be in SQL exprs */
          702  +  FuncDefHash aFunc;            /* Hash table of connection functions */
   692    703     Hash aCollSeq;                /* All collating sequences */
   693    704     BusyHandler busyHandler;      /* Busy callback */
   694    705     int busyTimeout;              /* Busy handler timeout, in msec */
   695    706     Db aDbStatic[2];              /* Static space for the 2 default backends */
   696    707   #ifdef SQLITE_SSE
   697    708     sqlite3_stmt *pFetch;         /* Used by SSE to fetch stored statements */
   698    709   #endif
................................................................................
   749    760   /*
   750    761   ** Each SQL function is defined by an instance of the following
   751    762   ** structure.  A pointer to this structure is stored in the sqlite.aFunc
   752    763   ** hash table.  When multiple functions have the same name, the hash table
   753    764   ** points to a linked list of these structures.
   754    765   */
   755    766   struct FuncDef {
   756         -  i16 nArg;            /* Number of arguments.  -1 means unlimited */
          767  +  i8 nArg;             /* Number of arguments.  -1 means unlimited */
   757    768     u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
   758    769     u8 needCollSeq;      /* True if sqlite3GetFuncCollSeq() might be called */
   759    770     u8 flags;            /* Some combination of SQLITE_FUNC_* */
   760    771     void *pUserData;     /* User data parameter */
   761    772     FuncDef *pNext;      /* Next function with same name */
   762    773     void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
   763    774     void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
   764    775     void (*xFinalize)(sqlite3_context*);                /* Aggregate finializer */
   765    776     char *zName;         /* SQL name of the function. */
          777  +  FuncDef *pHash;      /* Next with a different name but the same hash */
   766    778   };
   767    779   
   768    780   /*
   769    781   ** Possible values for FuncDef.flags
   770    782   */
   771         -#define SQLITE_FUNC_LIKE   0x01  /* Candidate for the LIKE optimization */
   772         -#define SQLITE_FUNC_CASE   0x02  /* Case-sensitive LIKE-type function */
   773         -#define SQLITE_FUNC_EPHEM  0x04  /* Ephermeral.  Delete with VDBE */
          783  +#define SQLITE_FUNC_LIKE     0x01  /* Candidate for the LIKE optimization */
          784  +#define SQLITE_FUNC_CASE     0x02  /* Case-sensitive LIKE-type function */
          785  +#define SQLITE_FUNC_EPHEM    0x04  /* Ephermeral.  Delete with VDBE */
   774    786   
   775    787   /*
   776    788   ** Each SQLite module (virtual table definition) is defined by an
   777    789   ** instance of the following structure, stored in the sqlite3.aModule
   778    790   ** hash table.
   779    791   */
   780    792   struct Module {
................................................................................
  2123   2135   void sqlite3BeginWriteOperation(Parse*, int, int);
  2124   2136   Expr *sqlite3ExprDup(sqlite3*,Expr*);
  2125   2137   void sqlite3TokenCopy(sqlite3*,Token*, Token*);
  2126   2138   ExprList *sqlite3ExprListDup(sqlite3*,ExprList*);
  2127   2139   SrcList *sqlite3SrcListDup(sqlite3*,SrcList*);
  2128   2140   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  2129   2141   Select *sqlite3SelectDup(sqlite3*,Select*);
         2142  +void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  2130   2143   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
  2131   2144   void sqlite3RegisterBuiltinFunctions(sqlite3*);
  2132   2145   void sqlite3RegisterDateTimeFunctions(sqlite3*);
         2146  +void sqlite3RegisterGlobalFunctions(void);
  2133   2147   int sqlite3GetBuiltinFunction(const char *, int, FuncDef **);
  2134   2148   #ifdef SQLITE_DEBUG
  2135   2149     int sqlite3SafetyOn(sqlite3*);
  2136   2150     int sqlite3SafetyOff(sqlite3*);
  2137   2151   #else
  2138   2152   # define sqlite3SafetyOn(A) 0
  2139   2153   # define sqlite3SafetyOff(A) 0
................................................................................
  2264   2278   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  2265   2279   char *sqlite3Utf16to8(sqlite3 *, const void*, int);
  2266   2280   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  2267   2281   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  2268   2282   #ifndef SQLITE_AMALGAMATION
  2269   2283   extern const unsigned char sqlite3UpperToLower[];
  2270   2284   extern struct Sqlite3Config sqlite3Config;
         2285  +extern FuncDefHash sqlite3FuncBuiltins;
  2271   2286   #endif
  2272   2287   void sqlite3RootPageMoved(Db*, int, int);
  2273   2288   void sqlite3Reindex(Parse*, Token*, Token*);
  2274   2289   void sqlite3AlterFunctions(sqlite3*);
  2275   2290   void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  2276   2291   int sqlite3GetToken(const unsigned char *, int *);
  2277   2292   void sqlite3NestedParse(Parse*, const char*, ...);

Deleted tool/mkfunction.c.

     1         -
     2         -/*
     3         -** This file contains a standalone program used to generate C code that
     4         -** implements a static hash table to store the definitions of built-in
     5         -** SQL functions in SQLite. 
     6         -*/
     7         -
     8         -#include <stdio.h>
     9         -#include <string.h>
    10         -#include <assert.h>
    11         -
    12         -/*
    13         -** The SQLite source file "func.c" is included below.
    14         -**
    15         -** By defining the 4 macros and typedef below before including "func.c",
    16         -** most of the code is excluded. What is left is an array of constant
    17         -** strings, aBuiltinFunc[], containing the names of SQLite's built-in 
    18         -** SQL functions. i.e.:
    19         -**
    20         -**   const char aBuiltinFunc[] = { "like", "glob", "min", "max" ... };
    21         -**
    22         -** The data from aBuiltinFunc[] is used by this program to create the
    23         -** static hash table.
    24         -*/
    25         -#define CREATE_BUILTIN_HASHTABLE 1
    26         -#define FUNCTION(zName,w,x,y,z)    #zName
    27         -#define AGGREGATE(zName,v,w,x,y,z) #zName
    28         -#define LIKEFUNC(zName,x,y,z)      #zName
    29         -#define FuncDef const char *
    30         -
    31         -#include "func.c"
    32         -
    33         -/* The number of buckets in the static hash table. */
    34         -#define HASHSIZE 127
    35         -
    36         -typedef unsigned char u8;
    37         -
    38         -/* An array to map all upper-case characters into their corresponding
    39         -** lower-case character. 
    40         -*/
    41         -static const u8 sqlite3UpperToLower[] = {
    42         -      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
    43         -     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
    44         -     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
    45         -     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
    46         -    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
    47         -    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
    48         -    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
    49         -    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
    50         -    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
    51         -    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
    52         -    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
    53         -    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
    54         -    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
    55         -    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
    56         -    252,253,254,255
    57         -};
    58         -#define UpperToLower sqlite3UpperToLower
    59         -
    60         -int sqlite3StrICmp(const char *zLeft, const char *zRight){
    61         -  register unsigned char *a, *b;
    62         -  a = (unsigned char *)zLeft;
    63         -  b = (unsigned char *)zRight;
    64         -  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
    65         -  return UpperToLower[*a] - UpperToLower[*b];
    66         -}
    67         -
    68         -static int hashstring(const char *zName){
    69         -  int ii;
    70         -  unsigned int iKey = 0;
    71         -  for(ii=0; zName[ii]; ii++){
    72         -    iKey = (iKey<<3) + (u8)sqlite3UpperToLower[(u8)zName[ii]];
    73         -  }
    74         -  iKey = iKey%HASHSIZE;
    75         -  return iKey;
    76         -}
    77         -
    78         -static void printarray(const char *zName, u8 *aArray, int nArray){
    79         -  int ii;
    80         -  printf("  static u8 %s[%d] = {", zName, nArray);
    81         -  for(ii=0; ii<nArray; ii++){
    82         -    if( (ii%16)==0 ){
    83         -      printf("\n    ");
    84         -    }
    85         -    printf("%2d, ", aArray[ii]);
    86         -  }
    87         -  printf("\n  };\n");
    88         -}
    89         -
    90         -
    91         -int main(int argc, char **argv){
    92         -  int nFunc;              /* Number of entries in the aBuiltinFunc array */
    93         -
    94         -  u8 anFunc[256];
    95         -  u8 aHash[HASHSIZE];
    96         -  u8 aNext[256];
    97         -  int ii;
    98         -  int iHead;
    99         -
   100         -  nFunc = (sizeof(aBuiltinFunc)/sizeof(const char *));
   101         -  assert(nFunc<256);
   102         -
   103         -  memset(aHash, (unsigned char)nFunc, sizeof(aHash));
   104         -  memset(aNext, (unsigned char)nFunc, sizeof(aNext));
   105         -  memset(anFunc, 0, sizeof(anFunc));
   106         -
   107         -  iHead = -1;
   108         -  for(ii=0; ii<nFunc; ii++){
   109         -    int iHash;
   110         -
   111         -    if( iHead>=0 && 0==sqlite3StrICmp(aBuiltinFunc[ii], aBuiltinFunc[iHead]) ){
   112         -      anFunc[iHead]++;
   113         -      continue;
   114         -    }else{
   115         -      /* The routine generated by this program assumes that if there are
   116         -      ** two or more entries in the aBuiltinFunc[] array with the same
   117         -      ** name (i.e. two versions of the "max" function), then they must
   118         -      ** be stored in adjacent slots. The following block detects the
   119         -      ** problem if this is not the case.
   120         -      */
   121         -      int jj;
   122         -      for(jj=0; jj<ii; jj++){
   123         -        if( 0==sqlite3StrICmp(aBuiltinFunc[ii], aBuiltinFunc[jj]) ){
   124         -          fprintf(stderr, "Error in func.c\n");
   125         -          return -1;
   126         -        }
   127         -      }
   128         -
   129         -      iHead = ii;
   130         -      anFunc[iHead] = 1;
   131         -    }
   132         -
   133         -    iHash = hashstring(aBuiltinFunc[ii]);
   134         -    if( aHash[iHash]!=nFunc ){
   135         -      int iNext = aHash[iHash];
   136         -      while( aNext[iNext]!=nFunc ){
   137         -        iNext = aNext[iNext];
   138         -      }
   139         -      aNext[iNext] = ii;
   140         -    }else{
   141         -      aHash[iHash] = ii;
   142         -    }
   143         -  }
   144         -
   145         -  printf(
   146         -  "/******* Automatically Generated code - do not edit **************/\n"
   147         -  "int sqlite3GetBuiltinFunction(\n"
   148         -  "  const char *zName,   \n"
   149         -  "  int nName, \n"
   150         -  "  FuncDef **paFunc\n"
   151         -  "){\n"
   152         -  );
   153         -
   154         -  printarray("aHash", aHash, HASHSIZE);
   155         -  printarray("anFunc", anFunc, nFunc);
   156         -  printarray("aNext", aNext, nFunc);
   157         -  printf("  FuncDef *pNoFunc = &aBuiltinFunc[%d];\n", nFunc);
   158         -
   159         -  printf(
   160         -  "  unsigned int iKey = 0;  /* Hash of case-insensitive string zName. */\n"
   161         -  "  int ii;\n"
   162         -  "  FuncDef *pFunc;\n"
   163         -  "\n"
   164         -  );
   165         -  printf(
   166         -  "  assert( (sizeof(aBuiltinFunc)/sizeof(aBuiltinFunc[0]))==%d );\n", nFunc
   167         -  );
   168         -  printf(
   169         -  "  /* Generate the hash of zName */\n"
   170         -  "  for(ii=0; ii<nName; ii++){\n"
   171         -  "    iKey = (iKey<<3) + (u8)sqlite3UpperToLower[(u8)zName[ii]];\n"
   172         -  "  }\n"
   173         -  "  iKey = iKey%%127;\n"
   174         -  "\n"
   175         -  "  pFunc = &aBuiltinFunc[iKey = aHash[iKey]];\n"
   176         -  "  while( pFunc!=pNoFunc && sqlite3StrNICmp(pFunc->zName, zName, nName) ){\n"
   177         -  "    pFunc = &aBuiltinFunc[iKey = aNext[iKey]];\n"
   178         -  "  }\n"
   179         -  "\n"
   180         -  "  *paFunc = pFunc;\n"
   181         -  "  return anFunc[iKey];\n"
   182         -  "}\n"
   183         -  );
   184         -
   185         -  return 0;
   186         -}

Changes to tool/mksqlite3c.tcl.

   252    252      alter.c
   253    253      analyze.c
   254    254      attach.c
   255    255      auth.c
   256    256      build.c
   257    257      callback.c
   258    258      delete.c
   259         -   func2.c
          259  +   func.c
   260    260      insert.c
   261    261      legacy.c
   262    262      loadext.c
   263    263      pragma.c
   264    264      prepare.c
   265    265      select.c
   266    266      table.c