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

Add in support for NULL_LOGICAL_AND and NULL_LOGICAL_OR binops #10016

Merged
merged 10 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 3 additions & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# =============================================================================
# Copyright (c) 2018-2021, NVIDIA CORPORATION.
# Copyright (c) 2018-2022, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -186,6 +186,8 @@ add_library(
src/binaryop/compiled/Mod.cu
src/binaryop/compiled/Mul.cu
src/binaryop/compiled/NullEquals.cu
src/binaryop/compiled/NullLogicalOr.cu
src/binaryop/compiled/NullLogicalAnd.cu
src/binaryop/compiled/NullMax.cu
src/binaryop/compiled/NullMin.cu
src/binaryop/compiled/PMod.cu
Expand Down
8 changes: 6 additions & 2 deletions cpp/include/cudf/binaryop.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,7 +72,11 @@ enum class binary_operator : int32_t {
///< operand when one is null; or invalid when both are null
GENERIC_BINARY, ///< generic binary operator to be generated with input
///< ptx code
INVALID_BINARY ///< invalid operation
NULL_LOGICAL_AND, ///< operator && with Spark rules: (null, null) is null, (null, true) is null,
///< (null, false) is false, and (valid, valid) == LOGICAL_AND(valid, valid)
NULL_LOGICAL_OR, ///< operator || with Spark rules: (null, null) is null, (null, true) is true,
///< (null, false) is null, and (valid, valid) == LOGICAL_OR(valid, valid)
INVALID_BINARY ///< invalid operation
};
/**
* @brief Performs a binary operation between a scalar and a column.
Expand Down
5 changes: 3 additions & 2 deletions cpp/src/binaryop/binaryop.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Copyright 2018-2019 BlazingDB, Inc.
* Copyright 2018 Christian Noboa Mardini <[email protected]>
Expand Down Expand Up @@ -74,7 +74,8 @@ rmm::device_buffer scalar_col_valid_mask_and(column_view const& col,
inline bool is_null_dependent(binary_operator op)
{
return op == binary_operator::NULL_EQUALS || op == binary_operator::NULL_MIN ||
op == binary_operator::NULL_MAX;
op == binary_operator::NULL_MAX || op == binary_operator::NULL_LOGICAL_AND ||
op == binary_operator::NULL_LOGICAL_OR;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions cpp/src/binaryop/compiled/NullLogicalAnd.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "binary_ops.cuh"

namespace cudf::binops::compiled {
template void apply_binary_op<ops::NullLogicalAnd>(mutable_column_device_view&,
column_device_view const&,
column_device_view const&,
bool is_lhs_scalar,
bool is_rhs_scalar,
rmm::cuda_stream_view);
} // namespace cudf::binops::compiled
26 changes: 26 additions & 0 deletions cpp/src/binaryop/compiled/NullLogicalOr.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "binary_ops.cuh"

namespace cudf::binops::compiled {
template void apply_binary_op<ops::NullLogicalOr>(mutable_column_device_view&,
column_device_view const&,
column_device_view const&,
bool is_lhs_scalar,
bool is_rhs_scalar,
rmm::cuda_stream_view);
} // namespace cudf::binops::compiled
4 changes: 3 additions & 1 deletion cpp/src/binaryop/compiled/binary_ops.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -339,6 +339,8 @@ case binary_operator::PMOD: apply_binary_op<ops::PMod>(out, lhs,
case binary_operator::NULL_EQUALS: apply_binary_op<ops::NullEquals>(out, lhs, rhs, is_lhs_scalar, is_rhs_scalar, stream); break;
case binary_operator::NULL_MAX: apply_binary_op<ops::NullMax>(out, lhs, rhs, is_lhs_scalar, is_rhs_scalar, stream); break;
case binary_operator::NULL_MIN: apply_binary_op<ops::NullMin>(out, lhs, rhs, is_lhs_scalar, is_rhs_scalar, stream); break;
case binary_operator::NULL_LOGICAL_AND: apply_binary_op<ops::NullLogicalAnd>(out, lhs, rhs, is_lhs_scalar, is_rhs_scalar, stream); break;
case binary_operator::NULL_LOGICAL_OR: apply_binary_op<ops::NullLogicalOr>(out, lhs, rhs, is_lhs_scalar, is_rhs_scalar, stream); break;
default:;
}
// clang-format on
Expand Down
6 changes: 5 additions & 1 deletion cpp/src/binaryop/compiled/binary_ops.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,6 +103,8 @@ struct ops_wrapper {
type_dispatcher(rhs.type(), type_casted_accessor<TypeCommon>{}, i, rhs, is_rhs_scalar);
auto result = [&]() {
if constexpr (std::is_same_v<BinaryOperator, ops::NullEquals> or
std::is_same_v<BinaryOperator, ops::NullLogicalAnd> or
std::is_same_v<BinaryOperator, ops::NullLogicalOr> or
std::is_same_v<BinaryOperator, ops::NullMax> or
std::is_same_v<BinaryOperator, ops::NullMin>) {
bool output_valid = false;
Expand Down Expand Up @@ -150,6 +152,8 @@ struct ops2_wrapper {
TypeRhs y = rhs.element<TypeRhs>(is_rhs_scalar ? 0 : i);
auto result = [&]() {
if constexpr (std::is_same_v<BinaryOperator, ops::NullEquals> or
std::is_same_v<BinaryOperator, ops::NullLogicalAnd> or
std::is_same_v<BinaryOperator, ops::NullLogicalOr> or
std::is_same_v<BinaryOperator, ops::NullMax> or
std::is_same_v<BinaryOperator, ops::NullMin>) {
bool output_valid = false;
Expand Down
34 changes: 33 additions & 1 deletion cpp/src/binaryop/compiled/operation.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -415,6 +415,38 @@ struct NullMin {
-> decltype(static_cast<common_t>(static_cast<common_t>(x) < static_cast<common_t>(y) ? x : y));
};

struct NullLogicalAnd {
template <typename TypeLhs, typename TypeRhs>
__device__ inline auto operator()(
TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) -> decltype(x && y)
{
bool lhs_false = lhs_valid && !x;
bool rhs_false = rhs_valid && !y;
bool both_valid = lhs_valid && rhs_valid;
output_valid = lhs_false || rhs_false || both_valid;
return both_valid && !lhs_false && !rhs_false;
}
// To allow std::is_invocable_v = true
template <typename TypeLhs, typename TypeRhs>
__device__ inline auto operator()(TypeLhs x, TypeRhs y) -> decltype(x && y);
};

struct NullLogicalOr {
template <typename TypeLhs, typename TypeRhs>
__device__ inline auto operator()(
TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) -> decltype(x || y)
{
bool lhs_true = lhs_valid && x;
bool rhs_true = rhs_valid && y;
bool both_valid = lhs_valid && rhs_valid;
output_valid = lhs_true || rhs_true || both_valid;
return lhs_true || rhs_true;
}
// To allow std::is_invocable_v = true
template <typename TypeLhs, typename TypeRhs>
__device__ inline auto operator()(TypeLhs x, TypeRhs y) -> decltype(x || y);
};

} // namespace ops
} // namespace compiled
} // namespace binops
Expand Down
9 changes: 7 additions & 2 deletions cpp/src/binaryop/compiled/util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,8 +71,9 @@ struct is_binary_operation_supported {
if constexpr (has_common_type_v<TypeLhs, TypeRhs>) {
using common_t = std::common_type_t<TypeLhs, TypeRhs>;
return std::is_invocable_v<BinaryOperator, common_t, common_t>;
} else
} else {
return std::is_invocable_v<BinaryOperator, TypeLhs, TypeRhs>;
}
} else {
return false;
}
Expand Down Expand Up @@ -166,6 +167,10 @@ struct is_supported_operation_functor {
case binary_operator::LESS_EQUAL: return bool_op<ops::LessEqual, TypeLhs, TypeRhs>(out);
case binary_operator::GREATER_EQUAL: return bool_op<ops::GreaterEqual, TypeLhs, TypeRhs>(out);
case binary_operator::NULL_EQUALS: return bool_op<ops::NullEquals, TypeLhs, TypeRhs>(out);
case binary_operator::NULL_LOGICAL_AND:
return bool_op<ops::NullLogicalAnd, TypeLhs, TypeRhs>(out);
case binary_operator::NULL_LOGICAL_OR:
return bool_op<ops::NullLogicalOr, TypeLhs, TypeRhs>(out);
default: return type_dispatcher(out, nested_support_functor<TypeLhs, TypeRhs>{}, op);
}
return false;
Expand Down
86 changes: 59 additions & 27 deletions cpp/tests/binaryop/binop-compiled-test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -475,6 +475,64 @@ TYPED_TEST(BinaryOperationCompiledTest_Logical, LogicalOr_Vector_Vector)
this->template test<cudf::library::operation::LogicalOr>(cudf::binary_operator::LOGICAL_OR);
}

template <typename T>
using column_wrapper = std::conditional_t<std::is_same_v<T, std::string>,
cudf::test::strings_column_wrapper,
cudf::test::fixed_width_column_wrapper<T>>;

template <typename TypeOut, typename TypeLhs, typename TypeRhs, class OP>
auto NullOp_Result(column_view lhs, column_view rhs)
{
auto [lhs_data, lhs_mask] = cudf::test::to_host<TypeLhs>(lhs);
auto [rhs_data, rhs_mask] = cudf::test::to_host<TypeRhs>(rhs);
std::vector<TypeOut> result(lhs.size());
std::vector<bool> result_mask;
std::transform(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(lhs.size()),
result.begin(),
[&lhs_data, &lhs_mask, &rhs_data, &rhs_mask, &result_mask](auto i) -> TypeOut {
auto lhs_valid = lhs_mask.data() and cudf::bit_is_set(lhs_mask.data(), i);
auto rhs_valid = rhs_mask.data() and cudf::bit_is_set(rhs_mask.data(), i);
bool output_valid = lhs_valid or rhs_valid;
auto result = OP{}(lhs_data[i], rhs_data[i], lhs_valid, rhs_valid, output_valid);
result_mask.push_back(output_valid);
return result;
});
return column_wrapper<TypeOut>(result.cbegin(), result.cend(), result_mask.cbegin());
}

TYPED_TEST(BinaryOperationCompiledTest_Logical, NullLogicalAnd_Vector_Vector)
{
using TypeOut = bool;
using TypeLhs = typename TestFixture::TypeLhs;
using TypeRhs = typename TestFixture::TypeRhs;
using NULL_AND = cudf::library::operation::NullLogicalAnd<TypeOut, TypeLhs, TypeRhs>;

auto lhs = lhs_random_column<TypeLhs>(col_size);
auto rhs = rhs_random_column<TypeRhs>(col_size);
auto const expected = NullOp_Result<TypeOut, TypeLhs, TypeRhs, NULL_AND>(lhs, rhs);

auto const result = cudf::binary_operation(
lhs, rhs, cudf::binary_operator::NULL_LOGICAL_AND, data_type(type_to_id<TypeOut>()));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}

TYPED_TEST(BinaryOperationCompiledTest_Logical, NullLogicalOr_Vector_Vector)
{
using TypeOut = bool;
using TypeLhs = typename TestFixture::TypeLhs;
using TypeRhs = typename TestFixture::TypeRhs;
using NULL_OR = cudf::library::operation::NullLogicalOr<TypeOut, TypeLhs, TypeRhs>;

auto lhs = lhs_random_column<TypeLhs>(col_size);
auto rhs = rhs_random_column<TypeRhs>(col_size);
auto const expected = NullOp_Result<TypeOut, TypeLhs, TypeRhs, NULL_OR>(lhs, rhs);

auto const result = cudf::binary_operation(
lhs, rhs, cudf::binary_operator::NULL_LOGICAL_OR, data_type(type_to_id<TypeOut>()));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}

// Comparison Operations ==, !=, <, >, <=, >=
// n<!=>n, t<!=>t, d<!=>d, s<!=>s, dc<!=>dc
using Comparison_types = cudf::test::Types<cudf::test::Types<bool, int8_t, int16_t>,
Expand Down Expand Up @@ -554,32 +612,6 @@ struct BinaryOperationCompiledTest_NullOps : public BinaryOperationCompiledTest<
};
TYPED_TEST_SUITE(BinaryOperationCompiledTest_NullOps, Null_types);

template <typename T>
using column_wrapper = std::conditional_t<std::is_same_v<T, std::string>,
cudf::test::strings_column_wrapper,
cudf::test::fixed_width_column_wrapper<T>>;

template <typename TypeOut, typename TypeLhs, typename TypeRhs, class OP>
auto NullOp_Result(column_view lhs, column_view rhs)
{
auto [lhs_data, lhs_mask] = cudf::test::to_host<TypeLhs>(lhs);
auto [rhs_data, rhs_mask] = cudf::test::to_host<TypeRhs>(rhs);
std::vector<TypeOut> result(lhs.size());
std::vector<bool> result_mask;
std::transform(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(lhs.size()),
result.begin(),
[&lhs_data, &lhs_mask, &rhs_data, &rhs_mask, &result_mask](auto i) -> TypeOut {
auto lhs_valid = lhs_mask.data() and cudf::bit_is_set(lhs_mask.data(), i);
auto rhs_valid = rhs_mask.data() and cudf::bit_is_set(rhs_mask.data(), i);
bool output_valid = lhs_valid or rhs_valid;
auto result = OP{}(lhs_data[i], rhs_data[i], lhs_valid, rhs_valid, output_valid);
result_mask.push_back(output_valid);
return result;
});
return column_wrapper<TypeOut>(result.cbegin(), result.cend(), result_mask.cbegin());
}

TYPED_TEST(BinaryOperationCompiledTest_NullOps, NullEquals_Vector_Vector)
{
using TypeOut = bool;
Expand Down
44 changes: 43 additions & 1 deletion cpp/tests/binaryop/util/operation.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Copyright 2018-2019 BlazingDB, Inc.
* Copyright 2018 Christian Noboa Mardini <[email protected]>
Expand Down Expand Up @@ -323,6 +323,48 @@ struct PyMod {
}
};

template <typename TypeOut, typename TypeLhs, typename TypeRhs>
struct NullLogicalAnd {
TypeOut operator()(TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) const
{
if (lhs_valid && !x) {
output_valid = true;
return false;
}
if (rhs_valid && !y) {
output_valid = true;
return false;
}
if (lhs_valid && rhs_valid) {
output_valid = true;
return true;
}
output_valid = false;
return false;
}
};

template <typename TypeOut, typename TypeLhs, typename TypeRhs>
struct NullLogicalOr {
TypeOut operator()(TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) const
{
if (lhs_valid && x) {
output_valid = true;
return true;
}
if (rhs_valid && y) {
output_valid = true;
return true;
}
if (lhs_valid && rhs_valid) {
output_valid = true;
return false;
}
output_valid = false;
return false;
}
};

template <typename TypeOut, typename TypeLhs, typename TypeRhs>
struct NullEquals {
TypeOut operator()(TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) const
Expand Down
Loading