| 1069 | /// Test the memory capacity allocation logic when converting numeric types to strings. |
| 1070 | #[test] |
| 1071 | fn test_numeric_view_allocation() { |
| 1072 | // For numeric types, the expected capacity calculation is as follows: |
| 1073 | // Row 1: 123456789 -> Number converts to the string "123456789" (length 9), 9 <= 12, so no capacity is added. |
| 1074 | // Row 2: 1000000000000 -> Treated as an I64 number; its string is "1000000000000" (length 13), |
| 1075 | // which is >12 and its absolute value is > 999_999_999_999, so 13 bytes are added. |
| 1076 | // Row 3: 3.1415 -> F32 number, a fixed estimate of 10 bytes is added. |
| 1077 | // Row 4: 2.718281828459045 -> F64 number, a fixed estimate of 10 bytes is added. |
| 1078 | // Total expected capacity = 13 + 10 + 10 = 33 bytes. |
| 1079 | let expected_capacity: usize = 33; |
| 1080 | |
| 1081 | let buf = r#" |
| 1082 | {"n": 123456789} |
| 1083 | {"n": 1000000000000} |
| 1084 | {"n": 3.1415} |
| 1085 | {"n": 2.718281828459045} |
| 1086 | "#; |
| 1087 | |
| 1088 | let schema = Arc::new(Schema::new(vec![Field::new("n", DataType::Utf8View, true)])); |
| 1089 | |
| 1090 | let batches = do_read(buf, 1024, true, false, schema); |
| 1091 | assert_eq!(batches.len(), 1, "Expected one record batch"); |
| 1092 | |
| 1093 | let col_n = batches[0].column(0); |
| 1094 | let string_view_array = col_n |
| 1095 | .as_any() |
| 1096 | .downcast_ref::<StringViewArray>() |
| 1097 | .expect("Column should be a StringViewArray"); |
| 1098 | |
| 1099 | // Check that the underlying data buffer capacity is at least the expected value. |
| 1100 | let data_buffer = string_view_array.to_data().buffers()[0].len(); |
| 1101 | assert!( |
| 1102 | data_buffer >= expected_capacity, |
| 1103 | "Data buffer length ({data_buffer}) should be at least {expected_capacity}", |
| 1104 | ); |
| 1105 | |
| 1106 | // Verify that the converted string values are correct. |
| 1107 | // Note: The format of the number converted to a string should match the actual implementation. |
| 1108 | assert_eq!(string_view_array.value(0), "123456789"); |
| 1109 | assert_eq!(string_view_array.value(1), "1000000000000"); |
| 1110 | assert_eq!(string_view_array.value(2), "3.1415"); |
| 1111 | assert_eq!(string_view_array.value(3), "2.718281828459045"); |
| 1112 | } |
| 1113 | |
| 1114 | #[test] |
| 1115 | fn test_string_with_uft8view() { |