-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_utils.hpp
171 lines (136 loc) · 4.82 KB
/
clean_utils.hpp
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
#ifndef CLEAN_UTILS_HPP
#define CLEAN_UTILS_HPP
#include <arrow/api.h>
#include <arrow/compute/api.h>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <regex>
#include <future>
namespace cp = arrow::compute;
namespace fs = arrow::fs;
namespace ac = arrow::acero;
namespace cp = ::arrow::compute;
const cp::FunctionDoc vehicle_color_doc
{
"Regex replacement",
"clean dirty data in Vehicle Color column",
{"color"}
};
arrow::Status vehicle_color_function(
cp::KernelContext* ctx, const cp::ExecSpan& batch, cp::ExecResult* out)
{
const char* chars = batch[0].array.GetValues<char>(2);
const int* idx = batch[0].array.GetValues<int>(1);
auto null_count = batch[0].array.GetBuffer(0);
arrow::TypedBufferBuilder<int32_t> len_builder;
arrow::BufferBuilder string_builder;
ARROW_RETURN_NOT_OK(len_builder.Reserve(batch.length+1));
int64_t strt = *idx++;
int64_t end{};
int64_t len_count=0;
ARROW_RETURN_NOT_OK(len_builder.Append(len_count));
for (int64_t i = 0; i < batch.length; ++i)
{
if (batch[0].array.IsNull(i))
{
ARROW_RETURN_NOT_OK(len_builder.Append(len_count));
idx++;
continue;
}
end = *idx++;
size_t n = end - strt;
char color[n];
std::strncpy(color, chars + strt, n);
strt = end;
if (std::regex_search(color, std::regex(
"^[Ww]{1}[Hh]{0,1}?[Ii]{0,1}?([Tt]{1})?[Ee.]{0,2}?$", std::regex_constants::ECMAScript)))
{
n = 6;
ARROW_RETURN_NOT_OK(string_builder.Append("WHITE", n));
}
else if (std::regex_search(color, std::regex(
"^[Gg]{1}[Rr]{0,1}?[Ee]{0,1}?[Yy.]{0,2}$",
std::regex_constants::ECMAScript)))
{
n = 5;
ARROW_RETURN_NOT_OK(string_builder.Append("GREY", n));
}
else if (std::regex_search(color, std::regex(
"^[Yy]{1}[Ee]{0,1}?[Ll]{0,2}?[Oo]{0,1}[Ww.]{0,2}$",
std::regex_constants::ECMAScript)))
{
n = 7;
ARROW_RETURN_NOT_OK(string_builder.Append("YELLOW", n));
}
else if (std::regex_search(color, std::regex(
"^[Bb]{1}[Ll]{0,1}?[Aa]{0,1}?[Cc]{0,1}[Kk.]{0,2}$", std::regex_constants::ECMAScript)))
{
n = 6;
ARROW_RETURN_NOT_OK(string_builder.Append("BLACK", n));
}
else
{
ARROW_RETURN_NOT_OK(string_builder.Append(color, n));
}
len_count += n;
ARROW_RETURN_NOT_OK(len_builder.Append(len_count));
}
ARROW_ASSIGN_OR_RAISE(auto len_buffer, len_builder.Finish(false));
ARROW_ASSIGN_OR_RAISE(auto string_buffer, string_builder.Finish());
arrow::ArrayData ad(
arrow::utf8(), batch.length, {null_count, len_buffer, string_buffer});
out->value = std::make_shared<arrow::ArrayData>(std::move(ad));
return arrow::Status::OK();
}
arrow::Datum get_chunk(
const std::shared_ptr<arrow::Array>& chunk,
const std::string& name)
{
auto maybe_datum = cp::CallFunction(name, {chunk});
if (!maybe_datum.ok())
throw std::runtime_error("Vehicle Color error");
return maybe_datum.ValueOrDie();
}
arrow::Status vehicle_color_ex(std::shared_ptr<arrow::Table>& table)
{
const std::string name = "clean_color";
auto func =
std::make_shared<cp::ScalarFunction>(
name, cp::Arity::Unary(), vehicle_color_doc);
cp::ScalarKernel kernel({arrow::utf8()},
arrow::utf8(), vehicle_color_function);
kernel.mem_allocation = cp::MemAllocation::PREALLOCATE;
kernel.null_handling = cp::NullHandling::INTERSECTION;
ARROW_RETURN_NOT_OK(func->AddKernel(std::move(kernel)));
auto registry = cp::GetFunctionRegistry();
ARROW_RETURN_NOT_OK(registry->AddFunction(std::move(func)));
auto color_column = table->GetColumnByName("Vehicle Color");
arrow::ArrayVector chunks = color_column->chunks();
std::vector<std::future<arrow::Datum>> futures(chunks.size());
for (size_t i=0; i<chunks.size(); i++)
{
futures[i] = std::async(std::launch::async, get_chunk, chunks[i], name);
}
for (size_t i=0; i<chunks.size(); i++)
{
try
{
chunks[i] = futures[i].get().make_array();
}
catch(const std::exception& e)
{
return arrow::Status::UnknownError(e.what());
}
}
auto ch_arr = std::make_shared<arrow::ChunkedArray>(std::move(chunks));
ARROW_ASSIGN_OR_RAISE(table, table->AddColumn(
table->num_columns(),
arrow::field("new_column", arrow::utf8()), ch_arr));
return arrow::Status::OK();
}
#endif