|
| 1 | +// Copyright 2022 Singularity Data |
| 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 risingwave_common::array::ListRef; |
| 16 | +use risingwave_common::error::Result; |
| 17 | +use risingwave_common::types::{Scalar, ToOwnedDatum}; |
| 18 | + |
| 19 | +#[inline(always)] |
| 20 | +pub fn array_access<T: Scalar>(l: Option<ListRef>, r: Option<i32>) -> Result<Option<T>> { |
| 21 | + match (l, r) { |
| 22 | + // index must be greater than 0 following a one-based numbering convention for arrays |
| 23 | + (Some(list), Some(index)) if index > 0 => { |
| 24 | + let datumref = list.value_at(index as usize)?; |
| 25 | + if let Some(scalar) = datumref.to_owned_datum() { |
| 26 | + Ok(Some(scalar.try_into()?)) |
| 27 | + } else { |
| 28 | + Ok(None) |
| 29 | + } |
| 30 | + } |
| 31 | + _ => Ok(None), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg(test)] |
| 36 | +mod tests { |
| 37 | + |
| 38 | + use risingwave_common::array::ListValue; |
| 39 | + use risingwave_common::types::ScalarImpl; |
| 40 | + |
| 41 | + use super::*; |
| 42 | + |
| 43 | + #[test] |
| 44 | + fn test_int32_array_access() { |
| 45 | + let v1 = ListValue::new(vec![ |
| 46 | + Some(ScalarImpl::Int32(1)), |
| 47 | + Some(ScalarImpl::Int32(2)), |
| 48 | + Some(ScalarImpl::Int32(3)), |
| 49 | + ]); |
| 50 | + let l1 = ListRef::ValueRef { val: &v1 }; |
| 51 | + |
| 52 | + assert_eq!(array_access::<i32>(Some(l1), Some(1)), Ok(Some(1))); |
| 53 | + assert_eq!(array_access::<i32>(Some(l1), Some(-1)), Ok(None)); |
| 54 | + assert_eq!(array_access::<i32>(Some(l1), Some(0)), Ok(None)); |
| 55 | + assert_eq!(array_access::<i32>(Some(l1), Some(4)), Ok(None)); |
| 56 | + } |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn test_utf8_array_access() { |
| 60 | + let v1 = ListValue::new(vec![ |
| 61 | + Some(ScalarImpl::Utf8("来自".into())), |
| 62 | + Some(ScalarImpl::Utf8("foo".into())), |
| 63 | + Some(ScalarImpl::Utf8("bar".into())), |
| 64 | + ]); |
| 65 | + let v2 = ListValue::new(vec![ |
| 66 | + Some(ScalarImpl::Utf8("fizz".into())), |
| 67 | + Some(ScalarImpl::Utf8("荷兰".into())), |
| 68 | + Some(ScalarImpl::Utf8("buzz".into())), |
| 69 | + ]); |
| 70 | + let v3 = ListValue::new(vec![None, None, Some(ScalarImpl::Utf8("的爱".into()))]); |
| 71 | + |
| 72 | + let l1 = ListRef::ValueRef { val: &v1 }; |
| 73 | + let l2 = ListRef::ValueRef { val: &v2 }; |
| 74 | + let l3 = ListRef::ValueRef { val: &v3 }; |
| 75 | + |
| 76 | + assert_eq!( |
| 77 | + array_access::<String>(Some(l1), Some(1)), |
| 78 | + Ok(Some("来自".into())) |
| 79 | + ); |
| 80 | + assert_eq!( |
| 81 | + array_access::<String>(Some(l2), Some(2)), |
| 82 | + Ok(Some("荷兰".into())) |
| 83 | + ); |
| 84 | + assert_eq!( |
| 85 | + array_access::<String>(Some(l3), Some(3)), |
| 86 | + Ok(Some("的爱".into())) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_nested_array_access() { |
| 92 | + let v = ListValue::new(vec![ |
| 93 | + Some(ScalarImpl::List(ListValue::new(vec![ |
| 94 | + Some(ScalarImpl::Utf8("foo".into())), |
| 95 | + Some(ScalarImpl::Utf8("bar".into())), |
| 96 | + ]))), |
| 97 | + Some(ScalarImpl::List(ListValue::new(vec![ |
| 98 | + Some(ScalarImpl::Utf8("fizz".into())), |
| 99 | + Some(ScalarImpl::Utf8("buzz".into())), |
| 100 | + ]))), |
| 101 | + ]); |
| 102 | + let l = ListRef::ValueRef { val: &v }; |
| 103 | + assert_eq!( |
| 104 | + array_access::<ListValue>(Some(l), Some(1)), |
| 105 | + Ok(Some(ListValue::new(vec![ |
| 106 | + Some(ScalarImpl::Utf8("foo".into())), |
| 107 | + Some(ScalarImpl::Utf8("bar".into())), |
| 108 | + ]))) |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments