/ Check-in [61f6e197]
Login

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

Overview
Comment:Update column naming rules. Ticket #3221. Rules for column naming are still subject to change (except for the AS rule which we promise to keep the same) but are more consistent now. And the rules are tested using a new test script. (CVS 5416)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:61f6e19755b85bcb065f85fc425c2172badea308
User & Date: drh 2008-07-15 20:56:17
Context
2008-07-15
21:32
Implement optimize() function. This merges all segments in the fts index into a single segment, including dropping delete cookies. (CVS 5417) check-in: b22e187b user: shess tags: trunk
20:56
Update column naming rules. Ticket #3221. Rules for column naming are still subject to change (except for the AS rule which we promise to keep the same) but are more consistent now. And the rules are tested using a new test script. (CVS 5416) check-in: 61f6e197 user: drh tags: trunk
14:47
Continuing work on improved test coverage. (CVS 5415) check-in: c942a38e user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/select.c.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
....
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
....
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
....
1431
1432
1433
1434
1435
1436
1437

1438
1439
1440




1441
1442
1443
1444
1445
1446
1447
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite.
**
** $Id: select.c,v 1.456 2008/07/15 00:27:35 drh Exp $
*/
#include "sqliteInt.h"


/*
** Delete all the content of a Select structure but do not deallocate
** the select structure itself.
................................................................................

/*
** Given a SELECT statement, generate a Table structure that describes
** the result set of that SELECT.
*/
Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
  Table *pTab;
  int i, j;
  ExprList *pEList;
  Column *aCol, *pCol;
  sqlite3 *db = pParse->db;





  if( sqlite3SelectResolve(pParse, pSelect, 0) ){
    return 0;
  }


  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
  if( prepSelectStmt(pParse, pSelect) ){
    return 0;


  }
  if( sqlite3SelectResolve(pParse, pSelect, 0) ){



    return 0;
  }
  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
  if( pTab==0 ){
    return 0;
  }
  pTab->nRef = 1;
