Skip to content

Commit

Permalink
Added JNI support for new is_integer (#7739)
Browse files Browse the repository at this point in the history
Adds JNI bindings for improved is_integer with bounds checks

Authors:
  - Robert (Bobby) Evans (@revans2)

Approvers:
  - Jason Lowe (@jlowe)

URL: #7739
  • Loading branch information
revans2 authored Mar 26, 2021
1 parent bf2e96c commit b0586c4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
21 changes: 19 additions & 2 deletions java/src/main/java/ai/rapids/cudf/ColumnView.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,34 @@ public final ColumnVector isNull() {
/**
* Returns a Boolean vector with the same number of rows as this instance, that has
* TRUE for any entry that is an integer, and FALSE if its not an integer. A null will be returned
* for null entries
* for null entries.
*
* NOTE: Integer doesn't mean a 32-bit integer. It means a number that is not a fraction.
* i.e. If this method returns true for a value it could still result in an overflow or underflow
* if you convert it to a Java integral type
*
* @return - Boolean vector
* @return Boolean vector
*/
public final ColumnVector isInteger() {
assert type.equals(DType.STRING);
return new ColumnVector(isInteger(getNativeView()));
}

/**
* Returns a Boolean vector with the same number of rows as this instance, that has
* TRUE for any entry that is an integer, and FALSE if its not an integer. A null will be returned
* for null entries.
*
* @param intType the data type that should be used for bounds checking. Note that only
* integer types are allowed.
* @return Boolean vector
*/
public final ColumnVector isInteger(DType intType) {
assert type.equals(DType.STRING);
return new ColumnVector(isIntegerWithType(getNativeView(),
intType.getTypeId().getNativeId(), intType.getScale()));
}

/**
* Returns a Boolean vector with the same number of rows as this instance, that has
* TRUE for any entry that is a float, and FALSE if its not a float. A null will be returned
Expand Down Expand Up @@ -2845,6 +2860,8 @@ private static native long rollingWindow(

private static native long isInteger(long viewHandle);

private static native long isIntegerWithType(long viewHandle, int typeId, int typeScale);

private static native long isNotNanNative(long viewHandle);

private static native long isNotNullNative(long viewHandle);
Expand Down
17 changes: 17 additions & 0 deletions java/src/main/native/src/ColumnViewJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,23 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isInteger(JNIEnv *env, jo
CATCH_STD(env, 0)
}

JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_isIntegerWithType(JNIEnv *env, jobject,
jlong handle,
jint j_dtype,
jint scale) {

JNI_NULL_CHECK(env, handle, "native view handle is null", 0)

try {
cudf::jni::auto_set_device(env);
cudf::column_view *view = reinterpret_cast<cudf::column_view *>(handle);
cudf::data_type int_dtype = cudf::jni::make_data_type(j_dtype, scale);
std::unique_ptr<cudf::column> result = cudf::strings::is_integer(*view, int_dtype);
return reinterpret_cast<jlong>(result.release());
}
CATCH_STD(env, 0)
}

JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_copyColumnViewToCV(JNIEnv *env, jobject j_object,
jlong handle) {

Expand Down
63 changes: 63 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ColumnVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3339,6 +3339,69 @@ void testNansToNulls() {
}
}

@Test
void testIsIntegerWithBounds() {
String[] intStrings = {"A", "nan", "Inf", "-Inf", "3.5",
String.valueOf(Byte.MIN_VALUE),
String.valueOf(Byte.MIN_VALUE + 1L),
String.valueOf(Byte.MIN_VALUE - 1L),
String.valueOf(Byte.MAX_VALUE),
String.valueOf(Byte.MAX_VALUE + 1L),
String.valueOf(Byte.MAX_VALUE - 1L),
String.valueOf(Short.MIN_VALUE),
String.valueOf(Short.MIN_VALUE + 1L),
String.valueOf(Short.MIN_VALUE - 1L),
String.valueOf(Short.MAX_VALUE),
String.valueOf(Short.MAX_VALUE + 1L),
String.valueOf(Short.MAX_VALUE - 1L),
String.valueOf(Integer.MIN_VALUE),
String.valueOf(Integer.MIN_VALUE + 1L),
String.valueOf(Integer.MIN_VALUE - 1L),
String.valueOf(Integer.MAX_VALUE),
String.valueOf(Integer.MAX_VALUE + 1L),
String.valueOf(Integer.MAX_VALUE - 1L),
String.valueOf(Long.MIN_VALUE),
String.valueOf(Long.MIN_VALUE + 1L),
"-9223372036854775809",
String.valueOf(Long.MAX_VALUE),
"9223372036854775808",
String.valueOf(Long.MAX_VALUE - 1L)};
try (ColumnVector intStringCV = ColumnVector.fromStrings(intStrings);
ColumnVector isByte = intStringCV.isInteger(DType.INT8);
ColumnVector expectedByte = ColumnVector.fromBoxedBooleans(
false, false, false, false, false,
true, true, false, true, false, true,
false, false, false, false, false, false,
false, false, false, false, false, false,
false, false, false, false, false, false);
ColumnVector isShort = intStringCV.isInteger(DType.INT16);
ColumnVector expectedShort = ColumnVector.fromBoxedBooleans(
false, false, false, false, false,
true, true, true, true, true, true,
true, true, false, true, false, true,
false, false, false, false, false, false,
false, false, false, false, false, false);
ColumnVector isInt = intStringCV.isInteger(DType.INT32);
ColumnVector expectedInt = ColumnVector.fromBoxedBooleans(
false, false, false, false, false,
true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, false, true, false, true,
false, false, false, false, false, false);
ColumnVector isLong = intStringCV.isInteger(DType.INT64);
ColumnVector expectedLong = ColumnVector.fromBoxedBooleans(
false, false, false, false, false,
true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, false, true, false, true)) {
assertColumnsAreEqual(expectedByte, isByte);
assertColumnsAreEqual(expectedShort, isShort);
assertColumnsAreEqual(expectedInt, isInt);
assertColumnsAreEqual(expectedLong, isLong);
}
}

@Test
void testIsInteger() {
String[] intStrings = {"A", "nan", "Inf", "-Inf", "Infinity", "infinity", "2147483647",
Expand Down

0 comments on commit b0586c4

Please sign in to comment.