Skip to content
Merged
23 changes: 3 additions & 20 deletions cpp/src/arrow/compute/kernels/scalar_cast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "arrow/chunked_array.h"
#include "arrow/extension_type.h"
#include "arrow/status.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/extension_type.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/random.h"
Expand All @@ -56,33 +57,15 @@ using internal::checked_pointer_cast;
namespace compute {

static std::shared_ptr<Array> InvalidUtf8(std::shared_ptr<DataType> type) {
return ArrayFromJSON(type,
"["
R"(
"Hi",
"olá mundo",
"你好世界",
"",
)"
"\"\xa0\xa1\""
"]");
return BinaryArrayFromStrings(type, {"Hi", "olá mundo", "你好世界", "", "\xa0\xa1"});
}

static std::shared_ptr<Array> FixedSizeInvalidUtf8(std::shared_ptr<DataType> type) {
if (type->id() == Type::FIXED_SIZE_BINARY) {
// Assume a particular width for testing
EXPECT_EQ(3, checked_cast<const FixedSizeBinaryType&>(*type).byte_width());
}
return ArrayFromJSON(type,
"["
R"(
"Hi!",
"lá",
"你",
" ",
)"
"\"\xa0\xa1\xa2\""
"]");
return BinaryArrayFromStrings(type, {"Hi!", "lá", "你", " ", "\xa0\xa1\xa2"});
}

static std::vector<std::shared_ptr<DataType>> kNumericTypes = {
Expand Down
81 changes: 36 additions & 45 deletions cpp/src/arrow/compute/kernels/scalar_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ TYPED_TEST(TestStringKernels, Utf8Reverse) {

// inputs with malformed utf8 chars would produce garbage output, but the end result
// would produce arrays with same lengths. Hence checking offset buffer equality
auto malformed_input = ArrayFromJSON(this->type(), "[\"ɑ\xFFɑa\", \"ɽ\xe1\xbdɽa\"]");
auto malformed_input = this->MakeArray({"ɑ\xFFɑa", "ɽ\xe1\xbdɽa"});
const Result<Datum>& res = CallFunction("utf8_reverse", {malformed_input});
ASSERT_TRUE(res->array()->buffers[1]->Equals(*malformed_input->data()->buffers[1]));
}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ TYPED_TEST(TestStringKernels, Utf8Upper) {
this->CheckUnary("utf8_upper", "[\"ɑɑɑɑ\"]", this->type(), "[\"ⱭⱭⱭⱭ\"]");

// Test invalid data
auto invalid_input = ArrayFromJSON(this->type(), "[\"ɑa\xFFɑ\", \"ɽ\xe1\xbdɽaa\"]");
auto invalid_input = this->MakeArray({"ɑa\xFFɑ", "ɽ\xe1\xbdɽaa"});
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("Invalid UTF8 sequence"),
CallFunction("utf8_upper", {invalid_input}));
}
Expand All @@ -1247,7 +1247,7 @@ TYPED_TEST(TestStringKernels, Utf8Lower) {
this->CheckUnary("utf8_lower", "[\"ȺȺȺȺ\"]", this->type(), "[\"ⱥⱥⱥⱥ\"]");

// Test invalid data
auto invalid_input = ArrayFromJSON(this->type(), "[\"Ⱥa\xFFⱭ\", \"Ɽ\xe1\xbdⱤaA\"]");
auto invalid_input = this->MakeArray({"Ⱥa\xFFⱭ", "Ɽ\xe1\xbdⱤaA"});
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("Invalid UTF8 sequence"),
CallFunction("utf8_lower", {invalid_input}));
}
Expand All @@ -1267,7 +1267,7 @@ TYPED_TEST(TestStringKernels, Utf8SwapCase) {
"[\"HeLLo, wOrLD!\", \"$. a35?\"]");

// Test invalid data
auto invalid_input = ArrayFromJSON(this->type(), "[\"Ⱥa\xFFⱭ\", \"Ɽ\xe1\xbdⱤaA\"]");
auto invalid_input = this->MakeArray({"Ⱥa\xFFⱭ", "Ɽ\xe1\xbdⱤaA"});
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("Invalid UTF8 sequence"),
CallFunction("utf8_swapcase", {invalid_input}));
}
Expand Down Expand Up @@ -2516,101 +2516,92 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) {
SliceOptions options{2, 4};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"a\xc2\xa2\", \"ab\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"\", \"\xa2\", \"\xc2\xa2\", \"\xc2\xff\"]", &options);
this->MakeArray({"", "a", "ab", "a\xc2\xa2", "ab\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "", "\xa2", "\xc2\xa2", "\xc2\xff"}), &options);
SliceOptions options_step{1, 5, 2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"a\xc2\xa2\", \"ab\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"b\", \"\xc2\", \"b\xa2\", \"b\xff\"]", &options_step);
this->MakeArray({"", "a", "ab", "a\xc2\xa2", "ab\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "b", "\xc2", "b\xa2", "b\xff"}), &options_step);
SliceOptions options_step_neg{5, 1, -2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"a\xc2\xa2\", \"ab\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"\", \"\xa2\", \"\xa2\", \"Z\xc2\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "a\xc2\xa2", "ab\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "", "\xa2", "\xa2", "Z\xc2"}), &options_step_neg);
options_step_neg.stop = 0;
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"a\xc2\xa2\", \"aZ\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"b\", \"\xa2\", \"\xa2Z\", \"Z\xc2\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "a\xc2\xa2", "aZ\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "b", "\xa2", "\xa2Z", "Z\xc2"}), &options_step_neg);
}

TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) {
SliceOptions options{2, -1};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"a\xc2\xa2\", \"aZ\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"\", \"\", \"\xc2\", \"\xc2\xff\"]", &options);
this->MakeArray({"", "a", "ab", "a\xc2\xa2", "aZ\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "", "", "\xc2", "\xc2\xff"}), &options);
SliceOptions options_step{1, -1, 2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"a\xc2\xa2\", \"aZ\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"\", \"\xc2\", \"Z\", \"b\xff\"]", &options_step);
this->MakeArray({"", "a", "ab", "a\xc2\xa2", "aZ\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "", "\xc2", "Z", "b\xff"}), &options_step);
SliceOptions options_step_neg{3, -4, -2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"a\", \"b\", \"\xa2Z\", \"\xa2Z\", \"\xff\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "a", "b", "\xa2Z", "\xa2Z", "\xff"}), &options_step_neg);
options_step_neg.stop = -5;
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"a\", \"b\", \"\xa2Z\", \"\xa2Z\", \"\xffP\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "a", "b", "\xa2Z", "\xa2Z", "\xffP"}), &options_step_neg);
}

TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) {
SliceOptions options{-2, -1};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"ab\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"a\", \"\xc2\", \"\xc2\", \"\xff\"]", &options);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "ab\xc2\xffZ"}),
this->MakeArray({"", "", "a", "\xc2", "\xc2", "\xff"}), &options);
SliceOptions options_step{-4, -1, 2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"a\", \"Z\", \"a\xc2\", \"P\xff\"]", &options_step);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "", "a", "Z", "a\xc2", "P\xff"}), &options_step);
SliceOptions options_step_neg{-1, -3, -2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"a\", \"b\", \"\xa2\", \"\xa2\", \"Z\"]", &options_step_neg);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "a", "b", "\xa2", "\xa2", "Z"}), &options_step_neg);
options_step_neg.stop = -4;
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"a\", \"b\", \"\xa2Z\", \"\xa2Z\", \"Z\xc2\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "a", "b", "\xa2Z", "\xa2Z", "Z\xc2"}), &options_step_neg);
}

TYPED_TEST(TestBinaryKernels, SliceBytesNegPos) {
SliceOptions options{-2, 4};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"a\", \"ab\", \"\xc2\xa2\", \"\xc2\xa2\", \"\xff\"]",
&options);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "a", "ab", "\xc2\xa2", "\xc2\xa2", "\xff"}), &options);
SliceOptions options_step{-4, 4, 2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"a\", \"a\", \"Z\xa2\", \"a\xc2\", \"P\xff\"]",
&options_step);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "a", "a", "Z\xa2", "a\xc2", "P\xff"}), &options_step);
SliceOptions options_step_neg{-1, 1, -2};
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"\", \"\xa2\", \"\xa2\", \"Z\xc2\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "", "", "\xa2", "\xa2", "Z\xc2"}), &options_step_neg);
options_step_neg.stop = 0;
this->CheckUnary(
"binary_slice",
"[\"\", \"a\", \"ab\", \"Z\xc2\xa2\", \"aZ\xc2\xa2\", \"aP\xc2\xffZ\"]",
this->type(), "[\"\", \"\", \"b\", \"\xa2\", \"\xa2Z\", \"Z\xc2\"]",
&options_step_neg);
this->MakeArray({"", "a", "ab", "Z\xc2\xa2", "aZ\xc2\xa2", "aP\xc2\xffZ"}),
this->MakeArray({"", "", "b", "\xa2", "\xa2Z", "Z\xc2"}), &options_step_neg);
}

TYPED_TEST(TestStringKernels, PadAscii) {
Expand Down
4 changes: 3 additions & 1 deletion cpp/src/arrow/extension/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "arrow/array/validate.h"
#include "arrow/ipc/test_common.h"
#include "arrow/record_batch.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/gtest_util.h"
#include "parquet/exception.h"

Expand Down Expand Up @@ -64,7 +65,8 @@ TEST_F(TestJsonExtensionType, JsonRoundtrip) {
TEST_F(TestJsonExtensionType, InvalidUTF8) {
for (const auto& storage_type : {utf8(), large_utf8(), utf8_view()}) {
auto json_type = json(storage_type);
auto invalid_input = ArrayFromJSON(storage_type, "[\"Ⱥa\xFF\", \"\xe1\xbdⱤaA\"]");
auto invalid_input =
BinaryArrayFromStrings(storage_type, {"Ⱥa\xFF", "\xe1\xbdⱤaA"});
auto ext_arr = ExtensionType::WrapArray(json_type, invalid_input);

ASSERT_RAISES_WITH_MESSAGE(Invalid,
Expand Down
Loading
Loading