Skip to content
Merged
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
22 changes: 22 additions & 0 deletions cpp/include/cudf/column/column_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <cudf/utilities/traits.hpp>
#include <cudf/utilities/type_dispatcher.hpp>

#include <cuda/std/span>

#include <limits>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -450,6 +452,26 @@ class column_view : public detail::column_view_base {
return device_span<T const>(data<T>(), size());
}

/**
* @brief Converts a column view into a cuda::std::span.
*
* Only numeric and chrono data types are supported. The column view must not
* be nullable.
*
* @tparam T The device span type. Must be const and match the column view's type.
* @throws cudf::logic_error if the column view type does not match the span type.
* @throws cudf::logic_error if the column view is nullable.
* @return A span of the column view's data
*/
template <typename T, CUDF_ENABLE_IF(cudf::is_numeric<T>() or cudf::is_chrono<T>())>
[[nodiscard]] operator cuda::std::span<T const>() const
{
CUDF_EXPECTS(type() == cudf::data_type{cudf::type_to_id<T>()},
"Span type must match column view type");
CUDF_EXPECTS(!nullable(), "A nullable column view cannot be converted to a span.");
return cuda::std::span<T const>(data<T>(), size());
}

protected:
/**
* @brief Returns pointer to the base device memory allocation.
Expand Down
70 changes: 63 additions & 7 deletions cpp/tests/column/column_view_device_span_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2024, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -14,6 +14,7 @@
#include <cudf/utilities/span.hpp>
#include <cudf/utilities/traits.hpp>

#include <cuda/std/span>
#include <thrust/iterator/counting_iterator.h>

#include <memory>
Expand All @@ -27,12 +28,12 @@ std::unique_ptr<cudf::column> example_column()
}

template <typename T>
struct ColumnViewDeviceSpanTests : public cudf::test::BaseFixture {};
struct ColumnViewSpanTests : public cudf::test::BaseFixture {};

using DeviceSpanTypes = cudf::test::FixedWidthTypesWithoutFixedPoint;
TYPED_TEST_SUITE(ColumnViewDeviceSpanTests, DeviceSpanTypes);
TYPED_TEST_SUITE(ColumnViewSpanTests, DeviceSpanTypes);

TYPED_TEST(ColumnViewDeviceSpanTests, conversion_round_trip)
TYPED_TEST(ColumnViewSpanTests, device_span_conversion_round_trip)
{
auto col = example_column<TypeParam>();
auto col_view = cudf::column_view{*col};
Expand All @@ -43,19 +44,74 @@ TYPED_TEST(ColumnViewDeviceSpanTests, conversion_round_trip)
CUDF_TEST_EXPECT_COLUMNS_EQUAL(col_view, col_view_from_device_span);
}

struct ColumnViewDeviceSpanErrorTests : public cudf::test::BaseFixture {};
struct ColumnViewSpanErrorTests : public cudf::test::BaseFixture {};

TEST_F(ColumnViewDeviceSpanErrorTests, type_mismatch)
TEST_F(ColumnViewSpanErrorTests, device_span_type_mismatch)
{
auto col = example_column<int32_t>();
auto col_view = cudf::column_view{*col};
EXPECT_THROW((void)cudf::device_span<float const>{col_view}, cudf::logic_error);
}

TEST_F(ColumnViewDeviceSpanErrorTests, nullable_column)
TEST_F(ColumnViewSpanErrorTests, device_span_nullable_column)
{
auto col = example_column<int32_t>();
col->set_null_mask(cudf::create_null_mask(col->size(), cudf::mask_state::ALL_NULL), col->size());
auto col_view = cudf::column_view{*col};
EXPECT_THROW((void)cudf::device_span<int32_t const>{col_view}, cudf::logic_error);
}

TYPED_TEST(ColumnViewSpanTests, std_span_conversion_to_span)
{
auto col = example_column<TypeParam>();
auto col_view = cudf::column_view{*col};

// Test implicit conversion to cuda::std::span
cuda::std::span<TypeParam const> cuda_span_from_col_view = col_view;

// Verify span properties match column view
EXPECT_EQ(cuda_span_from_col_view.size(), static_cast<std::size_t>(col_view.size()));
EXPECT_EQ(cuda_span_from_col_view.data(), col_view.data<TypeParam>());
EXPECT_FALSE(cuda_span_from_col_view.empty());
}

TYPED_TEST(ColumnViewSpanTests, std_span_explicit_conversion_to_span)
{
auto col = example_column<TypeParam>();
auto col_view = cudf::column_view{*col};

// Test explicit conversion to cuda::std::span
auto cuda_span_from_col_view = static_cast<cuda::std::span<TypeParam const>>(col_view);

// Verify span properties match column view
EXPECT_EQ(cuda_span_from_col_view.size(), static_cast<std::size_t>(col_view.size()));
EXPECT_EQ(cuda_span_from_col_view.data(), col_view.data<TypeParam>());
}

TYPED_TEST(ColumnViewSpanTests, std_span_empty_column_to_span)
{
cudf::test::fixed_width_column_wrapper<TypeParam> empty_col{};
auto col_view = cudf::column_view{empty_col};

// Test conversion of empty column to cuda::std::span
cuda::std::span<TypeParam const> cuda_span_from_col_view = col_view;

// Verify span properties for empty column
EXPECT_EQ(cuda_span_from_col_view.size(), 0u);
EXPECT_TRUE(cuda_span_from_col_view.empty());
}

TEST_F(ColumnViewSpanErrorTests, std_span_type_mismatch)
{
auto col = example_column<int32_t>();
auto col_view = cudf::column_view{*col};
EXPECT_THROW((void)cuda::std::span<float const>{col_view}, cudf::logic_error);
}

TEST_F(ColumnViewSpanErrorTests, std_span_nullable_column)
{
auto col = example_column<int32_t>();
col->set_null_mask(cudf::create_null_mask(col->size(), cudf::mask_state::ALL_NULL), col->size());
auto col_view = cudf::column_view{*col};
EXPECT_THROW((void)cuda::std::span<int32_t const>{col_view}, cudf::logic_error);
}