SQLite

Check-in [b4b5741366]
Login

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

Overview
Comment:Fix a problem with renaming a column that is used as part of an ORDER BY on a compound SELECT within a database view or trigger.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b4b5741366578b25ec6e4c415ab8239215e53b1c900be613575f40a826cfccc9
User & Date: dan 2019-01-16 14:58:37.269
References
2019-01-16
20:48
Fix a problem with fix [b4b57413]. (check-in: ca7b7aaed0 user: dan tags: trunk)
Context
2019-01-16
19:26
Fix a problem in the code generator for sorting results with SRT_EphemTab and a LIMIT clause. (check-in: 49fcde2f1f user: drh tags: trunk)
14:58
Fix a problem with renaming a column that is used as part of an ORDER BY on a compound SELECT within a database view or trigger. (check-in: b4b5741366 user: dan tags: trunk)
12:05
Avoid a dangling pointer comparison when renaming a table that has a trigger that itself contains a window function with an (illegal) column reference in a FOLLOWING expression. (check-in: d45bee36f2 user: dan tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/resolve.c.
1134
1135
1136
1137
1138
1139
1140
















1141

1142
1143
1144
1145






1146

1147
1148
1149
1150
1151
1152
1153
        if( iCol<=0 || iCol>pEList->nExpr ){
          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
          return 1;
        }
      }else{
        iCol = resolveAsName(pParse, pEList, pE);
        if( iCol==0 ){
















          pDup = sqlite3ExprDup(db, pE, 0);

          if( !db->mallocFailed ){
            assert(pDup);
            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
          }






          sqlite3ExprDelete(db, pDup);

        }
      }
      if( iCol>0 ){
        /* Convert the ORDER BY term into an integer column number iCol,
        ** taking care to preserve the COLLATE clause if it exists */
        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
        if( pNew==0 ) return 1;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>




>
>
>
>
>
>
|
>







1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
        if( iCol<=0 || iCol>pEList->nExpr ){
          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
          return 1;
        }
      }else{
        iCol = resolveAsName(pParse, pEList, pE);
        if( iCol==0 ){
          /* Now test if expression pE matches one of the values returned
          ** by pSelect. In the usual case this is done by duplicating the 
          ** expression, resolving any symbols in it, and then comparing
          ** it against each expression returned by the SELECT statement.
          ** Once the comparisons are finished, the duplicate expression
          ** is deleted.
          **
          ** Or, if this is running as part of an ALTER TABLE operation,
          ** resolve the symbols in the actual expression, not a duplicate.
          ** And, if one of the comparisons is successful, leave the expression
          ** as is instead of transforming it to an integer as in the usual
          ** case. This allows the code in alter.c to modify column
          ** refererences within the ORDER BY expression as required.  */
          if( IN_RENAME_OBJECT ){
            pDup = pE;
          }else{
            pDup = sqlite3ExprDup(db, pE, 0);
          }
          if( !db->mallocFailed ){
            assert(pDup);
            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
          }
          if( IN_RENAME_OBJECT ){
            if( iCol>0 ){
              pItem->done = 1;
              break;
            }
          }else{
            sqlite3ExprDelete(db, pDup);
          }
        }
      }
      if( iCol>0 ){
        /* Convert the ORDER BY term into an integer column number iCol,
        ** taking care to preserve the COLLATE clause if it exists */
        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
        if( pNew==0 ) return 1;
Changes to test/altertab2.test.
228
229
230
231
232
233
234
235






























236


      );
  END}
}

do_catchsql_test 5.3 {
  INSERT INTO t2x VALUES(1);
} {1 {no such column: b}}































finish_test










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
      );
  END}
}

do_catchsql_test 5.3 {
  INSERT INTO t2x VALUES(1);
} {1 {no such column: b}}

#-------------------------------------------------------------------------

do_execsql_test 6.0 {
  CREATE TABLE t3(a,b,c,d);
  CREATE TRIGGER r3 AFTER INSERT ON t3 WHEN new.a NOT NULL BEGIN
    SELECT a,b,c FROM t3 EXCEPT SELECT a,b,c FROM t3 ORDER BY a;
    SELECT rowid, * FROM t3;
  END;
} {}

do_execsql_test 6.1 {
  ALTER TABLE t3 RENAME TO t3x;
  SELECT sql FROM sqlite_master WHERE name = 'r3';
} {
  {CREATE TRIGGER r3 AFTER INSERT ON "t3x" WHEN new.a NOT NULL BEGIN
    SELECT a,b,c FROM "t3x" EXCEPT SELECT a,b,c FROM "t3x" ORDER BY a;
    SELECT rowid, * FROM "t3x";
  END}
}

do_execsql_test 6.2 {
  ALTER TABLE t3x RENAME a TO abcd;
  SELECT sql FROM sqlite_master WHERE name = 'r3';
} {
  {CREATE TRIGGER r3 AFTER INSERT ON "t3x" WHEN new.abcd NOT NULL BEGIN
    SELECT abcd,b,c FROM "t3x" EXCEPT SELECT abcd,b,c FROM "t3x" ORDER BY abcd;
    SELECT rowid, * FROM "t3x";
  END}
}

finish_test