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

Java: Support creating a scalar from utf8 string [skip ci] #8294

Merged
merged 2 commits into from
May 20, 2021
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
11 changes: 10 additions & 1 deletion java/src/main/java/ai/rapids/cudf/Scalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,19 @@ public static Scalar timestampFromLong(DType type, Long value) {
}

public static Scalar fromString(String value) {
return fromUTF8String(value == null ? null : value.getBytes(StandardCharsets.UTF_8));
}

/**
* Creates a String scalar from an array of UTF8 bytes.
* @param value the array of UTF8 bytes
* @return a String scalar
*/
public static Scalar fromUTF8String(byte[] value) {
if (value == null) {
return fromNull(DType.STRING);
}
return new Scalar(DType.STRING, makeStringScalar(value.getBytes(StandardCharsets.UTF_8), true));
return new Scalar(DType.STRING, makeStringScalar(value, true));
}

/**
Expand Down
17 changes: 17 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ScalarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static ai.rapids.cudf.TableTest.assertColumnsAreEqual;
Expand Down Expand Up @@ -244,6 +245,22 @@ public void testString() {
}
}

@Test
public void testUTF8String() {
try (Scalar s = Scalar.fromUTF8String("TEST".getBytes(StandardCharsets.UTF_8))) {
assertEquals(DType.STRING, s.getType());
assertTrue(s.isValid());
assertEquals("TEST", s.getJavaString());
assertArrayEquals(new byte[]{'T', 'E', 'S', 'T'}, s.getUTF8());
}
try (Scalar s = Scalar.fromUTF8String("".getBytes(StandardCharsets.UTF_8))) {
assertEquals(DType.STRING, s.getType());
assertTrue(s.isValid());
assertEquals("", s.getJavaString());
assertArrayEquals(new byte[]{}, s.getUTF8());
}
}

@Test
public void testList() {
// list of int
Expand Down