| 1015 | |
| 1016 | #[test] |
| 1017 | fn test_long_string_view_allocation() { |
| 1018 | // The JSON input contains field "a" with different string lengths. |
| 1019 | // According to the implementation in the decoder: |
| 1020 | // - For a string, capacity is only increased if its length > 12 bytes. |
| 1021 | // Therefore, for: |
| 1022 | // Row 1: "short" (5 bytes) -> capacity += 0 |
| 1023 | // Row 2: "this is definitely long" (24 bytes) -> capacity += 24 |
| 1024 | // Row 3: "hello" (5 bytes) -> capacity += 0 |
| 1025 | // Row 4: "\nfoobar😀asfgÿ" (17 bytes) -> capacity += 17 |
| 1026 | // Expected total capacity = 24 + 17 = 41 |
| 1027 | let expected_capacity: usize = 41; |
| 1028 | |
| 1029 | let buf = r#" |
| 1030 | {"a": "short", "b": "dummy"} |
| 1031 | {"a": "this is definitely long", "b": "dummy"} |
| 1032 | {"a": "hello", "b": "dummy"} |
| 1033 | {"a": "\nfoobar😀asfgÿ", "b": "dummy"} |
| 1034 | "#; |
| 1035 | |
| 1036 | let schema = Arc::new(Schema::new(vec![ |
| 1037 | Field::new("a", DataType::Utf8View, true), |
| 1038 | Field::new("b", DataType::LargeUtf8, true), |
| 1039 | ])); |
| 1040 | |
| 1041 | let batches = do_read(buf, 1024, false, false, schema); |
| 1042 | assert_eq!(batches.len(), 1, "Expected one record batch"); |
| 1043 | |
| 1044 | // Get the first column ("a") as a StringViewArray. |
| 1045 | let col_a = batches[0].column(0); |
| 1046 | let string_view_array = col_a |
| 1047 | .as_any() |
| 1048 | .downcast_ref::<StringViewArray>() |
| 1049 | .expect("Column should be a StringViewArray"); |
| 1050 | |
| 1051 | // Retrieve the underlying data buffer from the array. |
| 1052 | // The builder pre-allocates capacity based on the sum of lengths for long strings. |
| 1053 | let data_buffer = string_view_array.to_data().buffers()[0].len(); |
| 1054 | |
| 1055 | // Check that the allocated capacity is at least what we expected. |
| 1056 | // (The actual buffer may be larger than expected due to rounding or internal allocation strategies.) |
| 1057 | assert!( |
| 1058 | data_buffer >= expected_capacity, |
| 1059 | "Data buffer length ({data_buffer}) should be at least {expected_capacity}", |
| 1060 | ); |
| 1061 | |
| 1062 | // Additionally, verify that the decoded values are correct. |
| 1063 | assert_eq!(string_view_array.value(0), "short"); |
| 1064 | assert_eq!(string_view_array.value(1), "this is definitely long"); |
| 1065 | assert_eq!(string_view_array.value(2), "hello"); |
| 1066 | assert_eq!(string_view_array.value(3), "\nfoobar😀asfgÿ"); |
| 1067 | } |
| 1068 | |
| 1069 | /// Test the memory capacity allocation logic when converting numeric types to strings. |
| 1070 | #[test] |