................................................................................
    ** If we get here it means the result set contains one or more "*"
    ** operators that need to be expanded.  Loop through each expression
    ** in the result set and expand them one by one.
    */
    struct ExprList_item *a = pEList->a;
    ExprList *pNew = 0;
    int flags = pParse->db->flags;
    int longNames = (flags & SQLITE_FullColNames)!=0 &&
                      (flags & SQLITE_ShortColNames)==0;

    for(k=0; k<pEList->nExpr; k++){
      Expr *pE = a[k].pExpr;
      if( pE->op!=TK_ALL &&
           (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
        /* This particular expression does not need to be expanded.
        */
................................................................................
            if( pRight==0 ) break;
            setQuotedToken(pParse, &pRight->token, zName);
            if( longNames || pTabList->nSrc>1 ){
              Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
              if( pExpr==0 ) break;
              setQuotedToken(pParse, &pLeft->token, zTabName);

              setToken(&pExpr->span, 
                  sqlite3MPrintf(db, "%s.%s", zTabName, zName));
              pExpr->span.dyn = 1;




              pExpr->token.z = 0;
              pExpr->token.n = 0;
              pExpr->token.dyn = 0;
            }else{
              pExpr = pRight;
              pExpr->span = pExpr->token;
              pExpr->span.dyn = 0;







|







 







|



>

>
>
>
|
<
<
<
>
|
|
<
>
>
|
<
>
>
>







 







|
|







 







>



>
>
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
....
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
....
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
....
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite.
**
** $Id: select.c,v 1.457 2008/07/15 20:56:17 drh Exp $
*/
#include "sqliteInt.h"


/*
** Delete all the content of a Select structure but do not deallocate
** the select structure itself.
................................................................................

/*
** Given a SELECT statement, generate a Table structure that describes
** the result set of that SELECT.
*/
Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
  Table *pTab;
  int i, j, rc;
  ExprList *pEList;
  Column *aCol, *pCol;
  sqlite3 *db = pParse->db;
  int savedFlags;

  savedFlags = db->flags;
  db->flags &= ~SQLITE_FullColNames;
  db->flags |= SQLITE_ShortColNames;
  rc = sqlite3SelectResolve(pParse, pSelect, 0);



  if( rc==SQLITE_OK ){
    while( pSelect->pPrior ) pSelect = pSelect->pPrior;
    rc = prepSelectStmt(pParse, pSelect);

    if( rc==SQLITE_OK ){
      rc = sqlite3SelectResolve(pParse, pSelect, 0);
    }

  }
  db->flags = savedFlags;
  if( rc ){
    return 0;
  }
  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
  if( pTab==0 ){
    return 0;
  }
  pTab->nRef = 1;
................................................................................
    ** If we get here it means the result set contains one or more "*"
    ** operators that need to be expanded.  Loop through each expression
    ** in the result set and expand them one by one.
    */
    struct ExprList_item *a = pEList->a;
    ExprList *pNew = 0;
    int flags = pParse->db->flags;
    int longNames = (flags & SQLITE_FullColNames)!=0
                      && (flags & SQLITE_ShortColNames)==0;

    for(k=0; k<pEList->nExpr; k++){
      Expr *pE = a[k].pExpr;
      if( pE->op!=TK_ALL &&
           (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
        /* This particular expression does not need to be expanded.
        */
................................................................................
            if( pRight==0 ) break;
            setQuotedToken(pParse, &pRight->token, zName);
            if( longNames || pTabList->nSrc>1 ){
              Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
              if( pExpr==0 ) break;
              setQuotedToken(pParse, &pLeft->token, zTabName);
#if 1
              setToken(&pExpr->span, 
                  sqlite3MPrintf(db, "%s.%s", zTabName, zName));
              pExpr->span.dyn = 1;
#else
              pExpr->span = pRight->token;
              pExpr->span.dyn = 0;
#endif
              pExpr->token.z = 0;
              pExpr->token.n = 0;
              pExpr->token.dyn = 0;
            }else{
              pExpr = pRight;
              pExpr->span = pExpr->token;
              pExpr->span.dyn = 0;

Added test/colname.test.





























































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
# 2008 July 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. 
#
# The focus of this file is testing how SQLite generates the names
# of columns in a result set.
#
# $Id: colname.test,v 1.1 2008/07/15 20:56:17 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Rules (applied in order):
#
# (1) If there is an AS clause, use it.
#
# (2) A non-trival expression (not a table column name) then the name is
#     a copy of the expression text.
#
# (3) If short_column_names=ON, then just the abbreviated column name without
#     the table name.
#
# (4) When short_column_names=OFF and full_column_names=OFF then
#     use case (2) for simple queries and case (5) for joins.
#
# (5) When short_column_names=OFF and full_column_names=ON then
#     use the form: TABLE.COLUMN
#


# Verify the default settings for short_column_name and full_column_name
#
do_test colname-1.1 {
  db eval {PRAGMA short_column_names}
} {1}
do_test colname-1.2 {
  db eval {PRAGMA full_column_names}
} {0}

# Tests for then short=ON and full=any
#
do_test colname-2.1 {
  db eval {
    CREATE TABLE tabc(a,b,c);
    INSERT INTO tabc VALUES(1,2,3);
    CREATE TABLE txyz(x,y,z);
    INSERT INTO txyz VALUES(4,5,6);
    CREATE TABLE tboth(a,b,c,x,y,z);
    INSERT INTO tboth VALUES(11,12,13,14,15,16);
    CREATE VIEW v1 AS SELECT tabC.a, txyZ.x, * 
      FROM tabc, txyz ORDER BY 1 LIMIT 1;
    CREATE VIEW v2 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, *
      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
  }
  execsql2 {
    SELECT * FROM tabc;
  }
} {a 1 b 2 c 3}
do_test colname-2.2 {
  execsql2 {
    SELECT Tabc.a, tAbc.b, taBc.c, * FROM tabc
  }
} {a 1 b 2 c 3 a 1 b 2 c 3}
do_test colname-2.3 {
  execsql2 {
    SELECT +tabc.a, -tabc.b, tabc.c, * FROM tabc
  }
} {+tabc.a 1 -tabc.b -2 c 3 a 1 b 2 c 3}
do_test colname-2.4 {
  execsql2 {
    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC, * FROM tabc
  }
} {AAA 1 BBB -2 CCC 3 a 1 b 2 c 3}
do_test colname-2.5 {
  execsql2 {
    SELECT tabc.a, txyz.x, * FROM tabc, txyz;
  }
} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-2.6 {
  execsql2 {
    SELECT tabc.a, txyz.x, tabc.*, txyz.* FROM tabc, txyz;
  }
} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-2.7 {
  execsql2 {
    SELECT tabc.a, txyz.x, tboth.a, tboth.x, * FROM tabc, txyz, tboth;
  }
} {a 11 x 14 a 11 x 14 a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
do_test colname-2.8 {
  execsql2 {
    SELECT * FROM v1 ORDER BY 2;
  }
} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
do_test colname-2.9 {
  execsql2 {
    SELECT * FROM v2 ORDER BY 2;
  }
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}


# Tests for short=OFF and full=OFF
#
do_test colname-3.1 {
  db eval {
    PRAGMA short_column_names=OFF;
    PRAGMA full_column_names=OFF;
    CREATE VIEW v3 AS SELECT tabC.a, txyZ.x, *
      FROM tabc, txyz ORDER BY 1 LIMIT 1;
    CREATE VIEW v4 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, * 
      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
  }
  execsql2 {
    SELECT * FROM tabc;
  }
} {a 1 b 2 c 3}
do_test colname-3.2 {
  execsql2 {
    SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
  }
} {Tabc.a 1 tAbc.b 2 taBc.c 3}
do_test colname-3.3 {
  execsql2 {
    SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
  }
} {+tabc.a 1 -tabc.b -2 tabc.c 3}
do_test colname-3.4 {
  execsql2 {
    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
  }
} {AAA 1 BBB -2 CCC 3}
do_test colname-3.5 {
  execsql2 {
    SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
  }
} {Tabc.a 1 Txyz.x 4 a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-3.6 {
  execsql2 {
    SELECT tabc.*, txyz.* FROM tabc, txyz;
  }
} {a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-3.7 {
  execsql2 {
    SELECT * FROM tabc, txyz, tboth;
  }
} {a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
do_test colname-3.8 {
  execsql2 {
    SELECT v1.a, * FROM v1 ORDER BY 2;
  }
} {v1.a 1 a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
do_test colname-3.9 {
  execsql2 {
    SELECT * FROM v2 ORDER BY 2;
  }
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
do_test colname-3.10 {
  execsql2 {
    SELECT * FROM v3 ORDER BY 2;
  }
} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
do_test colname-3.11 {
  execsql2 {
    SELECT * FROM v4 ORDER BY 2;
  }
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}

# Test for short=OFF and full=ON
#
do_test colname-4.1 {
breakpoint
  db eval {
    PRAGMA short_column_names=OFF;
    PRAGMA full_column_names=ON;
    CREATE VIEW v5 AS SELECT tabC.a, txyZ.x, *
      FROM tabc, txyz ORDER BY 1 LIMIT 1;
    CREATE VIEW v6 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, * 
      FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
  }
  execsql2 {
    SELECT * FROM tabc;
  }
} {tabc.a 1 tabc.b 2 tabc.c 3}
do_test colname-4.2 {
  execsql2 {
    SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
  }
} {tabc.a 1 tabc.b 2 tabc.c 3}
do_test colname-4.3 {
  execsql2 {
    SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
  }
} {+tabc.a 1 -tabc.b -2 tabc.c 3}
do_test colname-4.4 {
  execsql2 {
    SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
  }
} {AAA 1 BBB -2 CCC 3}
do_test colname-4.5 {
  execsql2 {
    SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
  }
} {tabc.a 1 txyz.x 4 tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
do_test colname-4.6 {
  execsql2 {
    SELECT tabc.*, txyz.* FROM tabc, txyz;
  }
} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
do_test colname-4.7 {
  execsql2 {
    SELECT * FROM tabc, txyz, tboth;
  }
} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6 tboth.a 11 tboth.b 12 tboth.c 13 tboth.x 14 tboth.y 15 tboth.z 16}
do_test colname-4.8 {
  execsql2 {
    SELECT * FROM v1 ORDER BY 2;
  }
} {v1.a 1 v1.x 4 v1.a:1 1 v1.b 2 v1.c 3 v1.x:1 4 v1.y 5 v1.z 6}
do_test colname-4.9 {
  execsql2 {
    SELECT * FROM v2 ORDER BY 2;
  }
} {v2.a 1 v2.x 4 v2.a:1 11 v2.x:1 14 v2.a:2 1 v2.b 2 v2.c 3 v2.x:2 4 v2.y 5 v2.z 6 v2.a:3 11 v2.b:1 12 v2.c:1 13 v2.x:3 14 v2.y:1 15 v2.z:1 16}
do_test colname-4.10 {
  execsql2 {
    SELECT * FROM v3 ORDER BY 2;
  }
} {v3.a 1 v3.x 4 v3.a:1 1 v3.b 2 v3.c 3 v3.x:1 4 v3.y 5 v3.z 6}
do_test colname-4.11 {
  execsql2 {
    SELECT * FROM v4 ORDER BY 2;
  }
} {v4.a 1 v4.x 4 v4.a:1 11 v4.x:1 14 v4.a:2 1 v4.b 2 v4.c 3 v4.x:2 4 v4.y 5 v4.z 6 v4.a:3 11 v4.b:1 12 v4.c:1 13 v4.x:3 14 v4.y:1 15 v4.z:1 16}
do_test colname-4.12 {
breakpoint
  execsql2 {
    SELECT * FROM v5 ORDER BY 2;
  }
} {v5.a 1 v5.x 4 v5.a:1 1 v5.b 2 v5.c 3 v5.x:1 4 v5.y 5 v5.z 6}
do_test colname-4.13 {
  execsql2 {
    SELECT * FROM v6 ORDER BY 2;
  }
} {v6.a 1 v6.x 4 v6.a:1 11 v6.x:1 14 v6.a:2 1 v6.b 2 v6.c 3 v6.x:2 4 v6.y 5 v6.z 6 v6.a:3 11 v6.b:1 12 v6.c:1 13 v6.x:3 14 v6.y:1 15 v6.z:1 16}


finish_test