Skip to content
Closed
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
7 changes: 3 additions & 4 deletions cpp/src/arrow/compute/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class FunctionContext;
class ARROW_EXPORT OpKernel {
public:
virtual ~OpKernel() = default;
/// \brief EXPERIMENTAL The output data type of the kernel
/// \return the output type
virtual std::shared_ptr<DataType> out_type() const = 0;
};

/// \class Datum
Expand Down Expand Up @@ -188,10 +191,6 @@ class ARROW_EXPORT UnaryKernel : public OpKernel {
/// there will be a more generic mechansim for understanding the necessary
/// contracts.
virtual Status Call(FunctionContext* ctx, const Datum& input, Datum* out) = 0;

/// \brief EXPERIMENTAL The output data type of the kernel
/// \return the output type
virtual std::shared_ptr<DataType> out_type() const = 0;
};

/// \class BinaryKernel
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ arrow_install_all_headers("arrow/compute/kernels")
add_arrow_test(boolean-test PREFIX "arrow-compute")
add_arrow_test(cast-test PREFIX "arrow-compute")
add_arrow_test(hash-test PREFIX "arrow-compute")
add_arrow_test(util-internal-test PREFIX "arrow-compute")

# Aggregates
add_arrow_test(aggregate-test PREFIX "arrow-compute")
Expand Down
33 changes: 22 additions & 11 deletions cpp/src/arrow/compute/kernels/boolean-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TestBooleanKernel : public ComputeFixture, public TestBase {
ASSERT_OK(kernel(&this->ctx_, left, right, &result));
ASSERT_EQ(Datum::ARRAY, result.kind());
std::shared_ptr<Array> result_array = result.make_array();
ASSERT_TRUE(result_array->Equals(expected));
ASSERT_ARRAYS_EQUAL(*expected, *result_array);
}

