1#include "benchmark_runner.hpp"
2#include "duckdb_benchmark_macro.hpp"
3#include "duckdb/main/appender.hpp"
4
5#include <random>
6
7using namespace duckdb;
8using namespace std;
9
10#define CAST_COUNT 10000000
11
12DUCKDB_BENCHMARK(CastIntToString, "[cast]")
13void Load(DuckDBBenchmarkState *state) override {
14 state->conn.Query("CREATE TABLE integers(i INTEGER);");
15 Appender appender(state->conn, "integers");
16 // insert the elements into the database
17 for (int i = 0; i < CAST_COUNT; i++) {
18 appender.AppendRow(i);
19 }
20}
21string GetQuery() override {
22 return "SELECT CAST(i AS VARCHAR) FROM integers";
23}
24string VerifyResult(QueryResult *result) override {
25 if (!result->success) {
26 return result->error;
27 }
28 return string();
29}
30string BenchmarkInfo() override {
31 return "Cast integer to string";
32}
33FINISH_BENCHMARK(CastIntToString)
34
35DUCKDB_BENCHMARK(FormatIntToString, "[cast]")
36void Load(DuckDBBenchmarkState *state) override {
37 state->conn.Query("CREATE TABLE integers(i INTEGER);");
38 Appender appender(state->conn, "integers");
39 // insert the elements into the database
40 for (int i = 0; i < CAST_COUNT; i++) {
41 appender.AppendRow(i);
42 }
43}
44string GetQuery() override {
45 return "SELECT format('{}', i) FROM integers";
46}
47string VerifyResult(QueryResult *result) override {
48 if (!result->success) {
49 return result->error;
50 }
51 return string();
52}
53string BenchmarkInfo() override {
54 return "Convert integer to string using format function";
55}
56FINISH_BENCHMARK(FormatIntToString)
57
58DUCKDB_BENCHMARK(CastDoubleToString, "[cast]")
59void Load(DuckDBBenchmarkState *state) override {
60 state->conn.Query("CREATE TABLE doubles(i DOUBLE);");
61 Appender appender(state->conn, "doubles");
62 // insert the elements into the database
63 for (double i = 0; i < CAST_COUNT; i += 0.8) {
64 appender.AppendRow(i);
65 }
66}
67string GetQuery() override {
68 return "SELECT CAST(i AS VARCHAR) FROM doubles";
69}
70string VerifyResult(QueryResult *result) override {
71 if (!result->success) {
72 return result->error;
73 }
74 return string();
75}
76string BenchmarkInfo() override {
77 return "Cast double to string";
78}
79FINISH_BENCHMARK(CastDoubleToString)
80
81DUCKDB_BENCHMARK(CastStringToInt, "[cast]")
82void Load(DuckDBBenchmarkState *state) override {
83 state->conn.Query("CREATE TABLE varchars(i VARCHAR);");
84 Appender appender(state->conn, "varchars");
85 // insert the elements into the database
86 for (int i = 0; i < CAST_COUNT; i++) {
87 appender.AppendRow(Value(to_string(i)));
88 }
89}
90string GetQuery() override {
91 return "SELECT CAST(i AS INTEGER) FROM varchars";
92}
93string VerifyResult(QueryResult *result) override {
94 if (!result->success) {
95 return result->error;
96 }
97 return string();
98}
99string BenchmarkInfo() override {
100 return "Cast int to string";
101}
102FINISH_BENCHMARK(CastStringToInt)
103
104DUCKDB_BENCHMARK(CastStringToDouble, "[cast]")
105void Load(DuckDBBenchmarkState *state) override {
106 state->conn.Query("CREATE TABLE varchars(i VARCHAR);");
107 Appender appender(state->conn, "varchars");
108 // insert the elements into the database
109 for (int i = 0; i < CAST_COUNT; i++) {
110 appender.AppendRow(Value(to_string(i)));
111 }
112}
113string GetQuery() override {
114 return "SELECT CAST(i AS DOUBLE) FROM varchars";
115}
116string VerifyResult(QueryResult *result) override {
117 if (!result->success) {
118 return result->error;
119 }
120 return string();
121}
122string BenchmarkInfo() override {
123 return "Cast double to string";
124}
125FINISH_BENCHMARK(CastStringToDouble)
126
127DUCKDB_BENCHMARK(CastDateToString, "[cast]")
128void Load(DuckDBBenchmarkState *state) override {
129 std::uniform_int_distribution<> year_dist(1990, 2010), day_dist(1, 28), month_dist(1, 12);
130 std::mt19937 gen;
131 gen.seed(42);
132
133 state->conn.Query("CREATE TABLE dates(d DATE);");
134 Appender appender(state->conn, "dates");
135 // insert the elements into the database
136 for (int i = 0; i < CAST_COUNT; i++) {
137 appender.AppendRow(Value::DATE(year_dist(gen), month_dist(gen), day_dist(gen)));
138 }
139}
140string GetQuery() override {
141 return "SELECT CAST(d AS VARCHAR) FROM dates";
142}
143string VerifyResult(QueryResult *result) override {
144 if (!result->success) {
145 return result->error;
146 }
147 return string();
148}
149string BenchmarkInfo() override {
150 return "Cast date to string";
151}
152FINISH_BENCHMARK(CastDateToString)
153
154DUCKDB_BENCHMARK(CastTimeToString, "[cast]")
155void Load(DuckDBBenchmarkState *state) override {
156 std::uniform_int_distribution<> hour_dist(0, 23), min_dist(0, 59);
157 std::mt19937 gen;
158 gen.seed(42);
159
160 state->conn.Query("CREATE TABLE times(d TIME);");
161 Appender appender(state->conn, "times");
162 // insert the elements into the database
163 for (int i = 0; i < CAST_COUNT; i++) {
164 appender.AppendRow(Value::TIME(hour_dist(gen), min_dist(gen), min_dist(gen), 0));
165 }
166}
167string GetQuery() override {
168 return "SELECT CAST(d AS VARCHAR) FROM times";
169}
170string VerifyResult(QueryResult *result) override {
171 if (!result->success) {
172 return result->error;
173 }
174 return string();
175}
176string BenchmarkInfo() override {
177 return "Cast time to string";
178}
179FINISH_BENCHMARK(CastTimeToString)
180
181DUCKDB_BENCHMARK(CastTimestampToString, "[cast]")
182void Load(DuckDBBenchmarkState *state) override {
183 std::uniform_int_distribution<> year_dist(1990, 2010), day_dist(1, 28), month_dist(1, 12);
184 std::uniform_int_distribution<> hour_dist(0, 23), min_dist(0, 59);
185 std::mt19937 gen;
186 gen.seed(42);
187
188 state->conn.Query("CREATE TABLE timestamps(d TIMESTAMP);");
189 Appender appender(state->conn, "timestamps");
190 // insert the elements into the database
191 for (int i = 0; i < CAST_COUNT; i++) {
192 appender.AppendRow(Value::TIMESTAMP(year_dist(gen), month_dist(gen), day_dist(gen), hour_dist(gen),
193 min_dist(gen), min_dist(gen), 0));
194 }
195}
196string GetQuery() override {
197 return "SELECT CAST(d AS VARCHAR) FROM timestamps";
198}
199string VerifyResult(QueryResult *result) override {
200 if (!result->success) {
201 return result->error;
202 }
203 return string();
204}
205string BenchmarkInfo() override {
206 return "Cast timestamp to string";
207}
208FINISH_BENCHMARK(CastTimestampToString)
209