-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathiceberg_column_definition.cpp
More file actions
212 lines (196 loc) · 6.77 KB
/
iceberg_column_definition.cpp
File metadata and controls
212 lines (196 loc) · 6.77 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "metadata/iceberg_column_definition.hpp"
namespace duckdb {
// https://iceberg.apache.org/spec/#schemas
//! Hexadecimal values are given without the proper escape sequences, so we add them, for simplicity of conversion
static string AddEscapesToBlob(const string &hexadecimal_string) {
string result;
D_ASSERT(hexadecimal_string.size() % 2 == 0);
for (idx_t i = 0; i < hexadecimal_string.size() / 2; i++) {
result += "\\x";
result += hexadecimal_string.substr(i * 2, 2);
}
return result;
}
static Value ParseDefaultForType(const LogicalType &type, rest_api_objects::PrimitiveTypeValue &default_value) {
if (type.IsNested()) {
throw InvalidConfigurationException("Can't parse default value for nested type (%s)", type.ToString());
}
switch (type.id()) {
case LogicalTypeId::BOOLEAN: {
D_ASSERT(default_value.has_boolean_type_value);
return Value::BOOLEAN(default_value.boolean_type_value.value);
}
case LogicalTypeId::INTEGER: {
D_ASSERT(default_value.has_integer_type_value);
return Value::INTEGER(default_value.integer_type_value.value);
}
case LogicalTypeId::BIGINT: {
D_ASSERT(default_value.has_long_type_value);
return Value::BIGINT(default_value.long_type_value.value);
}
case LogicalTypeId::FLOAT: {
D_ASSERT(default_value.has_float_type_value);
return Value::FLOAT(default_value.float_type_value.value);
}
case LogicalTypeId::DOUBLE: {
D_ASSERT(default_value.has_double_type_value);
return Value::DOUBLE(default_value.double_type_value.value);
}
case LogicalTypeId::DECIMAL:
case LogicalTypeId::DATE:
case LogicalTypeId::TIME:
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_TZ:
case LogicalTypeId::VARCHAR:
case LogicalTypeId::UUID: {
D_ASSERT(default_value.has_string_type_value);
return Value(default_value.string_type_value.value).DefaultCastAs(type);
}
case LogicalTypeId::BLOB: {
D_ASSERT(default_value.has_binary_type_value);
return Value::BLOB(AddEscapesToBlob(default_value.binary_type_value.value));
}
default:
throw NotImplementedException("ParseDefaultForType not implemented for type: %s", type.ToString());
}
}
unique_ptr<IcebergColumnDefinition>
IcebergColumnDefinition::ParseType(const string &name, int32_t field_id, bool required, rest_api_objects::Type &type,
optional_ptr<rest_api_objects::PrimitiveTypeValue> initial_default) {
auto res = make_uniq<IcebergColumnDefinition>();
res->id = field_id;
res->required = required;
res->name = name;
if (type.has_primitive_type) {
res->type = ParsePrimitiveType(type.primitive_type);
} else if (type.has_struct_type) {
auto &struct_type = type.struct_type;
child_list_t<LogicalType> struct_children;
for (auto &field_p : struct_type.fields) {
auto &field = *field_p;
auto child = ParseType(field.name, field.id, field.required, *field.type,
field.has_initial_default ? &field.initial_default : nullptr);
struct_children.push_back(std::make_pair(child->name, child->type));
res->children.push_back(std::move(child));
}
res->type = LogicalType::STRUCT(std::move(struct_children));
} else if (type.has_list_type) {
auto &list_type = type.list_type;
auto child =
ParseType("element", list_type.element_id, list_type.element_required, *list_type.element, nullptr);
res->type = LogicalType::LIST(child->type);
res->children.push_back(std::move(child));
} else if (type.has_map_type) {
auto &map_type = type.map_type;
auto key = ParseType("key", map_type.key_id, true, *map_type.key, nullptr);
auto value = ParseType("value", map_type.value_id, map_type.value_required, *map_type.value, nullptr);
res->type = LogicalType::MAP(key->type, value->type);
res->children.push_back(std::move(key));
res->children.push_back(std::move(value));
} else {
throw InvalidConfigurationException("Encountered an invalid type in JSON schema");
}
if (initial_default) {
res->initial_default = ParseDefaultForType(res->type, *initial_default);
}
return res;
}
LogicalType IcebergColumnDefinition::ParsePrimitiveType(rest_api_objects::PrimitiveType &type) {
auto &type_str = type.value;
return ParsePrimitiveTypeString(type_str);
}
LogicalType IcebergColumnDefinition::ParsePrimitiveTypeString(const string &type_str) {
if (type_str == "boolean") {
return LogicalType::BOOLEAN;
}
if (type_str == "int") {
return LogicalType::INTEGER;
}
if (type_str == "long") {
return LogicalType::BIGINT;
}
if (type_str == "float") {
return LogicalType::FLOAT;
}
if (type_str == "double") {
return LogicalType::DOUBLE;
}
if (type_str == "date") {
return LogicalType::DATE;
}
if (type_str == "time") {
return LogicalType::TIME;
}
if (type_str == "timestamp") {
return LogicalType::TIMESTAMP;
}
if (type_str == "timestamptz") {
return LogicalType::TIMESTAMP_TZ;
}
if (type_str == "string") {
return LogicalType::VARCHAR;
}
if (type_str == "uuid") {
return LogicalType::UUID;
}
if (StringUtil::StartsWith(type_str, "fixed")) {
// FIXME: use fixed size type in DuckDB
return LogicalType::BLOB;
}
if (type_str == "binary") {
return LogicalType::BLOB;
}
if (StringUtil::StartsWith(type_str, "decimal")) {
D_ASSERT(type_str[7] == '(');
D_ASSERT(type_str.back() == ')');
auto start = type_str.find('(');
auto end = type_str.rfind(')');
auto raw_digits = type_str.substr(start + 1, end - start);
auto digits = StringUtil::Split(raw_digits, ',');
D_ASSERT(digits.size() == 2);
auto width = std::stoi(digits[0]);
auto scale = std::stoi(digits[1]);
return LogicalType::DECIMAL(width, scale);
}
throw InvalidConfigurationException("Unrecognized primitive type: %s", type_str);
}
unique_ptr<IcebergColumnDefinition> IcebergColumnDefinition::ParseStructField(rest_api_objects::StructField &field) {
return ParseType(field.name, field.id, field.required, *field.type,
field.has_initial_default ? &field.initial_default : nullptr);
}
bool IcebergColumnDefinition::IsIcebergPrimitiveType() {
switch (type.id()) {
case LogicalTypeId::TINYINT:
case LogicalTypeId::SMALLINT:
case LogicalTypeId::INTEGER:
case LogicalTypeId::BOOLEAN:
case LogicalTypeId::VARCHAR:
case LogicalTypeId::DATE:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::BIGINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::UUID:
case LogicalTypeId::BLOB:
case LogicalTypeId::TIME:
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_TZ:
return true;
default:
return false;
}
}
unique_ptr<IcebergColumnDefinition> IcebergColumnDefinition::Copy() const {
auto res = make_uniq<IcebergColumnDefinition>();
res->id = id;
res->name = name;
res->type = type;
res->initial_default = initial_default;
res->required = required;
for (auto &child : children) {
res->children.push_back(child->Copy());
}
return res;
}
} // namespace duckdb