diff --git a/java/src/main/native/include/jni_utils.hpp b/java/src/main/native/include/jni_utils.hpp index d7acaa679f6..317ef152492 100644 --- a/java/src/main/native/include/jni_utils.hpp +++ b/java/src/main/native/include/jni_utils.hpp @@ -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. @@ -15,6 +15,7 @@ */ #pragma once +#include #include #include @@ -68,6 +69,32 @@ inline void check_java_exception(JNIEnv *const env) { } } +/** + * @brief Helper to convert a pointer to a jlong. + * + * This is useful when, for instance, converting a cudf::column pointer + * to a jlong, for use in JNI. + */ +template jlong ptr_as_jlong(T *ptr) { + return reinterpret_cast(ptr); +} + +/** + * @brief Helper to release the data held by a unique_ptr, and return + * the pointer as a jlong. + */ +template jlong release_as_jlong(std::unique_ptr &&ptr) { + return ptr_as_jlong(ptr.release()); +} + +/** + * @brief Helper to release the data held by a unique_ptr, and return + * the pointer as a jlong. + */ +template jlong release_as_jlong(std::unique_ptr &ptr) { + return release_as_jlong(std::move(ptr)); +} + class native_jdoubleArray_accessor { public: jdouble *getArrayElements(JNIEnv *const env, jdoubleArray arr) const { @@ -256,6 +283,19 @@ template class nativ J_ARRAY_TYPE get_jArray() { return orig; } + /** + * @brief Conversion to std::vector + * + * @tparam target_t Target data type + * @return std::vector Vector with the copied contents + */ + template std::vector to_vector() const { + std::vector ret; + ret.reserve(size()); + std::copy(begin(), end(), std::back_inserter(ret)); + return ret; + } + /** * @brief if data has been written back into this array, don't commit * it. @@ -277,11 +317,34 @@ template class nativ ~native_jArray() { commit(); } }; -typedef native_jArray native_jdoubleArray; -typedef native_jArray native_jlongArray; -typedef native_jArray native_jintArray; -typedef native_jArray native_jbyteArray; -typedef native_jArray native_jbooleanArray; +using native_jdoubleArray = native_jArray; +using native_jlongArray = native_jArray; +using native_jintArray = native_jArray; +using native_jbyteArray = native_jArray; + +/** + * @brief Specialization of native_jArray for jboolean + * + * This class adds special support for conversion to std::vector, where the element + * value is chosen depending on the jboolean value. + */ +struct native_jbooleanArray + : native_jArray { + native_jbooleanArray(JNIEnv *const env, jbooleanArray orig) + : native_jArray(env, orig) {} + + native_jbooleanArray(native_jbooleanArray const &) = delete; + native_jbooleanArray &operator=(native_jbooleanArray const &) = delete; + + template + std::vector transform_if_else(target_t const &if_true, target_t const &if_false) const { + std::vector ret; + ret.reserve(size()); + std::transform(begin(), end(), std::back_inserter(ret), + [&](jboolean const &b) { return b ? if_true : if_false; }); + return ret; + } +}; /** * @brief wrapper around native_jlongArray to make it take pointers instead. @@ -336,6 +399,24 @@ template class native_jpointerArray { jlongArray get_jArray() { return wrapped.get_jArray(); } + void assert_no_nulls() const { + if (std::any_of(data(), data() + size(), [](T *const ptr) { return ptr == nullptr; })) { + throw_java_exception(env, NPE_CLASS, "pointer is NULL"); + } + } + + /** + * @brief Convert from `T*[]` to `vector`. + */ + std::vector get_dereferenced() const { + assert_no_nulls(); + auto ret = std::vector{}; + ret.reserve(size()); + std::transform(data(), data() + size(), std::back_inserter(ret), + [](T *const &p) { return *p; }); + return ret; + } + /** * @brief if data has been written back into this array, don't commit * it. diff --git a/java/src/main/native/src/ColumnVectorJni.cpp b/java/src/main/native/src/ColumnVectorJni.cpp index b0286f9ac27..0e559ad0403 100644 --- a/java/src/main/native/src/ColumnVectorJni.cpp +++ b/java/src/main/native/src/ColumnVectorJni.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include + #include #include #include @@ -33,6 +35,10 @@ #include "cudf_jni_apis.hpp" #include "dtype_utils.hpp" +#include "jni_utils.hpp" + +using cudf::jni::ptr_as_jlong; +using cudf::jni::release_as_jlong; extern "C" { @@ -44,13 +50,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_sequence(JNIEnv *env, j cudf::jni::auto_set_device(env); auto initial_val = reinterpret_cast(j_initial_val); auto step = reinterpret_cast(j_step); - std::unique_ptr col; - if (step) { - col = cudf::sequence(row_count, *initial_val, *step); - } else { - col = cudf::sequence(row_count, *initial_val); - } - return reinterpret_cast(col.release()); + return release_as_jlong(step ? cudf::sequence(row_count, *initial_val, *step) : + cudf::sequence(row_count, *initial_val)); } CATCH_STD(env, 0); } @@ -66,13 +67,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_sequences(JNIEnv *env, auto start = reinterpret_cast(j_start_handle); auto size = reinterpret_cast(j_size_handle); auto step = reinterpret_cast(j_step_handle); - std::unique_ptr col; - if (step) { - col = cudf::lists::sequences(*start, *step, *size); - } else { - col = cudf::lists::sequences(*start, *size); - } - return reinterpret_cast(col.release()); + auto ret = + step ? cudf::lists::sequences(*start, *step, *size) : cudf::lists::sequences(*start, *size); + return release_as_jlong(ret); } CATCH_STD(env, 0); } @@ -143,12 +140,11 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_fromArrow( std::shared_ptr schema = std::make_shared(fields); auto arrow_table = arrow::Table::Make(schema, std::vector>{arrow_array}); - std::unique_ptr table_result = cudf::from_arrow(*(arrow_table)); - std::vector> retCols = table_result->release(); + auto retCols = cudf::from_arrow(*(arrow_table))->release(); if (retCols.size() != 1) { JNI_THROW_NEW(env, "java/lang/IllegalArgumentException", "Must result in one column", 0); } - return reinterpret_cast(retCols[0].release()); + return release_as_jlong(retCols[0]); } CATCH_STD(env, 0); } @@ -167,14 +163,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_stringConcatenation( cudf::strings::separator_on_nulls::NO; cudf::jni::native_jpointerArray n_cudf_columns(env, column_handles); - std::vector column_views; - std::transform(n_cudf_columns.data(), n_cudf_columns.data() + n_cudf_columns.size(), - std::back_inserter(column_views), - [](auto const &p_column) { return *p_column; }); - - std::unique_ptr result = cudf::strings::concatenate( - cudf::table_view(column_views), separator_scalar, narep_scalar, null_policy); - return reinterpret_cast(result.release()); + auto column_views = n_cudf_columns.get_dereferenced(); + return release_as_jlong(cudf::strings::concatenate( + cudf::table_view(column_views), separator_scalar, narep_scalar, null_policy)); } CATCH_STD(env, 0); } @@ -194,17 +185,12 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_stringConcatenationSepC cudf::strings::separator_on_nulls::NO; cudf::jni::native_jpointerArray n_cudf_columns(env, column_handles); - std::vector column_views; - std::transform(n_cudf_columns.data(), n_cudf_columns.data() + n_cudf_columns.size(), - std::back_inserter(column_views), - [](auto const &p_column) { return *p_column; }); - + auto column_views = n_cudf_columns.get_dereferenced(); cudf::column_view *column = reinterpret_cast(sep_handle); cudf::strings_column_view strings_column(*column); - std::unique_ptr result = - cudf::strings::concatenate(cudf::table_view(column_views), strings_column, - separator_narep_scalar, col_narep_scalar, null_policy); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::concatenate(cudf::table_view(column_views), + strings_column, separator_narep_scalar, + col_narep_scalar, null_policy)); } CATCH_STD(env, 0); } @@ -219,14 +205,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_concatListByRow(JNIEnv cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW; cudf::jni::native_jpointerArray n_cudf_columns(env, column_handles); - std::vector column_views; - std::transform(n_cudf_columns.data(), n_cudf_columns.data() + n_cudf_columns.size(), - std::back_inserter(column_views), - [](auto const &p_column) { return *p_column; }); - - std::unique_ptr result = - cudf::lists::concatenate_rows(cudf::table_view(column_views), null_policy); - return reinterpret_cast(result.release()); + auto column_views = n_cudf_columns.get_dereferenced(); + return release_as_jlong( + cudf::lists::concatenate_rows(cudf::table_view(column_views), null_policy)); } CATCH_STD(env, 0); } @@ -238,12 +219,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_makeList(JNIEnv *env, j JNI_NULL_CHECK(env, handles, "native view handles are null", 0) try { cudf::jni::auto_set_device(env); - std::unique_ptr ret; - cudf::jni::native_jpointerArray children(env, handles); - std::vector children_vector(children.size()); - for (int i = 0; i < children.size(); i++) { - children_vector[i] = *children[i]; - } + auto children = cudf::jni::native_jpointerArray(env, handles); + auto children_vector = children.get_dereferenced(); auto zero = cudf::make_numeric_scalar(cudf::data_type(cudf::type_id::INT32)); zero->set_valid_async(true); static_cast(zero.get())->set_value(0); @@ -253,8 +230,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_makeList(JNIEnv *env, j auto offsets = cudf::make_column_from_scalar(*zero, row_count + 1); cudf::data_type n_data_type = cudf::jni::make_data_type(j_type, scale); auto empty_col = cudf::make_empty_column(n_data_type); - ret = cudf::make_lists_column(row_count, std::move(offsets), std::move(empty_col), 0, - rmm::device_buffer()); + return release_as_jlong(cudf::make_lists_column( + row_count, std::move(offsets), std::move(empty_col), 0, rmm::device_buffer())); } else { auto count = cudf::make_numeric_scalar(cudf::data_type(cudf::type_id::INT32)); count->set_valid_async(true); @@ -262,11 +239,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_makeList(JNIEnv *env, j std::unique_ptr offsets = cudf::sequence(row_count + 1, *zero, *count); auto data_col = cudf::interleave_columns(cudf::table_view(children_vector)); - ret = cudf::make_lists_column(row_count, std::move(offsets), std::move(data_col), 0, - rmm::device_buffer()); + return release_as_jlong(cudf::make_lists_column( + row_count, std::move(offsets), std::move(data_col), 0, rmm::device_buffer())); } - - return reinterpret_cast(ret.release()); } CATCH_STD(env, 0); } @@ -282,10 +257,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_makeListFromOffsets( CUDF_EXPECTS(offsets_cv->type().id() == cudf::type_id::INT32, "Input offsets does not have type INT32."); - auto result = cudf::make_lists_column(static_cast(row_count), - std::make_unique(*offsets_cv), - std::make_unique(*child_cv), 0, {}); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::make_lists_column( + static_cast(row_count), std::make_unique(*offsets_cv), + std::make_unique(*child_cv), 0, {})); } CATCH_STD(env, 0); } @@ -297,7 +271,6 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_fromScalar(JNIEnv *env, try { cudf::jni::auto_set_device(env); auto scalar_val = reinterpret_cast(j_scalar); - std::unique_ptr col; if (scalar_val->type().id() == cudf::type_id::STRING) { // Tests fail when using the cudf implementation, complaining no child for string column. // So here take care of the String type itself. @@ -309,17 +282,17 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_fromScalar(JNIEnv *env, auto str_col = cudf::make_strings_column(row_count, std::move(offsets), std::move(data), 0, std::move(mask_buffer)); - col = cudf::fill(str_col->view(), 0, row_count, *scalar_val); + return release_as_jlong(cudf::fill(str_col->view(), 0, row_count, *scalar_val)); } else if (scalar_val->type().id() == cudf::type_id::STRUCT && row_count == 0) { // Specialize the creation of empty struct column, since libcudf doesn't support it. auto struct_scalar = reinterpret_cast(j_scalar); auto children = cudf::empty_like(struct_scalar->view())->release(); auto mask_buffer = cudf::create_null_mask(0, cudf::mask_state::UNALLOCATED); - col = cudf::make_structs_column(0, std::move(children), 0, std::move(mask_buffer)); + return release_as_jlong( + cudf::make_structs_column(0, std::move(children), 0, std::move(mask_buffer))); } else { - col = cudf::make_column_from_scalar(*scalar_val, row_count); + return release_as_jlong(cudf::make_column_from_scalar(*scalar_val, row_count)); } - return reinterpret_cast(col.release()); } CATCH_STD(env, 0); } @@ -331,19 +304,11 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_concatenate(JNIEnv *env using cudf::column_view; try { cudf::jni::auto_set_device(env); - cudf::jni::native_jpointerArray columns(env, column_handles); - std::vector columns_vector(columns.size()); - for (int i = 0; i < columns.size(); ++i) { - JNI_NULL_CHECK(env, columns[i], "column to concat is null", 0); - columns_vector[i] = *columns[i]; - } - std::unique_ptr result; - if (columns_vector[0].type().id() == cudf::type_id::LIST) { - result = cudf::lists::detail::concatenate(columns_vector); - } else { - result = cudf::concatenate(columns_vector); - } - return reinterpret_cast(result.release()); + auto columns = + cudf::jni::native_jpointerArray{env, column_handles}.get_dereferenced(); + auto const is_lists_column = columns[0].type().id() == cudf::type_id::LIST; + return release_as_jlong(is_lists_column ? cudf::lists::detail::concatenate(columns) : + cudf::concatenate(columns)); } CATCH_STD(env, 0); } @@ -354,16 +319,10 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_hash(JNIEnv *env, jobje JNI_NULL_CHECK(env, column_handles, "array of column handles is null", 0); try { - cudf::jni::native_jpointerArray n_cudf_columns(env, column_handles); - std::vector column_views; - std::transform(n_cudf_columns.data(), n_cudf_columns.data() + n_cudf_columns.size(), - std::back_inserter(column_views), - [](auto const &p_column) { return *p_column; }); - cudf::table_view input_table{column_views}; - - std::unique_ptr result = - cudf::hash(input_table, static_cast(hash_function_id), seed); - return reinterpret_cast(result.release()); + auto column_views = + cudf::jni::native_jpointerArray{env, column_handles}.get_dereferenced(); + return release_as_jlong(cudf::hash(cudf::table_view{column_views}, + static_cast(hash_function_id), seed)); } CATCH_STD(env, 0); } @@ -405,9 +364,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_getNativeColumnView(JNI try { cudf::jni::auto_set_device(env); cudf::column *column = reinterpret_cast(handle); - std::unique_ptr view(new cudf::column_view()); - *view.get() = column->view(); - return reinterpret_cast(view.release()); + return ptr_as_jlong(new cudf::column_view{*column}); } CATCH_STD(env, 0); } @@ -419,9 +376,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnVector_makeEmptyCudfColumn(JNI try { cudf::jni::auto_set_device(env); cudf::data_type n_data_type = cudf::jni::make_data_type(j_type, scale); - - std::unique_ptr column(cudf::make_empty_column(n_data_type)); - return reinterpret_cast(column.release()); + return release_as_jlong(cudf::make_empty_column(n_data_type)); } CATCH_STD(env, 0); } diff --git a/java/src/main/native/src/ColumnViewJni.cpp b/java/src/main/native/src/ColumnViewJni.cpp index d2cc2ab7d1c..38c6bb3740e 100644 --- a/java/src/main/native/src/ColumnViewJni.cpp +++ b/java/src/main/native/src/ColumnViewJni.cpp @@ -75,6 +75,9 @@ #include "jni_utils.hpp" #include "map_lookup.hpp" +using cudf::jni::ptr_as_jlong; +using cudf::jni::release_as_jlong; + namespace { std::size_t calc_device_memory_size(cudf::column_view const &view) { @@ -107,9 +110,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_upperStrings(JNIEnv *env, cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); cudf::strings_column_view strings_column(*column); - - std::unique_ptr result = cudf::strings::to_upper(strings_column); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::to_upper(strings_column)); } CATCH_STD(env, 0); } @@ -122,9 +123,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_lowerStrings(JNIEnv *env, cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); cudf::strings_column_view strings_column(*column); - - std::unique_ptr result = cudf::strings::to_lower(strings_column); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::to_lower(strings_column)); } CATCH_STD(env, 0); } @@ -138,8 +137,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_replaceNullsScalar(JNIEnv cudf::jni::auto_set_device(env); cudf::column_view col = *reinterpret_cast(j_col); auto val = reinterpret_cast(j_scalar); - std::unique_ptr result = cudf::replace_nulls(col, *val); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::replace_nulls(col, *val)); } CATCH_STD(env, 0); } @@ -153,8 +151,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_replaceNullsColumn(JNIEnv cudf::jni::auto_set_device(env); auto col = reinterpret_cast(j_col); auto replacements = reinterpret_cast(j_replace_col); - std::unique_ptr result = cudf::replace_nulls(*col, *replacements); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::replace_nulls(*col, *replacements)); } CATCH_STD(env, 0); } @@ -166,9 +163,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_replaceNullsPolicy(JNIEnv try { cudf::jni::auto_set_device(env); cudf::column_view col = *reinterpret_cast(j_col); - std::unique_ptr result = cudf::replace_nulls( - col, is_preceding ? cudf::replace_policy::PRECEDING : cudf::replace_policy::FOLLOWING); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::replace_nulls( + col, is_preceding ? cudf::replace_policy::PRECEDING : cudf::replace_policy::FOLLOWING)); } CATCH_STD(env, 0); } @@ -184,8 +180,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_ifElseVV(JNIEnv *env, jcl auto pred_vec = reinterpret_cast(j_pred_vec); auto true_vec = reinterpret_cast(j_true_vec); auto false_vec = reinterpret_cast(j_false_vec); - std::unique_ptr result = cudf::copy_if_else(*true_vec, *false_vec, *pred_vec); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::copy_if_else(*true_vec, *false_vec, *pred_vec)); } CATCH_STD(env, 0); } @@ -201,8 +196,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_ifElseVS(JNIEnv *env, jcl auto pred_vec = reinterpret_cast(j_pred_vec); auto true_vec = reinterpret_cast(j_true_vec); auto false_scalar = reinterpret_cast(j_false_scalar); - std::unique_ptr result = cudf::copy_if_else(*true_vec, *false_scalar, *pred_vec); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::copy_if_else(*true_vec, *false_scalar, *pred_vec)); } CATCH_STD(env, 0); } @@ -219,8 +213,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_ifElseSV(JNIEnv *env, jcl auto pred_vec = reinterpret_cast(j_pred_vec); auto true_scalar = reinterpret_cast(j_true_scalar); auto false_vec = reinterpret_cast(j_false_vec); - std::unique_ptr result = cudf::copy_if_else(*true_scalar, *false_vec, *pred_vec); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::copy_if_else(*true_scalar, *false_vec, *pred_vec)); } CATCH_STD(env, 0); } @@ -237,9 +230,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_ifElseSS(JNIEnv *env, jcl auto pred_vec = reinterpret_cast(j_pred_vec); auto true_scalar = reinterpret_cast(j_true_scalar); auto false_scalar = reinterpret_cast(j_false_scalar); - std::unique_ptr result = - cudf::copy_if_else(*true_scalar, *false_scalar, *pred_vec); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::copy_if_else(*true_scalar, *false_scalar, *pred_vec)); } CATCH_STD(env, 0); } @@ -250,8 +241,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_getElement(JNIEnv *env, j try { cudf::jni::auto_set_device(env); auto from_vec = reinterpret_cast(from); - std::unique_ptr result = cudf::get_element(*from_vec, index); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::get_element(*from_vec, index)); } CATCH_STD(env, 0); } @@ -266,9 +256,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_reduce(JNIEnv *env, jclas auto col = reinterpret_cast(j_col_view); auto agg = reinterpret_cast(j_agg); cudf::data_type out_dtype = cudf::jni::make_data_type(j_dtype, scale); - - std::unique_ptr result = cudf::reduce(*col, agg->clone(), out_dtype); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::reduce(*col, agg->clone(), out_dtype)); } CATCH_STD(env, 0); } @@ -282,11 +270,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_scan(JNIEnv *env, jclass, cudf::jni::auto_set_device(env); auto col = reinterpret_cast(j_col_view); auto agg = reinterpret_cast(j_agg); - - std::unique_ptr result = cudf::scan( - *col, agg->clone(), is_inclusive ? cudf::scan_type::INCLUSIVE : cudf::scan_type::EXCLUSIVE, - include_nulls ? cudf::null_policy::INCLUDE : cudf::null_policy::EXCLUDE); - return reinterpret_cast(result.release()); + auto scan_type = is_inclusive ? cudf::scan_type::INCLUSIVE : cudf::scan_type::EXCLUSIVE; + auto null_policy = include_nulls ? cudf::null_policy::INCLUDE : cudf::null_policy::EXCLUDE; + return release_as_jlong(cudf::scan(*col, agg->clone(), scan_type, null_policy)); } CATCH_STD(env, 0); } @@ -303,8 +289,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_approxPercentile(JNIEnv * auto const tdigest_view = tdigest_column_view{structs_column_view{*reinterpret_cast(input_column)}}; auto const p_percentiles = reinterpret_cast(percentiles_column); - auto result = percentile_approx(tdigest_view, *p_percentiles); - return reinterpret_cast(result.release()); + return release_as_jlong(percentile_approx(tdigest_view, *p_percentiles)); } CATCH_STD(env, 0); } @@ -321,9 +306,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_quantile(JNIEnv *env, jcl native_quantiles.data() + native_quantiles.size()); cudf::column_view *n_input_column = reinterpret_cast(input_column); cudf::interpolation n_quantile_method = static_cast(quantile_method); - std::unique_ptr result = - cudf::quantile(*n_input_column, quantiles, n_quantile_method); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::quantile(*n_input_column, quantiles, n_quantile_method)); } CATCH_STD(env, 0); } @@ -365,7 +348,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_rollingWindow( ret = cudf::rolling_window(*n_input_col, preceding, following, min_periods, *agg); } } - return reinterpret_cast(ret.release()); + return release_as_jlong(ret); } CATCH_STD(env, 0); } @@ -380,22 +363,16 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_ColumnView_slice(JNIEnv *env, j cudf::jni::auto_set_device(env); cudf::column_view *n_column = reinterpret_cast(input_column); cudf::jni::native_jintArray n_slice_indices(env, slice_indices); - - std::vector indices(n_slice_indices.size()); - for (int i = 0; i < n_slice_indices.size(); i++) { - indices[i] = n_slice_indices[i]; - } + std::vector indices(n_slice_indices.begin(), n_slice_indices.end()); std::vector result = cudf::slice(*n_column, indices); cudf::jni::native_jlongArray n_result(env, result.size()); - std::vector> column_result(result.size()); - for (size_t i = 0; i < result.size(); i++) { - column_result[i].reset(new cudf::column(result[i])); - n_result[i] = reinterpret_cast(column_result[i].get()); - } - for (size_t i = 0; i < result.size(); i++) { - column_result[i].release(); - } + + std::transform(result.begin(), result.end(), n_result.begin(), + [](cudf::column_view const &result_col) { + return ptr_as_jlong(new cudf::column{result_col}); + }); + return n_result.get_jArray(); } CATCH_STD(env, NULL); @@ -409,9 +386,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_extractListElement(JNIEnv cudf::jni::auto_set_device(env); cudf::column_view *cv = reinterpret_cast(column_view); cudf::lists_column_view lcv(*cv); - - std::unique_ptr ret = cudf::lists::extract_list_element(lcv, index); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::lists::extract_list_element(lcv, index)); } CATCH_STD(env, 0); } @@ -423,9 +398,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_dropListDuplicates(JNIEnv cudf::jni::auto_set_device(env); cudf::column_view const *cv = reinterpret_cast(column_view); cudf::lists_column_view lcv(*cv); - - std::unique_ptr ret = cudf::lists::drop_list_duplicates(lcv); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::lists::drop_list_duplicates(lcv)); } CATCH_STD(env, 0); } @@ -486,10 +459,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_dropListDuplicatesWithKey auto out_structs = cudf::make_structs_column(out_child_size, std::move(out_structs_members), 0, {}); - auto result = cudf::make_lists_column(input_cv->size(), std::move(out_offsets), - std::move(out_structs), 0, {}); - - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::make_lists_column(input_cv->size(), std::move(out_offsets), + std::move(out_structs), 0, {})); } CATCH_STD(env, 0); } @@ -504,9 +475,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_listContains(JNIEnv *env, cudf::column_view *cv = reinterpret_cast(column_view); cudf::lists_column_view lcv(*cv); cudf::scalar *lookup_scalar = reinterpret_cast(lookup_key); - - std::unique_ptr ret = cudf::lists::contains(lcv, *lookup_scalar); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::lists::contains(lcv, *lookup_scalar)); } CATCH_STD(env, 0); } @@ -518,7 +487,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_listContainsNulls(JNIEnv cudf::jni::auto_set_device(env); auto cv = reinterpret_cast(column_view); auto lcv = cudf::lists_column_view{*cv}; - return reinterpret_cast(cudf::lists::contains_nulls(lcv).release()); + return release_as_jlong(cudf::lists::contains_nulls(lcv)); } CATCH_STD(env, 0); } @@ -533,9 +502,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_listContainsColumn(JNIEnv cudf::column_view *cv = reinterpret_cast(column_view); cudf::lists_column_view lcv(*cv); cudf::column_view *lookup_cv = reinterpret_cast(lookup_key_cv); - - std::unique_ptr ret = cudf::lists::contains(lcv, *lookup_cv); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::lists::contains(lcv, *lookup_cv)); } CATCH_STD(env, 0); } @@ -553,8 +520,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_listIndexOfScalar(JNIEnv auto const lookup_key_scalar = reinterpret_cast(lookup_key); auto const find_option = is_find_first ? cudf::lists::duplicate_find_option::FIND_FIRST : cudf::lists::duplicate_find_option::FIND_LAST; - auto result = cudf::lists::index_of(lcv, *lookup_key_scalar, find_option); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::lists::index_of(lcv, *lookup_key_scalar, find_option)); } CATCH_STD(env, 0); } @@ -572,8 +538,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_listIndexOfColumn(JNIEnv auto const lookup_key_column = reinterpret_cast(lookup_keys); auto const find_option = is_find_first ? cudf::lists::duplicate_find_option::FIND_FIRST : cudf::lists::duplicate_find_option::FIND_LAST; - auto result = cudf::lists::index_of(lcv, *lookup_key_column, find_option); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::lists::index_of(lcv, *lookup_key_column, find_option)); } CATCH_STD(env, 0); } @@ -588,8 +553,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_listSortRows(JNIEnv *env, auto sort_order = is_descending ? cudf::order::DESCENDING : cudf::order::ASCENDING; auto null_order = is_null_smallest ? cudf::null_order::BEFORE : cudf::null_order::AFTER; auto *cv = reinterpret_cast(column_view); - auto ret = cudf::lists::sort_lists(cudf::lists_column_view(*cv), sort_order, null_order); - return reinterpret_cast(ret.release()); + return release_as_jlong( + cudf::lists::sort_lists(cudf::lists_column_view(*cv), sort_order, null_order)); } CATCH_STD(env, 0); } @@ -622,9 +587,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringSplitRecord(JNIEnv cudf::column_view *cv = reinterpret_cast(column_view); cudf::strings_column_view scv(*cv); cudf::string_scalar *ss_scalar = reinterpret_cast(delimiter); - - std::unique_ptr ret = cudf::strings::split_record(scv, *ss_scalar, max_split); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::strings::split_record(scv, *ss_scalar, max_split)); } CATCH_STD(env, 0); } @@ -639,19 +602,16 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_ColumnView_split(JNIEnv *env, j cudf::jni::auto_set_device(env); cudf::column_view *n_column = reinterpret_cast(input_column); cudf::jni::native_jintArray n_split_indices(env, split_indices); - - std::vector indices(n_split_indices.size()); - for (int i = 0; i < n_split_indices.size(); i++) { - indices[i] = n_split_indices[i]; - } + std::vector indices(n_split_indices.begin(), n_split_indices.end()); std::vector result = cudf::split(*n_column, indices); - cudf::jni::native_jlongArray n_result(env, result.size()); - for (size_t i = 0; i < result.size(); i++) { - cudf::column_view const *c = new cudf::column_view(result[i]); - n_result[i] = reinterpret_cast(c); - } + + std::transform(result.begin(), result.end(), n_result.begin(), + [](cudf::column_view const &result_col) { + return ptr_as_jlong(new cudf::column_view{result_col}); + }); + return n_result.get_jArray(); } CATCH_STD(env, NULL); @@ -663,9 +623,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_countElements(JNIEnv *env try { cudf::jni::auto_set_device(env); cudf::column_view *n_column = reinterpret_cast(view_handle); - std::unique_ptr result = - cudf::lists::count_elements(cudf::lists_column_view(*n_column)); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::lists::count_elements(cudf::lists_column_view(*n_column))); } CATCH_STD(env, 0); } @@ -676,9 +634,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_charLengths(JNIEnv *env, try { cudf::jni::auto_set_device(env); cudf::column_view *n_column = reinterpret_cast(view_handle); - std::unique_ptr result = - cudf::strings::count_characters(cudf::strings_column_view(*n_column)); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::count_characters(cudf::strings_column_view(*n_column))); } CATCH_STD(env, 0); } @@ -689,9 +645,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_byteCount(JNIEnv *env, jc try { cudf::jni::auto_set_device(env); cudf::column_view *n_column = reinterpret_cast(view_handle); - std::unique_ptr result = - cudf::strings::count_bytes(cudf::strings_column_view(*n_column)); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::count_bytes(cudf::strings_column_view(*n_column))); } CATCH_STD(env, 0); } @@ -712,11 +666,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_findAndReplaceAll(JNIEnv column_view *input_column = reinterpret_cast(input_handle); column_view *old_values_column = reinterpret_cast(old_values_handle); column_view *new_values_column = reinterpret_cast(new_values_handle); - - std::unique_ptr result = - cudf::find_and_replace_all(*input_column, *old_values_column, *new_values_column); - - return reinterpret_cast(result.release()); + return release_as_jlong( + cudf::find_and_replace_all(*input_column, *old_values_column, *new_values_column)); } CATCH_STD(env, 0); } @@ -727,8 +678,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isNullNative(JNIEnv *env, try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(handle); - std::unique_ptr ret = cudf::is_null(*input); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::is_null(*input)); } CATCH_STD(env, 0); } @@ -739,8 +689,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isNotNullNative(JNIEnv *e try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(handle); - std::unique_ptr ret = cudf::is_valid(*input); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::is_valid(*input)); } CATCH_STD(env, 0); } @@ -751,8 +700,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isNanNative(JNIEnv *env, try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(handle); - std::unique_ptr ret = cudf::is_nan(*input); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::is_nan(*input)); } CATCH_STD(env, 0); } @@ -763,8 +711,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isNotNanNative(JNIEnv *en try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(handle); - std::unique_ptr ret = cudf::is_not_nan(*input); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::is_not_nan(*input)); } CATCH_STD(env, 0); } @@ -777,8 +724,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_unaryOperation(JNIEnv *en cudf::jni::auto_set_device(env); cudf::column_view *input = reinterpret_cast(input_ptr); cudf::unary_operator op = static_cast(int_op); - std::unique_ptr ret = cudf::unary_operation(*input, op); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::unary_operation(*input, op)); } CATCH_STD(env, 0); } @@ -791,8 +737,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_round(JNIEnv *env, jclass cudf::jni::auto_set_device(env); cudf::column_view *input = reinterpret_cast(input_ptr); cudf::rounding_method method = static_cast(rounding_method); - std::unique_ptr ret = cudf::round(*input, decimal_places, method); - return reinterpret_cast(ret.release()); + return release_as_jlong(cudf::round(*input, decimal_places, method)); } CATCH_STD(env, 0); } @@ -802,8 +747,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_year(JNIEnv *env, jclass, try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_year(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_year(*input)); } CATCH_STD(env, 0); } @@ -813,8 +757,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_month(JNIEnv *env, jclass try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_month(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_month(*input)); } CATCH_STD(env, 0); } @@ -824,8 +767,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_day(JNIEnv *env, jclass, try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_day(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_day(*input)); } CATCH_STD(env, 0); } @@ -835,8 +777,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_hour(JNIEnv *env, jclass, try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_hour(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_hour(*input)); } CATCH_STD(env, 0); } @@ -847,8 +788,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_minute(JNIEnv *env, jclas try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_minute(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_minute(*input)); } CATCH_STD(env, 0); } @@ -859,8 +799,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_second(JNIEnv *env, jclas try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_second(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_second(*input)); } CATCH_STD(env, 0); } @@ -871,8 +810,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_weekDay(JNIEnv *env, jcla try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_weekday(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_weekday(*input)); } CATCH_STD(env, 0); } @@ -883,8 +821,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_lastDayOfMonth(JNIEnv *en try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::last_day_of_month(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::last_day_of_month(*input)); } CATCH_STD(env, 0); } @@ -895,8 +832,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_dayOfYear(JNIEnv *env, jc try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::day_of_year(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::day_of_year(*input)); } CATCH_STD(env, 0); } @@ -907,8 +843,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_quarterOfYear(JNIEnv *env try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::extract_quarter(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::extract_quarter(*input)); } CATCH_STD(env, 0); } @@ -922,8 +857,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_addCalendricalMonths(JNIE cudf::jni::auto_set_device(env); const cudf::column_view *ts = reinterpret_cast(ts_ptr); const cudf::column_view *months = reinterpret_cast(months_ptr); - std::unique_ptr output = cudf::datetime::add_calendrical_months(*ts, *months); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::add_calendrical_months(*ts, *months)); } CATCH_STD(env, 0); } @@ -934,8 +868,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isLeapYear(JNIEnv *env, j try { cudf::jni::auto_set_device(env); const cudf::column_view *input = reinterpret_cast(input_ptr); - std::unique_ptr output = cudf::datetime::is_leap_year(*input); - return reinterpret_cast(output.release()); + return release_as_jlong(cudf::datetime::is_leap_year(*input)); } CATCH_STD(env, 0); } @@ -947,16 +880,14 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_castTo(JNIEnv *env, jclas cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); cudf::data_type n_data_type = cudf::jni::make_data_type(type, scale); - std::unique_ptr result; if (n_data_type == column->type()) { - std::unique_ptr copy(new cudf::column(*column)); - return reinterpret_cast(copy.release()); + return ptr_as_jlong(new cudf::column(*column)); } if (n_data_type.id() == cudf::type_id::STRING) { switch (column->type().id()) { - case cudf::type_id::BOOL8: result = cudf::strings::from_booleans(*column); break; + case cudf::type_id::BOOL8: return release_as_jlong(cudf::strings::from_booleans(*column)); case cudf::type_id::FLOAT32: - case cudf::type_id::FLOAT64: result = cudf::strings::from_floats(*column); break; + case cudf::type_id::FLOAT64: return release_as_jlong(cudf::strings::from_floats(*column)); case cudf::type_id::INT8: case cudf::type_id::UINT8: case cudf::type_id::INT16: @@ -964,17 +895,19 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_castTo(JNIEnv *env, jclas case cudf::type_id::INT32: case cudf::type_id::UINT32: case cudf::type_id::INT64: - case cudf::type_id::UINT64: result = cudf::strings::from_integers(*column); break; + case cudf::type_id::UINT64: return release_as_jlong(cudf::strings::from_integers(*column)); case cudf::type_id::DECIMAL32: case cudf::type_id::DECIMAL64: - case cudf::type_id::DECIMAL128: result = cudf::strings::from_fixed_point(*column); break; + case cudf::type_id::DECIMAL128: + return release_as_jlong(cudf::strings::from_fixed_point(*column)); default: JNI_THROW_NEW(env, "java/lang/IllegalArgumentException", "Invalid data type", 0); } } else if (column->type().id() == cudf::type_id::STRING) { switch (n_data_type.id()) { - case cudf::type_id::BOOL8: result = cudf::strings::to_booleans(*column); break; + case cudf::type_id::BOOL8: return release_as_jlong(cudf::strings::to_booleans(*column)); case cudf::type_id::FLOAT32: - case cudf::type_id::FLOAT64: result = cudf::strings::to_floats(*column, n_data_type); break; + case cudf::type_id::FLOAT64: + return release_as_jlong(cudf::strings::to_floats(*column, n_data_type)); case cudf::type_id::INT8: case cudf::type_id::UINT8: case cudf::type_id::INT16: @@ -983,13 +916,11 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_castTo(JNIEnv *env, jclas case cudf::type_id::UINT32: case cudf::type_id::INT64: case cudf::type_id::UINT64: - result = cudf::strings::to_integers(*column, n_data_type); - break; + return release_as_jlong(cudf::strings::to_integers(*column, n_data_type)); case cudf::type_id::DECIMAL32: case cudf::type_id::DECIMAL64: case cudf::type_id::DECIMAL128: - result = cudf::strings::to_fixed_point(*column, n_data_type); - break; + return release_as_jlong(cudf::strings::to_fixed_point(*column, n_data_type)); default: JNI_THROW_NEW(env, "java/lang/IllegalArgumentException", "Invalid data type", 0); } } else if (cudf::is_timestamp(n_data_type) && cudf::is_numeric(column->type())) { @@ -1010,7 +941,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_castTo(JNIEnv *env, jclas cudf::data_type duration_type = cudf::jni::timestamp_to_duration(n_data_type); cudf::column_view duration_view = cudf::column_view( duration_type, column->size(), column->head(), column->null_mask(), column->null_count()); - result = cudf::cast(duration_view, n_data_type); + return release_as_jlong(cudf::cast(duration_view, n_data_type)); } else if (cudf::is_timestamp(column->type()) && cudf::is_numeric(n_data_type)) { // This is a temporary workaround to allow Java to cast from timestamp types to integral types // without forcing an intermediate duration column to be manifested. Ultimately this style of @@ -1018,11 +949,10 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_castTo(JNIEnv *env, jclas cudf::data_type duration_type = cudf::jni::timestamp_to_duration(column->type()); cudf::column_view duration_view = cudf::column_view( duration_type, column->size(), column->head(), column->null_mask(), column->null_count()); - result = cudf::cast(duration_view, n_data_type); + return release_as_jlong(cudf::cast(duration_view, n_data_type)); } else { - result = cudf::cast(*column, n_data_type); + return release_as_jlong(cudf::cast(*column, n_data_type)); } - return reinterpret_cast(result.release()); } CATCH_STD(env, 0); } @@ -1034,9 +964,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_bitCastTo(JNIEnv *env, jc cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); cudf::data_type n_data_type = cudf::jni::make_data_type(type, scale); - std::unique_ptr result = std::make_unique(); - *result = cudf::bit_cast(*column, n_data_type); - return reinterpret_cast(result.release()); + return ptr_as_jlong(new cudf::column_view{cudf::bit_cast(*column, n_data_type)}); } CATCH_STD(env, 0); } @@ -1049,8 +977,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_byteListCast(JNIEnv *env, cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); cudf::flip_endianness config(static_cast(endianness_config)); - std::unique_ptr result = byte_cast(*column, config); - return reinterpret_cast(result.release()); + return release_as_jlong(byte_cast(*column, config)); } CATCH_STD(env, 0); } @@ -1066,9 +993,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringTimestampToTimestam cudf::column_view *column = reinterpret_cast(handle); cudf::strings_column_view strings_column(*column); - std::unique_ptr result = cudf::strings::to_timestamps( - strings_column, cudf::data_type(static_cast(time_unit)), format.get()); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::to_timestamps( + strings_column, cudf::data_type(static_cast(time_unit)), format.get())); } CATCH_STD(env, 0); } @@ -1084,10 +1010,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isTimestamp(JNIEnv *env, cudf::jni::native_jstring format(env, formatObj); cudf::column_view *column = reinterpret_cast(handle); cudf::strings_column_view strings_column(*column); - - std::unique_ptr result = - cudf::strings::is_timestamp(strings_column, format.get()); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::is_timestamp(strings_column, format.get())); } CATCH_STD(env, 0); } @@ -1101,9 +1024,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_timestampToStringTimestam cudf::jni::auto_set_device(env); cudf::jni::native_jstring format(env, j_format); cudf::column_view *column = reinterpret_cast(handle); - - std::unique_ptr result = cudf::strings::from_timestamps(*column, format.get()); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::from_timestamps(*column, format.get())); } CATCH_STD(env, 0); } @@ -1133,9 +1054,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_containsVector(JNIEnv *en cudf::jni::auto_set_device(env); cudf::column_view *haystack = reinterpret_cast(j_haystack_handle); cudf::column_view *needle = reinterpret_cast(j_needle_handle); - - std::unique_ptr result = std::move(cudf::contains(*haystack, *needle)); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::contains(*haystack, *needle)); } CATCH_STD(env, 0); } @@ -1148,9 +1067,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_transform(JNIEnv *env, jo cudf::column_view *column = reinterpret_cast(handle); cudf::jni::native_jstring n_j_udf(env, j_udf); std::string n_udf(n_j_udf.get()); - std::unique_ptr result = - cudf::transform(*column, n_udf, cudf::data_type(cudf::type_id::INT32), j_is_ptx); - return reinterpret_cast(result.release()); + return release_as_jlong( + cudf::transform(*column, n_udf, cudf::data_type(cudf::type_id::INT32), j_is_ptx)); } CATCH_STD(env, 0); } @@ -1167,9 +1085,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringStartWith(JNIEnv *e cudf::column_view *column_view = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_column(*column_view); cudf::string_scalar *comp_scalar = reinterpret_cast(comp_string); - - std::unique_ptr result = cudf::strings::starts_with(strings_column, *comp_scalar); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::starts_with(strings_column, *comp_scalar)); } CATCH_STD(env, 0); } @@ -1185,9 +1101,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringEndWith(JNIEnv *env cudf::column_view *column_view = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_column(*column_view); cudf::string_scalar *comp_scalar = reinterpret_cast(comp_string); - - std::unique_ptr result = cudf::strings::ends_with(strings_column, *comp_scalar); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::ends_with(strings_column, *comp_scalar)); } CATCH_STD(env, 0); } @@ -1203,9 +1117,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringContains(JNIEnv *en cudf::column_view *column_view = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_column(*column_view); cudf::string_scalar *comp_scalar = reinterpret_cast(comp_string); - - std::unique_ptr result = cudf::strings::contains(strings_column, *comp_scalar); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::contains(strings_column, *comp_scalar)); } CATCH_STD(env, 0); } @@ -1221,9 +1133,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_matchesRe(JNIEnv *env, jo cudf::column_view *column_view = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_column(*column_view); cudf::jni::native_jstring pattern(env, patternObj); - - std::unique_ptr result = cudf::strings::matches_re(strings_column, pattern.get()); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::matches_re(strings_column, pattern.get())); } CATCH_STD(env, 0); } @@ -1239,10 +1149,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_containsRe(JNIEnv *env, j cudf::column_view *column_view = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_column(*column_view); cudf::jni::native_jstring pattern(env, patternObj); - - std::unique_ptr result = - cudf::strings::contains_re(strings_column, pattern.get()); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::contains_re(strings_column, pattern.get())); } CATCH_STD(env, 0); } @@ -1260,8 +1167,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_binaryOpVV(JNIEnv *env, j cudf::data_type n_data_type = cudf::jni::make_data_type(out_dtype, scale); cudf::binary_operator op = static_cast(int_op); - std::unique_ptr result = cudf::binary_operation(*lhs, *rhs, op, n_data_type); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::binary_operation(*lhs, *rhs, op, n_data_type)); } CATCH_STD(env, 0); } @@ -1291,8 +1197,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_binaryOpVS(JNIEnv *env, j cudf::data_type n_data_type = cudf::jni::make_data_type(out_dtype, scale); cudf::binary_operator op = static_cast(int_op); - std::unique_ptr result = cudf::binary_operation(*lhs, *rhs, op, n_data_type); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::binary_operation(*lhs, *rhs, op, n_data_type)); } CATCH_STD(env, 0); } @@ -1305,11 +1210,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_substring(JNIEnv *env, jc cudf::jni::auto_set_device(env); cudf::column_view *cv = reinterpret_cast(column_view); cudf::strings_column_view scv(*cv); - - std::unique_ptr result = - (end == -1 ? cudf::strings::slice_strings(scv, start) : - cudf::strings::slice_strings(scv, start, end)); - return reinterpret_cast(result.release()); + return release_as_jlong((end == -1 ? cudf::strings::slice_strings(scv, start) : + cudf::strings::slice_strings(scv, start, end))); } CATCH_STD(env, 0); } @@ -1327,9 +1229,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_substringColumn(JNIEnv *e cudf::strings_column_view scv(*cv); cudf::column_view *sc = reinterpret_cast(start_column); cudf::column_view *ec = reinterpret_cast(end_column); - - std::unique_ptr result = cudf::strings::slice_strings(scv, *sc, *ec); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::slice_strings(scv, *sc, *ec)); } CATCH_STD(env, 0); } @@ -1345,9 +1245,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_substringLocate(JNIEnv *e cudf::column_view *cv = reinterpret_cast(column_view); cudf::strings_column_view scv(*cv); cudf::string_scalar *ss_scalar = reinterpret_cast(substring); - - std::unique_ptr result = cudf::strings::find(scv, *ss_scalar, start, end); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::find(scv, *ss_scalar, start, end)); } CATCH_STD(env, 0); } @@ -1364,9 +1262,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringReplace(JNIEnv *env cudf::strings_column_view scv(*cv); cudf::string_scalar *ss_target = reinterpret_cast(target); cudf::string_scalar *ss_replace = reinterpret_cast(replace); - - std::unique_ptr result = cudf::strings::replace(scv, *ss_target, *ss_replace); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::replace(scv, *ss_target, *ss_replace)); } CATCH_STD(env, 0); } @@ -1380,9 +1276,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_mapLookup(JNIEnv *env, jc cudf::jni::auto_set_device(env); cudf::column_view *cv = reinterpret_cast(map_column_view); cudf::string_scalar *ss_key = reinterpret_cast(lookup_key); - - std::unique_ptr result = cudf::jni::map_lookup(*cv, *ss_key); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::jni::map_lookup(*cv, *ss_key)); } CATCH_STD(env, 0); } @@ -1396,9 +1290,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_mapContains(JNIEnv *env, cudf::jni::auto_set_device(env); cudf::column_view *cv = reinterpret_cast(map_column_view); cudf::string_scalar *ss_key = reinterpret_cast(lookup_key); - - std::unique_ptr result = cudf::jni::map_contains(*cv, *ss_key); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::jni::map_contains(*cv, *ss_key)); } CATCH_STD(env, 0); } @@ -1417,10 +1309,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_replaceRegex(JNIEnv *env, cudf::strings_column_view scv(*cv); cudf::jni::native_jstring pattern(env, j_pattern); auto repl = reinterpret_cast(j_repl); - - std::unique_ptr result = - cudf::strings::replace_re(scv, pattern.get(), *repl, j_maxrepl); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::replace_re(scv, pattern.get(), *repl, j_maxrepl)); } CATCH_STD(env, 0); } @@ -1440,10 +1329,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_replaceMultiRegex(JNIEnv cudf::jni::native_jstringArray patterns(env, j_patterns); auto repl_cv = reinterpret_cast(j_repls); cudf::strings_column_view repl_scv(*repl_cv); - - std::unique_ptr result = - cudf::strings::replace_re(scv, patterns.as_cpp_vector(), repl_scv); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::replace_re(scv, patterns.as_cpp_vector(), repl_scv)); } CATCH_STD(env, 0); } @@ -1460,10 +1346,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringReplaceWithBackrefs cudf::strings_column_view scv(*cv); cudf::jni::native_jstring ss_pattern(env, patternObj); cudf::jni::native_jstring ss_replace(env, replaceObj); - - std::unique_ptr result = - cudf::strings::replace_with_backrefs(scv, ss_pattern.get(), ss_replace.get()); - return reinterpret_cast(result.release()); + return release_as_jlong( + cudf::strings::replace_with_backrefs(scv, ss_pattern.get(), ss_replace.get())); } CATCH_STD(env, 0); } @@ -1477,9 +1361,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_zfill(JNIEnv *env, jclass cudf::column_view *cv = reinterpret_cast(column_view); cudf::strings_column_view scv(*cv); cudf::size_type width = reinterpret_cast(j_width); - - std::unique_ptr result = cudf::strings::zfill(scv, width); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::zfill(scv, width)); } CATCH_STD(env, 0); } @@ -1497,9 +1379,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_pad(JNIEnv *env, jclass, cudf::size_type width = reinterpret_cast(j_width); cudf::strings::pad_side side = static_cast(j_side); cudf::jni::native_jstring ss_fill(env, fill_char); - - std::unique_ptr result = cudf::strings::pad(scv, width, side, ss_fill.get()); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::pad(scv, width, side, ss_fill.get())); } CATCH_STD(env, 0); } @@ -1516,9 +1396,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringStrip(JNIEnv *env, cudf::strings_column_view scv(*cv); cudf::strings::strip_type s_striptype = static_cast(strip_type); cudf::string_scalar *ss_tostrip = reinterpret_cast(to_strip); - - std::unique_ptr result = cudf::strings::strip(scv, s_striptype, *ss_tostrip); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::strip(scv, s_striptype, *ss_tostrip)); } CATCH_STD(env, 0); } @@ -1550,8 +1428,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_urlDecode(JNIEnv *env, jc cudf::jni::auto_set_device(env); auto view_ptr = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_view(*view_ptr); - auto result = cudf::strings::url_decode(strings_view); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::url_decode(strings_view)); } CATCH_STD(env, 0); } @@ -1564,8 +1441,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_urlEncode(JNIEnv *env, jc cudf::jni::auto_set_device(env); auto view_ptr = reinterpret_cast(j_view_handle); cudf::strings_column_view strings_view(*view_ptr); - auto result = cudf::strings::url_encode(strings_view); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::url_encode(strings_view)); } CATCH_STD(env, 0); } @@ -1578,8 +1454,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_normalizeNANsAndZeros(JNI JNI_NULL_CHECK(env, input_column, "Input column is null", 0); try { cudf::jni::auto_set_device(env); - return reinterpret_cast( - cudf::normalize_nans_and_zeros(*reinterpret_cast(input_column)).release()); + return release_as_jlong( + cudf::normalize_nans_and_zeros(*reinterpret_cast(input_column))); } CATCH_STD(env, 0); } @@ -1595,17 +1471,11 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_bitwiseMergeAndSetValidit cudf::jni::native_jpointerArray n_cudf_columns(env, column_handles); if (n_cudf_columns.size() == 0) { - rmm::device_buffer null_mask{}; - copy->set_null_mask(null_mask); - return reinterpret_cast(copy.release()); + copy->set_null_mask({}, 0); + return release_as_jlong(copy); } - std::vector column_views; - std::transform(n_cudf_columns.data(), n_cudf_columns.data() + n_cudf_columns.size(), - std::back_inserter(column_views), - [](auto const &p_column) { return *p_column; }); - cudf::table_view input_table{column_views}; - + auto input_table = cudf::table_view{n_cudf_columns.get_dereferenced()}; cudf::binary_operator op = static_cast(bin_op); switch (op) { case cudf::binary_operator::BITWISE_AND: { @@ -1621,7 +1491,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_bitwiseMergeAndSetValidit default: JNI_THROW_NEW(env, cudf::jni::ILLEGAL_ARG_CLASS, "Unsupported merge operation", 0); } - return reinterpret_cast(copy.release()); + return release_as_jlong(copy); } CATCH_STD(env, 0); } @@ -1632,11 +1502,10 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_copyWithBooleanColumnAsVa JNI_NULL_CHECK(env, validity_column_handle, "Validity column handle is null", 0); try { cudf::jni::auto_set_device(env); - auto const exemplar = *reinterpret_cast(exemplar_handle); auto const validity = *reinterpret_cast(validity_column_handle); - auto deep_copy = cudf::jni::new_column_with_boolean_column_as_validity(exemplar, validity); - return reinterpret_cast(deep_copy.release()); + return release_as_jlong( + cudf::jni::new_column_with_boolean_column_as_validity(exemplar, validity)); } CATCH_STD(env, 0); } @@ -1656,7 +1525,6 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_makeCudfColumnView( cudf::type_id n_type = static_cast(j_type); cudf::data_type n_data_type = cudf::jni::make_data_type(j_type, scale); - std::unique_ptr ret; void *data = reinterpret_cast(j_data); cudf::bitmask_type *valid = reinterpret_cast(j_valid); if (valid == nullptr) { @@ -1665,7 +1533,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_makeCudfColumnView( if (n_type == cudf::type_id::STRING) { if (size == 0) { - ret.reset( + return ptr_as_jlong( new cudf::column_view(cudf::data_type{cudf::type_id::STRING}, 0, nullptr, nullptr, 0)); } else { JNI_NULL_CHECK(env, j_offset, "offset is null", 0); @@ -1676,8 +1544,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_makeCudfColumnView( cudf::size_type *offsets = reinterpret_cast(j_offset); cudf::column_view offsets_column(cudf::data_type{cudf::type_id::INT32}, size + 1, offsets); cudf::column_view data_column(cudf::data_type{cudf::type_id::INT8}, j_data_size, data); - ret.reset(new cudf::column_view(cudf::data_type{cudf::type_id::STRING}, size, nullptr, - valid, j_null_count, 0, {offsets_column, data_column})); + return ptr_as_jlong(new cudf::column_view(cudf::data_type{cudf::type_id::STRING}, size, + nullptr, valid, j_null_count, 0, + {offsets_column, data_column})); } } else if (n_type == cudf::type_id::LIST) { JNI_NULL_CHECK(env, j_children, "children of a list are null", 0); @@ -1692,22 +1561,18 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_makeCudfColumnView( } cudf::column_view offsets_column(cudf::data_type{cudf::type_id::INT32}, offsets_size, offsets); - ret.reset(new cudf::column_view(cudf::data_type{cudf::type_id::LIST}, size, nullptr, valid, - j_null_count, 0, {offsets_column, *children[0]})); + return ptr_as_jlong(new cudf::column_view(cudf::data_type{cudf::type_id::LIST}, size, nullptr, + valid, j_null_count, 0, + {offsets_column, *children[0]})); } else if (n_type == cudf::type_id::STRUCT) { JNI_NULL_CHECK(env, j_children, "children of a struct are null", 0); cudf::jni::native_jpointerArray children(env, j_children); - std::vector children_vector(children.size()); - for (int i = 0; i < children.size(); i++) { - children_vector[i] = *children[i]; - } - ret.reset(new cudf::column_view(cudf::data_type{cudf::type_id::STRUCT}, size, nullptr, valid, - j_null_count, 0, children_vector)); + std::vector children_vector = children.get_dereferenced(); + return ptr_as_jlong(new cudf::column_view(cudf::data_type{cudf::type_id::STRUCT}, size, + nullptr, valid, j_null_count, 0, children_vector)); } else { - ret.reset(new cudf::column_view(n_data_type, size, data, valid, j_null_count)); + return ptr_as_jlong(new cudf::column_view(n_data_type, size, data, valid, j_null_count)); } - - return reinterpret_cast(ret.release()); } CATCH_STD(env, 0); } @@ -1843,20 +1708,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_getChildCvPointer(JNIEnv try { cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); - if (column->type().id() == cudf::type_id::LIST) { - std::unique_ptr view = - std::make_unique(*column); - // first child is always offsets which we do not want to get from this call - std::unique_ptr next_view = - std::make_unique(column->child(1 + child_index)); - return reinterpret_cast(next_view.release()); - } else { - std::unique_ptr view = - std::make_unique(*column); - std::unique_ptr next_view = - std::make_unique(column->child(child_index)); - return reinterpret_cast(next_view.release()); - } + auto const is_list = column->type().id() == cudf::type_id::LIST; + auto const child = column->child(child_index + (is_list ? 1 : 0)); + return ptr_as_jlong(new cudf::column_view(child)); } CATCH_STD(env, 0); } @@ -1872,13 +1726,13 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_getNativeOffsetsAddress(J if (column->size() > 0) { cudf::strings_column_view view = cudf::strings_column_view(*column); cudf::column_view offsets_view = view.offsets(); - result = reinterpret_cast(offsets_view.data()); + result = ptr_as_jlong(offsets_view.data()); } } else if (column->type().id() == cudf::type_id::LIST) { if (column->size() > 0) { cudf::lists_column_view view = cudf::lists_column_view(*column); cudf::column_view offsets_view = view.offsets(); - result = reinterpret_cast(offsets_view.data()); + result = ptr_as_jlong(offsets_view.data()); } } return result; @@ -1917,7 +1771,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_getNativeValidityAddress( try { cudf::jni::auto_set_device(env); cudf::column_view *column = reinterpret_cast(handle); - return reinterpret_cast(column->null_mask()); + return ptr_as_jlong(column->null_mask()); } CATCH_STD(env, 0); } @@ -1978,10 +1832,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_clamper(JNIEnv *env, jobj cudf::scalar *hi_scalar = reinterpret_cast(j_hi_scalar); cudf::scalar *hi_replace_scalar = reinterpret_cast(j_hi_replace_scalar); - std::unique_ptr result = - clamp(*column_view, *lo_scalar, *lo_replace_scalar, *hi_scalar, *hi_replace_scalar); - - return reinterpret_cast(result.release()); + return release_as_jlong( + clamp(*column_view, *lo_scalar, *lo_replace_scalar, *hi_scalar, *hi_replace_scalar)); } CATCH_STD(env, 0); } @@ -1994,8 +1846,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_title(JNIEnv *env, jobjec try { cudf::jni::auto_set_device(env); cudf::column_view *view = reinterpret_cast(handle); - std::unique_ptr result = cudf::strings::title(*view); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::title(*view)); } CATCH_STD(env, 0); } @@ -2011,8 +1862,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_capitalize(JNIEnv *env, j cudf::jni::auto_set_device(env); cudf::column_view *view = reinterpret_cast(strs_handle); cudf::string_scalar *deli = reinterpret_cast(delimiters_handle); - std::unique_ptr result = cudf::strings::capitalize(*view, *deli); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::capitalize(*view, *deli)); } CATCH_STD(env, 0); } @@ -2024,16 +1874,10 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_makeStructView(JNIEnv *en JNI_NULL_CHECK(env, handles, "native view handles are null", 0) try { cudf::jni::auto_set_device(env); - std::unique_ptr ret; - cudf::jni::native_jpointerArray children(env, handles); - std::vector children_vector(children.size()); - for (int i = 0; i < children.size(); i++) { - children_vector[i] = *children[i]; - } - ret.reset(new cudf::column_view(cudf::data_type{cudf::type_id::STRUCT}, row_count, nullptr, - nullptr, 0, 0, children_vector)); - - return reinterpret_cast(ret.release()); + auto children = cudf::jni::native_jpointerArray{env, handles}; + auto children_vector = children.get_dereferenced(); + return ptr_as_jlong(new cudf::column_view(cudf::data_type{cudf::type_id::STRUCT}, row_count, + nullptr, nullptr, 0, 0, children_vector)); } CATCH_STD(env, 0); } @@ -2045,19 +1889,18 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_nansToNulls(JNIEnv *env, try { cudf::jni::auto_set_device(env); - cudf::column_view *view = reinterpret_cast(handle); + auto const input = *reinterpret_cast(handle); // get a new null mask by setting all the nans to null - std::pair, cudf::size_type> pair = - cudf::nans_to_nulls(*view); + auto [new_nullmask, new_null_count] = cudf::nans_to_nulls(input); // create a column_view which is a no-copy wrapper around the original column without the null // mask - std::unique_ptr copy_view( - new cudf::column_view(view->type(), view->size(), view->data())); - // create a column by deep copying the copy_view - std::unique_ptr copy(new cudf::column(*copy_view)); - // set the null mask with nans set to null - copy->set_null_mask(std::move(*pair.first), pair.second); - return reinterpret_cast(copy.release()); + auto const input_without_nullmask = cudf::column_view( + input.type(), input.size(), input.head(), nullptr, 0, input.offset(), + std::vector{input.child_begin(), input.child_end()}); + // create a column by deep copying `input_without_nullmask`. + auto deep_copy = std::make_unique(input_without_nullmask); + deep_copy->set_null_mask(std::move(*new_nullmask), new_null_count); + return release_as_jlong(deep_copy); } CATCH_STD(env, 0) } @@ -2070,8 +1913,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isFloat(JNIEnv *env, jobj try { cudf::jni::auto_set_device(env); cudf::column_view *view = reinterpret_cast(handle); - std::unique_ptr result = cudf::strings::is_float(*view); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::is_float(*view)); } CATCH_STD(env, 0) } @@ -2084,8 +1926,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isInteger(JNIEnv *env, jo try { cudf::jni::auto_set_device(env); cudf::column_view *view = reinterpret_cast(handle); - std::unique_ptr result = cudf::strings::is_integer(*view); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::is_integer(*view)); } CATCH_STD(env, 0) } @@ -2100,8 +1941,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isFixedPoint(JNIEnv *env, cudf::jni::auto_set_device(env); cudf::column_view *view = reinterpret_cast(handle); cudf::data_type fp_dtype = cudf::jni::make_data_type(j_dtype, scale); - std::unique_ptr result = cudf::strings::is_fixed_point(*view, fp_dtype); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::is_fixed_point(*view, fp_dtype)); } CATCH_STD(env, 0) } @@ -2116,8 +1956,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isIntegerWithType(JNIEnv cudf::jni::auto_set_device(env); cudf::column_view *view = reinterpret_cast(handle); cudf::data_type int_dtype = cudf::jni::make_data_type(j_dtype, scale); - std::unique_ptr result = cudf::strings::is_integer(*view, int_dtype); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::is_integer(*view, int_dtype)); } CATCH_STD(env, 0) } @@ -2130,10 +1969,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_copyColumnViewToCV(JNIEnv try { cudf::jni::auto_set_device(env); - std::unique_ptr ret; cudf::column_view *view = reinterpret_cast(handle); - ret.reset(reinterpret_cast(new cudf::column(*view))); - return reinterpret_cast(ret.release()); + return ptr_as_jlong(new cudf::column(*view)); } CATCH_STD(env, 0) } @@ -2150,10 +1987,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_getJSONObject(JNIEnv *env cudf::column_view *n_column_view = reinterpret_cast(j_view_handle); cudf::strings_column_view n_strings_col_view(*n_column_view); cudf::string_scalar *n_scalar_path = reinterpret_cast(j_scalar_handle); - - auto result = cudf::strings::get_json_object(n_strings_col_view, *n_scalar_path); - - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::get_json_object(n_strings_col_view, *n_scalar_path)); } CATCH_STD(env, 0) } @@ -2179,10 +2013,9 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringConcatenationListEl cudf::strings_column_view strings_column(*column); cudf::column_view *cv = reinterpret_cast(column_handle); cudf::lists_column_view lcv(*cv); - std::unique_ptr result = + return release_as_jlong( cudf::strings::join_list_elements(lcv, strings_column, separator_narep_scalar, - col_narep_scalar, null_policy, empty_list_output); - return reinterpret_cast(result.release()); + col_narep_scalar, null_policy, empty_list_output)); } CATCH_STD(env, 0); } @@ -2205,9 +2038,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_stringConcatenationListEl cudf::column_view *cv = reinterpret_cast(column_handle); cudf::lists_column_view lcv(*cv); - std::unique_ptr result = cudf::strings::join_list_elements( - lcv, separator_scalar, narep_scalar, null_policy, empty_list_output); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::strings::join_list_elements(lcv, separator_scalar, narep_scalar, + null_policy, empty_list_output)); } CATCH_STD(env, 0); } @@ -2220,7 +2052,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_repeatStrings(JNIEnv *env cudf::jni::auto_set_device(env); auto const cv = *reinterpret_cast(strings_handle); auto const strs_col = cudf::strings_column_view(cv); - return reinterpret_cast(cudf::strings::repeat_strings(strs_col, repeat_times).release()); + return release_as_jlong(cudf::strings::repeat_strings(strs_col, repeat_times)); } CATCH_STD(env, 0); } @@ -2237,11 +2069,10 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_repeatStringsWithColumnRe auto const repeat_times_cv = *reinterpret_cast(repeat_times_handle); if (output_sizes_handle != 0) { auto const output_sizes_cv = *reinterpret_cast(output_sizes_handle); - return reinterpret_cast( - cudf::strings::repeat_strings(strs_col, repeat_times_cv, output_sizes_cv).release()); + return release_as_jlong( + cudf::strings::repeat_strings(strs_col, repeat_times_cv, output_sizes_cv)); } else { - return reinterpret_cast( - cudf::strings::repeat_strings(strs_col, repeat_times_cv).release()); + return release_as_jlong(cudf::strings::repeat_strings(strs_col, repeat_times_cv)); } } CATCH_STD(env, 0); @@ -2260,7 +2091,7 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_ColumnView_repeatStringsSizes( auto [output_sizes, total_bytes] = cudf::strings::repeat_strings_output_sizes(strs_col, repeat_times_cv); auto results = cudf::jni::native_jlongArray(env, 2); - results[0] = reinterpret_cast(output_sizes.release()); + results[0] = release_as_jlong(output_sizes); results[1] = static_cast(total_bytes); return results.get_jArray(); } diff --git a/java/src/main/native/src/TableJni.cpp b/java/src/main/native/src/TableJni.cpp index 7f7499ee238..10f295e27bf 100644 --- a/java/src/main/native/src/TableJni.cpp +++ b/java/src/main/native/src/TableJni.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include "cudf_jni_apis.hpp" #include "dtype_utils.hpp" @@ -487,7 +488,7 @@ class jni_arrow_input_stream final : public arrow::io::InputStream { arrow::Result Read(int64_t nbytes, void *out) override { JNIEnv *env = cudf::jni::get_jni_env(jvm); - jlong ret = read_into(env, reinterpret_cast(out), nbytes); + jlong ret = read_into(env, ptr_as_jlong(out), nbytes); total_read += ret; return ret; } @@ -499,7 +500,7 @@ class jni_arrow_input_stream final : public arrow::io::InputStream { if (!tmp_buffer.ok()) { return tmp_buffer; } - jlong amount_read = read_into(env, reinterpret_cast((*tmp_buffer)->data()), nbytes); + jlong amount_read = read_into(env, ptr_as_jlong((*tmp_buffer)->data()), nbytes); arrow::Status stat = (*tmp_buffer)->Resize(amount_read); if (!stat.ok()) { return stat; @@ -612,10 +613,10 @@ convert_table_for_return(JNIEnv *env, std::unique_ptr &table_result int num_columns = table_cols + extra_columns.size(); cudf::jni::native_jlongArray outcol_handles(env, num_columns); for (int i = 0; i < table_cols; i++) { - outcol_handles[i] = reinterpret_cast(ret[i].release()); + outcol_handles[i] = release_as_jlong(ret[i]); } for (size_t i = 0; i < extra_columns.size(); i++) { - outcol_handles[i + table_cols] = reinterpret_cast(extra_columns[i].release()); + outcol_handles[i + table_cols] = release_as_jlong(extra_columns[i]); } return outcol_handles.get_jArray(); } @@ -778,10 +779,10 @@ jlongArray gather_maps_to_java(JNIEnv *env, auto right_map_buffer = std::make_unique(maps.second->release()); cudf::jni::native_jlongArray result(env, 5); result[0] = static_cast(left_map_buffer->size()); - result[1] = reinterpret_cast(left_map_buffer->data()); - result[2] = reinterpret_cast(left_map_buffer.release()); - result[3] = reinterpret_cast(right_map_buffer->data()); - result[4] = reinterpret_cast(right_map_buffer.release()); + result[1] = ptr_as_jlong(left_map_buffer->data()); + result[2] = release_as_jlong(left_map_buffer); + result[3] = ptr_as_jlong(right_map_buffer->data()); + result[4] = release_as_jlong(right_map_buffer); return result.get_jArray(); } @@ -796,8 +797,8 @@ jlongArray gather_map_to_java(JNIEnv *env, auto gather_map_buffer = std::make_unique(map->release()); cudf::jni::native_jlongArray result(env, 3); result[0] = static_cast(gather_map_buffer->size()); - result[1] = reinterpret_cast(gather_map_buffer->data()); - result[2] = reinterpret_cast(gather_map_buffer.release()); + result[1] = ptr_as_jlong(gather_map_buffer->data()); + result[2] = release_as_jlong(gather_map_buffer); return result.get_jArray(); } @@ -905,19 +906,18 @@ jlongArray mixed_join_size(JNIEnv *env, jlong j_left_keys, jlong j_right_keys, auto const condition = reinterpret_cast(j_condition); auto const nulls_equal = j_nulls_equal ? cudf::null_equality::EQUAL : cudf::null_equality::UNEQUAL; - std::pair>> join_size_info = + auto [join_size, matches_per_row] = join_size_func(*left_keys, *right_keys, *left_condition, *right_condition, condition->get_top_expression(), nulls_equal); - if (join_size_info.second->size() > std::numeric_limits::max()) { + if (matches_per_row->size() > std::numeric_limits::max()) { throw std::runtime_error("Too many values in device buffer to convert into a column"); } - auto col_size = join_size_info.second->size(); - auto col_data = join_size_info.second->release(); - auto col = std::make_unique(cudf::data_type{cudf::type_id::INT32}, col_size, - std::move(col_data), rmm::device_buffer{}, 0); + auto col_size = static_cast(matches_per_row->size()); + auto col_data = matches_per_row->release(); cudf::jni::native_jlongArray result(env, 2); - result[0] = static_cast(join_size_info.first); - result[1] = reinterpret_cast(col.release()); + result[0] = static_cast(join_size); + result[1] = ptr_as_jlong(new cudf::column{cudf::data_type{cudf::type_id::INT32}, col_size, + std::move(col_data), rmm::device_buffer{}, 0}); return result.get_jArray(); } CATCH_STD(env, NULL); @@ -977,9 +977,8 @@ std::pair> get_mixed_size_info(JNIEnv *env, jlong j_output_row_count, jlong j_matches_view) { auto const row_count = static_cast(j_output_row_count); auto const matches = reinterpret_cast(j_matches_view); - return std::pair>( - row_count, cudf::device_span(matches->template data(), - matches->size())); + return std::make_pair(row_count, cudf::device_span( + matches->template data(), matches->size())); } // Returns a table view containing only the columns at the specified indices @@ -1011,12 +1010,11 @@ cudf::table_view const get_non_keys_table(cudf::table_view const *t, jlongArray combine_join_results(JNIEnv *env, std::vector> left_cols, std::vector> right_cols) { cudf::jni::native_jlongArray outcol_handles(env, left_cols.size() + right_cols.size()); - auto iter = std::transform( - left_cols.begin(), left_cols.end(), outcol_handles.begin(), - [](std::unique_ptr &col) { return reinterpret_cast(col.release()); }); - std::transform( - right_cols.begin(), right_cols.end(), iter, - [](std::unique_ptr &col) { return reinterpret_cast(col.release()); }); + auto iter = + std::transform(left_cols.begin(), left_cols.end(), outcol_handles.begin(), + [](std::unique_ptr &col) { return release_as_jlong(col); }); + std::transform(right_cols.begin(), right_cols.end(), iter, + [](std::unique_ptr &col) { return release_as_jlong(col); }); return outcol_handles.get_jArray(); } @@ -1039,21 +1037,19 @@ cudf::column_view remove_validity_from_col(cudf::column_view column_view) { return cudf::column_view(column_view); } } else { - std::unique_ptr ret; std::vector children; children.reserve(column_view.num_children()); for (auto it = column_view.child_begin(); it != column_view.child_end(); it++) { children.push_back(remove_validity_from_col(*it)); } if (!column_view.nullable() || column_view.null_count() != 0) { - ret.reset(new cudf::column_view(column_view.type(), column_view.size(), nullptr, - column_view.null_mask(), column_view.null_count(), - column_view.offset(), children)); + return cudf::column_view(column_view.type(), column_view.size(), nullptr, + column_view.null_mask(), column_view.null_count(), + column_view.offset(), children); } else { - ret.reset(new cudf::column_view(column_view.type(), column_view.size(), nullptr, nullptr, 0, - column_view.offset(), children)); + return cudf::column_view(column_view.type(), column_view.size(), nullptr, nullptr, 0, + column_view.offset(), children); } - return *ret.release(); } } @@ -1072,6 +1068,9 @@ cudf::table_view remove_validity_if_needed(cudf::table_view *input_table_view) { } // namespace jni } // namespace cudf +using cudf::jni::ptr_as_jlong; +using cudf::jni::release_as_jlong; + extern "C" { // This is a method purely added for testing remove_validity_if_needed method @@ -1084,10 +1083,8 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_removeNullMasksIfNeeded(J cudf::table m_tbl(result); std::vector> cols = m_tbl.release(); auto results = cudf::jni::native_jlongArray(env, cols.size()); - int i = 0; - for (auto it = cols.begin(); it != cols.end(); it++) { - results[i++] = reinterpret_cast(it->release()); - } + std::transform(cols.begin(), cols.end(), results.begin(), + [](auto &col) { return release_as_jlong(col); }); return results.get_jArray(); } CATCH_STD(env, 0); @@ -1101,12 +1098,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_createCudfTableView(JNIEnv *en cudf::jni::auto_set_device(env); cudf::jni::native_jpointerArray n_cudf_columns(env, j_cudf_columns); - std::vector column_views(n_cudf_columns.size()); - for (int i = 0; i < n_cudf_columns.size(); i++) { - column_views[i] = *n_cudf_columns[i]; - } - cudf::table_view *tv = new cudf::table_view(column_views); - return reinterpret_cast(tv); + std::vector column_views = n_cudf_columns.get_dereferenced(); + return ptr_as_jlong(new cudf::table_view(column_views)); } CATCH_STD(env, 0); } @@ -1143,8 +1136,7 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_columnViewsFromPacked(JNI // In the ideal case we would keep the view where it is at, and pass in a pointer to it // That pointer would then be copied when Java takes ownership of it, but that adds an // extra JNI call that I would like to avoid for performance reasons. - cudf::column_view *cv = new cudf::column_view(table.column(i)); - views[i] = reinterpret_cast(cv); + views[i] = ptr_as_jlong(new cudf::column_view(table.column(i))); } views.commit(); @@ -1182,23 +1174,13 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_sortOrder(JNIEnv *env, jclass, JNI_ARG_CHECK(env, num_columns_null_smallest == num_columns, "columns and is_descending lengths don't match", 0); - std::vector order(n_is_descending.size()); - for (int i = 0; i < n_is_descending.size(); i++) { - order[i] = n_is_descending[i] ? cudf::order::DESCENDING : cudf::order::ASCENDING; - } - std::vector null_order(n_are_nulls_smallest.size()); - for (int i = 0; i < n_are_nulls_smallest.size(); i++) { - null_order[i] = n_are_nulls_smallest[i] ? cudf::null_order::BEFORE : cudf::null_order::AFTER; - } - - std::vector columns(num_columns); - for (int i = 0; i < num_columns; i++) { - columns[i] = *n_sort_keys_columns[i]; - } - cudf::table_view keys(columns); + std::vector order = + n_is_descending.transform_if_else(cudf::order::DESCENDING, cudf::order::ASCENDING); + std::vector null_order = + n_are_nulls_smallest.transform_if_else(cudf::null_order::BEFORE, cudf::null_order::AFTER); - auto sorted_col = cudf::sorted_order(keys, order, null_order); - return reinterpret_cast(sorted_col.release()); + std::vector sort_keys = n_sort_keys_columns.get_dereferenced(); + return release_as_jlong(cudf::sorted_order(cudf::table_view{sort_keys}, order, null_order)); } CATCH_STD(env, 0); } @@ -1232,22 +1214,14 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_orderBy(JNIEnv *env, jcla JNI_ARG_CHECK(env, num_columns_null_smallest == num_columns, "columns and areNullsSmallest lengths don't match", 0); - std::vector order(n_is_descending.size()); - for (int i = 0; i < n_is_descending.size(); i++) { - order[i] = n_is_descending[i] ? cudf::order::DESCENDING : cudf::order::ASCENDING; - } - std::vector null_order(n_are_nulls_smallest.size()); - for (int i = 0; i < n_are_nulls_smallest.size(); i++) { - null_order[i] = n_are_nulls_smallest[i] ? cudf::null_order::BEFORE : cudf::null_order::AFTER; - } + std::vector order = + n_is_descending.transform_if_else(cudf::order::DESCENDING, cudf::order::ASCENDING); - std::vector columns(num_columns); - for (int i = 0; i < num_columns; i++) { - columns[i] = *n_sort_keys_columns[i]; - } - cudf::table_view keys(columns); + std::vector null_order = + n_are_nulls_smallest.transform_if_else(cudf::null_order::BEFORE, cudf::null_order::AFTER); - auto sorted_col = cudf::sorted_order(keys, order, null_order); + std::vector sort_keys = n_sort_keys_columns.get_dereferenced(); + auto sorted_col = cudf::sorted_order(cudf::table_view{sort_keys}, order, null_order); cudf::table_view *input_table = reinterpret_cast(j_input_table); std::unique_ptr result = cudf::gather(*input_table, sorted_col->view()); @@ -1286,25 +1260,12 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_merge(JNIEnv *env, jclass JNI_ARG_CHECK(env, num_columns_null_smallest == num_columns, "columns and areNullsSmallest lengths don't match", NULL); - std::vector indexes(n_sort_key_indexes.size()); - for (int i = 0; i < n_sort_key_indexes.size(); i++) { - indexes[i] = n_sort_key_indexes[i]; - } - std::vector order(n_is_descending.size()); - for (int i = 0; i < n_is_descending.size(); i++) { - order[i] = n_is_descending[i] ? cudf::order::DESCENDING : cudf::order::ASCENDING; - } - std::vector null_order(n_are_nulls_smallest.size()); - for (int i = 0; i < n_are_nulls_smallest.size(); i++) { - null_order[i] = n_are_nulls_smallest[i] ? cudf::null_order::BEFORE : cudf::null_order::AFTER; - } - - jsize num_tables = n_table_handles.size(); - std::vector tables; - tables.reserve(num_tables); - for (int i = 0; i < num_tables; i++) { - tables.push_back(*n_table_handles[i]); - } + std::vector indexes = n_sort_key_indexes.to_vector(); + std::vector order = + n_is_descending.transform_if_else(cudf::order::DESCENDING, cudf::order::ASCENDING); + std::vector null_order = + n_are_nulls_smallest.transform_if_else(cudf::null_order::BEFORE, cudf::null_order::AFTER); + std::vector tables = n_table_handles.get_dereferenced(); std::unique_ptr result = cudf::merge(tables, indexes, order, null_order); return cudf::jni::convert_table_for_return(env, result); @@ -1347,9 +1308,10 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readCSV( NULL); } data_types.reserve(n_types.size()); - for (int index = 0; index < n_types.size(); index++) { - data_types.emplace_back(cudf::jni::make_data_type(n_types[index], n_scales[index])); - } + std::transform(n_types.begin(), n_types.end(), n_scales.begin(), + std::back_inserter(data_types), [](auto type, auto scale) { + return cudf::data_type{static_cast(type), scale}; + }); } cudf::jni::native_jstring filename(env, inputfilepath); @@ -1363,14 +1325,11 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readCSV( cudf::jni::native_jstringArray n_false_values(env, false_values); cudf::jni::native_jstringArray n_filter_col_names(env, filter_col_names); - std::unique_ptr source; - if (read_buffer) { - source.reset(new cudf::io::source_info(reinterpret_cast(buffer), buffer_length)); - } else { - source.reset(new cudf::io::source_info(filename.get())); - } + auto source = read_buffer ? cudf::io::source_info{reinterpret_cast(buffer), + static_cast(buffer_length)} : + cudf::io::source_info{filename.get()}; - cudf::io::csv_reader_options opts = cudf::io::csv_reader_options::builder(*source) + cudf::io::csv_reader_options opts = cudf::io::csv_reader_options::builder(source) .delimiter(delim) .header(header_row) .names(n_col_names.as_cpp_vector()) @@ -1423,9 +1382,10 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readJSON( NULL); } data_types.reserve(n_types.size()); - for (int index = 0; index < n_types.size(); index++) { - data_types.emplace_back(cudf::jni::make_data_type(n_types[index], n_scales[index])); - } + std::transform(n_types.begin(), n_types.end(), n_scales.begin(), + std::back_inserter(data_types), [](auto const &type, auto const &scale) { + return cudf::data_type{static_cast(type), scale}; + }); } cudf::jni::native_jstring filename(env, inputfilepath); @@ -1434,14 +1394,11 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readJSON( NULL); } - std::unique_ptr source; - if (read_buffer) { - source.reset(new cudf::io::source_info(reinterpret_cast(buffer), buffer_length)); - } else { - source.reset(new cudf::io::source_info(filename.get())); - } + auto source = read_buffer ? cudf::io::source_info{reinterpret_cast(buffer), + static_cast(buffer_length)} : + cudf::io::source_info{filename.get()}; - cudf::io::json_reader_options_builder opts = cudf::io::json_reader_options::builder(*source) + cudf::io::json_reader_options_builder opts = cudf::io::json_reader_options::builder(source) .dayfirst(static_cast(day_first)) .lines(static_cast(lines)); @@ -1475,9 +1432,11 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readJSON( // turn name and its index in table into map std::map m; - for (size_t i = 0; i < result.metadata.column_names.size(); i++) { - m.insert(std::make_pair(result.metadata.column_names[i], i)); - } + std::transform(result.metadata.column_names.begin(), result.metadata.column_names.end(), + thrust::make_counting_iterator(0), std::inserter(m, m.end()), + [](auto const &column_name, auto const &index) { + return std::make_pair(column_name, index); + }); auto col_names_vec = n_col_names.as_cpp_vector(); std::vector indices; @@ -1532,15 +1491,12 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readParquet(JNIEnv *env, cudf::jni::native_jstringArray n_filter_col_names(env, filter_col_names); - std::unique_ptr source; - if (read_buffer) { - source.reset(new cudf::io::source_info(reinterpret_cast(buffer), buffer_length)); - } else { - source.reset(new cudf::io::source_info(filename.get())); - } + auto source = read_buffer ? cudf::io::source_info(reinterpret_cast(buffer), + static_cast(buffer_length)) : + cudf::io::source_info(filename.get()); cudf::io::parquet_reader_options opts = - cudf::io::parquet_reader_options::builder(*source) + cudf::io::parquet_reader_options::builder(source) .columns(n_filter_col_names.as_cpp_vector()) .convert_strings_to_categories(false) .timestamp_type(cudf::data_type(static_cast(unit))) @@ -1572,13 +1528,13 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeParquetBufferBegin( createTableMetaData(env, j_num_children, j_col_names, j_children, j_col_nullability, j_isInt96, j_precisions, j_is_map, metadata); - cudf::jni::native_jstringArray meta_keys(env, j_metadata_keys); - cudf::jni::native_jstringArray meta_values(env, j_metadata_values); + auto meta_keys = cudf::jni::native_jstringArray{env, j_metadata_keys}.as_cpp_vector(); + auto meta_values = cudf::jni::native_jstringArray{env, j_metadata_values}.as_cpp_vector(); std::map kv_metadata; - for (auto i = 0; i < meta_keys.size(); ++i) { - kv_metadata[meta_keys[i].get()] = meta_values[i].get(); - } + std::transform(meta_keys.begin(), meta_keys.end(), meta_values.begin(), + std::inserter(kv_metadata, kv_metadata.end()), + [](auto const &key, auto const &value) { return std::make_pair(key, value); }); chunked_parquet_writer_options opts = chunked_parquet_writer_options::builder(sink) @@ -1590,7 +1546,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeParquetBufferBegin( auto writer_ptr = std::make_unique(opts); cudf::jni::native_parquet_writer_handle *ret = new cudf::jni::native_parquet_writer_handle(std::move(writer_ptr), std::move(data_sink)); - return reinterpret_cast(ret); + return ptr_as_jlong(ret); } CATCH_STD(env, 0) } @@ -1614,13 +1570,13 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeParquetFileBegin( createTableMetaData(env, j_num_children, j_col_names, j_children, j_col_nullability, j_isInt96, j_precisions, j_is_map, metadata); - cudf::jni::native_jstringArray meta_keys(env, j_metadata_keys); - cudf::jni::native_jstringArray meta_values(env, j_metadata_values); + auto meta_keys = cudf::jni::native_jstringArray{env, j_metadata_keys}.as_cpp_vector(); + auto meta_values = cudf::jni::native_jstringArray{env, j_metadata_values}.as_cpp_vector(); std::map kv_metadata; - for (auto i = 0; i < meta_keys.size(); ++i) { - kv_metadata[meta_keys[i].get()] = meta_values[i].get(); - } + std::transform(meta_keys.begin(), meta_keys.end(), meta_values.begin(), + std::inserter(kv_metadata, kv_metadata.end()), + [](auto const &key, auto const &value) { return std::make_pair(key, value); }); sink_info sink{output_path.get()}; chunked_parquet_writer_options opts = @@ -1634,7 +1590,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeParquetFileBegin( auto writer_ptr = std::make_unique(opts); cudf::jni::native_parquet_writer_handle *ret = new cudf::jni::native_parquet_writer_handle(std::move(writer_ptr)); - return reinterpret_cast(ret); + return ptr_as_jlong(ret); } CATCH_STD(env, 0) } @@ -1704,15 +1660,12 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_readORC( cudf::jni::native_jstringArray n_dec128_col_names(env, dec128_col_names); - std::unique_ptr source; - if (read_buffer) { - source.reset(new cudf::io::source_info(reinterpret_cast(buffer), buffer_length)); - } else { - source.reset(new cudf::io::source_info(filename.get())); - } + auto source = read_buffer ? + cudf::io::source_info(reinterpret_cast(buffer), buffer_length) : + cudf::io::source_info(filename.get()); cudf::io::orc_reader_options opts = - cudf::io::orc_reader_options::builder(*source) + cudf::io::orc_reader_options::builder(source) .columns(n_filter_col_names.as_cpp_vector()) .use_index(false) .use_np_dtypes(static_cast(usingNumPyTypes)) @@ -1744,13 +1697,13 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeORCBufferBegin( createTableMetaData(env, j_num_children, j_col_names, j_children, j_col_nullability, j_is_int96, j_precisions, j_is_map, metadata); - cudf::jni::native_jstringArray meta_keys(env, j_metadata_keys); - cudf::jni::native_jstringArray meta_values(env, j_metadata_values); + auto meta_keys = cudf::jni::native_jstringArray{env, j_metadata_keys}.as_cpp_vector(); + auto meta_values = cudf::jni::native_jstringArray{env, j_metadata_values}.as_cpp_vector(); std::map kv_metadata; - for (auto i = 0; i < meta_keys.size(); ++i) { - kv_metadata[meta_keys[i].get()] = meta_values[i].get(); - } + std::transform(meta_keys.begin(), meta_keys.end(), meta_values.begin(), + std::inserter(kv_metadata, kv_metadata.end()), + [](const std::string &k, const std::string &v) { return std::make_pair(k, v); }); std::unique_ptr data_sink( new cudf::jni::jni_writer_data_sink(env, consumer)); @@ -1764,7 +1717,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeORCBufferBegin( auto writer_ptr = std::make_unique(opts); cudf::jni::native_orc_writer_handle *ret = new cudf::jni::native_orc_writer_handle(std::move(writer_ptr), std::move(data_sink)); - return reinterpret_cast(ret); + return ptr_as_jlong(ret); } CATCH_STD(env, 0) } @@ -1789,13 +1742,13 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeORCFileBegin( createTableMetaData(env, j_num_children, j_col_names, j_children, j_col_nullability, j_is_int96, j_precisions, j_is_map, metadata); - cudf::jni::native_jstringArray meta_keys(env, j_metadata_keys); - cudf::jni::native_jstringArray meta_values(env, j_metadata_values); + auto meta_keys = cudf::jni::native_jstringArray{env, j_metadata_keys}.as_cpp_vector(); + auto meta_values = cudf::jni::native_jstringArray{env, j_metadata_values}.as_cpp_vector(); std::map kv_metadata; - for (auto i = 0; i < meta_keys.size(); ++i) { - kv_metadata[meta_keys[i].get()] = meta_values[i].get(); - } + std::transform(meta_keys.begin(), meta_keys.end(), meta_values.begin(), + std::inserter(kv_metadata, kv_metadata.end()), + [](const std::string &k, const std::string &v) { return std::make_pair(k, v); }); sink_info sink{output_path.get()}; chunked_orc_writer_options opts = chunked_orc_writer_options::builder(sink) @@ -1807,7 +1760,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeORCFileBegin( auto writer_ptr = std::make_unique(opts); cudf::jni::native_orc_writer_handle *ret = new cudf::jni::native_orc_writer_handle(std::move(writer_ptr)); - return reinterpret_cast(ret); + return ptr_as_jlong(ret); } CATCH_STD(env, 0) } @@ -1862,7 +1815,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeArrowIPCBufferBegin(JNIEnv cudf::jni::native_arrow_ipc_writer_handle *ret = new cudf::jni::native_arrow_ipc_writer_handle(col_names.as_cpp_vector(), data_sink); - return reinterpret_cast(ret); + return ptr_as_jlong(ret); } CATCH_STD(env, 0) } @@ -1879,7 +1832,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_writeArrowIPCFileBegin(JNIEnv * cudf::jni::native_arrow_ipc_writer_handle *ret = new cudf::jni::native_arrow_ipc_writer_handle(col_names.as_cpp_vector(), output_path.get()); - return reinterpret_cast(ret); + return ptr_as_jlong(ret); } CATCH_STD(env, 0) } @@ -1896,13 +1849,14 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_convertCudfToArrowTable(JNIEnv try { cudf::jni::auto_set_device(env); - std::unique_ptr> result( - new std::shared_ptr(nullptr)); - *result = cudf::to_arrow(*tview, state->get_column_metadata(*tview)); - if (!result->get()) { - return 0; - } - return reinterpret_cast(result.release()); + // The semantics of this function are confusing: + // The return value is a pointer to a heap-allocated shared_ptr. + // i.e. the shared_ptr<> is on the heap. + // The pointer to the shared_ptr<> is returned as a jlong. + using result_t = std::shared_ptr; + + auto result = cudf::to_arrow(*tview, state->get_column_metadata(*tview)); + return ptr_as_jlong(new result_t{result}); } CATCH_STD(env, 0) } @@ -1946,10 +1900,7 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_readArrowIPCFileBegin(JNIEnv *e try { cudf::jni::auto_set_device(env); cudf::jni::native_jstring input_path(env, j_input_path); - - cudf::jni::native_arrow_ipc_reader_handle *ret = - new cudf::jni::native_arrow_ipc_reader_handle(input_path.get()); - return reinterpret_cast(ret); + return ptr_as_jlong(new cudf::jni::native_arrow_ipc_reader_handle(input_path.get())); } CATCH_STD(env, 0) } @@ -1959,13 +1910,9 @@ JNIEXPORT long JNICALL Java_ai_rapids_cudf_Table_readArrowIPCBufferBegin(JNIEnv JNI_NULL_CHECK(env, provider, "null provider", 0); try { cudf::jni::auto_set_device(env); - std::shared_ptr data_source( new cudf::jni::jni_arrow_input_stream(env, provider)); - - cudf::jni::native_arrow_ipc_reader_handle *ret = - new cudf::jni::native_arrow_ipc_reader_handle(data_source); - return reinterpret_cast(ret); + return ptr_as_jlong(new cudf::jni::native_arrow_ipc_reader_handle(data_source)); } CATCH_STD(env, 0) } @@ -1982,13 +1929,8 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_readArrowIPCChunkToArrowTable( cudf::jni::auto_set_device(env); // This is a little odd because we have to return a pointer // and arrow wants to deal with shared pointers for everything. - std::unique_ptr> result( - new std::shared_ptr(nullptr)); - *result = state->next(row_target); - if (!result->get()) { - return 0; - } - return reinterpret_cast(result.release()); + auto result = state->next(row_target); + return result ? ptr_as_jlong(new std::shared_ptr{result}) : 0; } CATCH_STD(env, 0) } @@ -2781,8 +2723,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_interleaveColumns(JNIEnv *env, try { cudf::jni::auto_set_device(env); cudf::table_view *table_view = reinterpret_cast(j_cudf_table_view); - std::unique_ptr result = cudf::interleave_columns(*table_view); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::interleave_columns(*table_view)); } CATCH_STD(env, 0); } @@ -3076,12 +3017,8 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_groupByReplaceNulls( } cudf::table_view n_replace_table(n_replace_cols); - std::vector policies; - policies.reserve(n_is_preceding.size()); - for (int i = 0; i < n_is_preceding.size(); i++) { - policies.push_back(n_is_preceding[i] ? cudf::replace_policy::PRECEDING : - cudf::replace_policy::FOLLOWING); - } + std::vector policies = n_is_preceding.transform_if_else( + cudf::replace_policy::PRECEDING, cudf::replace_policy::FOLLOWING); std::pair, std::unique_ptr> result = grouper.replace_nulls(n_replace_table, policies); @@ -3158,9 +3095,8 @@ Java_ai_rapids_cudf_Table_convertToRowsFixedWidthOptimized(JNIEnv *env, jclass, cudf::jni::convert_to_rows_fixed_width_optimized(*n_input_table); int num_columns = cols.size(); cudf::jni::native_jlongArray outcol_handles(env, num_columns); - for (int i = 0; i < num_columns; i++) { - outcol_handles[i] = reinterpret_cast(cols[i].release()); - } + std::transform(cols.begin(), cols.end(), outcol_handles.begin(), + [](auto &col) { return release_as_jlong(col); }); return outcol_handles.get_jArray(); } CATCH_STD(env, 0); @@ -3216,9 +3152,8 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_convertToRows(JNIEnv *env std::vector> cols = cudf::jni::convert_to_rows(*n_input_table); int num_columns = cols.size(); cudf::jni::native_jlongArray outcol_handles(env, num_columns); - for (int i = 0; i < num_columns; i++) { - outcol_handles[i] = reinterpret_cast(cols[i].release()); - } + std::transform(cols.begin(), cols.end(), outcol_handles.begin(), + [](auto &col) { return release_as_jlong(col); }); return outcol_handles.get_jArray(); } CATCH_STD(env, 0); @@ -3235,10 +3170,13 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_convertFromRowsFixedWidth cudf::lists_column_view list_input(*input); cudf::jni::native_jintArray n_types(env, types); cudf::jni::native_jintArray n_scale(env, scale); - std::vector types_vec; - for (int i = 0; i < n_types.size(); i++) { - types_vec.emplace_back(cudf::jni::make_data_type(n_types[i], n_scale[i])); + if (n_types.size() != n_scale.size()) { + JNI_THROW_NEW(env, "java/lang/IllegalArgumentException", "types and scales must match size", + NULL); } + std::vector types_vec; + std::transform(n_types.begin(), n_types.end(), n_scale.begin(), std::back_inserter(types_vec), + [](jint type, jint scale) { return cudf::jni::make_data_type(type, scale); }); std::unique_ptr result = cudf::jni::convert_from_rows_fixed_width_optimized(list_input, types_vec); return cudf::jni::convert_table_for_return(env, result); @@ -3259,10 +3197,13 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_convertFromRows(JNIEnv *e cudf::lists_column_view list_input(*input); cudf::jni::native_jintArray n_types(env, types); cudf::jni::native_jintArray n_scale(env, scale); - std::vector types_vec; - for (int i = 0; i < n_types.size(); i++) { - types_vec.emplace_back(cudf::jni::make_data_type(n_types[i], n_scale[i])); + if (n_types.size() != n_scale.size()) { + JNI_THROW_NEW(env, "java/lang/IllegalArgumentException", "types and scales must match size", + NULL); } + std::vector types_vec; + std::transform(n_types.begin(), n_types.end(), n_scale.begin(), std::back_inserter(types_vec), + [](jint type, jint scale) { return cudf::jni::make_data_type(type, scale); }); std::unique_ptr result = cudf::jni::convert_from_rows(list_input, types_vec); return cudf::jni::convert_table_for_return(env, result); } @@ -3314,25 +3255,17 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_bound(JNIEnv *env, jclass, jlo cudf::jni::native_jbooleanArray const n_desc_flags(env, desc_flags); cudf::jni::native_jbooleanArray const n_are_nulls_smallest(env, are_nulls_smallest); - std::vector column_desc_flags(n_desc_flags.size()); - std::vector column_null_orders(n_are_nulls_smallest.size()); + std::vector column_desc_flags{ + n_desc_flags.transform_if_else(cudf::order::DESCENDING, cudf::order::ASCENDING)}; + std::vector column_null_orders{ + n_are_nulls_smallest.transform_if_else(cudf::null_order::BEFORE, cudf::null_order::AFTER)}; JNI_ARG_CHECK(env, (column_desc_flags.size() == column_null_orders.size()), "null-order and sort-order size mismatch", 0); - size_t num_columns = column_null_orders.size(); - for (size_t i = 0; i < num_columns; i++) { - column_desc_flags[i] = n_desc_flags[i] ? cudf::order::DESCENDING : cudf::order::ASCENDING; - column_null_orders[i] = - n_are_nulls_smallest[i] ? cudf::null_order::BEFORE : cudf::null_order::AFTER; - } - std::unique_ptr result; - if (is_upper_bound) { - result = std::move(cudf::upper_bound(*input, *values, column_desc_flags, column_null_orders)); - } else { - result = std::move(cudf::lower_bound(*input, *values, column_desc_flags, column_null_orders)); - } - return reinterpret_cast(result.release()); + return release_as_jlong( + is_upper_bound ? cudf::upper_bound(*input, *values, column_desc_flags, column_null_orders) : + cudf::lower_bound(*input, *values, column_desc_flags, column_null_orders)); } CATCH_STD(env, 0); } @@ -3407,13 +3340,13 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_rollingWindowAggregate( int agg_column_index = values[i]; if (default_output[i] != nullptr) { - result_columns.emplace_back(std::move(cudf::grouped_rolling_window( + result_columns.emplace_back(cudf::grouped_rolling_window( groupby_keys, input_table->column(agg_column_index), *default_output[i], preceding[i], - following[i], min_periods[i], *agg))); + following[i], min_periods[i], *agg)); } else { - result_columns.emplace_back(std::move( + result_columns.emplace_back( cudf::grouped_rolling_window(groupby_keys, input_table->column(agg_column_index), - preceding[i], following[i], min_periods[i], *agg))); + preceding[i], following[i], min_periods[i], *agg)); } } @@ -3499,7 +3432,7 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_rangeRollingWindowAggrega JNI_ARG_CHECK(env, agg != nullptr, "aggregation is not an instance of rolling_aggregation", nullptr); - result_columns.emplace_back(std::move(cudf::grouped_range_rolling_window( + result_columns.emplace_back(cudf::grouped_range_rolling_window( groupby_keys, order_by_column, orderbys_ascending[i] ? cudf::order::ASCENDING : cudf::order::DESCENDING, input_table->column(agg_column_index), @@ -3507,7 +3440,7 @@ JNIEXPORT jlongArray JNICALL Java_ai_rapids_cudf_Table_rangeRollingWindowAggrega cudf::range_window_bounds::get(*preceding[i]), unbounded_following[i] ? cudf::range_window_bounds::unbounded(unbounded_type) : cudf::range_window_bounds::get(*following[i]), - min_periods[i], *agg))); + min_periods[i], *agg)); } auto result_table = std::make_unique(std::move(result_columns)); @@ -3577,8 +3510,7 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_Table_rowBitCount(JNIEnv *env, jclas try { cudf::jni::auto_set_device(env); auto t = reinterpret_cast(j_table); - std::unique_ptr result = cudf::row_bit_count(*t); - return reinterpret_cast(result.release()); + return release_as_jlong(cudf::row_bit_count(*t)); } CATCH_STD(env, 0); }