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 JNI backend for Spark SQL function conv for (hexa)decimals #1314

Merged
merged 20 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
75 changes: 75 additions & 0 deletions src/main/cpp/src/CastStringJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
*/

#include "cast_string.hpp"
#include <cudf/column/column_factories.hpp>
#include <cudf/replace.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/strings/extract.hpp>
#include <cudf/strings/convert/convert_integers.hpp>
#include <cudf/strings/strings_column_view.hpp>
#include <cudf/strings/regex/regex_program.hpp>

#include "cudf_jni_apis.hpp"
#include "dtype_utils.hpp"
Expand Down Expand Up @@ -111,4 +118,72 @@ 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_toIntegersWithBase(
JNIEnv* env, jclass, jlong input_column, jint base)
{
JNI_NULL_CHECK(env, input_column, "input column is null", 0);
using namespace cudf;
try {
jni::auto_set_device(env);
auto const res_data_type = data_type(type_id::UINT64);

auto const input_view{*reinterpret_cast<column_view const*>(input_column)};
const strings_column_view input_strings{input_view};
auto integer_view = [&] {
switch (base) {
case 10: {
// TODO implement it in the kernel
auto const regex = strings::regex_program::create(R"(^\s*([0-9]+).*)");
auto const dec_str_table = strings::extract(input_strings, *regex);
const strings_column_view dec_str_view{dec_str_table->get_column(0)};
return strings::to_integers(dec_str_view, res_data_type);
} break;
case 16: {
auto const regex = strings::regex_program::create(R"(^\s*([0-9a-fA-F]+).*)");
auto const hex_str_table = strings::extract(input_strings, *regex);
const strings_column_view hex_str_view{hex_str_table->get_column(0)};
return strings::hex_to_integers(hex_str_view, res_data_type);
}
default: {
auto const error_msg = "Bases supported 10, 16; Actual: " + std::to_string(base);
throw spark_rapids_jni::cast_error(0, error_msg);
}
}
}();

return jni::release_as_jlong(integer_view);
}
CATCH_CAST_EXCEPTION(env, 0);
}

JNIEXPORT jlong JNICALL Java_com_nvidia_spark_rapids_jni_CastStrings_fromIntegersWithBase(
JNIEnv* env, jclass, jlong input_column, jint base)
{
JNI_NULL_CHECK(env, input_column, "input column is null", 0);
using namespace cudf;
try {
jni::auto_set_device(env);
auto input_view{*reinterpret_cast<column_view const*>(input_column)};
auto result = [&] {
switch (base) {
case 10: {
return strings::from_integers(input_view);
} break;
case 16: {
auto pre_res = strings::integers_to_hex(input_view);
auto const regex = strings::regex_program::create("^0?([0-9a-fA-F]+)$");
auto const wo_leading_zeros = strings::extract(strings_column_view(*pre_res), *regex);
return std::move(wo_leading_zeros->release()[0]);
}
default: {
auto const error_msg = "Bases supported 10, 16; Actual: " + std::to_string(base);
throw spark_rapids_jni::cast_error(0, error_msg);
}
}
}();
return jni::release_as_jlong(result);
}
CATCH_CAST_EXCEPTION(env, 0);
}
}
4 changes: 2 additions & 2 deletions src/main/cpp/src/row_conversion.cu
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ struct tile_info {
*
*/
struct row_batch {
size_type num_bytes; // number of bytes in this batch
size_type row_count; // number of rows in the batch
size_type num_bytes; // number of bytes in this batch
size_type row_count; // number of rows in the batch
device_uvector<size_type> row_offsets; // offsets column of output cudf column
};

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/nvidia/spark/rapids/jni/CastStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static ColumnVector toDecimal(ColumnView cv, boolean ansiMode, boolean st

/**
* Convert a decimal column to a string column.
*
*
* @param cv the column data to process
* @return the converted column
*/
Expand All @@ -102,10 +102,21 @@ public static ColumnVector toFloat(ColumnView cv, boolean ansiMode, DType type)
return new ColumnVector(toFloat(cv.getNativeView(), ansiMode, type.getTypeId().getNativeId()));
}


public static ColumnVector toIntegersWithBase(ColumnView cv, int base) {
return new ColumnVector(toIntegersWithBase(cv.getNativeView(), base));
}

public static ColumnVector fromIntegersWithBase(ColumnView cv, int base) {
return new ColumnVector(fromIntegersWithBase(cv.getNativeView(), base));
}

private static native long toInteger(long nativeColumnView, boolean ansi_enabled, boolean strip,
int dtype);
private static native long toDecimal(long nativeColumnView, boolean ansi_enabled, boolean strip,
int precision, int scale);
private static native long toFloat(long nativeColumnView, boolean ansi_enabled, int dtype);
private static native long fromDecimal(long nativeColumnView);
private static native long toIntegersWithBase(long nativeColumnView, int base);
private static native long fromIntegersWithBase(long nativeColumnView, int base);
}