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

case when improvement: avoid copy_if_else #2079

Merged
merged 10 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions src/main/cpp/src/case_when.cu
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ struct select_first_true_fn {
bool found_true = false;
cudf::size_type first_true_index = col_num;
for (auto col_idx = 0; !found_true && col_idx < col_num; col_idx++) {
if (d_table.column(col_idx).element<bool>(row_idx)) {
auto const& col = d_table.column(col_idx);
if (!col.is_null(row_idx) && col.element<bool>(row_idx)) {
// Predicate is true and not null
found_true = true;
first_true_index = col_idx;
}
Expand Down Expand Up @@ -114,7 +116,7 @@ std::unique_ptr<cudf::column> select_from_index(
cudf::size_type scalar_idx = d_select_index.element<cudf::size_type>(row_idx);

// return <str_ptr, str_size> pair
if (scalar_idx < num_of_scalar) {
if (scalar_idx < num_of_scalar && !d_scalars.is_null(scalar_idx)) {
auto const d_str = d_scalars.element<cudf::string_view>(scalar_idx);
return str_view{d_str.data(), d_str.size_bytes()};
} else {
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/com/nvidia/spark/rapids/jni/CaseWhenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.nvidia.spark.rapids.jni;

import ai.rapids.cudf.*;
import ai.rapids.cudf.HostColumnVector.Builder;

import org.junit.jupiter.api.Test;

import static ai.rapids.cudf.AssertUtils.assertColumnsAreEqual;
Expand All @@ -42,6 +44,47 @@ void selectIndexTest() {
}
}

public static ColumnVector fromBooleansWithNulls(Boolean... values) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Byte[] bytes = new Byte[values.length];
for (int i = 0; i < values.length; i++) {
if (values[i] == null) {
bytes[i] = null;
} else {
bytes[i] = values[i] ? (byte) 1 : (byte) 0;
}
}

try (Builder builder = HostColumnVector.builder(DType.BOOL8, bytes.length)) {
for (Byte bool : bytes) {
if (bool == null) {
builder.appendNull();
} else {
builder.append(bool.byteValue());
}
}
return builder.buildAndPutOnDevice();
}
}

@Test
void selectIndexTestWithNull() {
try (
ColumnVector b0 = fromBooleansWithNulls(
null, false, false, null, false);
ColumnVector b1 = fromBooleansWithNulls(
null, null, false, true, true);
ColumnVector b2 = fromBooleansWithNulls(
null, null, false, true, false);
ColumnVector b3 = fromBooleansWithNulls(
null, null, null, true, null);
ColumnVector expected = ColumnVector.fromInts(4, 4, 4, 1, 1)) {
ColumnVector[] boolColumns = new ColumnVector[] { b0, b1, b2, b3 };
try (ColumnVector actual = CaseWhen.selectFirstTrueIndex(boolColumns)) {
assertColumnsAreEqual(expected, actual);
}
}
}

@Test
void selectTest() {
try (ColumnVector values = ColumnVector.fromStrings(
Expand All @@ -53,4 +96,16 @@ void selectTest() {
assertColumnsAreEqual(expected, actual);
}
}

@Test
void selectTestWillNull() {
try (ColumnVector values = ColumnVector.fromStrings(
"s0", null, "s2", "s3");
ColumnVector selects = ColumnVector.fromInts(0, 1, 2, 3, 3, 2, 1, 0, 4, 5, 6);
ColumnVector expected = ColumnVector.fromStrings("s0", null, "s2", "s3", "s3", "s2", null, "s0", null, null,
null);
ColumnVector actual = CaseWhen.selectFromIndex(values, selects)) {
assertColumnsAreEqual(expected, actual);
}
}
}
Loading