|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use databend_common_arrow::arrow::array::Array; |
| 16 | +use databend_common_arrow::arrow::array::BinaryViewArray; |
| 17 | +use databend_common_arrow::arrow::array::Utf8ViewArray; |
| 18 | +use databend_common_arrow::arrow::bitmap::Bitmap; |
| 19 | +use databend_common_arrow::arrow::datatypes::DataType; |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn basics_string_view() { |
| 23 | + let data = vec![ |
| 24 | + Some("hello"), |
| 25 | + None, |
| 26 | + // larger than 12 bytes. |
| 27 | + Some("Databend Cloud is a Cost-Effective alternative to Snowflake."), |
| 28 | + ]; |
| 29 | + |
| 30 | + let array: Utf8ViewArray = data.into_iter().collect(); |
| 31 | + |
| 32 | + assert_eq!(array.value(0), "hello"); |
| 33 | + assert_eq!(array.value(1), ""); |
| 34 | + assert_eq!( |
| 35 | + array.value(2), |
| 36 | + "Databend Cloud is a Cost-Effective alternative to Snowflake." |
| 37 | + ); |
| 38 | + assert_eq!( |
| 39 | + unsafe { array.value_unchecked(2) }, |
| 40 | + "Databend Cloud is a Cost-Effective alternative to Snowflake." |
| 41 | + ); |
| 42 | + assert_eq!( |
| 43 | + array.validity(), |
| 44 | + Some(&Bitmap::from_u8_slice([0b00000101], 3)) |
| 45 | + ); |
| 46 | + assert!(array.is_valid(0)); |
| 47 | + assert!(!array.is_valid(1)); |
| 48 | + assert!(array.is_valid(2)); |
| 49 | + |
| 50 | + let array2 = Utf8ViewArray::new_unchecked( |
| 51 | + DataType::Utf8View, |
| 52 | + array.views().clone(), |
| 53 | + array.data_buffers().clone(), |
| 54 | + array.validity().cloned(), |
| 55 | + array.total_bytes_len(), |
| 56 | + array.total_buffer_len(), |
| 57 | + ); |
| 58 | + |
| 59 | + assert_eq!(array, array2); |
| 60 | + |
| 61 | + let array = array.sliced(1, 2); |
| 62 | + |
| 63 | + assert_eq!(array.value(0), ""); |
| 64 | + assert_eq!( |
| 65 | + array.value(1), |
| 66 | + "Databend Cloud is a Cost-Effective alternative to Snowflake." |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +fn basics_binary_view() { |
| 72 | + let data = vec![ |
| 73 | + Some(b"hello".to_vec()), |
| 74 | + None, |
| 75 | + // larger than 12 bytes. |
| 76 | + Some(b"Databend Cloud is a Cost-Effective alternative to Snowflake.".to_vec()), |
| 77 | + ]; |
| 78 | + |
| 79 | + let array: BinaryViewArray = data.into_iter().collect(); |
| 80 | + |
| 81 | + assert_eq!(array.value(0), b"hello"); |
| 82 | + assert_eq!(array.value(1), b""); |
| 83 | + assert_eq!( |
| 84 | + array.value(2), |
| 85 | + b"Databend Cloud is a Cost-Effective alternative to Snowflake." |
| 86 | + ); |
| 87 | + assert_eq!( |
| 88 | + unsafe { array.value_unchecked(2) }, |
| 89 | + b"Databend Cloud is a Cost-Effective alternative to Snowflake." |
| 90 | + ); |
| 91 | + assert_eq!( |
| 92 | + array.validity(), |
| 93 | + Some(&Bitmap::from_u8_slice([0b00000101], 3)) |
| 94 | + ); |
| 95 | + assert!(array.is_valid(0)); |
| 96 | + assert!(!array.is_valid(1)); |
| 97 | + assert!(array.is_valid(2)); |
| 98 | + |
| 99 | + let array2 = BinaryViewArray::new_unchecked( |
| 100 | + DataType::Utf8View, |
| 101 | + array.views().clone(), |
| 102 | + array.data_buffers().clone(), |
| 103 | + array.validity().cloned(), |
| 104 | + array.total_bytes_len(), |
| 105 | + array.total_buffer_len(), |
| 106 | + ); |
| 107 | + |
| 108 | + assert_eq!(array, array2); |
| 109 | + |
| 110 | + let array = array.sliced(1, 2); |
| 111 | + |
| 112 | + assert_eq!(array.value(0), b""); |
| 113 | + assert_eq!( |
| 114 | + array.value(1), |
| 115 | + b"Databend Cloud is a Cost-Effective alternative to Snowflake." |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +#[test] |
| 120 | +fn from() { |
| 121 | + let array = Utf8ViewArray::from([Some("hello"), Some(" "), None]); |
| 122 | + |
| 123 | + let a = array.validity().unwrap(); |
| 124 | + assert_eq!(a, &Bitmap::from([true, true, false])); |
| 125 | + |
| 126 | + let array = BinaryViewArray::from([Some(b"hello".to_vec()), Some(b" ".to_vec()), None]); |
| 127 | + |
| 128 | + let a = array.validity().unwrap(); |
| 129 | + assert_eq!(a, &Bitmap::from([true, true, false])); |
| 130 | +} |
0 commit comments