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 support for org.apache.spark.sql.catalyst.expressions.Bin #2760

Open
wants to merge 6 commits into
base: branch-25.02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion src/main/cpp/CMakeLists.txt
ustcfy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# =============================================================================
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
# Copyright (c) 2022-2025, 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 @@ -211,6 +211,7 @@ add_library(
src/case_when.cu
src/cast_decimal_to_string.cu
src/cast_float_to_string.cu
src/cast_long_to_binary_string.cu
src/cast_string.cu
src/cast_string_to_float.cu
src/datetime_rebase.cu
Expand Down
17 changes: 16 additions & 1 deletion src/main/cpp/src/CastStringJni.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, NVIDIA CORPORATION.
* Copyright (c) 2022-2025, 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 @@ -156,6 +156,21 @@ JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_CastStrings_fromDecimal
CATCH_CAST_EXCEPTION(env, 0);
}

JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_CastStrings_fromLongToBinary(
JNIEnv* env, jclass, jlong input_column)
{
JNI_NULL_CHECK(env, input_column, "input column is null", 0);

try {
cudf::jni::auto_set_device(env);

auto const& cv = *reinterpret_cast<cudf::column_view const*>(input_column);
return cudf::jni::release_as_jlong(
spark_rapids_jni::long_to_binary_string(cv, cudf::get_default_stream()));
}
CATCH_CAST_EXCEPTION(env, 0);
}

JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_CastStrings_toIntegersWithBase(
JNIEnv* env, jclass, jlong input_column, jint base, jboolean ansi_enabled, jint j_dtype)
{
Expand Down
118 changes: 118 additions & 0 deletions src/main/cpp/src/cast_long_to_binary_string.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2025, 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 "cast_string.hpp"

#include <cudf/column/column_device_view.cuh>
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless include (not sure)?

#include <cudf/strings/detail/strings_children.cuh>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/type_dispatcher.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/exec_policy.hpp>

namespace spark_rapids_jni {

namespace detail {
namespace {

template <typename LongType>
ustcfy marked this conversation as resolved.
Show resolved Hide resolved
struct long_to_binary_string_fn {
cudf::column_device_view d_longs;
cudf::size_type* d_sizes;
char* d_chars;
cudf::detail::input_offsetalator d_offsets;

__device__ cudf::size_type compute_output_size(LongType value)
{
return max(64 - __clzll(value), 1); // If the value is 0, the output size should be 1
}

__device__ void long_to_binary_string(cudf::size_type idx)
{
auto const value = d_longs.element<LongType>(idx);
char* d_buffer = d_chars + d_offsets[idx];
for (auto i = d_sizes[idx] - 1; i >= 0; --i) {
*d_buffer++ = value & (1LL << i) ? '1' : '0';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*d_buffer++ = '0' + ((value & (1LL << i)) >> i); perhaps this approach is more efficient since it avoids branching, which might degrade performance on GPUs with warp divergence.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am not sure if it is a good practice which is really effective. I would like to hear your opinions on this issue @res-life @ttnghia .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe *d_buffer++ = '0' + ((value & (1LL << i)) != 0);? It will be (very slightly) cheaper and easier to read.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thirtiseven
Yes, this one is also a Branch-Free expression since the compiler shall use setne instruction avoids branching by directly setting a register based on the zero flag (ZF):

cmp rax, 0
setne al
and al, 1
add eax, 48

The corresponding codes of my alternative would be translated into:

sar     rax, cl
add     rax, 48

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am not sure if it is a good practice which is really effective. I would like to hear your opinions on this issue @res-life @ttnghia .

Yes, I think this approach is more efficient.
You may conduct a benchmark test to double confirm.

}
}

__device__ void operator()(cudf::size_type idx)
{
if (d_longs.is_null(idx)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Just some nice-to-have improvement, use constexpr if instead of if and add an extra template variable nullable for this functor. Because we already knew whether the column_view is nullable or NOT.

if (d_chars == nullptr) { d_sizes[idx] = 0; }
return;
}
if (d_chars != nullptr) {
long_to_binary_string(idx);
} else {
d_sizes[idx] = compute_output_size(d_longs.element<LongType>(idx));
}
}
};

struct dispatch_long_to_binary_string_fn {
template <typename LongType, CUDF_ENABLE_IF(std::is_same_v<LongType, std::int64_t>)>
std::unique_ptr<cudf::column> operator()(cudf::column_view const& input,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr) const
{
auto const d_column = cudf::column_device_view::create(input, stream);

auto [offsets, chars] = cudf::strings::detail::make_strings_children(
long_to_binary_string_fn<LongType>{*d_column}, input.size(), stream, mr);

return cudf::make_strings_column(input.size(),
std::move(offsets),
chars.release(),
input.null_count(),
cudf::detail::copy_bitmask(input, stream, mr));
}

template <typename LongType, CUDF_ENABLE_IF(not std::is_same_v<LongType, std::int64_t>)>
std::unique_ptr<cudf::column> operator()(cudf::column_view const&,
rmm::cuda_stream_view,
rmm::device_async_resource_ref) const
{
CUDF_FAIL("Values for long_to_binary_string function must be a long type.");
}
};

} // namespace

std::unique_ptr<cudf::column> long_to_binary_string(cudf::column_view const& input,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
if (input.is_empty()) return cudf::make_empty_column(cudf::type_id::STRING);
return type_dispatcher(input.type(), dispatch_long_to_binary_string_fn{}, input, stream, mr);
}

} // namespace detail

// external API
std::unique_ptr<cudf::column> long_to_binary_string(cudf::column_view const& input,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
ustcfy marked this conversation as resolved.
Show resolved Hide resolved
return detail::long_to_binary_string(input, stream, mr);
}

} // namespace spark_rapids_jni
7 changes: 6 additions & 1 deletion src/main/cpp/src/cast_string.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, NVIDIA CORPORATION.
* Copyright (c) 2022-2025, 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 @@ -133,4 +133,9 @@ std::unique_ptr<cudf::column> decimal_to_non_ansi_string(
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());

std::unique_ptr<cudf::column> long_to_binary_string(
cudf::column_view const& input,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());

} // namespace spark_rapids_jni
5 changes: 4 additions & 1 deletion src/main/cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
# Copyright (c) 2022-2025, 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 @@ -58,6 +58,9 @@ ConfigureTest(FORMAT_FLOAT
ConfigureTest(CAST_FLOAT_TO_STRING
cast_float_to_string.cpp)

