Skip to content

Commit

Permalink
[Java] Add JNI bindings for integers_to_hex (#14205)
Browse files Browse the repository at this point in the history
This PR adds a method to ColumnView class to allow for conversion from Integers to hex
closes #14081

Authors:
  - Raza Jafri (https://github.com/razajafri)

Approvers:
  - Kuhu Shukla (https://github.com/kuhushukla)
  - Robert (Bobby) Evans (https://github.com/revans2)

URL: #14205
  • Loading branch information
razajafri authored Sep 27, 2023
1 parent a97020f commit bff0fcd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
27 changes: 27 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ColumnView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4089,6 +4089,8 @@ static DeviceMemoryBufferView getOffsetsBuffer(long viewHandle) {

private static native long isFixedPoint(long viewHandle, int nativeTypeId, int scale);

private static native long toHex(long viewHandle);

/**
* Native method to concatenate a list column of strings (each row is a list of strings),
* concatenates the strings within each row and returns a single strings column result.
Expand Down Expand Up @@ -5231,4 +5233,29 @@ static ColumnView[] getColumnViewsFromPointers(long[] nativeHandles) {
}
}
}

/**
* Convert this integer column to hexadecimal column and return a new strings column
*
* Any null entries will result in corresponding null entries in the output column.
*
* The output character set is '0'-'9' and 'A'-'F'. The output string width will
* be a multiple of 2 depending on the size of the integer type. A single leading
* zero is applied to the first non-zero output byte if it is less than 0x10.
*
* Example:
* input = [123, -1, 0, 27, 342718233]
* s = input.toHex()
* s is [ '04D2', 'FFFFFFFF', '00', '1B', '146D7719']
*
* The example above shows an `INT32` type column where each integer is 4 bytes.
* Leading zeros are suppressed unless filling out a complete byte as in
* `123 -> '04D2'` instead of `000004D2` or `4D2`.
*
* @return new string ColumnVector
*/
public ColumnVector toHex() {
assert getType().isIntegral() : "Only integers are supported";
return new ColumnVector(toHex(this.getNativeView()));
}
}
19 changes: 19 additions & 0 deletions java/src/main/java/ai/rapids/cudf/DType.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,14 @@ public boolean isDurationType() {
}

/**
* Returns true for strictly Integer types not a type backed by
* ints
*/
public boolean isIntegral() {
return INTEGRALS.contains(this.typeId);
}

/**
* Returns true for nested types
*/
public boolean isNestedType() {
Expand Down Expand Up @@ -506,4 +514,15 @@ public boolean hasOffsets() {
DTypeEnum.STRING,
DTypeEnum.LIST
);

private static final EnumSet<DTypeEnum> INTEGRALS = EnumSet.of(
DTypeEnum.INT8,
DTypeEnum.INT16,
DTypeEnum.INT32,
DTypeEnum.INT64,
DTypeEnum.UINT8,
DTypeEnum.UINT16,
DTypeEnum.UINT32,
DTypeEnum.UINT64
);
}
9 changes: 9 additions & 0 deletions java/src/main/native/src/ColumnViewJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2563,4 +2563,13 @@ Java_ai_rapids_cudf_ColumnView_purgeNonEmptyNulls(JNIEnv *env, jclass, jlong col
CATCH_STD(env, 0);
}

JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_toHex(JNIEnv *env, jclass, jlong input_ptr) {
JNI_NULL_CHECK(env, input_ptr, "input is null", 0);
try {
cudf::jni::auto_set_device(env);
const cudf::column_view *input = reinterpret_cast<cudf::column_view *>(input_ptr);
return release_as_jlong(cudf::strings::integers_to_hex(*input));
}
CATCH_STD(env, 0);
}
} // extern "C"
10 changes: 10 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ColumnVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6876,4 +6876,14 @@ public void testUseAfterFree() {
vector.close();
assertThrows(NullPointerException.class, vector::getDeviceMemorySize);
}

@Test
public void testConvertIntegerToHex() {
try (
ColumnVector input = ColumnVector.fromInts(14, 2621, 50);
ColumnVector expected = ColumnVector.fromStrings("0E", "0A3D", "32");
ColumnVector actual = input.toHex()) {
assertColumnsAreEqual(expected, actual);
}
}
}

0 comments on commit bff0fcd

Please sign in to comment.