1#include "catch.hpp"
2#include "duckdb/common/helper.hpp"
3#include "duckdb/planner/expression/bound_operator_expression.hpp"
4#include "duckdb/optimizer/rule/like_optimizations.hpp"
5#include "expression_helper.hpp"
6
7using namespace duckdb;
8using namespace std;
9
10TEST_CASE("Test Like Optimization Rules", "[like-optimizer]") {
11 ExpressionHelper helper;
12
13 REQUIRE(helper.AddColumns("S VARCHAR").empty());
14
15 helper.AddRule<LikeOptimizationRule>();
16
17 string input, expected_output;
18
19 input = "S ~~ 'aaa%'";
20 expected_output = "prefix(S, 'aaa')";
21 REQUIRE(helper.VerifyRewrite(input, expected_output));
22
23 input = "S ~~ '%aaa'";
24 expected_output = "suffix(S, 'aaa')";
25 REQUIRE(helper.VerifyRewrite(input, expected_output));
26
27 input = "S ~~ '%aaa%'";
28 expected_output = "contains(S, 'aaa')";
29 REQUIRE(helper.VerifyRewrite(input, expected_output));
30
31 // REQUIRE_FAIL ----------------
32 input = "S ~~ 'a_a%'";
33 expected_output = "prefix(S, 'aaa')";
34 REQUIRE(helper.VerifyRewrite(input, expected_output, true) == false);
35
36 input = "S ~~ '%a_a'";
37 expected_output = "suffix(S, 'aaa')";
38 REQUIRE(helper.VerifyRewrite(input, expected_output, true) == false);
39
40 input = "S ~~ '%a_a%'";
41 expected_output = "contains(S, 'a_a')";
42 REQUIRE(helper.VerifyRewrite(input, expected_output, true) == false);
43}
44