ConfigureTest(CAST_LONG_TO_BINARY_STRING
cast_long_to_binary_string.cpp)

ConfigureTest(DATETIME_REBASE
datetime_rebase.cpp)

Expand Down
48 changes: 48 additions & 0 deletions src/main/cpp/tests/cast_long_to_binary_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2025, 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 <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>

#include <rmm/device_uvector.hpp>

#include <cast_string.hpp>

#include <limits>

using namespace cudf;

constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};

struct LongToBinaryStringTests : public cudf::test::BaseFixture {};

TEST_F(LongToBinaryStringTests, FromLongToBinary)
{
auto const longs = cudf::test::fixed_width_column_wrapper<int64_t>{
0L, 1L, 10L, -1L, std::numeric_limits<int64_t>::max(), std::numeric_limits<int64_t>::min()};

auto results = spark_rapids_jni::long_to_binary_string(longs, cudf::get_default_stream());

auto const expected = cudf::test::strings_column_wrapper{
"0",
"1",
"1010",
"1111111111111111111111111111111111111111111111111111111111111111",
"111111111111111111111111111111111111111111111111111111111111111",
"1000000000000000000000000000000000000000000000000000000000000000"};

CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected, verbosity);
}
7 changes: 6 additions & 1 deletion src/main/java/com/nvidia/spark/rapids/jni/CastStrings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
* Copyright (c) 2022-2025, 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 @@ -111,6 +111,10 @@ public static ColumnVector fromDecimal(ColumnView cv) {
return new ColumnVector(fromDecimal(cv.getNativeView()));
}

public static ColumnVector fromLongToBinary(ColumnView cv) {
return new ColumnVector(fromLongToBinary(cv.getNativeView()));
}

/**
* Convert a string column to a given floating-point type column.
*
Expand Down Expand Up @@ -160,6 +164,7 @@ private static native long toDecimal(long nativeColumnView, boolean ansi_enabled
private static native long fromDecimal(long nativeColumnView);
private static native long fromFloatWithFormat(long nativeColumnView, int digits);
private static native long fromFloat(long nativeColumnView);
private static native long fromLongToBinary(long nativeColumnView);
private static native long toIntegersWithBase(long nativeColumnView, int base,
boolean ansiEnabled, int dtype);
private static native long fromIntegersWithBase(long nativeColumnView, int base);
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/nvidia/spark/rapids/jni/CastStringsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
* Copyright (c) 2022-2025, 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 @@ -272,6 +272,18 @@ void castToDecimalNoStripTest() {
}
}

@Test
void castFromLongToBinaryStringTest() {
try (ColumnVector v0 = ColumnVector.fromBoxedLongs(null, 0L, 1L, 10L, -1L, Long.MAX_VALUE, Long.MIN_VALUE);
ColumnVector result = CastStrings.fromLongToBinary(v0);
ColumnVector expected = ColumnVector.fromStrings(null, "0", "1", "1010",
"1111111111111111111111111111111111111111111111111111111111111111",
"111111111111111111111111111111111111111111111111111111111111111",
"1000000000000000000000000000000000000000000000000000000000000000")) {
AssertUtils.assertColumnsAreEqual(expected, result);
}
}

private void convTestInternal(Table input, Table expected, int fromBase) {
try(
ColumnVector intCol = CastStrings.toIntegersWithBase(input.getColumn(0), fromBase, false,
Expand Down
Loading