Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strings like jni and native method #12032

Merged
merged 16 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ColumnView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3276,6 +3276,46 @@ public final ColumnVector extractAllRecord(String pattern, int idx) {
return new ColumnVector(extractAllRecord(this.getNativeView(), pattern, idx));
}

/**
* Returns a boolean ColumnVector identifying rows which
* match the given like pattern.
*
* The like pattern expects only 2 wildcard special characters
* - `%` any number of any character (including no characters)
* - `_` any single character
*
* ```
* cv = ["azaa", "ababaabba", "aaxa"]
* r = cv.like("%a_aa%", "\\")
* r is now [true, true, false]
* r = cv.like("a__a", "\\")
* r is now [true, false, true]
* ```
*
* The escape character is specified to include either `%` or `_` in the search,
* which is expected to be either 0 or 1 character.
* If more than one character is specified, only the first character is used.
*
* ```
* cv = ["abc_def", "abc1def", "abc_"]
* r = cv.like("abc/_d%", "/")
* r is now [true, false, false]
* ```
* Any null string entries return corresponding null output column entries.
*
* @param pattern Like pattern to match to each string.
* @param escapeChar Character specifies the escape prefix; default is "\\".
* @return New ColumnVector of boolean results for each string.
*/
public final ColumnVector like(Scalar pattern, Scalar escapeChar) {
assert type.equals(DType.STRING) : "column type must be a String";
assert pattern != null : "pattern scalar must not be null";
assert pattern.getType().equals(DType.STRING) : "pattern scalar must be a string scalar";
assert escapeChar != null : "escapeChar scalar must not be null";
assert escapeChar.getType().equals(DType.STRING) : "escapeChar scalar must be a string scalar";
return new ColumnVector(like(getNativeView(), pattern.getScalarHandle(), escapeChar.getScalarHandle()));
}


/**
* Converts all character sequences starting with '%' into character code-points
Expand Down Expand Up @@ -4034,6 +4074,16 @@ private static native long stringReplaceWithBackrefs(long columnView, String pat
*/
private static native long containsRe(long cudfViewHandle, String pattern) throws CudfException;

/**
* Native method for checking if strings match the passed in like pattern
* and escape character.
* @param cudfViewHandle native handle of the cudf::column_view being operated on.
* @param patternHandle handle of scalar containing the string like pattern.
* @param escapeCharHandle handle of scalar containing the string escape character.
* @return native handle of the resulting cudf column containing the boolean results.
*/
private static native long like(long cudfViewHandle, long patternHandle, long escapeCharHandle) throws CudfException;

/**
* Native method for checking if strings in a column contains a specified comparison string.
* @param cudfViewHandle native handle of the cudf::column_view being operated on.
Expand Down
18 changes: 18 additions & 0 deletions java/src/main/native/src/ColumnViewJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,24 @@ JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_containsRe(JNIEnv *env, j
CATCH_STD(env, 0);
}

JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_like(JNIEnv *env, jobject j_object,
jlong j_view_handle, jlong pattern,
jlong escapeChar) {
JNI_NULL_CHECK(env, j_view_handle, "column is null", false);
JNI_NULL_CHECK(env, pattern, "pattern is null", false);
JNI_NULL_CHECK(env, escapeChar, "escape character is null", false);

try {
cudf::jni::auto_set_device(env);
auto const column_view = reinterpret_cast<cudf::column_view const *>(j_view_handle);
auto const strings_column = cudf::strings_column_view{*column_view};
auto const pattern_scalar = reinterpret_cast<cudf::string_scalar const *>(pattern);
auto const escape_scalar = reinterpret_cast<cudf::string_scalar const *>(escapeChar);
return release_as_jlong(cudf::strings::like(strings_column, *pattern_scalar, *escape_scalar));
}
CATCH_STD(env, 0);
}

JNIEXPORT jlong JNICALL Java_ai_rapids_cudf_ColumnView_binaryOpVV(JNIEnv *env, jclass,
jlong lhs_view, jlong rhs_view,
jint int_op, jint out_dtype,
Expand Down
57 changes: 57 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ColumnVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4193,6 +4193,63 @@ void testContainsReEmptyInput() {
}
}

@Test
void testLike() {
// Default escape character
try (ColumnVector testStrings = ColumnVector.fromStrings(
"a", "aa", "aaa", "aba", "b", "bb", "bba", "", "áéêú", "a1b2c3");
Scalar patternString1 = Scalar.fromString("a1b2c3");
Scalar patternString2 = Scalar.fromString("__a%");
Scalar defaultEscape = Scalar.fromString("\\");
ColumnVector res1 = testStrings.like(patternString1, defaultEscape);
ColumnVector res2 = testStrings.like(patternString2, defaultEscape);
ColumnVector expected1 = ColumnVector.fromBoxedBooleans(
false, false, false, false, false, false, false, false, false, true);
ColumnVector expected2 = ColumnVector.fromBoxedBooleans(
false, false, true, true, false, false, true, false, false, false)) {
assertColumnsAreEqual(expected1, res1);
assertColumnsAreEqual(expected2, res2);
}
// Non-default escape character
try (ColumnVector testStrings = ColumnVector.fromStrings(
"10%-20%", "10-20", "10%%-20%", "a_b", "b_a", "___", "", "aéb", "_%_", "_%a");
Scalar patternString1 = Scalar.fromString("10%%%%-20%%");
Scalar patternString2 = Scalar.fromString("___%%");
Scalar escapeChar1 = Scalar.fromString("%");
Scalar escapeChar2 = Scalar.fromString("_");
ColumnVector res1 = testStrings.like(patternString1, escapeChar1);
ColumnVector res2 = testStrings.like(patternString2, escapeChar2);
ColumnVector expected1 = ColumnVector.fromBoxedBooleans(
false, false, true, false, false, false, false, false, false, false);
ColumnVector expected2 = ColumnVector.fromBoxedBooleans(
false, false, false, false, false, false, false, false, true, true)) {
assertColumnsAreEqual(expected1, res1);
assertColumnsAreEqual(expected2, res2);
}
assertThrows(AssertionError.class, () -> {
try (ColumnVector testStrings = ColumnVector.fromStrings("a", "B", "cd", null, "");
Scalar defaultEscape = Scalar.fromString("\\");
ColumnVector res = testStrings.like(null, defaultEscape)) {}
});
assertThrows(AssertionError.class, () -> {
try (ColumnVector testStrings = ColumnVector.fromStrings("a", "B", "cd", null, "");
Scalar patternString = Scalar.fromString("");
ColumnVector res = testStrings.like(patternString, null)) {}
});
assertThrows(AssertionError.class, () -> {
try (ColumnVector testStrings = ColumnVector.fromStrings("a", "B", "cd", null, "");
Scalar patternString = Scalar.fromString("");
Scalar intScalar = Scalar.fromInt(1);
ColumnVector res = testStrings.like(patternString, intScalar)) {}
});
assertThrows(AssertionError.class, () -> {
try (ColumnVector testStrings = ColumnVector.fromStrings("a", "B", "cd", null, "");
Scalar intScalar = Scalar.fromInt(1);
Scalar defaultEscape = Scalar.fromString("\\");
ColumnVector res = testStrings.like(intScalar, defaultEscape)) {}
});
}

@Test
void testUrlDecode() {
String[] inputs = new String[] {
Expand Down