-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathBlockUtils.cpp
98 lines (85 loc) · 2.95 KB
/
BlockUtils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "BlockUtils.h"
#include <Columns/ColumnsNumber.h>
#include <Core/Types.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/DataTypeString.h>
#include <Databases/DatabaseFactory.h>
#include <Interpreters/Context.h>
#include <Common/logger_useful.h>
#include <Common/typeid_cast.h>
namespace DB
{
namespace ErrorCodes
{
extern const int OK;
}
namespace Streaming
{
Block buildBlock(
const std::vector<std::pair<String, String>> & string_cols,
const std::vector<std::pair<String, Int32>> & int32_cols,
const std::vector<std::pair<String, UInt64>> & uint64_cols)
{
Block block;
const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
auto string_type = data_type_factory.get("string", nullptr);
for (const auto & p : string_cols)
{
auto col = string_type->createColumn();
col->insertData(p.second.data(), p.second.size());
ColumnWithTypeAndName col_with_type(std::move(col), string_type, p.first);
block.insert(col_with_type);
}
auto int32_type = data_type_factory.get("int32", nullptr);
for (const auto & p : int32_cols)
{
auto col = int32_type->createColumn();
auto int32_col = typeid_cast<ColumnInt32 *>(col.get());
int32_col->insertValue(p.second);
ColumnWithTypeAndName col_with_type(std::move(col), int32_type, p.first);
block.insert(col_with_type);
}
auto uint64_type = data_type_factory.get("uint64", nullptr);
for (const auto & p : uint64_cols)
{
auto col = uint64_type->createColumn();
auto uint64_col = typeid_cast<ColumnUInt64 *>(col.get());
uint64_col->insertValue(p.second);
ColumnWithTypeAndName col_with_type(std::move(col), uint64_type, p.first);
block.insert(col_with_type);
}
return block;
}
Block buildBlock(
const std::vector<std::pair<String, std::vector<String>>> & string_cols,
const std::vector<std::pair<String, std::vector<Int64>>> & int64_cols)
{
Block block;
const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
auto string_type = data_type_factory.get("string", nullptr);
for (const auto & p : string_cols)
{
auto col = string_type->createColumn();
for (auto v = p.second.begin(); v != p.second.end(); ++v)
{
col->insertData(v->data(), v->size());
}
ColumnWithTypeAndName col_with_type(std::move(col), string_type, p.first);
block.insert(col_with_type);
}
auto int64_type = data_type_factory.get("int64", nullptr);
for (const auto & p : int64_cols)
{
auto col = int64_type->createColumn();
auto int64_col = typeid_cast<ColumnInt64 *>(col.get());
for (auto v = p.second.begin(); v != p.second.end(); ++v)
{
int64_col->insertValue(*v);
}
ColumnWithTypeAndName col_with_type(std::move(col), int64_type, p.first);
block.insert(col_with_type);
}
return block;
}
}
}