Skip to content

Commit f04a126

Browse files
author
Giorgi Lomia
committed
Added an ArrowVisitor WIP
1 parent 7e588ee commit f04a126

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

tools/graph-stats/graph-memory-stats.cpp

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
#include <string>
2323
#include <unordered_map>
2424

25+
#include <arrow/array/builder_base.h>
26+
#include <arrow/type_traits.h>
27+
28+
#include "katana/ArrowVisitor.h"
2529
#include "katana/Galois.h"
30+
#include "katana/Logging.h"
2631
#include "katana/OfflineGraph.h"
2732
#include "llvm/Support/CommandLine.h"
2833

@@ -38,6 +43,140 @@ using map_string_element = std::unordered_map<std::string, std::string>;
3843
using memory_map = std::unordered_map<
3944
std::string, std::variant<map_element, map_string_element>>;
4045

46+
struct Visitor : public katana::ArrowVisitor {
47+
using ResultType = katana::Result<int64_t>;
48+
using AcceptTypes = std::tuple<
49+
arrow::Int8Type, arrow::UInt8Type, arrow::Int16Type, arrow::UInt16Type,
50+
arrow::Int32Type, arrow::UInt32Type, arrow::Int64Type, arrow::UInt64Type,
51+
arrow::FloatType, arrow::DoubleType, arrow::FloatType, arrow::DoubleType,
52+
arrow::BooleanType, arrow::Date32Type, arrow::Date64Type,
53+
arrow::Time32Type, arrow::Time64Type, arrow::TimestampType,
54+
arrow::StringType, arrow::LargeStringType, arrow::StructType,
55+
arrow::NullType>;
56+
57+
template <typename ArrowType, typename ScalarType>
58+
std::enable_if_t<
59+
arrow::is_number_type<ArrowType>::value ||
60+
arrow::is_boolean_type<ArrowType>::value ||
61+
arrow::is_temporal_type<ArrowType>::value,
62+
ResultType>
63+
Call(const ScalarType& scalar) {
64+
return scalar.value;
65+
}
66+
67+
template <typename ArrowType, typename ScalarType>
68+
arrow::enable_if_string_like<ArrowType, ResultType> Call(
69+
const ScalarType& scalar) {
70+
const ScalarType* typed_scalar = static_cast<ScalarType*>(scalar.get());
71+
auto res = (arrow::util::string_view)(*typed_scalar->value);
72+
// TODO (giorgi): make this KATANA_CHECKED
73+
// if (!res.ok()) {
74+
// return KATANA_ERROR(
75+
// katana::ErrorCode::ArrowError, "arrow builder failed append: {}",
76+
// res);
77+
// }
78+
return res;
79+
}
80+
81+
ResultType AcceptFailed(const arrow::Scalar& scalar) {
82+
return KATANA_ERROR(
83+
katana::ErrorCode::ArrowError, "no matching type {}",
84+
scalar.type->name());
85+
}
86+
};
87+
88+
// struct ToArrayVisitor : public katana::ArrowVisitor {
89+
// // Internal data and constructor
90+
// const std::shared_ptr<arrow::Array> scalars;
91+
// ToArrayVisitor(const std::shared_ptr<arrow::Array> input) : scalars(input) {}
92+
93+
// using ResultType = katana::Result<std::shared_ptr<arrow::Array>>;
94+
95+
// using AcceptTypes = std::tuple<katana::AcceptAllArrowTypes>;
96+
97+
// template <typename ArrowType, typename BuilderType>
98+
// arrow::enable_if_null<ArrowType, ResultType> Call(BuilderType* builder) {
99+
// return KATANA_CHECKED(builder->Finish());
100+
// }
101+
102+
// template <typename ArrowType, typename BuilderType>
103+
// std::enable_if_t<
104+
// arrow::is_number_type<ArrowType>::value ||
105+
// arrow::is_boolean_type<ArrowType>::value ||
106+
// arrow::is_temporal_type<ArrowType>::value,
107+
// ResultType>
108+
// Call(BuilderType* builder) {
109+
// using ScalarType = typename arrow::TypeTraits<ArrowType>::ScalarType;
110+
111+
// KATANA_CHECKED(builder->Reserve(scalars->length()));
112+
// for (auto j = 0; j < scalars->length(); j++) {
113+
// auto scalar = *scalars->GetScalar(j);
114+
// if (scalar != nullptr && scalar->is_valid) {
115+
// const ScalarType* typed_scalar = static_cast<ScalarType*>(scalar.get());
116+
// builder->UnsafeAppend(typed_scalar->value);
117+
// } else {
118+
// builder->UnsafeAppendNull();
119+
// }
120+
// }
121+
// return KATANA_CHECKED(builder->Finish());
122+
// }
123+
124+
// template <typename ArrowType, typename BuilderType>
125+
// arrow::enable_if_string_like<ArrowType, ResultType> Call(
126+
// BuilderType* builder) {
127+
// using ScalarType = typename arrow::TypeTraits<ArrowType>::ScalarType;
128+
// // same as above, but with string_view and Append instead of UnsafeAppend
129+
// for (auto j = 0; j < scalars->length(); j++) {
130+
// auto scalar = *scalars->GetScalar(j);
131+
// if (scalar != nullptr && scalar->is_valid) {
132+
// // ->value->ToString() works, scalar->ToString() yields "..."
133+
// const ScalarType* typed_scalar = static_cast<ScalarType*>(scalar.get());
134+
// if (auto res = builder->Append(
135+
// (arrow::util::string_view)(*typed_scalar->value));
136+
// !res.ok()) {
137+
// return KATANA_ERROR(
138+
// katana::ErrorCode::ArrowError, "arrow builder failed append: {}",
139+
// res);
140+
// }
141+
// } else {
142+
// if (auto res = builder->AppendNull(); !res.ok()) {
143+
// return KATANA_ERROR(
144+
// katana::ErrorCode::ArrowError,
145+
// "arrow builder failed append null: {}", res);
146+
// }
147+
// }
148+
// }
149+
// return KATANA_CHECKED(builder->Finish());
150+
// }
151+
152+
// template <typename ArrowType, typename BuilderType>
153+
// std::enable_if_t<
154+
// arrow::is_list_type<ArrowType>::value ||
155+
// arrow::is_struct_type<ArrowType>::value,
156+
// ResultType>
157+
// Call(BuilderType* builder) {
158+
// using ScalarType = typename arrow::TypeTraits<ArrowType>::ScalarType;
159+
// // use a visitor to traverse more complex types
160+
// katana::AppendScalarToBuilder visitor(builder);
161+
// for (auto j = 0; j < scalars->length(); j++) {
162+
// auto scalar = *scalars->GetScalar(j);
163+
// if (scalar != nullptr && scalar->is_valid) {
164+
// const ScalarType* typed_scalar = static_cast<ScalarType*>(scalar.get());
165+
// KATANA_CHECKED(visitor.Call<ArrowType>(*typed_scalar));
166+
// } else {
167+
// KATANA_CHECKED(builder->AppendNull());
168+
// }
169+
// }
170+
// return KATANA_CHECKED(builder->Finish());
171+
// }
172+
173+
// ResultType AcceptFailed(const arrow::ArrayBuilder* builder) {
174+
// return KATANA_ERROR(
175+
// katana::ErrorCode::ArrowError, "no matching type {}",
176+
// builder->type()->name());
177+
// }
178+
// };
179+
41180
void
42181
PrintAtomicTypes(const std::vector<std::string>& atomic_types) {
43182
for (auto atype : atomic_types) {
@@ -63,6 +202,21 @@ PrintStringMapping(const std::unordered_map<std::string, std::string>& u) {
63202
std::cout << "\n";
64203
}
65204

205+
katana::Result<std::shared_ptr<arrow::Array>>
206+
RunVisit(const std::shared_ptr<arrow::Array> scalars) {
207+
Visitor v;
208+
int64_t total = 0;
209+
for (auto j = 0; j < scalars->length(); j++) {
210+
auto s = *scalars->GetScalar(j);
211+
auto res = katana::VisitArrow(v, *s);
212+
KATANA_LOG_VASSERT(res, "unexpected errror {}", res.error());
213+
total += res.value();
214+
}
215+
216+
KATANA_LOG_VASSERT(
217+
total == scalars->length(), "{} != {}", total, scalars->length());
218+
}
219+
66220
void
67221
InsertPropertyTypeMemoryData(
68222
const std::unique_ptr<katana::PropertyGraph>& g,
@@ -104,7 +258,7 @@ GatherMemoryAllocation(
104258
alloc_size = 0;
105259
prop_size = 0;
106260
auto bit_width = arrow::bit_width(dtype->id());
107-
261+
auto visited_arr = RunVisit(prop_field);
108262
for (auto j = 0; j < prop_field->length(); j++) {
109263
if (prop_field->IsValid(j)) {
110264
auto scal_ptr = *prop_field->GetScalar(j);
@@ -121,6 +275,7 @@ GatherMemoryAllocation(
121275
usage.insert(std::pair(prop_name, prop_size));
122276
width.insert(std::pair(prop_name, bit_width));
123277
types.insert(std::pair(prop_name, dtype->name()));
278+
// std::cout << "Size: " << visited_arr->value().size() << "\n";
124279
}
125280
allocations.insert(std::pair("Total-Alloc", total_alloc));
126281
usage.insert(std::pair("Total-Usage", total_usage));

0 commit comments

Comments
 (0)