Skip to content
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
3 changes: 2 additions & 1 deletion cpp/src/gandiva/function_registry_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ std::vector<NativeFunction> GetStringFunctionRegistry() {
NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors),

NativeFunction("binary_string", {}, DataTypeVector{utf8()}, binary(),
kResultNullIfNull, "binary_string", NativeFunction::kNeedsContext),
kResultNullIfNull, "binary_string",
NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors),

NativeFunction("left", {}, DataTypeVector{utf8(), int32()}, utf8(),
kResultNullIfNull, "left_utf8_int32", NativeFunction::kNeedsContext),
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/gandiva/precompiled/string_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,11 @@ const char* right_utf8_int32(gdv_int64 context, const char* text, gdv_int32 text
FORCE_INLINE
const char* binary_string(gdv_int64 context, const char* text, gdv_int32 text_len,
gdv_int32* out_len) {
if (text_len == 0) {
*out_len = 0;
return "";

Choose a reason for hiding this comment

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

Java version is NullHandling.NULL_IF_NULL
Are we going to return empty string for null? If so - that's inconsistency we can't have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It returns null, the same as Java.

}

gdv_binary ret =
reinterpret_cast<gdv_binary>(gdv_fn_context_arena_malloc(context, text_len));

Expand All @@ -2261,11 +2266,6 @@ const char* binary_string(gdv_int64 context, const char* text, gdv_int32 text_le
return "";
}

if (text_len == 0) {
*out_len = 0;
return "";
}

// converting hex encoded string to normal string
int j = 0;
for (int i = 0; i < text_len; i++, j++) {
Expand Down
20 changes: 16 additions & 4 deletions cpp/src/gandiva/precompiled/string_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1883,10 +1883,6 @@ TEST(TestStringOps, TestBinaryString) {
std::string output = std::string(out_str, out_len);
EXPECT_EQ(output, "TestString");

out_str = binary_string(ctx_ptr, "", 0, &out_len);
output = std::string(out_str, out_len);
EXPECT_EQ(output, "");

out_str = binary_string(ctx_ptr, "T", 1, &out_len);
output = std::string(out_str, out_len);
EXPECT_EQ(output, "T");
Expand All @@ -1912,6 +1908,22 @@ TEST(TestStringOps, TestBinaryString) {
EXPECT_EQ(output, "OM");
}

TEST(TestStringOps, TestBinaryStringNull) {
// This test is only valid if it is the first to trigger a memory allocation in the
// context.
gandiva::ExecutionContext ctx;
uint64_t ctx_ptr = reinterpret_cast<gdv_int64>(&ctx);
gdv_int32 out_len = 0;
const char* out_str;

std::string output;

out_str = binary_string(ctx_ptr, "", 0, &out_len);
ASSERT_FALSE(ctx.has_error());
output = std::string(out_str, out_len);
EXPECT_EQ(output, "");
}

TEST(TestStringOps, TestSplitPart) {
gandiva::ExecutionContext ctx;
uint64_t ctx_ptr = reinterpret_cast<gdv_int64>(&ctx);
Expand Down
Loading