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

Explicitly disable groupby on unsupported key types. #9227

Merged
merged 7 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
25 changes: 25 additions & 0 deletions cpp/include/cudf/utilities/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ constexpr inline bool is_equality_comparable()
return detail::is_equality_comparable_impl<L, R>::value;
}

namespace detail {
/**
* @brief Helper functor to check if a specified type `T` supports equality comparisons.
*/
struct unary_equality_comparable_functor {
template <typename T>
bool operator()() const
{
return cudf::is_equality_comparable<T, T>();
}
};
} // namespace detail

/**
* @brief Checks whether `data_type` `type` supports equality comparisons.
*
* @param type Data_type for comparison.
* @return true If `type` supports equality comparisons.
* @return false If `type` does not support equality comparisons.
*/
inline bool is_equality_comparable(data_type type)
{
return cudf::type_dispatcher(type, detail::unary_equality_comparable_functor{});
}

/**
* @brief Indicates whether the type `T` is a numeric type.
*
Expand Down
10 changes: 9 additions & 1 deletion cpp/src/groupby/common/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-20, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, 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 @@ -39,6 +39,14 @@ inline std::vector<aggregation_result> extract_results(host_span<RequestType con
return results;
}

/**
* @brief Fails an assert if any of the specified `keys` columns do not support equality
* comparisons.
*
* @param keys Grouping keys to be checked for equality comparisons.
*/
void assert_keys_equality_comparable(cudf::table_view const& keys);
mythrocks marked this conversation as resolved.
Show resolved Hide resolved

} // namespace detail
} // namespace groupby
} // namespace cudf
5 changes: 5 additions & 0 deletions cpp/src/groupby/groupby.cu
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/dictionary/dictionary_column_view.hpp>
#include <cudf/groupby.hpp>
#include <cudf/strings/string_view.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/error.hpp>
#include <cudf/utilities/traits.hpp>
#include <structs/utilities.hpp>

