aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <>2021-07-30 23:30:30 +0000
committerdrh <>2021-07-30 23:30:30 +0000
commitc2df4d6adb44e5f9587bf962c917c46c603825c9 (patch)
tree463be0579cf67d7eae8f67743f9e5c3c715b7936 /src
parente48f261ebfd36b4935c2d700269790239dac37e5 (diff)
downloadsqlite-c2df4d6adb44e5f9587bf962c917c46c603825c9.tar.gz
sqlite-c2df4d6adb44e5f9587bf962c917c46c603825c9.zip
Recognize certain standard datatypes ("INT", "INTEGER", "REAL", "TEXT", and
"BLOB") and if a column has one of those datatypes, store the type part of the bit-field information in the Column structure to save space. FossilOrigin-Name: d2da62a9df63036b02dadca3798de9e623c2680b3ef0c37d2b18bb88693afd7f
Diffstat (limited to 'src')
-rw-r--r--src/build.c36
-rw-r--r--src/global.c20
-rw-r--r--src/sqliteInt.h19
-rw-r--r--src/util.c3
4 files changed, 72 insertions, 6 deletions
diff --git a/src/build.c b/src/build.c
index 43112a4cd..8b45df3ca 100644
--- a/src/build.c
+++ b/src/build.c
@@ -1382,6 +1382,9 @@ void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
sqlite3 *db = pParse->db;
u8 hName;
Column *aNew;
+ u8 eType = COLTYPE_CUSTOM;
+ u8 szEst = 1;
+ char affinity = SQLITE_AFF_BLOB;
if( (p = pParse->pNewTable)==0 ) return;
if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
@@ -1407,7 +1410,25 @@ void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
}
}
- z = sqlite3DbMallocRaw(db, sName.n + sType.n + 2);
+ /* Check for standard typenames. For standard typenames we will
+ ** set the Column.eType field rather than storing the typename after
+ ** the column name, in order to save space. */
+ if( sType.n>=3 ){
+ sqlite3DequoteToken(&sType);
+ for(i=0; i<SQLITE_N_STDTYPE; i++){
+ if( sType.n==sqlite3StdTypeLen[i]
+ && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0
+ ){
+ sType.n = 0;
+ eType = i+1;
+ affinity = sqlite3StdTypeAffinity[i];
+ if( affinity<=SQLITE_AFF_TEXT ) szEst = 5;
+ break;
+ }
+ }
+ }
+
+ z = sqlite3DbMallocRaw(db, sName.n + 1 + sType.n + (sType.n>0) );
if( z==0 ) return;
if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName);
memcpy(z, sName.z, sName.n);
@@ -1436,11 +1457,14 @@ void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
if( sType.n==0 ){
/* If there is no type specified, columns have the default affinity
** 'BLOB' with a default size of 4 bytes. */
- pCol->affinity = SQLITE_AFF_BLOB;
- pCol->szEst = 1;
+ pCol->affinity = affinity;
+ pCol->eType = eType;
+ pCol->szEst = szEst;
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- if( 4>=sqlite3GlobalConfig.szSorterRef ){
- pCol->colFlags |= COLFLAG_SORTERREF;
+ if( affinity==SQLITE_AFF_BLOB ){
+ if( 4>=sqlite3GlobalConfig.szSorterRef ){
+ pCol->colFlags |= COLFLAG_SORTERREF;
+ }
}
#endif
}else{
@@ -1729,7 +1753,7 @@ void sqlite3AddPrimaryKey(
}
if( nTerm==1
&& pCol
- && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
+ && pCol->eType==COLTYPE_INTEGER
&& sortOrder!=SQLITE_SO_DESC
){
if( IN_RENAME_OBJECT && pList ){
diff --git a/src/global.c b/src/global.c
index 4648c26d9..a1398fef5 100644
--- a/src/global.c
+++ b/src/global.c
@@ -347,3 +347,23 @@ const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
** Name of the default collating sequence
*/
const char sqlite3StrBINARY[] = "BINARY";
+
+/*
+** Standard typenames. These names must match the COLTYPE_* definitions.
+** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
+*/
+const unsigned char sqlite3StdTypeLen[] = { 4, 3, 7, 4, 4 };
+const char sqlite3StdTypeAffinity[] = {
+ SQLITE_AFF_BLOB,
+ SQLITE_AFF_INTEGER,
+ SQLITE_AFF_INTEGER,
+ SQLITE_AFF_REAL,
+ SQLITE_AFF_TEXT
+};
+const char *sqlite3StdType[] = {
+ "BLOB",
+ "INT",
+ "INTEGER",
+ "REAL",
+ "TEXT"
+};
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 7d6fda4c6..8f63d777a 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2037,9 +2037,25 @@ struct Column {
char affinity; /* One of the SQLITE_AFF_... values */
u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
u8 hName; /* Column name hash for faster lookup */
+ u8 eType; /* One of the standard types */
u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
};
+/* Allowed values for Column.eType.
+**
+** Values must match entries in the global constant arrays
+** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
+** than the offset into these arrays for the corresponding name.
+** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
+*/
+#define COLTYPE_CUSTOM 0 /* Type appended to zName */
+#define COLTYPE_BLOB 1
+#define COLTYPE_INT 2
+#define COLTYPE_INTEGER 3
+#define COLTYPE_REAL 4
+#define COLTYPE_TEXT 5
+#define SQLITE_N_STDTYPE 5 /* Number of standard types */
+
/* Allowed values for Column.colFlags.
**
** Constraints:
@@ -4794,6 +4810,9 @@ void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
#ifndef SQLITE_AMALGAMATION
extern const unsigned char sqlite3OpcodeProperty[];
extern const char sqlite3StrBINARY[];
+extern const unsigned char sqlite3StdTypeLen[];
+extern const char sqlite3StdTypeAffinity[];
+extern const char *sqlite3StdType[];
extern const unsigned char sqlite3UpperToLower[];
extern const unsigned char *sqlite3aLTb;
extern const unsigned char *sqlite3aEQb;
diff --git a/src/util.c b/src/util.c
index 8bcf1d261..d513d8f79 100644
--- a/src/util.c
+++ b/src/util.c
@@ -91,6 +91,9 @@ int sqlite3Strlen30(const char *z){
char *sqlite3ColumnType(Column *pCol, char *zDflt){
if( pCol->colFlags & COLFLAG_HASTYPE ){
return pCol->zName + strlen(pCol->zName) + 1;
+ }else if( pCol->eType ){
+ assert( pCol->eType<=SQLITE_N_STDTYPE );
+ return (char*)sqlite3StdType[pCol->eType-1];
}else{
return zDflt;
}