Skip to content

Commit 0656b3f

Browse files
committed
Nullable params
1 parent 23d700c commit 0656b3f

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

clickhouse/base/wire_format.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ inline const char* find_quoted_chars(const char* start, const char* end) {
113113
return nullptr;
114114
}
115115

116-
void WireFormat::WriteQuotedString(OutputStream& output, std::string_view value) {
116+
void WireFormat::WriteQuotedString(OutputStream& output, std::optional<std::string_view> opt) {
117+
if (!opt) //NULL
118+
{
119+
WriteVarint64(output, 5);
120+
WriteAll(output, "'\\\\N'", 5);
121+
return;
122+
}
123+
std::string_view value = *opt;
117124
auto size = value.size();
118125
const char* start = value.data();
119126
const char* end = start + size;

clickhouse/base/wire_format.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string>
44
#include <cstdint>
5+
#include <optional>
56

67
namespace clickhouse {
78

@@ -22,7 +23,7 @@ class WireFormat {
2223
static void WriteFixed(OutputStream& output, const T& value);
2324
static void WriteBytes(OutputStream& output, const void* buf, size_t len);
2425
static void WriteString(OutputStream& output, std::string_view value);
25-
static void WriteQuotedString(OutputStream& output, std::string_view value);
26+
static void WriteQuotedString(OutputStream& output, std::optional<std::string_view> value);
2627
static void WriteUInt64(OutputStream& output, const uint64_t value);
2728
static void WriteVarint64(OutputStream& output, uint64_t value);
2829

clickhouse/query.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ struct QuerySettingsField {
2626
};
2727

2828
using QuerySettings = std::unordered_map<std::string, QuerySettingsField>;
29-
using QueryParams = std::unordered_map<std::string, std::string>;
29+
using QueryParamValue = std::optional<std::string>;
30+
using QueryParams = std::unordered_map<std::string, QueryParamValue>;
3031

3132
struct Profile {
3233
uint64_t rows = 0;
@@ -123,7 +124,7 @@ class Query : public QueryEvents {
123124
return *this;
124125
}
125126

126-
inline Query& SetParam(const std::string& name, const std::string& value) {
127+
inline Query& SetParam(const std::string& name, const QueryParamValue& value) {
127128
query_params_[name] = value;
128129
return *this;
129130
}

tests/simple/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,33 @@ inline void ParamExample(Client& client) {
278278
client.Execute("DROP TEMPORARY TABLE test_client");
279279
}
280280

281+
inline void ParamNullExample(Client& client) {
282+
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS test_client (id UInt64, name Nullable(String))");
283+
284+
Query query("insert into test_client values ({id: UInt64}, {name: Nullable(String)})");
285+
286+
query.SetParam("id", "123").SetParam("name", QueryParamValue());
287+
client.Execute(query);
288+
289+
query.SetParam("id", "456").SetParam("name", "String Value");
290+
client.Execute(query);
291+
292+
client.Select("SELECT id, name FROM test_client", [](const Block& block) {
293+
for (size_t c = 0; c < block.GetRowCount(); ++c) {
294+
std::cerr << block[0]->As<ColumnUInt64>()->At(c) << " ";
295+
296+
auto col_string = block[1]->As<ColumnNullable>();
297+
if (col_string->IsNull(c)) {
298+
std::cerr << "\\N\n";
299+
} else {
300+
std::cerr << col_string->Nested()->As<ColumnString>()->At(c) << "\n";
301+
}
302+
}
303+
});
304+
305+
client.Execute("DROP TEMPORARY TABLE test_client");
306+
}
307+
281308
inline void NullableExample(Client& client) {
282309
/// Create a table.
283310
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS test_client (id Nullable(UInt64), date Nullable(Date))");
@@ -523,6 +550,7 @@ inline void IPExample(Client &client) {
523550

524551
static void RunTests(Client& client) {
525552
ParamExample(client);
553+
ParamNullExample(client);
526554
ArrayExample(client);
527555
CancelableExample(client);
528556
DateExample(client);

0 commit comments

Comments
 (0)