diff --git a/cpp/src/gandiva/precompiled/string_ops.cc b/cpp/src/gandiva/precompiled/string_ops.cc index bbbd2494dd97e..bf8fa373501b2 100644 --- a/cpp/src/gandiva/precompiled/string_ops.cc +++ b/cpp/src/gandiva/precompiled/string_ops.cc @@ -1807,9 +1807,9 @@ gdv_int32 locate_utf8_utf8_int32(gdv_int64 context, const char* sub_str, gdv_fn_context_set_error_msg(context, "Start position must be greater than 0"); return 0; } - - if (str_len == 0 || sub_str_len == 0) { - return 0; + // TO align with vanilla spark. + if (sub_str_len == 0) { + return 1; } gdv_int32 byte_pos = utf8_byte_pos(context, str, str_len, start_pos - 1); diff --git a/cpp/src/gandiva/precompiled/string_ops_test.cc b/cpp/src/gandiva/precompiled/string_ops_test.cc index 8a8d5c4104a11..7359d779f6a7c 100644 --- a/cpp/src/gandiva/precompiled/string_ops_test.cc +++ b/cpp/src/gandiva/precompiled/string_ops_test.cc @@ -1539,7 +1539,11 @@ TEST(TestStringOps, TestLocate) { EXPECT_FALSE(ctx.has_error()); pos = locate_utf8_utf8_int32(ctx_ptr, "", 0, "str", 3, 1); - EXPECT_EQ(pos, 0); + EXPECT_EQ(pos, 1); + EXPECT_FALSE(ctx.has_error()); + + pos = locate_utf8_utf8_int32(ctx_ptr, "", 0, "", 0, 1); + EXPECT_EQ(pos, 1); EXPECT_FALSE(ctx.has_error()); pos = locate_utf8_utf8_int32(ctx_ptr, "bar", 3, "barbar", 6, 0);