#include <rmm/cuda_stream_view.hpp>
Expand Down Expand Up @@ -76,6 +78,9 @@ std::pair<std::unique_ptr<table>, std::vector<aggregation_result>> groupby::disp
// Optionally flatten nested key columns.
auto [flattened_keys, _, __, ___] =
flatten_nested_columns(_keys, {}, {}, column_nullability::FORCE);
auto is_supported_key_type = [](auto col) { return cudf::is_equality_comparable(col.type()); };
CUDF_EXPECTS(std::all_of(flattened_keys.begin(), flattened_keys.end(), is_supported_key_type),
"Unsupported groupby key type does not support equality comparison");
auto [grouped_keys, results] =
detail::hash::groupby(flattened_keys, requests, _include_null_keys, stream, mr);
return std::make_pair(unflatten_nested_columns(std::move(grouped_keys), _keys),
Expand Down
5 changes: 5 additions & 0 deletions cpp/src/groupby/sort/sort_helper.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#include <cudf/detail/iterator.cuh>
#include <cudf/detail/scatter.hpp>
#include <cudf/detail/sorting.hpp>
#include <cudf/strings/string_view.hpp>
#include <cudf/table/row_operators.cuh>
#include <cudf/table/table_device_view.cuh>
#include <cudf/utilities/traits.hpp>
#include <structs/utilities.hpp>

#include <rmm/cuda_stream_view.hpp>
Expand Down Expand Up @@ -102,6 +104,9 @@ sort_groupby_helper::sort_groupby_helper(table_view const& keys,

auto [flattened_keys, _, __, struct_null_vectors] =
flatten_nested_columns(keys, {}, {}, column_nullability::FORCE);
auto is_supported_key_type = [](auto col) { return cudf::is_equality_comparable(col.type()); };
CUDF_EXPECTS(std::all_of(flattened_keys.begin(), flattened_keys.end(), is_supported_key_type),
"Unsupported groupby key type does not support equality comparison");
_struct_null_vectors = std::move(struct_null_vectors);
_keys = flattened_keys;

Expand Down
1 change: 1 addition & 0 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ConfigureTest(GROUPBY_TEST
groupby/count_tests.cpp
groupby/groups_tests.cpp
groupby/keys_tests.cpp
groupby/lists_tests.cpp
groupby/m2_tests.cpp
groupby/min_tests.cpp
groupby/max_scan_tests.cpp
Expand Down
69 changes: 69 additions & 0 deletions cpp/tests/groupby/lists_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2021, 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 <tests/groupby/groupby_test_util.hpp>

#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>

#include <cudf/detail/aggregation/aggregation.hpp>

namespace cudf {
namespace test {

template <typename V>
struct groupby_lists_test : public cudf::test::BaseFixture {
};

TYPED_TEST_SUITE(groupby_lists_test, cudf::test::FixedWidthTypes);

namespace {
// Checking with a single aggregation, and aggregation column.
// This test is orthogonal to the aggregation type; it focuses on testing the grouping
// with LISTS keys.
auto sum_agg() { return cudf::make_sum_aggregation<groupby_aggregation>(); }

void test_sort_based_sum_agg(column_view const& keys, column_view const& values)
{
test_single_agg(
keys, values, keys, values, sum_agg(), force_use_sort_impl::YES, null_policy::INCLUDE);
}

void test_hash_based_sum_agg(column_view const& keys, column_view const& values)
{
test_single_agg(
keys, values, keys, values, sum_agg(), force_use_sort_impl::NO, null_policy::INCLUDE);
}

} // namespace

TYPED_TEST(groupby_lists_test, top_level_lists_are_unsupported)
{
// Test that grouping on LISTS columns fails visibly.

// clang-format off
auto keys = lists_column_wrapper<TypeParam, int32_t> { {1,1}, {2,2}, {3,3}, {1,1}, {2,2} };
auto values = fixed_width_column_wrapper<int32_t> { 0, 1, 2, 3, 4 };
// clang-format on

EXPECT_THROW(test_sort_based_sum_agg(keys, values), cudf::logic_error);
EXPECT_THROW(test_hash_based_sum_agg(keys, values), cudf::logic_error);
}

} // namespace test
} // namespace cudf
35 changes: 25 additions & 10 deletions cpp/tests/groupby/structs_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <cudf_test/type_lists.hpp>

#include <cudf/detail/aggregation/aggregation.hpp>
#include "cudf/aggregation.hpp"
#include "cudf/types.hpp"

using namespace cudf::test::iterators;

Expand All @@ -34,7 +32,7 @@ template <typename V>
struct groupby_structs_test : public cudf::test::BaseFixture {
};

TYPED_TEST_CASE(groupby_structs_test, cudf::test::FixedWidthTypes);
TYPED_TEST_SUITE(groupby_structs_test, cudf::test::FixedWidthTypes);

using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
Expand Down Expand Up @@ -79,27 +77,43 @@ void print_agg_results(column_view const& keys, column_view const& vals)
}
}

void test_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
void test_sort_based_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
{
test_single_agg(keys,
values,
expected_keys,
expected_values,
sum_agg(),
force_use_sort_impl::NO,
force_use_sort_impl::YES,
null_policy::INCLUDE);
}

void test_hash_based_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
{
test_single_agg(keys,
values,
expected_keys,
expected_values,
sum_agg(),
force_use_sort_impl::YES,
force_use_sort_impl::NO,
null_policy::INCLUDE);
}

void test_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
{
test_sort_based_sum_agg(keys, values, expected_keys, expected_values);
test_hash_based_sum_agg(keys, values, expected_keys, expected_values);
}

} // namespace

TYPED_TEST(groupby_structs_test, basic)
Expand Down Expand Up @@ -312,7 +326,8 @@ TYPED_TEST(groupby_structs_test, lists_are_unsupported)
// clang-format on
auto keys = structs{{member_0, member_1}};

EXPECT_THROW(test_sum_agg(keys, values, keys, values), cudf::logic_error);
EXPECT_THROW(test_sort_based_sum_agg(keys, values, keys, values), cudf::logic_error);
EXPECT_THROW(test_hash_based_sum_agg(keys, values, keys, values), cudf::logic_error);
}

} // namespace test
Expand Down