/ Check-in [3e7d84db]
Login

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

Overview
Comment:Add commentary to the ExprList object to explain how zSpan is overloaded. Add test cases for the new name resolution functionality.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | name-resolution-fix
Files: files | file ages | folders
SHA1: 3e7d84db7861911c9b2c7dcdabe0b213bf483d79
User & Date: drh 2012-12-19 13:41:03
Context
2013-01-02
12:29
Merge all the latest trunk changes into the name-resolution enhancement branch. check-in: a5f4d2b6 user: drh tags: name-resolution-fix
2012-12-19
13:41
Add commentary to the ExprList object to explain how zSpan is overloaded. Add test cases for the new name resolution functionality. check-in: 3e7d84db user: drh tags: name-resolution-fix
02:36
Better resolution of table and column names in joins where some of the terms of the FROM clause are parenthesized. check-in: 7344e791 user: drh tags: name-resolution-fix
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqliteInt.h.

  1767   1767   /*
  1768   1768   ** A list of expressions.  Each expression may optionally have a
  1769   1769   ** name.  An expr/name combination can be used in several ways, such
  1770   1770   ** as the list of "expr AS ID" fields following a "SELECT" or in the
  1771   1771   ** list of "ID = expr" items in an UPDATE.  A list of expressions can
  1772   1772   ** also be used as the argument to a function, in which case the a.zName
  1773   1773   ** field is not used.
         1774  +**
         1775  +** By default the Expr.zSpan field holds a human-readable description of
         1776  +** the expression that is used in the generation of error messages and
         1777  +** column labels.  In this case, Expr.zSpan is typically the text of a
         1778  +** column expression as it exists in a SELECT statement.  However, if
         1779  +** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
         1780  +** of the table to which the column of a FROM-clause subquery refers.
  1774   1781   */
  1775   1782   struct ExprList {
  1776   1783     int nExpr;             /* Number of expressions on the list */
  1777   1784     int iECursor;          /* VDBE Cursor associated with this ExprList */
  1778   1785     struct ExprList_item { /* For each expression in the list */
  1779         -    Expr *pExpr;           /* The list of expressions */
  1780         -    char *zName;           /* Token associated with this expression */
  1781         -    char *zSpan;           /* Original text of the expression */
  1782         -    u8 sortOrder;          /* 1 for DESC or 0 for ASC */
  1783         -    u8 done;               /* A flag to indicate when processing is finished */
  1784         -    u16 iOrderByCol;       /* For ORDER BY, column number in result set */
  1785         -    u16 iAlias;            /* Index into Parse.aAlias[] for zName */
         1786  +    Expr *pExpr;            /* The list of expressions */
         1787  +    char *zName;            /* Token associated with this expression */
         1788  +    char *zSpan;            /* Original text of the expression */
         1789  +    u8 sortOrder;           /* 1 for DESC or 0 for ASC */
         1790  +    unsigned done :1;       /* A flag to indicate when processing is finished */
         1791  +    unsigned bSpanIsTab :1; /* zSpan holds table name, not the span */
         1792  +    u16 iOrderByCol;        /* For ORDER BY, column number in result set */
         1793  +    u16 iAlias;             /* Index into Parse.aAlias[] for zName */
  1786   1794     } *a;                  /* Alloc a power of two greater or equal to nExpr */
  1787   1795   };
  1788   1796   
  1789   1797   /*
  1790   1798   ** An instance of this structure is used by the parser to record both
  1791   1799   ** the parse tree for an expression and the span of input text for an
  1792   1800   ** expression.

Added test/selectD.test.

            1  +# 2012 December 19
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for name resolution in SELECT
           12  +# statements that have parenthesized FROM clauses.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +do_test selectD-1.1 {
           19  +  db eval {
           20  +    CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(111,'x1');
           21  +    CREATE TABLE t2(a,b); INSERT INTO t2 VALUES(222,'x2');
           22  +    CREATE TABLE t3(a,b); INSERT INTO t3 VALUES(333,'x3');
           23  +    CREATE TABLE t4(a,b); INSERT INTO t4 VALUES(444,'x4');
           24  +
           25  +    SELECT *
           26  +      FROM (t1), (t2), (t3), (t4)
           27  +     WHERE t4.a=t3.a+111 
           28  +       AND t3.a=t2.a+111
           29  +       AND t2.a=t1.a+111;
           30  +  }
           31  +} {111 x1 222 x2 333 x3 444 x4}
           32  +do_test selectD-1.2 {
           33  +  db eval {
           34  +    SELECT *
           35  +      FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
           36  +                            ON t3.a=t2.a+111)
           37  +                   ON t2.a=t1.a+111;
           38  +  }
           39  +} {111 x1 222 x2 333 x3 444 x4}
           40  +do_test selectD-1.3 {
           41  +  db eval {
           42  +    UPDATE t2 SET a=111;
           43  +    UPDATE t3 SET a=111;
           44  +    UPDATE t4 SET a=111;
           45  +    SELECT *
           46  +      FROM t1 JOIN (t2 JOIN (t3 JOIN t4 USING(a)) USING (a)) USING (a);
           47  +  }
           48  +} {111 x1 x2 x3 x4}
           49  +do_test selectD-1.4 {
           50  +  db eval {
           51  +    UPDATE t2 SET a=111;
           52  +    UPDATE t3 SET a=111;
           53  +    UPDATE t4 SET a=111;
           54  +    SELECT *
           55  +      FROM t1 LEFT JOIN (t2 LEFT JOIN (t3 LEFT JOIN t4 USING(a))
           56  +                                      USING (a))
           57  +                         USING (a);
           58  +  }
           59  +} {111 x1 x2 x3 x4}
           60  +do_test selectD-1.5 {
           61  +  db eval {
           62  +    UPDATE t3 SET a=222;
           63  +    UPDATE t4 SET a=222;
           64  +    SELECT *
           65  +      FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
           66  +           ON t1.a=t3.a-111;
           67  +  }
           68  +} {111 x1 x2 222 x3 x4}
           69  +do_test selectD-1.6 {
           70  +  db eval {
           71  +    UPDATE t4 SET a=333;
           72  +    SELECT *
           73  +      FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
           74  +           ON t1.a=t3.a-111;
           75  +  }
           76  +} {111 x1 x2 222 x3 {}}
           77  +
           78  +finish_test