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

Create a String column from UTF8 String byte arrays [skip ci] #8257

Merged
merged 5 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ColumnVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,16 @@ public static ColumnVector fromStrings(String... values) {
}
}

/**
* Create a new string vector from the given values. This API
* supports inline nulls.
*/
public static ColumnVector fromUTF8StringsBytes(byte[]... values) {
try (HostColumnVector host = HostColumnVector.fromUTF8StringsBytes(values)) {
return host.copyToDevice();
}
}

/**
* Create a new vector from the given values. This API supports inline nulls,
* but is much slower than building from primitive array of unscaledValues.
Expand Down
39 changes: 38 additions & 1 deletion java/src/main/java/ai/rapids/cudf/HostColumnVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -577,6 +578,40 @@ public static HostColumnVector fromStrings(String... values) {
});
}

/**
* Create a new string vector from the given values. This API
* supports inline nulls.
*/
public static HostColumnVector fromUTF8StringsBytes(byte[]... values) {
jlowe marked this conversation as resolved.
Show resolved Hide resolved
int rows = values.length;
long nullCount = 0;
long bufferSize = 0;
// How many bytes do we need to hold the data.
for (byte[] s: values) {
if (s == null) {
nullCount++;
} else {
bufferSize += s.length;
}
}

BiConsumer<Builder, byte[]> appendUTF8 = nullCount == 0 ?
(b, s) -> b.appendUTF8String(s) :
(b, s) -> {
if (s == null) {
b.appendNull();
} else {
b.appendUTF8String(s);
}
};

return build(rows, bufferSize, (b) -> {
for (byte[] s: values) {
appendUTF8.accept(b, s);
}
});
}

/**
* Create a new vector from the given values. This API supports inline nulls,
* but is much slower than building from primitive array of unscaledValues.
Expand Down Expand Up @@ -1085,9 +1120,11 @@ private void appendChildOrNull(ColumnBuilder childBuilder, Object listElement) {
} else if (listElement instanceof BigDecimal) {
childBuilder.append((BigDecimal) listElement);
} else if (listElement instanceof List) {
childBuilder.append((List) listElement);
childBuilder.append((List<?>) listElement);
wjxiz1992 marked this conversation as resolved.
Show resolved Hide resolved
} else if (listElement instanceof StructData) {
childBuilder.append((StructData) listElement);
} else if (listElement instanceof byte[]) {
childBuilder.appendUTF8String((byte[]) listElement);
jlowe marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new IllegalStateException("Unexpected element type: " + listElement.getClass());
}
Expand Down
14 changes: 14 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ColumnVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -176,6 +177,19 @@ void testStringCreation() {
}
}

@Test
void testUTF8StringCreation() {
try (ColumnVector cv = ColumnVector.fromUTF8StringsBytes(
"d".getBytes(StandardCharsets.UTF_8),
"sd".getBytes(StandardCharsets.UTF_8),
"sde".getBytes(StandardCharsets.UTF_8),
null,
"END".getBytes(StandardCharsets.UTF_8));
ColumnVector expected = ColumnVector.fromStrings("d", "sd", "sde", null, "END")) {
TableTest.assertColumnsAreEqual(expected, cv);
}
}

@Test
void testRefCountLeak() throws InterruptedException {
assumeTrue(Boolean.getBoolean("ai.rapids.cudf.flaky-tests-enabled"));
Expand Down