@@ -2275,10 +2275,6 @@ TEST(Cast, BooleanToString) {
2275
2275
TEST (Cast, ListToPrimitive) {
2276
2276
ASSERT_RAISES (NotImplemented,
2277
2277
Cast (*ArrayFromJSON (list (int8 ()), " [[1, 2], [3, 4]]" ), uint8 ()));
2278
-
2279
- ASSERT_RAISES (
2280
- NotImplemented,
2281
- Cast (*ArrayFromJSON (list (binary ()), R"( [["1", "2"], ["3", "4"]])" ), utf8 ()));
2282
2278
}
2283
2279
2284
2280
using make_list_t = std::shared_ptr<DataType>(const std::shared_ptr<DataType>&);
@@ -2429,6 +2425,40 @@ TEST(Cast, FSLToList) {
2429
2425
CheckCast (fsl_int32, ArrayFromJSON (fixed_size_list (int16 (), 1 ), " [[32689]]" ), options);
2430
2426
}
2431
2427
2428
+ TEST (Cast, FSLToString) {
2429
+ auto CheckFSLToStringCast = [](const std::shared_ptr<DataType>& fsl_type,
2430
+ const std::string& fsl_json,
2431
+ const std::string& expected_str) {
2432
+ std::shared_ptr<Array> src = ArrayFromJSON (fsl_type, fsl_json);
2433
+ ASSERT_OK_AND_ASSIGN (auto casted_str, Cast (*src, utf8 ()));
2434
+
2435
+ std::shared_ptr<Array> expected_array = ArrayFromJSON (utf8 (), expected_str);
2436
+ ASSERT_TRUE (casted_str->Equals (expected_array)) << casted_str->ToString ();
2437
+ };
2438
+
2439
+ // Example with int32 list
2440
+ std::shared_ptr<DataType> fsl_type = fixed_size_list (int32 (), 3 );
2441
+ const std::string fsl_json = R"( [[1, 2, 3], [4, 5, 6], [7, null, 8], null])" ;
2442
+ const std::string expected_str =
2443
+ " [\" fixed_size_list<item: int32>[3][1, 2, 3]\" , "
2444
+ " \" fixed_size_list<item: int32>[3][4, 5, 6]\" , "
2445
+ " \" fixed_size_list<item: int32>[3][7, null, 8]\" , "
2446
+ " \" null\" ]" ;
2447
+
2448
+ CheckFSLToStringCast (fsl_type, fsl_json, expected_str);
2449
+
2450
+ // Example with nested fixed_size_list<int32> of size 2
2451
+ fsl_type = fixed_size_list (fixed_size_list (int32 (), 2 ), 2 );
2452
+ const std::string nested_fsl_json = R"( [[[1, 2], [3, 4]], [[null, 5], null]])" ;
2453
+ const std::string expected_nested_str =
2454
+ " [\" fixed_size_list<item: fixed_size_list<item: int32>[2]>[2]"
2455
+ " [fixed_size_list<item: int32>[2][1, 2], fixed_size_list<item: int32>[2][3, 4]]\" , "
2456
+ " \" fixed_size_list<item: fixed_size_list<item: int32>[2]>[2]"
2457
+ " [fixed_size_list<item: int32>[2][null, 5], null]\" ]" ;
2458
+
2459
+ CheckFSLToStringCast (fsl_type, nested_fsl_json, expected_nested_str);
2460
+ }
2461
+
2432
2462
TEST (Cast, ListToFSL) {
2433
2463
CheckCastList (list (int16 ()), fixed_size_list (int16 (), 2 ),
2434
2464
" [[0, 1], [2, 3], null, [null, 5], null]" );
@@ -2476,6 +2506,40 @@ TEST(Cast, ListToFSL) {
2476
2506
CastOptions::Safe (fixed_size_list (int32 (), 3 ))));
2477
2507
}
2478
2508
2509
+ TEST (Cast, ListToString) {
2510
+ auto CheckListToStringCast = [](const std::shared_ptr<DataType>& list_type,
2511
+ const std::string& list_json,
2512
+ const std::string& expected_str) {
2513
+ std::shared_ptr<Array> src = ArrayFromJSON (list_type, list_json);
2514
+ ASSERT_OK_AND_ASSIGN (auto casted_str, Cast (*src, utf8 ()));
2515
+
2516
+ std::shared_ptr<Array> expected_array = ArrayFromJSON (utf8 (), expected_str);
2517
+ ASSERT_TRUE (casted_str->Equals (expected_array)) << casted_str->ToString ();
2518
+ };
2519
+
2520
+ // Example with int32 list
2521
+ std::shared_ptr<DataType> list_type = list (int32 ());
2522
+ const std::string list_json = R"( [[1, 2, 3], [4, 5], [6], []])" ;
2523
+ const std::string expected_str =
2524
+ R"( ["list<item: int32>[1, 2, 3]",
2525
+ "list<item: int32>[4, 5]",
2526
+ "list<item: int32>[6]",
2527
+ "list<item: int32>[]"])" ;
2528
+
2529
+ CheckListToStringCast (list_type, list_json, expected_str);
2530
+
2531
+ // Example with nested list of int32
2532
+ list_type = list (list (int32 ()));
2533
+ const std::string nested_list_json = R"( [[[1, 2], [3, 4]], [[5], [6, 7]], [[]], []])" ;
2534
+ const std::string expected_nested_str =
2535
+ R"( ["list<item: list<item: int32>>[list<item: int32>[1, 2], list<item: int32>[3, 4]]",
2536
+ "list<item: list<item: int32>>[list<item: int32>[5], list<item: int32>[6, 7]]",
2537
+ "list<item: list<item: int32>>[list<item: int32>[]]",
2538
+ "list<item: list<item: int32>>[]"])" ;
2539
+
2540
+ CheckListToStringCast (list_type, nested_list_json, expected_nested_str);
2541
+ }
2542
+
2479
2543
TEST (Cast, CastMap) {
2480
2544
const std::string map_json =
2481
2545
" [[[\" x\" , 1], [\" y\" , 8], [\" z\" , 9]], [[\" x\" , 6]], [[\" y\" , 36]]]" ;
@@ -2509,9 +2573,9 @@ TEST(Cast, CastMap) {
2509
2573
std::shared_ptr<DataType> dst_type = map (utf8 (), field (" y" , list (field (" b" , int64 ()))));
2510
2574
2511
2575
std::shared_ptr<Array> src =
2512
- ArrayFromJSON (src_type, " [[[\" 1 \ " , [1,2,3]]], [[\" 2 \ " , [4,5,6]]]]" );
2576
+ ArrayFromJSON (src_type, R"( [[["1 ", [1,2,3]]], [["2 ", [4,5,6]]]]) " );
2513
2577
std::shared_ptr<Array> dst =
2514
- ArrayFromJSON (dst_type, " [[[\" 1 \ " , [1,2,3]]], [[\" 2 \ " , [4,5,6]]]]" );
2578
+ ArrayFromJSON (dst_type, R"( [[["1 ", [1,2,3]]], [["2 ", [4,5,6]]]]) " );
2515
2579
2516
2580
CheckCast (src, dst);
2517
2581
@@ -2524,6 +2588,49 @@ TEST(Cast, CastMap) {
2524
2588
Cast(src, dst_type));
2525
2589
}
2526
2590
2591
+ void CheckMapToStringCast (const std::string& map_json,
2592
+ const std::string& map_json_nullable,
2593
+ const std::vector<std::string>& expected_str,
2594
+ const std::vector<std::string>& expected_str_nullable,
2595
+ const std::shared_ptr<DataType>& src_type) {
2596
+ auto check_cast = [&](const std::string& json,
2597
+ const std::vector<std::string>& expected) {
2598
+ std::shared_ptr<Array> src = ArrayFromJSON (src_type, json);
2599
+ for (int64_t i = 0 ; i < src->length (); ++i) {
2600
+ ASSERT_OK_AND_ASSIGN (auto scalar, src->GetScalar (i));
2601
+ ASSERT_OK_AND_ASSIGN (auto casted_str, Cast (scalar, utf8 ()));
2602
+ ASSERT_EQ (casted_str.scalar ()->type ->id (), utf8 ()->id ());
2603
+ ASSERT_EQ (casted_str.scalar ()->ToString (), expected[i]);
2604
+ }
2605
+ };
2606
+
2607
+ check_cast (map_json, expected_str);
2608
+ check_cast (map_json_nullable, expected_str_nullable);
2609
+ }
2610
+
2611
+ TEST (Cast, MapToString) {
2612
+ const std::string map_json =
2613
+ " [[[\" x\" , 1], [\" y\" , 8], [\" z\" , 9]], [[\" x\" , 6]], [[\" y\" , 36]]]" ;
2614
+ const std::string map_json_nullable =
2615
+ " [[[\" x\" , 1], [\" y\" , null], [\" z\" , 9]], null, [[\" y\" , 36]]]" ;
2616
+
2617
+ const std::vector<std::string> expected_str = {
2618
+ " map<string ('x'), int64 ('y')>[{x:string = x, y:int64 = 1}, "
2619
+ " {x:string = y, y:int64 = 8}, {x:string = z, y:int64 = 9}]" ,
2620
+ " map<string ('x'), int64 ('y')>[{x:string = x, y:int64 = 6}]" ,
2621
+ " map<string ('x'), int64 ('y')>[{x:string = y, y:int64 = 36}]" };
2622
+
2623
+ const std::vector<std::string> expected_str_nullable = {
2624
+ " map<string ('x'), int64 ('y')>[{x:string = x, y:int64 = 1}, "
2625
+ " {x:string = y, y:int64 = null}, {x:string = z, y:int64 = 9}]" ,
2626
+ " null" , " map<string ('x'), int64 ('y')>[{x:string = y, y:int64 = 36}]" };
2627
+
2628
+ auto src_type =
2629
+ std::make_shared<MapType>(field (" x" , utf8 (), false ), field (" y" , int64 ()));
2630
+ CheckMapToStringCast (map_json, map_json_nullable, expected_str, expected_str_nullable,
2631
+ src_type);
2632
+ }
2633
+
2527
2634
static void CheckStructToStruct (
2528
2635
const std::vector<std::shared_ptr<DataType>>& value_types) {
2529
2636
for (const auto & src_value_type : value_types) {
0 commit comments