Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow arrow::ListType, arrow::StringType until permanent solution in place #251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libsupport/include/katana/ArrowVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ VisitArrowInternal(
TYPE_CASE(TIME32) // since midnight in seconds or millis
TYPE_CASE(TIME64) // since midnight in micros or nanos
TYPE_CASE(TIMESTAMP) // since UNIX epoch in seconds or smaller
TYPE_CASE(STRING) // TODO(daniel) DEPRECATED
TYPE_CASE(LARGE_STRING)
TYPE_CASE(STRUCT)
TYPE_CASE(LIST) // TODO(daniel) DEPRECATED
TYPE_CASE(LARGE_LIST)
#undef TYPE_CASE
default:
Expand Down
65 changes: 42 additions & 23 deletions libsupport/src/ArrowVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ struct ToArrayVisitor {

using ReturnType = std::shared_ptr<arrow::Array>;
using ResultType = katana::Result<ReturnType>;

template <typename BuilderType>
ResultType Finish(BuilderType* builder) {
std::shared_ptr<arrow::Array> array;
auto res = builder->Finish(&array);
if (!res.ok()) {
return KATANA_ERROR(
katana::ErrorCode::ArrowError, "arrow builder finish: {}", res);
}
return array;
}

template <typename ArrowType, typename BuilderType>
arrow::enable_if_t<
arrow::is_number_type<ArrowType>::value ||
Expand All @@ -34,24 +46,20 @@ struct ToArrayVisitor {
builder->UnsafeAppendNull();
}
}
std::shared_ptr<arrow::Array> array;
auto res = builder->Finish(&array);
if (!res.ok()) {
return KATANA_ERROR(
katana::ErrorCode::ArrowError, "arrow builder finish: {}", res);
}
return array;
return Finish(builder);
}

template <typename ArrowType, typename BuilderType>
arrow::enable_if_string_like<ArrowType, ResultType> Call(
BuilderType* builder) {
using ScalarType = typename arrow::TypeTraits<ArrowType>::ScalarType;
// same as above, but use scalar.value->ToString()
// same as above, but with string_view and Append instead of UnsafeAppend
for (const auto& scalar : scalars) {
if (scalar->is_valid) {
// ->value->ToString() works, scalar->ToString() yields "..."
const ScalarType* typed_scalar = static_cast<ScalarType*>(scalar.get());
if (auto res = builder->Append(typed_scalar->value->ToString());
if (auto res = builder->Append(
(arrow::util::string_view)(*typed_scalar->value));
!res.ok()) {
return KATANA_ERROR(
katana::ErrorCode::ArrowError, "arrow builder failed append: {}",
Expand All @@ -65,22 +73,33 @@ struct ToArrayVisitor {
}
}
}
std::shared_ptr<arrow::Array> array;
auto res = builder->Finish(&array);
if (!res.ok()) {
return KATANA_ERROR(
katana::ErrorCode::ArrowError, "arrow builder finish: {}", res);
}
return array;
}
template <typename ArrowType, typename BuilderType>
katana::enable_if_list_type_patched<ArrowType, ResultType> Call(
BuilderType*) {
KATANA_LOG_FATAL("List visitors not yet supported");
return Finish(builder);
}

template <typename ArrowType, typename BuilderType>
arrow::enable_if_struct<ArrowType, ResultType> Call(BuilderType*) {
KATANA_LOG_FATAL("Struct visitors not yet supported");
std::enable_if_t<
katana::is_list_type_patched<ArrowType>::value ||
arrow::is_struct_type<ArrowType>::value,
ResultType>
Call(BuilderType* builder) {
using ScalarType = typename arrow::TypeTraits<ArrowType>::ScalarType;
// use a visitor to traverse more complex types
katana::AppendScalarToBuilder visitor(builder);
for (const auto& scalar : scalars) {
if (scalar->is_valid) {
const ScalarType* typed_scalar = static_cast<ScalarType*>(scalar.get());
if (auto res = visitor.Call<ArrowType>(*typed_scalar); !res) {
return res.error();
}
} else {
if (auto res = builder->AppendNull(); !res.ok()) {
return KATANA_ERROR(
katana::ErrorCode::ArrowError,
"arrow builder failed append null: {}", res);
}
}
}
return Finish(builder);
}
};

Expand Down