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

refactor(c): Check MakeArray/Batch Error codes with macro #1959

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
49 changes: 12 additions & 37 deletions c/validation/adbc_validation_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,54 +273,38 @@ int MakeArray(struct ArrowArray* parent, struct ArrowArray* array,
if constexpr (std::is_same<T, bool>::value || std::is_same<T, int8_t>::value ||
std::is_same<T, int16_t>::value || std::is_same<T, int32_t>::value ||
std::is_same<T, int64_t>::value) {
if (int errno_res = ArrowArrayAppendInt(array, *v); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendInt(array, *v));
// XXX: cpplint gets weird here and thinks this is an unbraced if
} else if constexpr (std::is_same<T, // NOLINT(readability/braces)
uint8_t>::value ||
std::is_same<T, uint16_t>::value ||
std::is_same<T, uint32_t>::value ||
std::is_same<T, uint64_t>::value) {
if (int errno_res = ArrowArrayAppendUInt(array, *v); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendUInt(array, *v));
} else if constexpr (std::is_same<T, float>::value || // NOLINT(readability/braces)
std::is_same<T, double>::value) {
if (int errno_res = ArrowArrayAppendDouble(array, *v); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendDouble(array, *v));
} else if constexpr (std::is_same<T, std::string>::value) {
struct ArrowBufferView view;
view.data.as_char = v->c_str();
view.size_bytes = v->size();
if (int errno_res = ArrowArrayAppendBytes(array, view); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendBytes(array, view));
} else if constexpr (std::is_same<T, std::vector<std::byte>>::value) {
static_assert(std::is_same_v<uint8_t, unsigned char>);
struct ArrowBufferView view;
view.data.as_uint8 = reinterpret_cast<const uint8_t*>(v->data());
view.size_bytes = v->size();
if (int errno_res = ArrowArrayAppendBytes(array, view); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendBytes(array, view));
} else if constexpr (std::is_same<T, ArrowInterval*>::value) {
if (int errno_res = ArrowArrayAppendInterval(array, *v); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendInterval(array, *v));
} else if constexpr (std::is_same<T, ArrowDecimal*>::value) {
if (int errno_res = ArrowArrayAppendDecimal(array, *v); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendDecimal(array, *v));
} else {
static_assert(!sizeof(T), "Not yet implemented");
return ENOTSUP;
}
} else {
if (int errno_res = ArrowArrayAppendNull(array, 1); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayAppendNull(array, 1));
}
}
return 0;
Expand All @@ -336,23 +320,16 @@ template <typename First, typename... Rest>
int MakeBatchImpl(struct ArrowArray* batch, size_t i, struct ArrowError* error,
const std::vector<std::optional<First>>& first,
const std::vector<std::optional<Rest>>&... rest) {
if (int errno_res = MakeArray<First>(batch, batch->children[i], first);
errno_res != 0) {
return errno_res;
}
CHECK_OK(MakeArray<First>(batch, batch->children[i], first));
return MakeBatchImpl(batch, i + 1, error, rest...);
}

/// \brief Make a batch from columns of C types.
template <typename... T>
int MakeBatch(struct ArrowArray* batch, struct ArrowError* error,
const std::vector<std::optional<T>>&... columns) {
if (int errno_res = ArrowArrayStartAppending(batch); errno_res != 0) {
return errno_res;
}
if (int errno_res = MakeBatchImpl(batch, 0, error, columns...); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayStartAppending(batch));
CHECK_OK(MakeBatchImpl(batch, 0, error, columns...));
for (size_t i = 0; i < static_cast<size_t>(batch->n_children); i++) {
if (batch->length > 0 && batch->children[i]->length != batch->length) {
ADD_FAILURE() << "Column lengths are inconsistent: column " << i << " has length "
Expand All @@ -367,9 +344,7 @@ int MakeBatch(struct ArrowArray* batch, struct ArrowError* error,
template <typename... T>
int MakeBatch(struct ArrowSchema* schema, struct ArrowArray* batch,
struct ArrowError* error, const std::vector<std::optional<T>>&... columns) {
if (int errno_res = ArrowArrayInitFromSchema(batch, schema, error); errno_res != 0) {
return errno_res;
}
CHECK_OK(ArrowArrayInitFromSchema(batch, schema, error));
return MakeBatch(batch, error, columns...);
}

Expand Down
Loading