Skip to content

Commit

Permalink
Create a String column from UTF8 String byte arrays (#8257)
Browse files Browse the repository at this point in the history
This PR is to support creating a `ColumnVector ` from the byte arrays of UTF8 Strings.

And also let the `Struct` children creation support UTF8 Strings.

Closes #8137

Signed-off-by: Firestarman <[email protected]>

Authors:
  - Liangcai Li (https://github.com/firestarman)

Approvers:
  - Allen Xu (https://github.com/wjxiz1992)
  - Jason Lowe (https://github.com/jlowe)
  - Robert (Bobby) Evans (https://github.com/revans2)
  - Alfred Xu (https://github.com/sperlingxx)

URL: #8257
  • Loading branch information
firestarman authored May 20, 2021
1 parent c732cef commit 2da8473
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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 @@ -1276,6 +1276,16 @@ public static ColumnVector fromStrings(String... values) {
}
}

/**
* Create a new string vector from the given values. This API
* supports inline nulls.
*/
public static ColumnVector fromUTF8Strings(byte[]... values) {
try (HostColumnVector host = HostColumnVector.fromUTF8Strings(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 fromUTF8Strings(byte[]... values) {
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);
} else if (listElement instanceof StructData) {
childBuilder.append((StructData) listElement);
} else if (listElement instanceof byte[]) {
childBuilder.appendUTF8String((byte[]) listElement);
} 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.fromUTF8Strings(
"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

0 comments on commit 2da8473

Please sign in to comment.