1#include "catch.hpp"
2#include "test_helpers.hpp"
3
4using namespace duckdb;
5using namespace std;
6
7TEST_CASE("Test concat function", "[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 strings(s VARCHAR)"));
14 REQUIRE_NO_FAIL(con.Query("INSERT INTO strings VALUES ('hello'), ('world'), (NULL)"));
15
16 // normal concat
17 result = con.Query("SELECT s || ' ' || s FROM strings ORDER BY s");
18 REQUIRE(CHECK_COLUMN(result, 0, {Value(), "hello hello", "world world"}));
19
20 // unicode concat
21 result = con.Query("SELECT s || ' ' || '🦆' FROM strings ORDER BY s");
22 REQUIRE(CHECK_COLUMN(result, 0, {Value(), "hello 🦆", "world 🦆"}));
23
24 // varargs concat
25 result = con.Query("SELECT s || ' ' || '🦆' FROM strings ORDER BY s");
26 REQUIRE(CHECK_COLUMN(result, 0, {Value(), "hello 🦆", "world 🦆"}));
27
28 // concat with constant NULL
29 result = con.Query("SELECT s || ' ' || '🦆' || NULL FROM strings ORDER BY s");
30 REQUIRE(CHECK_COLUMN(result, 0, {Value(), Value(), Value()}));
31
32 // concat requires at least one argument
33 REQUIRE_FAIL(con.Query("SELECT CONCAT()"));
34
35 // concat with one argument works
36 result = con.Query("SELECT CONCAT('hello')");
37 REQUIRE(CHECK_COLUMN(result, 0, {"hello"}));
38
39 // automatic casting also works for vargs
40 result = con.Query("SELECT CONCAT('hello', 33, 22)");
41 REQUIRE(CHECK_COLUMN(result, 0, {"hello3322"}));
42
43 // CONCAT ignores null values
44 result = con.Query("SELECT CONCAT('hello', 33, NULL, 22, NULL)");
45 REQUIRE(CHECK_COLUMN(result, 0, {"hello3322"}));
46 // this also applies to non-constant null values
47 result = con.Query("SELECT CONCAT('hello', ' ', s) FROM strings ORDER BY s");
48 REQUIRE(CHECK_COLUMN(result, 0, {"hello ", "hello hello", "hello world"}));
49}
50
51TEST_CASE("Test length function", "[function]") {
52 unique_ptr<QueryResult> result;
53 DuckDB db(nullptr);
54 Connection con(db);
55 con.EnableQueryVerification();
56
57 REQUIRE_NO_FAIL(con.Query("CREATE TABLE strings(s VARCHAR)"));
58 REQUIRE_NO_FAIL(con.Query("INSERT INTO strings VALUES ('hello'), ('world'), (NULL)"));
59
60 // normal length
61 result = con.Query("SELECT length(s) FROM strings ORDER BY s");
62 REQUIRE(CHECK_COLUMN(result, 0, {Value(), 5, 5}));
63
64 // length after concat
65 result = con.Query("SELECT length(s || ' ' || '🦆') FROM strings ORDER BY s");
66 REQUIRE(CHECK_COLUMN(result, 0, {Value(), 7, 7}));
67}
68