# 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.
#

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 {
  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 {
  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}

# ticket #3229
do_test colname-5.1 {
  lreplace [db eval {
    SELECT x.* FROM sqlite_master X LIMIT 1;
  }] 3 3 x
} {table tabc tabc x {CREATE TABLE tabc(a,b,c)}}

# ticket #3370, #3371, #3372
#
do_test colname-6.1 {
  db close
  sqlite3 db test.db
  db eval {
    CREATE TABLE t6(a, ['a'], ["a"], "[a]", [`a`]);
    INSERT INTO t6 VALUES(1,2,3,4,5);
  }
  execsql2 {SELECT * FROM t6}
} {a 1 'a' 2 {"a"} 3 {[a]} 4 `a` 5}
do_test colname-6.2 {
  execsql2 {SELECT ['a'], [`a`], "[a]", [a], ["a"] FROM t6}
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
do_test colname-6.3 {
  execsql2 {SELECT "'a'", "`a`", "[a]", "a", """a""" FROM t6}
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
do_test colname-6.4 {
  execsql2 {SELECT `'a'`, ```a```, `[a]`, `a`, `"a"` FROM t6}
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
do_test colname-6.11 {
  execsql2 {SELECT a, max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.12 {
  execsql2 {SELECT `a`, max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.13 {
  execsql2 {SELECT "a", max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.14 {
  execsql2 {SELECT [a], max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.15 {
  execsql2 {SELECT t6.a, max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.16 {
  execsql2 {SELECT ['a'], max(['a']) AS m FROM t6}
} {'a' 2 m 2}
do_test colname-6.17 {
  execsql2 {SELECT ["a"], max(["a"]) AS m FROM t6}
} {{"a"} 3 m 3}
do_test colname-6.18 {
  execsql2 {SELECT "[a]", max("[a]") AS m FROM t6}
} {{[a]} 4 m 4}
do_test colname-6.19 {
  execsql2 {SELECT "`a`", max([`a`]) AS m FROM t6}
} {`a` 5 m 5}


# Ticket #3429
# We cannot find anything wrong, but it never hurts to add another
# test case.
#
do_test colname-7.1 {
  db eval {
    CREATE TABLE t7(x INTEGER PRIMARY KEY, y);
    INSERT INTO t7 VALUES(1,2);
  }
  execsql2 {SELECT rowid, * FROM t7}
} {x 1 x 1 y 2}

# Tickets #3893 and #3984.  (Same problem; independently reported)
#
do_test colname-8.1 {
  db eval {
    CREATE TABLE "t3893"("x");
    INSERT INTO t3893 VALUES(123);
    SELECT "y"."x" FROM (SELECT "x" FROM "t3893") AS "y";
  }
} {123}

# 2017-07-29: Interaction between column naming and query flattening.
# For years now, the query flattener has inserted AS clauses on the
# outer query that were the original SQL text of the column.  This caused
# column-name shifts when the query flattener was enhanced, breaking
# legacy applications.  See https://sqlite.org/src/info/41c27bc0ff1d3135
# for details.
#
# To fix this, the column naming logic was moved ahead of the query
# flattener so that column names are assigned before the query flattener
# runs.
#
db close
sqlite3 db :memory:
do_test colname-9.100 {
  db eval {
    CREATE TABLE t1(a,b);
    INSERT INTO t1 VALUES(1,2);
    CREATE VIEW v1(x,y) AS SELECT a,b FROM t1;
  }
  execsql2 {SELECT v1.x, (Y) FROM v1}
  # Prior to the fix, this would return:  "v1.x 1 (Y) 2"
} {x 1 y 2}
do_test colname-9.110 {
  execsql2 {SELECT * FROM v1}
} {x 1 y 2}
do_test colname-9.120 {
  db eval {
    CREATE VIEW v2(x,y) AS SELECT a,b FROM t1 LIMIT 10;
  }
  execsql2 {SELECT * FROM v2 WHERE 1}
} {x 1 y 2}
do_test colname-9.130 {
  execsql2 {SELECT v2.x, [v2].[y] FROM v2 WHERE 1}
} {x 1 y 2}
do_test colname-9.140 {
  execsql2 {SELECT +x, +y FROM v2 WHERE 1}
} {+x 1 +y 2}

do_test colname-9.200 {
  db eval {
    CREATE TABLE t2(c,d);
    INSERT INTO t2 VALUES(3,4);
    CREATE VIEW v3 AS SELECT c AS a, d AS b FROM t2;
  }
  execsql2 {SELECT t1.a, v3.a AS n FROM t1 LEFT JOIN v3}
} {a 1 n 3}
do_test colname-9.211 {
  execsql2 {SELECT t1.a AS n, v3.a FROM t1 JOIN v3}
} {n 1 a 3}
do_test colname-9.210 {
  execsql2 {SELECT t1.a, v3.a AS n FROM t1 JOIN v3}
} {a 1 n 3}

# 2017-12-23:  Ticket https://www.sqlite.org/src/info/3b4450072511e621
# Inconsistent column names in CREATE TABLE AS
#
# Verify that the names of columns in the created table of a CREATE TABLE AS
# are the same as the names of result columns in the SELECT statement.
#
do_execsql_test colname-9.300 {
  DROP TABLE IF EXISTS t1;
  DROP TABLE IF EXISTS t2;
  CREATE TABLE t1(aaa INT);
  INSERT INTO t1(aaa) VALUES(123);
}
do_test colname-9.310 {
  execsql2 {SELECT BBb FROM (SELECT aaa AS Bbb FROM t1)}
} {Bbb 123}
ifcapable vtab {
  do_execsql_test colname-9.320 {
    CREATE TABLE t2 AS SELECT BBb FROM (SELECT aaa AS Bbb FROM t1);
    SELECT name FROM pragma_table_info('t2');
  } {Bbb}
}
do_execsql_test colname-9.330 { -- added 2019-08-10 to invalidate
  DROP TABLE IF EXISTS t1;      -- a couple assert()s that were
  CREATE TABLE t1(a);           -- added by ticket 3b44500725
  INSERT INTO t1 VALUES(17),(2),(99),(-3),(7);
  SELECT (SELECT avg(a) UNION SELECT min(a) OVER()) FROM t1;
} {17}

# Issue detected by OSSFuzz on 2017-12-24 (Christmas Eve)
# caused by check-in https://sqlite.org/src/info/6b2ff26c25
#
# Prior to being fixed, the following CREATE TABLE was dereferencing
# a NULL pointer and segfaulting.
#
do_catchsql_test colname-9.400 {
  CREATE TABLE t4 AS SELECT #0;
} {1 {near "#0": syntax error}}

# Issue detected by OSSFuzz on 2017-12-25 (Christmas Day)
# also caused by check-in https://sqlite.org/src/info/6b2ff26c25
#
# Prior to being fixed, the following CREATE TABLE caused an
# assertion fault.
#
do_catchsql_test colname-9.410 {
  CREATE TABLE t5 AS SELECT RAISE(abort,a);
} {1 {RAISE() may only be used within a trigger-program}}

# Make sure the quotation marks get removed from the column names
# when constructing a new table from an aggregate SELECT.
# Email from Juergen Palm on 2017-07-11.
#
do_execsql_test colname-10.100 {
  DROP TABLE IF EXISTS t1;
  CREATE TABLE t1("with space" TEXT);
  DROP TABLE IF EXISTS t2;
  CREATE TABLE t2 AS SELECT "with space" FROM t1;
  PRAGMA table_info(t2);
} {0 {with space} TEXT 0 {} 0}
do_execsql_test colname-10.110 {
  DROP TABLE IF EXISTS t3;
  CREATE TABLE t3 AS SELECT "with space" FROM t1 GROUP BY 1;
  PRAGMA table_info(t3);
} {0 {with space} TEXT 0 {} 0}


finish_test