# 2012 December 19
#
# 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 name resolution in SELECT
# statements that have parenthesized FROM clauses.
#

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

do_test selectD-1.1 {
  db eval {
    CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(111,'x1');
    CREATE TABLE t2(a,b); INSERT INTO t2 VALUES(222,'x2');
    CREATE TABLE t3(a,b); INSERT INTO t3 VALUES(333,'x3');
    CREATE TABLE t4(a,b); INSERT INTO t4 VALUES(444,'x4');

    SELECT *
      FROM (t1), (t2), (t3), (t4)
     WHERE t4.a=t3.a+111 
       AND t3.a=t2.a+111
       AND t2.a=t1.a+111;
  }
} {111 x1 222 x2 333 x3 444 x4}
do_test selectD-1.2 {
  db eval {
    SELECT *
      FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
                            ON t3.a=t2.a+111)
                   ON t2.a=t1.a+111;
  }
} {111 x1 222 x2 333 x3 444 x4}
do_test selectD-1.3 {
  db eval {
    UPDATE t2 SET a=111;
    UPDATE t3 SET a=111;
    UPDATE t4 SET a=111;
    SELECT *
      FROM t1 JOIN (t2 JOIN (t3 JOIN t4 USING(a)) USING (a)) USING (a);
  }
} {111 x1 x2 x3 x4}
do_test selectD-1.4 {
  db eval {
    UPDATE t2 SET a=111;
    UPDATE t3 SET a=111;
    UPDATE t4 SET a=111;
    SELECT *
      FROM t1 LEFT JOIN (t2 LEFT JOIN (t3 LEFT JOIN t4 USING(a))
                                      USING (a))
                         USING (a);
  }
} {111 x1 x2 x3 x4}
do_test selectD-1.5 {
  db eval {
    UPDATE t3 SET a=222;
    UPDATE t4 SET a=222;
    SELECT *
      FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
           ON t1.a=t3.a-111;
  }
} {111 x1 x2 222 x3 x4}
do_test selectD-1.6 {
  db eval {
    UPDATE t4 SET a=333;
    SELECT *
      FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
           ON t1.a=t3.a-111;
  }
} {111 x1 x2 222 x3 {}}

finish_test
