diff --git a/cpp/src/arrow/compute/kernels/scalar_cast_string.cc b/cpp/src/arrow/compute/kernels/scalar_cast_string.cc index 586aeaf5328..5df6d75af6c 100644 --- a/cpp/src/arrow/compute/kernels/scalar_cast_string.cc +++ b/cpp/src/arrow/compute/kernels/scalar_cast_string.cc @@ -147,10 +147,13 @@ void AddNumberToStringCasts(std::shared_ptr out_ty, CastFunction* func std::vector> GetBinaryLikeCasts() { auto cast_binary = std::make_shared("cast_binary", Type::BINARY); AddCommonCasts(Type::BINARY, binary(), cast_binary.get()); + AddZeroCopyCast(Type::STRING, {utf8()}, binary(), cast_binary.get()); auto cast_large_binary = std::make_shared("cast_large_binary", Type::LARGE_BINARY); AddCommonCasts(Type::LARGE_BINARY, large_binary(), cast_large_binary.get()); + AddZeroCopyCast(Type::LARGE_STRING, {large_utf8()}, large_binary(), + cast_large_binary.get()); auto cast_fsb = std::make_shared("cast_fixed_size_binary", Type::FIXED_SIZE_BINARY); diff --git a/cpp/src/arrow/compute/kernels/scalar_cast_test.cc b/cpp/src/arrow/compute/kernels/scalar_cast_test.cc index a3a683c1af5..81d5605f078 100644 --- a/cpp/src/arrow/compute/kernels/scalar_cast_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_cast_test.cc @@ -157,6 +157,27 @@ class TestCast : public TestBase { src_type, strings, all, dest_type, strings, options); } + template + void TestCastStringToBinary() { + CastOptions options; + auto src_type = TypeTraits::type_singleton(); + auto dest_type = TypeTraits::type_singleton(); + + // All valid except the last one + std::vector all = {1, 1, 1, 1, 1}; + std::vector valid = {1, 1, 1, 1, 0}; + std::vector strings = {"Hi", "olá mundo", "你好世界", "", kInvalidUtf8}; + + std::shared_ptr array; + + // Should accept when invalid but null. + ArrayFromVector(src_type, valid, strings, &array); + CheckZeroCopy(*array, dest_type); + + CheckCase( + src_type, strings, all, dest_type, strings, options); + } + template void TestCastNumberToString() { auto dest_type = TypeTraits::type_singleton(); @@ -1363,6 +1384,12 @@ TEST_F(TestCast, LargeBinaryToLargeString) { TestCastBinaryToString(); } +TEST_F(TestCast, StringToBinary) { TestCastStringToBinary(); } + +TEST_F(TestCast, LargeStringToLargeBinary) { + TestCastStringToBinary(); +} + TEST_F(TestCast, NumberToString) { TestCastNumberToString(); } TEST_F(TestCast, NumberToLargeString) { TestCastNumberToString(); }