1 | #include "catch.hpp" |
2 | #include "test_helpers.hpp" |
3 | |
4 | using namespace duckdb; |
5 | using namespace std; |
6 | |
7 | TEST_CASE("Test NOP arithmetic expressions" , "[arithmetic]" ) { |
8 | unique_ptr<QueryResult> result; |
9 | DuckDB db(nullptr); |
10 | Connection con(db); |
11 | con.EnableQueryVerification(); |
12 | |
13 | con.Query("CREATE TABLE test (a INTEGER, b INTEGER)" ); |
14 | con.Query("INSERT INTO test VALUES (42, 10), (43, 100);" ); |
15 | |
16 | // a + 0 |
17 | result = con.Query("SELECT a + 0 FROM test" ); |
18 | REQUIRE(CHECK_COLUMN(result, 0, {42, 43})); |
19 | // 0 + a |
20 | result = con.Query("SELECT 0 + a FROM test" ); |
21 | REQUIRE(CHECK_COLUMN(result, 0, {42, 43})); |
22 | |
23 | // a - 0 |
24 | result = con.Query("SELECT a - 0 FROM test" ); |
25 | REQUIRE(CHECK_COLUMN(result, 0, {42, 43})); |
26 | // 0 - a |
27 | result = con.Query("SELECT 0 - a FROM test" ); |
28 | REQUIRE(CHECK_COLUMN(result, 0, {-42, -43})); |
29 | |
30 | // a * 1 |
31 | result = con.Query("SELECT a * 1 FROM test" ); |
32 | REQUIRE(CHECK_COLUMN(result, 0, {42, 43})); |
33 | // 1 * a |
34 | result = con.Query("SELECT 1 * a FROM test" ); |
35 | REQUIRE(CHECK_COLUMN(result, 0, {42, 43})); |
36 | // a * 0 => 0 |
37 | result = con.Query("SELECT a * 0 FROM test" ); |
38 | REQUIRE(CHECK_COLUMN(result, 0, {0, 0})); |
39 | result = con.Query("SELECT 0 * a FROM test" ); |
40 | REQUIRE(CHECK_COLUMN(result, 0, {0, 0})); |
41 | |
42 | // a / 1 |
43 | result = con.Query("SELECT a / 1 FROM test" ); |
44 | REQUIRE(CHECK_COLUMN(result, 0, {42, 43})); |
45 | // 1 / a |
46 | result = con.Query("SELECT 1 / a FROM test" ); |
47 | REQUIRE(CHECK_COLUMN(result, 0, {0, 0})); |
48 | |
49 | // a / 0 => NULL |
50 | result = con.Query("SELECT a / 0 FROM test" ); |
51 | REQUIRE(CHECK_COLUMN(result, 0, {Value(), Value()})); |
52 | // 0 / a => 0 |
53 | result = con.Query("SELECT 0 / a FROM test" ); |
54 | REQUIRE(CHECK_COLUMN(result, 0, {0, 0})); |
55 | |
56 | // test expressions involving NULL as well |
57 | REQUIRE_NO_FAIL(con.Query("UPDATE test SET a=NULL" )); |
58 | |
59 | // NULL * 0 = NULL |
60 | result = con.Query("SELECT a * 0 FROM test" ); |
61 | REQUIRE(CHECK_COLUMN(result, 0, {Value(), Value()})); |
62 | |
63 | // 0 / NULL = NULL |
64 | result = con.Query("SELECT 0 / a FROM test" ); |
65 | REQUIRE(CHECK_COLUMN(result, 0, {Value(), Value()})); |
66 | } |
67 | |