1#include "catch.hpp"
2#include "test_helpers.hpp"
3
4using namespace duckdb;
5using namespace std;
6
7TEST_CASE("Table functions", "[function]") {
8 unique_ptr<QueryResult> result;
9 DuckDB db(nullptr);
10 Connection con(db);
11 con.EnableQueryVerification();
12
13 REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER, j INTEGER);"));
14
15 // SELECT * from table function
16 result = con.Query("SELECT * FROM pragma_table_info('integers');");
17 REQUIRE(CHECK_COLUMN(result, 0, {0, 1}));
18 REQUIRE(CHECK_COLUMN(result, 1, {"i", "j"}));
19 REQUIRE(CHECK_COLUMN(result, 2, {"INTEGER", "INTEGER"}));
20 REQUIRE(CHECK_COLUMN(result, 3, {false, false}));
21 REQUIRE(CHECK_COLUMN(result, 4, {Value(), Value()}));
22 REQUIRE(CHECK_COLUMN(result, 5, {false, false}));
23
24 // project single column
25 result = con.Query("SELECT name FROM pragma_table_info('integers');");
26 REQUIRE(CHECK_COLUMN(result, 0, {"i", "j"}));
27
28 // project column that is not in function return
29 REQUIRE_FAIL(con.Query("SELECT blablabla FROM pragma_table_info('integers');"));
30
31 // join with table function
32 REQUIRE_NO_FAIL(con.Query("CREATE TABLE join_table(name VARCHAR, value INTEGER);"));
33 REQUIRE_NO_FAIL(con.Query("INSERT INTO join_table VALUES ('i', 33), ('j', 44)"));
34
35 result = con.Query("SELECT a.name, cid, value FROM pragma_table_info('integers') AS a "
36 "INNER JOIN join_table ON a.name=join_table.name ORDER BY a.name;");
37 REQUIRE(CHECK_COLUMN(result, 0, {"i", "j"}));
38 REQUIRE(CHECK_COLUMN(result, 1, {0, 1}));
39 REQUIRE(CHECK_COLUMN(result, 2, {33, 44}));
40
41 // table function in subquery
42 result = con.Query("SELECT cid, name FROM (SELECT * FROM "
43 "pragma_table_info('integers')) AS a");
44 REQUIRE(CHECK_COLUMN(result, 0, {0, 1}));
45 REQUIRE(CHECK_COLUMN(result, 1, {"i", "j"}));
46}
47