File: test_get_table_names.cpp

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (271 lines) | stat: -rw-r--r-- 9,706 bytes parent folder | download | duplicates (3)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "catch.hpp"
#include "test_helpers.hpp"
#include "duckdb/parser/parser.hpp"
#include "duckdb/planner/logical_operator.hpp"

#include <chrono>
#include <thread>

using namespace duckdb;
using namespace std;

TEST_CASE("Test GetTableNames", "[api]") {
	DuckDB db(nullptr);
	Connection con(db);
	unordered_set<string> table_names;

	// standard
	table_names = con.GetTableNames("SELECT * FROM my_table");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table"));

	// fetch a specific column
	table_names = con.GetTableNames("SELECT col_a FROM my_table");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table"));

	// multiple tables
	table_names = con.GetTableNames("SELECT * FROM my_table1, my_table2, my_table3");
	REQUIRE(table_names.size() == 3);
	REQUIRE(table_names.count("my_table1"));
	REQUIRE(table_names.count("my_table2"));
	REQUIRE(table_names.count("my_table3"));

	// same table is mentioned multiple times
	table_names = con.GetTableNames("SELECT col_a FROM my_table, my_table m2, my_table m3");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table"));

	// cte
	table_names = con.GetTableNames("WITH cte AS (SELECT * FROM my_table) SELECT * FROM cte");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table"));

	// subqueries
	table_names = con.GetTableNames("SELECT * FROM (SELECT * FROM (SELECT * FROM my_table) bla) bla3");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table"));

	// join
	table_names = con.GetTableNames("SELECT col_a FROM my_table JOIN my_table2 ON (my_table.col_b=my_table2.col_d)");
	REQUIRE(table_names.size() == 2);
	REQUIRE(table_names.count("my_table"));
	REQUIRE(table_names.count("my_table2"));

	// scalar subquery
	table_names = con.GetTableNames("SELECT (SELECT COUNT(*) FROM my_table)");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table"));

	// set operations
	table_names =
	    con.GetTableNames("SELECT * FROM my_table UNION ALL SELECT * FROM my_table2 INTERSECT SELECT * FROM my_table3");
	REQUIRE(table_names.size() == 3);
	REQUIRE(table_names.count("my_table"));
	REQUIRE(table_names.count("my_table2"));
	REQUIRE(table_names.count("my_table3"));

	// window functions
	table_names = con.GetTableNames("SELECT row_number() OVER (ORDER BY (SELECT i+j FROM my_table2)) FROM my_table");
	REQUIRE(table_names.size() == 2);
	REQUIRE(table_names.count("my_table"));
	REQUIRE(table_names.count("my_table2"));

	// views are expanded
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE my_table_for_view(i INT)"));
	REQUIRE_NO_FAIL(con.Query("CREATE VIEW v1 AS SELECT * FROM my_table_for_view"));

	table_names = con.GetTableNames("SELECT col_a FROM v1");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("my_table_for_view"));

	// * exclude
	table_names = con.GetTableNames("select * exclude (x) from df");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("df"));

	// * replace
	table_names = con.GetTableNames("select * replace (42 as x) from df");
	REQUIRE(table_names.size() == 1);
	REQUIRE(table_names.count("df"));

	// qualified with schema.table and catalog.schema.table
	string query = "SELECT * FROM schema1.table1, catalog2.schema2.table2";
	table_names = con.GetTableNames(query, true);
	REQUIRE(table_names.size() == 2);
	REQUIRE(table_names.count("schema1.table1"));
	REQUIRE(table_names.count("catalog2.schema2.table2"));

	// qualified and escaped
	query = "SELECT * FROM schema1.table1, catalog3.\"schema.2\".\"table.2\"";
	table_names = con.GetTableNames(query, true);
	REQUIRE(table_names.size() == 2);
	REQUIRE(table_names.count("schema1.table1"));
	REQUIRE(table_names.count("catalog3.\"schema.2\".\"table.2\""));

	// With alias
	query = "SELECT * FROM schema1.table1 alias1, catalog3.\"schema.2\".\"table.2\" alias2";
	table_names = con.GetTableNames(query, true); // qualified
	REQUIRE(table_names.size() == 2);
	REQUIRE(table_names.count("schema1.table1 AS alias1"));
	REQUIRE(table_names.count("catalog3.\"schema.2\".\"table.2\" AS alias2"));
	table_names = con.GetTableNames(query); // default
	REQUIRE(table_names.size() == 2);
	REQUIRE(table_names.count("table1"));
	REQUIRE(table_names.count("table.2"));

	// generate_series
	table_names = con.GetTableNames("with series_generator as (select * from generate_series(TIMESTAMP '2001-04-10', "
	                                "TIMESTAMP '2001-04-11', INTERVAL 1 HOUR)) select * from series_generator");
	REQUIRE(table_names.empty());

	if (!db.ExtensionIsLoaded("tpch")) {
		return;
	}

	// TPCH
	// run all TPC-H queries twice
	// one WITHOUT the tables in the catalog
	// once WITH the tables in the catalog
	for (idx_t i = 0; i < 2; i++) {
		table_names = con.GetTableNames("PRAGMA tpch(1)");
		REQUIRE(table_names.size() == 1);
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(2)");
		REQUIRE(table_names.size() == 5);
		REQUIRE(table_names.count("part"));
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("partsupp"));
		REQUIRE(table_names.count("nation"));
		REQUIRE(table_names.count("region"));

		table_names = con.GetTableNames("PRAGMA tpch(3)");
		REQUIRE(table_names.size() == 3);
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(4)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(5)");
		REQUIRE(table_names.size() == 6);
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("nation"));
		REQUIRE(table_names.count("region"));

		table_names = con.GetTableNames("PRAGMA tpch(6)");
		REQUIRE(table_names.size() == 1);
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(7)");
		REQUIRE(table_names.size() == 5);
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("nation"));

		table_names = con.GetTableNames("PRAGMA tpch(8)");
		REQUIRE(table_names.size() == 7);
		REQUIRE(table_names.count("part"));
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("nation"));
		REQUIRE(table_names.count("region"));

		table_names = con.GetTableNames("PRAGMA tpch(9)");
		REQUIRE(table_names.size() == 6);
		REQUIRE(table_names.count("part"));
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("partsupp"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("nation"));

		table_names = con.GetTableNames("PRAGMA tpch(10)");
		REQUIRE(table_names.size() == 4);
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("nation"));

		table_names = con.GetTableNames("PRAGMA tpch(11)");
		REQUIRE(table_names.size() == 3);
		REQUIRE(table_names.count("partsupp"));
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("nation"));

		table_names = con.GetTableNames("PRAGMA tpch(12)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(13)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("orders"));

		table_names = con.GetTableNames("PRAGMA tpch(14)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("part"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(15)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(16)");
		REQUIRE(table_names.size() == 3);
		REQUIRE(table_names.count("partsupp"));
		REQUIRE(table_names.count("part"));
		REQUIRE(table_names.count("supplier"));

		table_names = con.GetTableNames("PRAGMA tpch(17)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("part"));

		table_names = con.GetTableNames("PRAGMA tpch(18)");
		REQUIRE(table_names.size() == 3);
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(19)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("part"));

		table_names = con.GetTableNames("PRAGMA tpch(20)");
		REQUIRE(table_names.size() == 5);
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("nation"));
		REQUIRE(table_names.count("partsupp"));
		REQUIRE(table_names.count("part"));
		REQUIRE(table_names.count("lineitem"));

		table_names = con.GetTableNames("PRAGMA tpch(21)");
		REQUIRE(table_names.size() == 4);
		REQUIRE(table_names.count("supplier"));
		REQUIRE(table_names.count("lineitem"));
		REQUIRE(table_names.count("orders"));
		REQUIRE(table_names.count("nation"));

		table_names = con.GetTableNames("PRAGMA tpch(22)");
		REQUIRE(table_names.size() == 2);
		REQUIRE(table_names.count("customer"));
		REQUIRE(table_names.count("orders"));

		REQUIRE_NO_FAIL(con.Query("CALL dbgen(sf=0)"));
	}
}