forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvertMySQLDataType.cpp
More file actions
300 lines (275 loc) · 11.5 KB
/
Copy pathconvertMySQLDataType.cpp
File metadata and controls
300 lines (275 loc) · 11.5 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "config.h"
#if USE_MYSQL
#if __has_include(<mysql.h>)
#include <mysql.h>
#else
#include <mysql/mysql.h>
#endif
/// NB: <mysql.h> must be included before the unit's header, otherwise the build breaks because of the
/// forward declaration of `enum_field_types` in mysqlxx/Types.h. See a similar case in mysqlxx/Row.cpp.
#endif
#include <DataTypes/convertMySQLDataType.h>
#include <Core/Field.h>
#include <base/types.h>
#include <Core/MultiEnum.h>
#include <Core/SettingsEnums.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/IAST.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDate32.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypesDecimal.h>
#include <DataTypes/DataTypeFixedString.h>
#include <DataTypes/DataTypeNothing.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeCustomGeo.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/IDataType.h>
#include <Common/logger_useful.h>
namespace DB
{
DataTypePtr convertMySQLDataType(MultiEnum<MySQLDataTypesSupport> type_support,
const std::string & mysql_data_type,
bool is_nullable,
bool is_unsigned,
size_t length,
size_t precision,
size_t scale)
{
// Mysql returns mysql_data_type as below:
// 1. basic_type
// 2. basic_type options
// 3. type_with_params(param1, param2, ...)
// 4. type_with_params(param1, param2, ...) options
// The options can be unsigned, zerofill, or some other strings.
auto data_type = std::string_view(mysql_data_type);
const auto type_end_pos = data_type.find_first_of(R"(( )"); // FIXME: fix style-check script instead
const auto type_name = data_type.substr(0, type_end_pos);
DataTypePtr res;
if (type_name == "tinyint")
{
if (is_unsigned)
res = std::make_shared<DataTypeUInt8>();
else
res = std::make_shared<DataTypeInt8>();
}
else if (type_name == "smallint")
{
if (is_unsigned)
res = std::make_shared<DataTypeUInt16>();
else
res = std::make_shared<DataTypeInt16>();
}
else if (type_name == "int" || type_name == "mediumint" || type_name == "integer")
{
if (is_unsigned)
res = std::make_shared<DataTypeUInt32>();
else
res = std::make_shared<DataTypeInt32>();
}
else if (type_name == "bigint")
{
if (is_unsigned)
res = std::make_shared<DataTypeUInt64>();
else
res = std::make_shared<DataTypeInt64>();
}
else if (type_name == "float")
res = std::make_shared<DataTypeFloat32>();
else if (type_name == "double")
res = std::make_shared<DataTypeFloat64>();
else if (type_name == "date")
{
if (type_support.isSet(MySQLDataTypesSupport::DATE2DATE32))
res = std::make_shared<DataTypeDate32>();
else if (type_support.isSet(MySQLDataTypesSupport::DATE2STRING))
res = std::make_shared<DataTypeString>();
else
res = std::make_shared<DataTypeDate>();
}
else if (type_name == "binary")
{
//compatible with binary(0) DataType
if (length == 0) length = 1;
res = std::make_shared<DataTypeFixedString>(length);
}
else if (type_name == "datetime" || type_name == "timestamp")
{
if (!type_support.isSet(MySQLDataTypesSupport::DATETIME64))
{
res = std::make_shared<DataTypeDateTime>();
}
else if (type_name == "timestamp" && scale == 0)
{
res = std::make_shared<DataTypeDateTime>();
}
else if (type_name == "datetime" || type_name == "timestamp")
{
res = std::make_shared<DataTypeDateTime64>(scale);
}
}
else if (type_name == "bit")
{
res = std::make_shared<DataTypeUInt64>();
}
else if (type_support.isSet(MySQLDataTypesSupport::DECIMAL) && (type_name == "numeric" || type_name == "decimal"))
{
if (precision <= DataTypeDecimalBase<Decimal32>::maxPrecision())
res = std::make_shared<DataTypeDecimal<Decimal32>>(precision, scale);
else if (precision <= DataTypeDecimalBase<Decimal64>::maxPrecision())
res = std::make_shared<DataTypeDecimal<Decimal64>>(precision, scale);
else if (precision <= DataTypeDecimalBase<Decimal128>::maxPrecision())
res = std::make_shared<DataTypeDecimal<Decimal128>>(precision, scale);
else if (precision <= DataTypeDecimalBase<Decimal256>::maxPrecision())
res = std::make_shared<DataTypeDecimal<Decimal256>>(precision, scale);
}
else if (type_name == "point")
{
/// `Point` is supported unconditionally for backward compatibility (since version 24.1).
res = DataTypeFactory::instance().get("Point");
}
else if (type_support.isSet(MySQLDataTypesSupport::GEOMETRY) && type_name == "linestring")
{
res = DataTypeFactory::instance().get("LineString");
}
else if (type_support.isSet(MySQLDataTypesSupport::GEOMETRY) && type_name == "polygon")
{
res = DataTypeFactory::instance().get("Polygon");
}
else if (type_support.isSet(MySQLDataTypesSupport::GEOMETRY) && type_name == "multilinestring")
{
res = DataTypeFactory::instance().get("MultiLineString");
}
else if (type_support.isSet(MySQLDataTypesSupport::GEOMETRY) && type_name == "multipolygon")
{
res = DataTypeFactory::instance().get("MultiPolygon");
}
else if (type_support.isSet(MySQLDataTypesSupport::GEOMETRY) && type_name == "multipoint")
{
res = DataTypeFactory::instance().get("MultiPoint");
}
else if (type_support.isSet(MySQLDataTypesSupport::GEOMETRY) && type_name == "geometry")
{
/// The generic `GEOMETRY` column can hold a value of any subtype, so it is mapped to the
/// umbrella `Geometry` type (a `Variant` over the concrete geometric types). Reading a value
/// whose subtype has no ClickHouse counterpart (`GEOMETRYCOLLECTION`) throws at
/// read time; this incompatibility is accepted in exchange for a proper geometric type.
res = DataTypeFactory::instance().get("Geometry");
}
/// String is the fallback for all unknown types. In particular, the spatial type
/// `GEOMETRYCOLLECTION` has no ClickHouse counterpart and falls back to String.
if (!res)
res = std::make_shared<DataTypeString>();
if (is_nullable)
{
/// A mapped type that cannot be inside `Nullable` (the `Array`/`Variant`-based geometric
/// types) falls back to `Nullable(String)` holding MySQL's own bytes (SRID prefix + WKB),
/// matching the result-set path.
if (res->canBeInsideNullable())
res = std::make_shared<DataTypeNullable>(res);
else
res = std::make_shared<DataTypeNullable>(std::make_shared<DataTypeString>());
}
return res;
}
#if USE_MYSQL
DataTypePtr convertMySQLDataType(MultiEnum<MySQLDataTypesSupport> type_support, MYSQL_FIELD & field, bool use_nulls)
{
const bool is_nullable = use_nulls && !(field.flags & NOT_NULL_FLAG);
const bool is_unsigned = (field.flags & UNSIGNED_FLAG);
DataTypePtr res;
switch (field.type)
{
case enum_field_types::MYSQL_TYPE_TINY:
if (is_unsigned)
res = std::make_shared<DataTypeUInt8>();
else
res = std::make_shared<DataTypeInt8>();
break;
case enum_field_types::MYSQL_TYPE_SHORT:
if (is_unsigned)
res = std::make_shared<DataTypeUInt16>();
else
res = std::make_shared<DataTypeInt16>();
break;
case enum_field_types::MYSQL_TYPE_INT24: /// Treat int24 as int32.
case enum_field_types::MYSQL_TYPE_LONG:
if (is_unsigned)
res = std::make_shared<DataTypeUInt32>();
else
res = std::make_shared<DataTypeInt32>();
break;
case enum_field_types::MYSQL_TYPE_LONGLONG:
if (is_unsigned)
res = std::make_shared<DataTypeUInt64>();
else
res = std::make_shared<DataTypeInt64>();
break;
case enum_field_types::MYSQL_TYPE_DECIMAL:
case enum_field_types::MYSQL_TYPE_NEWDECIMAL:
if (type_support.isSet(MySQLDataTypesSupport::DECIMAL))
{
/// MySQL includes the room for the sign and the decimal point in the declared length.
UInt32 precision = static_cast<UInt32>(field.length);
if (field.decimals > 0 && precision > 0)
precision -= 1; /// Decimal point.
if (!is_unsigned && precision > 0)
precision -= 1; /// Sign.
if (precision > 0 && precision <= DataTypeDecimalBase<Decimal256>::maxPrecision())
res = createDecimal<DataTypeDecimal>(precision, field.decimals);
}
break;
case enum_field_types::MYSQL_TYPE_FLOAT:
res = std::make_shared<DataTypeFloat32>();
break;
case enum_field_types::MYSQL_TYPE_DOUBLE:
res = std::make_shared<DataTypeFloat64>();
break;
case enum_field_types::MYSQL_TYPE_BIT:
res = std::make_shared<DataTypeUInt64>();
break;
case enum_field_types::MYSQL_TYPE_DATETIME:
case enum_field_types::MYSQL_TYPE_TIMESTAMP:
if (!type_support.isSet(MySQLDataTypesSupport::DATETIME64))
res = std::make_shared<DataTypeDateTime>();
else if (field.type == enum_field_types::MYSQL_TYPE_TIMESTAMP && field.decimals == 0)
res = std::make_shared<DataTypeDateTime>();
else
res = std::make_shared<DataTypeDateTime64>(field.decimals);
break;
case enum_field_types::MYSQL_TYPE_DATE:
case enum_field_types::MYSQL_TYPE_NEWDATE:
if (type_support.isSet(MySQLDataTypesSupport::DATE2DATE32))
res = std::make_shared<DataTypeDate32>();
else if (type_support.isSet(MySQLDataTypesSupport::DATE2STRING))
res = std::make_shared<DataTypeString>();
else
res = std::make_shared<DataTypeDate>();
break;
case enum_field_types::MYSQL_TYPE_GEOMETRY:
/// A query result set reports `MYSQL_TYPE_GEOMETRY` for a value of any spatial subtype
/// (`POINT`, `LINESTRING`, `POLYGON`, ...) without exposing which one it is, so the concrete
/// ClickHouse geometric type cannot be inferred here. Guessing `Point` makes reading any
/// non-`Point` value throw at read time (`Only Point data type is supported`), so the value is
/// left as the raw WKB `String` fallback instead. The concrete-type mapping controlled by
/// `mysql_datatypes_support_level` is applied only where MySQL exposes the concrete column type
/// (the table-name path, via the string overload above).
break;
case enum_field_types::MYSQL_TYPE_NULL:
res = std::make_shared<DataTypeNothing>();
break;
default:
/// String is the fallback for TIME, YEAR, all string/blob types, and any unknown type.
break;
}
if (!res)
res = std::make_shared<DataTypeString>();
if (is_nullable && !res->isNullable())
res = std::make_shared<DataTypeNullable>(res);
return res;
}
#endif
}