Refactor names of column index transformation functions, for clarity.
Get generated columns working with ALTER TABLE RENAME COLUMN.
FossilOrigin-Name: 27ab41c9102e7801ff829488fc123a8040da008bef373d6704efbe2f93e1da90
diff --git a/src/alter.c b/src/alter.c
index f8cada0..b7389bf 100644
--- a/src/alter.c
+++ b/src/alter.c
@@ -1334,6 +1334,11 @@
sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
}
}
+#ifndef SQLITE_OMIT_GENERATED_COLUMNS
+ for(i=0; i<sParse.pNewTable->nCol; i++){
+ sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
+ }
+#endif
for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
for(i=0; i<pFKey->nCol; i++){
diff --git a/src/analyze.c b/src/analyze.c
index 8f73853..6df6e7a 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -1182,7 +1182,7 @@
int j, k, regKey;
regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
for(j=0; j<pPk->nKeyCol; j++){
- k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
+ k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
assert( k>=0 && k<pIdx->nColumn );
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
diff --git a/src/build.c b/src/build.c
index f00dc32..48f3a1c 100644
--- a/src/build.c
+++ b/src/build.c
@@ -877,10 +877,12 @@
}
/*
-** Return the true column number of index pIdx that corresponds to table
-** true column iCol. Return -1 if not found.
+** Convert an table column number into a index column number. That is,
+** for the column iCol in the table (as defined by the CREATE TABLE statement)
+** find the (first) offset of that column in index pIdx. Or return -1
+** if column iCol is not used in index pIdx.
*/
-i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
+i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
int i;
for(i=0; i<pIdx->nColumn; i++){
if( iCol==pIdx->aiColumn[i] ) return i;
@@ -889,20 +891,20 @@
}
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
-/* Convert a storage column number into a true column number.
+/* Convert a storage column number into a table column number.
**
** The storage column number (0,1,2,....) is the index of the value
** as it appears in the record on disk. The true column number
** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
**
-** The storage column number is less than the true column number if
-** and only there are virtual columns to the left.
+** The storage column number is less than the table column number if
+** and only there are VIRTUAL columns to the left.
**
** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
**
-** This function is the inverse of sqlite3ColumnOfTable().
+** This function is the inverse of sqlite3TableColumnToStorage().
*/
-i16 sqlite3ColumnOfStorage(Table *pTab, i16 iCol){
+i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
if( pTab->tabFlags & TF_HasVirtual ){
int i;
for(i=0; i<=iCol; i++){
@@ -914,7 +916,7 @@
#endif
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
-/* Convert a true column number into a storage column number.
+/* Convert a table column number into a storage column number.
**
** The storage column number (0,1,2,....) is the index of the value
** as it appears in the record on disk. The true column number
@@ -922,9 +924,9 @@
**
** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
**
-** This function is the inverse of sqlite3ColumnOfStorage().
+** This function is the inverse of sqlite3StorageColumnToTable().
*/
-i16 sqlite3ColumnOfTable(Table *pTab, i16 iCol){
+i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
int i;
i16 n;
assert( iCol<pTab->nCol );
@@ -1577,11 +1579,12 @@
u8 eType = COLFLAG_VIRTUAL;
Table *pTab = pParse->pNewTable;
Column *pCol;
- if( IN_RENAME_OBJECT ){
- sqlite3RenameExprUnmap(pParse, pExpr);
- }
if( pTab==0 ) goto generated_done;
pCol = &(pTab->aCol[pTab->nCol-1]);
+ if( IN_DECLARE_VTAB ){
+ sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
+ goto generated_done;
+ }
if( pCol->pDflt ) goto generated_error;
if( pType ){
if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
@@ -1597,7 +1600,8 @@
assert( TF_HasVirtual==COLFLAG_VIRTUAL );
assert( TF_HasStored==COLFLAG_STORED );
pTab->tabFlags |= eType;
- pCol->pDflt = sqlite3ExprDup(pParse->db, pExpr, 0);
+ pCol->pDflt = pExpr;
+ pExpr = 0;
goto generated_done;
generated_error:
@@ -3651,13 +3655,13 @@
/* If this index contains every column of its table, then mark
** it as a covering index */
assert( HasRowid(pTab)
- || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
+ || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
recomputeColumnsNotIndexed(pIndex);
if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
pIndex->isCovering = 1;
for(j=0; j<pTab->nCol; j++){
if( j==pTab->iPKey ) continue;
- if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
+ if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
pIndex->isCovering = 0;
break;
}
diff --git a/src/expr.c b/src/expr.c
index 41ffe71..c57a000 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -3410,10 +3410,10 @@
return;
#endif
}else if( !HasRowid(pTab) ){
- x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
+ x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
op = OP_Column;
}else{
- x = sqlite3ColumnOfTable(pTab,iCol);
+ x = sqlite3TableColumnToStorage(pTab,iCol);
op = OP_Column;
}
sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
@@ -3591,7 +3591,8 @@
return -1-pParse->iSelfTab;
}
pCol = pTab->aCol + pExpr->iColumn;
- iSrc = sqlite3ColumnOfTable(pTab, pExpr->iColumn) - pParse->iSelfTab;
+ iSrc = sqlite3TableColumnToStorage(pTab, pExpr->iColumn)
+ - pParse->iSelfTab;
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
if( pCol->colFlags & COLFLAG_GENERATED ){
if( pCol->colFlags & COLFLAG_BUSY ){
@@ -5320,7 +5321,7 @@
static int exprIdxCover(Walker *pWalker, Expr *pExpr){
if( pExpr->op==TK_COLUMN
&& pExpr->iTable==pWalker->u.pIdxCover->iCur
- && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
+ && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
){
pWalker->eCode = 1;
return WRC_Abort;
diff --git a/src/insert.c b/src/insert.c
index fe275d5..88b8e6a 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -1055,7 +1055,7 @@
int k;
u32 colFlags;
assert( i>=nHidden );
- assert( iRegStore==sqlite3ColumnOfTable(pTab,i)+regRowid+1 );
+ assert( iRegStore==sqlite3TableColumnToStorage(pTab,i)+regRowid+1 );
if( i==pTab->iPKey ){
/* The value of the INTEGER PRIMARY KEY column is always a NULL.
** Whenever this column is read, the rowid will be substituted
@@ -1464,7 +1464,7 @@
pParse->iSelfTab = 0;
if( onError==OE_Replace ) onError = OE_Abort;
}else{
- iReg = sqlite3ColumnOfTable(pTab, i) + regNewData + 1;
+ iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
}
switch( onError ){
case OE_Replace: {
@@ -1782,7 +1782,7 @@
VdbeComment((v, "%s column %d", pIdx->zName, i));
#endif
}else{
- x = sqlite3ColumnOfTable(pTab, iField) + regNewData + 1;
+ x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
VdbeComment((v, "%s", pTab->aCol[iField].zName));
}
@@ -1873,7 +1873,7 @@
if( pIdx!=pPk ){
for(i=0; i<pPk->nKeyCol; i++){
assert( pPk->aiColumn[i]>=0 );
- x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
+ x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
VdbeComment((v, "%s.%s", pTab->zName,
pTab->aCol[pPk->aiColumn[i]].zName));
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index a1e6e17..10b2eea 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -1818,8 +1818,21 @@
};
/*
-** information about each column of an SQL table is held in an instance
-** of this structure.
+** Information about each column of an SQL table is held in an instance
+** of the Column structure, in the Table.aCol[] array.
+**
+** Definitions:
+**
+** "table column index" This is the index of the column in the
+** Table.aCol[] array, and also the index of
+** the column in the original CREATE TABLE stmt.
+**
+** "storage column index" This is the index of the column in the
+** record BLOB generated by the OP_MakeRecord
+** opcode. The storage column index is less than
+** or equal to the table column index. It is
+** equal if and only if there are no VIRTUAL
+** columns to the left.
*/
struct Column {
char *zName; /* Name of this column, \000, then the type */
@@ -3952,13 +3965,13 @@
Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
void sqlite3OpenMasterTable(Parse *, int);
Index *sqlite3PrimaryKeyIndex(Table*);
-i16 sqlite3ColumnOfIndex(Index*, i16);
+i16 sqlite3TableColumnToIndex(Index*, i16);
#ifdef SQLITE_OMIT_GENERATED_COLUMNS
-# define sqlite3ColumnOfTable(T,X) (X) /* No-op pass-through */
-# define sqlite3ColumnOfStorage(T,X) (X) /* No-op pass-through */
+# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
+# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
#else
- i16 sqlite3ColumnOfTable(Table*, i16);
- i16 sqlite3ColumnOfStorage(Table*, i16);
+ i16 sqlite3TableColumnToStorage(Table*, i16);
+ i16 sqlite3StorageColumnToTable(Table*, i16);
#endif
void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
#if SQLITE_ENABLE_HIDDEN_COLUMNS
diff --git a/src/upsert.c b/src/upsert.c
index 7beb9ff..5db4f5f 100644
--- a/src/upsert.c
+++ b/src/upsert.c
@@ -226,7 +226,7 @@
for(i=0; i<nPk; i++){
int k;
assert( pPk->aiColumn[i]>=0 );
- k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
+ k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
VdbeComment((v, "%s.%s", pIdx->zName,
pTab->aCol[pPk->aiColumn[i]].zName));
diff --git a/src/vdbeapi.c b/src/vdbeapi.c
index c7968ee..074d458 100644
--- a/src/vdbeapi.c
+++ b/src/vdbeapi.c
@@ -1831,7 +1831,7 @@
goto preupdate_old_out;
}
if( p->pPk ){
- iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
+ iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
}
if( iIdx>=p->pCsr->nField || iIdx<0 ){
rc = SQLITE_RANGE;
@@ -1921,7 +1921,7 @@
goto preupdate_new_out;
}
if( p->pPk && p->op!=SQLITE_UPDATE ){
- iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
+ iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
}
if( iIdx>=p->pCsr->nField || iIdx<0 ){
rc = SQLITE_RANGE;
diff --git a/src/where.c b/src/where.c
index 252e906..ec2d384 100644
--- a/src/where.c
+++ b/src/where.c
@@ -5379,9 +5379,9 @@
x = pPk->aiColumn[x];
assert( x>=0 );
}else{
- x = sqlite3ColumnOfStorage(pTab,x);
+ x = sqlite3StorageColumnToTable(pTab,x);
}
- x = sqlite3ColumnOfIndex(pIdx, x);
+ x = sqlite3TableColumnToIndex(pIdx, x);
if( x>=0 ){
pOp->p2 = x;
pOp->p1 = pLevel->iIdxCur;
diff --git a/src/wherecode.c b/src/wherecode.c
index af395a3..686a8d6 100644
--- a/src/wherecode.c
+++ b/src/wherecode.c
@@ -823,7 +823,7 @@
assert( pHint->pIdx!=0 );
if( pExpr->op==TK_COLUMN
&& pExpr->iTable==pHint->iTabCur
- && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
+ && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
){
pWalker->eCode = 1;
}
@@ -891,7 +891,7 @@
pExpr->iTable = reg;
}else if( pHint->pIdx!=0 ){
pExpr->iTable = pHint->iIdxCur;
- pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
+ pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
assert( pExpr->iColumn>=0 );
}
}else if( pExpr->op==TK_AGG_FUNCTION ){
@@ -1826,7 +1826,7 @@
Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
for(j=0; j<pPk->nKeyCol; j++){
- k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
+ k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
}
sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,