void TestChunkedArrayBinary(const BinaryKernelFunc& kernel,
Expand Down Expand Up @@ -99,25 +99,23 @@ class TestBooleanKernel : public ComputeFixture, public TestBase {
};

TEST_F(TestBooleanKernel, Invert) {
vector<bool> values1 = {true, false, true};
vector<bool> values2 = {false, true, false};
vector<bool> values1 = {true, false, true, false};
vector<bool> values2 = {false, true, false, true};

auto type = boolean();
auto a1 = _MakeArray<BooleanType, bool>(type, values1, {});
auto a2 = _MakeArray<BooleanType, bool>(type, values2, {});
auto a1 = _MakeArray<BooleanType, bool>(type, values1, {true, true, true, false});
auto a2 = _MakeArray<BooleanType, bool>(type, values2, {true, true, true, false});

// Plain array
Datum result;
ASSERT_OK(Invert(&this->ctx_, a1, &result));
ASSERT_EQ(Datum::ARRAY, result.kind());
std::shared_ptr<Array> result_array = result.make_array();
ASSERT_TRUE(result_array->Equals(a2));
ASSERT_ARRAYS_EQUAL(*a2, *result.make_array());

// Array with offset
ASSERT_OK(Invert(&this->ctx_, a1->Slice(1), &result));
ASSERT_EQ(Datum::ARRAY, result.kind());
result_array = result.make_array();
ASSERT_TRUE(result_array->Equals(a2->Slice(1)));
ASSERT_ARRAYS_EQUAL(*a2->Slice(1), *result.make_array());

// ChunkedArray
std::vector<std::shared_ptr<Array>> ca1_arrs = {a1, a1->Slice(1)};
Expand All @@ -127,7 +125,7 @@ TEST_F(TestBooleanKernel, Invert) {
ASSERT_OK(Invert(&this->ctx_, ca1, &result));
ASSERT_EQ(Datum::CHUNKED_ARRAY, result.kind());
std::shared_ptr<ChunkedArray> result_ca = result.chunked_array();
ASSERT_TRUE(result_ca->Equals(ca2));
ASSERT_ARRAYS_EQUAL(*ca2, *result_ca);
}

TEST_F(TestBooleanKernel, InvertEmptyArray) {
Expand All @@ -139,7 +137,20 @@ TEST_F(TestBooleanKernel, InvertEmptyArray) {

Datum result;
ASSERT_OK(Invert(&this->ctx_, input, &result));
ASSERT_TRUE(result.make_array()->Equals(input.make_array()));
ASSERT_ARRAYS_EQUAL(*input.make_array(), *result.make_array());
}

TEST_F(TestBooleanKernel, BinaryOpOnEmptyArray) {
auto type = boolean();
std::vector<std::shared_ptr<Buffer>> data_buffers(2);
Datum input;
input.value = ArrayData::Make(boolean(), 0 /* length */, std::move(data_buffers),
0 /* null_count */);

Datum result;
ASSERT_OK(And(&this->ctx_, input, input, &result));
// Result should be empty as well.
ASSERT_ARRAYS_EQUAL(*input.make_array(), *result.make_array());
}

TEST_F(TestBooleanKernel, And) {
Expand Down
57 changes: 25 additions & 32 deletions cpp/src/arrow/compute/kernels/boolean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class InvertKernel : public BooleanUnaryKernel {

// Handle output data buffer
if (in_data.length > 0) {
RETURN_NOT_OK(detail::PropagateNulls(ctx, in_data, result.get()));
const Buffer& data_buffer = *in_data.buffers[1];
DCHECK_LE(BitUtil::BytesForBits(in_data.length), data_buffer.size());
InvertBitmap(data_buffer.data(), in_data.offset, in_data.length,
Expand All @@ -65,8 +66,8 @@ class InvertKernel : public BooleanUnaryKernel {
};

Status Invert(FunctionContext* ctx, const Datum& value, Datum* out) {
detail::PrimitiveAllocatingUnaryKernel kernel(
std::unique_ptr<UnaryKernel>(new InvertKernel()), boolean());
InvertKernel invert;
detail::PrimitiveAllocatingUnaryKernel kernel(&invert);

std::vector<Datum> result;
RETURN_NOT_OK(detail::InvokeUnaryArrayKernel(ctx, &kernel, value, &result));
Expand All @@ -86,73 +87,65 @@ class BinaryBooleanKernel : public BinaryKernel {

const ArrayData& left_data = *left.array();
const ArrayData& right_data = *right.array();
DCHECK_EQ(left_data.length, right_data.length);
ArrayData* result;
out->value = ArrayData::Make(boolean(), right_data.length);

result = out->array().get();

// If one of the arrays has a null value, the result will have a null.
std::shared_ptr<Buffer> validity_bitmap;
RETURN_NOT_OK(BitmapAnd(ctx->memory_pool(), left_data.buffers[0]->data(),
left_data.offset, right_data.buffers[0]->data(),
right_data.offset, right_data.length, 0, &validity_bitmap));
result->buffers.push_back(validity_bitmap);

result->null_count =
result->length - CountSetBits(validity_bitmap->data(), 0, result->length);

RETURN_NOT_OK(detail::AssignNullIntersection(ctx, left_data, right_data, result));
return Compute(ctx, left_data, right_data, result);
}

std::shared_ptr<DataType> out_type() const override { return boolean(); }
};

class AndKernel : public BinaryBooleanKernel {
Status Compute(FunctionContext* ctx, const ArrayData& left, const ArrayData& right,
ArrayData* out) override {
std::shared_ptr<Buffer> data_bitmap;
RETURN_NOT_OK(BitmapAnd(ctx->memory_pool(), left.buffers[1]->data(), left.offset,
right.buffers[1]->data(), right.offset, right.length, 0,
&data_bitmap));
out->buffers.push_back(data_bitmap);
if (right.length > 0) {
BitmapAnd(left.buffers[1]->data(), left.offset, right.buffers[1]->data(),
right.offset, right.length, 0, out->buffers[1]->mutable_data());
}
return Status::OK();
}
};

Status And(FunctionContext* ctx, const Datum& left, const Datum& right, Datum* out) {
AndKernel kernel;
AndKernel and_kernel;
detail::PrimitiveAllocatingBinaryKernel kernel(&and_kernel);
return detail::InvokeBinaryArrayKernel(ctx, &kernel, left, right, out);
}

class OrKernel : public BinaryBooleanKernel {
Status Compute(FunctionContext* ctx, const ArrayData& left, const ArrayData& right,
ArrayData* out) override {
std::shared_ptr<Buffer> data_bitmap;
RETURN_NOT_OK(BitmapOr(ctx->memory_pool(), left.buffers[1]->data(), left.offset,
right.buffers[1]->data(), right.offset, right.length, 0,
&data_bitmap));
out->buffers.push_back(data_bitmap);
if (right.length > 0) {
BitmapOr(left.buffers[1]->data(), left.offset, right.buffers[1]->data(),
right.offset, right.length, 0, out->buffers[1]->mutable_data());
}
return Status::OK();
}
};

Status Or(FunctionContext* ctx, const Datum& left, const Datum& right, Datum* out) {
OrKernel kernel;
OrKernel or_kernel;
detail::PrimitiveAllocatingBinaryKernel kernel(&or_kernel);
return detail::InvokeBinaryArrayKernel(ctx, &kernel, left, right, out);
}

class XorKernel : public BinaryBooleanKernel {
Status Compute(FunctionContext* ctx, const ArrayData& left, const ArrayData& right,
ArrayData* out) override {
std::shared_ptr<Buffer> data_bitmap;
RETURN_NOT_OK(BitmapXor(ctx->memory_pool(), left.buffers[1]->data(), left.offset,
right.buffers[1]->data(), right.offset, right.length, 0,
&data_bitmap));
out->buffers.push_back(data_bitmap);
if (right.length > 0) {
BitmapXor(left.buffers[1]->data(), left.offset, right.buffers[1]->data(),
right.offset, right.length, 0, out->buffers[1]->mutable_data());
}
return Status::OK();
}
};

Status Xor(FunctionContext* ctx, const Datum& left, const Datum& right, Datum* out) {
XorKernel kernel;
XorKernel xor_kernel;
detail::PrimitiveAllocatingBinaryKernel kernel(&xor_kernel);
return detail::InvokeBinaryArrayKernel(ctx, &kernel, left, right, out);
}

Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/compute/kernels/boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FunctionContext;
ARROW_EXPORT
Status Invert(FunctionContext* context, const Datum& value, Datum* out);

/// \brief Element-wise AND of two boolean dates
/// \brief Element-wise AND of two boolean datums
/// \param[in] context the FunctionContext
/// \param[in] left left operand (array)
/// \param[in] right right operand (array)
Expand All @@ -48,7 +48,7 @@ Status Invert(FunctionContext* context, const Datum& value, Datum* out);
ARROW_EXPORT
Status And(FunctionContext* context, const Datum& left, const Datum& right, Datum* out);

/// \brief Element-wise OR of two boolean dates
/// \brief Element-wise OR of two boolean datums
/// \param[in] context the FunctionContext
/// \param[in] left left operand (array)
/// \param[in] right right operand (array)
Expand All @@ -59,7 +59,7 @@ Status And(FunctionContext* context, const Datum& left, const Datum& right, Datu
ARROW_EXPORT
Status Or(FunctionContext* context, const Datum& left, const Datum& right, Datum* out);

/// \brief Element-wise XOR of two boolean dates
/// \brief Element-wise XOR of two boolean datums
/// \param[in] context the FunctionContext
/// \param[in] left left operand (array)
/// \param[in] right right operand (array)
Expand Down
5 changes: 4 additions & 1 deletion cpp/src/arrow/compute/kernels/cast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ struct CastFunctor<Date64Type, TimestampType> {

ShiftTime<int64_t, int64_t>(ctx, options, conversion.first, conversion.second, input,
output);
if (!ctx->status().ok()) {
return;
}

// Ensure that intraday milliseconds have been zeroed out
auto out_data = output->GetMutableValues<int64_t>(1);
Expand Down Expand Up @@ -634,7 +637,7 @@ Status InvokeWithAllocation(FunctionContext* ctx, UnaryKernel* func, const Datum
std::vector<Datum> result;
if (NeedToPreallocate(*func->out_type())) {
// Create wrapper that allocates output memory for primitive types
detail::PrimitiveAllocatingUnaryKernel wrapper(func, func->out_type());
detail::PrimitiveAllocatingUnaryKernel wrapper(func);
RETURN_NOT_OK(detail::InvokeUnaryArrayKernel(ctx, &wrapper, input, &result));
} else {
RETURN_NOT_OK(detail::InvokeUnaryArrayKernel(ctx, func, input, &result));
Expand Down
Loading