-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathASTToJSONUtils.cpp
83 lines (70 loc) · 2.25 KB
/
ASTToJSONUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "ASTToJSONUtils.h"
#include <Common/ProtonCommon.h>
#include <Common/SettingsChanges.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/DataTypeNullable.h>
#include <Parsers/queryToString.h>
namespace DB
{
namespace Streaming
{
void columnDeclarationToJSON(Poco::JSON::Object & column_mapping_json, const ASTColumnDeclaration & col_decl)
{
const auto & column_type = DataTypeFactory::instance().get(col_decl.type);
column_mapping_json.set("name", col_decl.name);
String type = column_type->getName();
if (column_type->isNullable())
{
type = removeNullable(column_type)->getName();
column_mapping_json.set("nullable", true);
}
else
{
column_mapping_json.set("nullable", false);
}
column_mapping_json.set("type", type);
if (col_decl.default_expression)
{
if (col_decl.default_specifier == "DEFAULT")
{
String default_str = queryToString(col_decl.default_expression);
if (type == "string")
{
default_str = default_str.substr(1, default_str.length() - 2);
}
column_mapping_json.set("default", default_str);
}
else if (col_decl.default_specifier == "ALIAS")
{
column_mapping_json.set("alias", queryToString(col_decl.default_expression));
}
}
if (col_decl.comment)
{
const String & comment = queryToString(col_decl.comment);
column_mapping_json.set("comment", comment.substr(1, comment.length() - 2));
}
if (col_decl.codec)
{
column_mapping_json.set("codec", queryToString(col_decl.codec));
}
if (col_decl.ttl)
{
column_mapping_json.set("ttl", queryToString(col_decl.ttl));
}
}
void settingsToJSON(Poco::JSON::Object & settings_json, const SettingsChanges & changes)
{
for (const auto & change : changes)
for (const auto & [k, v] : ProtonConsts::LOG_STORE_SETTING_NAME_TO_KAFKA)
if (change.name == k)
settings_json.set(change.name, toString(change.value));
}
String jsonToString(const Poco::JSON::Object & json)
{
std::stringstream str_stream; /// STYLE_CHECK_ALLOW_STD_STRING_STREAM
json.stringify(str_stream, 0);
return str_stream.str();
}
}
}