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 2 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)
revans2 marked this conversation as resolved.
Show resolved Hide resolved
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
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
52 changes: 51 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,56 @@ 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>
CUDA_DEVICE_CALLABLE auto operator()(
TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) -> decltype(x && y)
{
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;
}
revans2 marked this conversation as resolved.
Show resolved Hide resolved
// To allow std::is_invocable_v = true
template <typename TypeLhs, typename TypeRhs>
CUDA_DEVICE_CALLABLE auto operator()(TypeLhs x, TypeRhs y) -> decltype(x && y);
};

struct NullLogicalOr {
template <typename TypeLhs, typename TypeRhs>
CUDA_DEVICE_CALLABLE auto operator()(
TypeLhs x, TypeRhs y, bool lhs_valid, bool rhs_valid, bool& output_valid) -> decltype(x || y)
{
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;
}
// To allow std::is_invocable_v = true
template <typename TypeLhs, typename TypeRhs>
CUDA_DEVICE_CALLABLE 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
36 changes: 35 additions & 1 deletion 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 @@ -580,6 +580,40 @@ auto NullOp_Result(column_view lhs, column_view rhs)
return column_wrapper<TypeOut>(result.cbegin(), result.cend(), result_mask.cbegin());
}

// Yes this is ugly but these logical tests need some things from nullops to work, and this
// kept the changes smallest
revans2 marked this conversation as resolved.
Show resolved Hide resolved
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());
}

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
6 changes: 4 additions & 2 deletions java/src/main/java/ai/rapids/cudf/BinaryOp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-2020,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 @@ -49,8 +49,10 @@ public enum BinaryOp {
GREATER_EQUAL(25), // >=
NULL_EQUALS(26), // like EQUAL but NULL == NULL is TRUE and NULL == not NULL is FALSE
NULL_MAX(27), // MAX but NULL < not NULL
NULL_MIN(28); // MIN but NULL > not NULL
NULL_MIN(28), // MIN but NULL > not NULL
//NOT IMPLEMENTED YET GENERIC_BINARY(29);
NULL_LOGICAL_AND(30),
NULL_LOGICAL_OR(31);
revans2 marked this conversation as resolved.
Show resolved Hide resolved


static final EnumSet<BinaryOp> COMPARISON = EnumSet.of(
Expand Down
42 changes: 41 additions & 1 deletion java/src/test/java/ai/rapids/cudf/BinaryOpTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2019-2020, 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 @@ -1363,6 +1363,46 @@ public void testBitXor() {
}
}

@Test
public void testNullAnd() {
jlowe marked this conversation as resolved.
Show resolved Hide resolved
try (ColumnVector icv1 = ColumnVector.fromBoxedBooleans(
true, true, true,
false, false, false,
null, null, null);
ColumnVector icv2 = ColumnVector.fromBoxedBooleans(
true, false, null,
true, false, null,
true, false, null)) {
try (ColumnVector answer = icv1.binaryOp(BinaryOp.NULL_LOGICAL_AND, icv2, DType.BOOL8);
ColumnVector expected = ColumnVector.fromBoxedBooleans(
true, false, null,
false, false, false,
null, false, null)) {
assertColumnsAreEqual(expected, answer, "boolean NULL AND boolean");
}
}
}

@Test
public void testNullOr() {
try (ColumnVector icv1 = ColumnVector.fromBoxedBooleans(
true, true, true,
false, false, false,
null, null, null);
ColumnVector icv2 = ColumnVector.fromBoxedBooleans(
true, false, null,
true, false, null,
true, false, null)) {
try (ColumnVector answer = icv1.binaryOp(BinaryOp.NULL_LOGICAL_OR, icv2, DType.BOOL8);
ColumnVector expected = ColumnVector.fromBoxedBooleans(
true, true, true,
true, false, null,
true, null, null)) {
assertColumnsAreEqual(expected, answer, "boolean NULL OR boolean");
}
}
}

@Test
public void testAnd() {
try (ColumnVector icv1 = ColumnVector.fromBoxedBooleans(BOOLEANS_1);
Expand Down