risingwave_expr_impl/scalar/array_range_access.rs
1// Copyright 2025 RisingWave 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
15use risingwave_common::array::ListRef;
16use risingwave_expr::function;
17
18/// If the case is `array[1,2,3][:2]`, then start will be 0 set by the frontend
19/// If the case is `array[1,2,3][1:]`, then end will be `i32::MAX` set by the frontend
20#[function("array_range_access(anyarray, int4, int4) -> anyarray")]
21pub fn array_range_access(
22 list: ListRef<'_>,
23 start: i32,
24 end: i32,
25 writer: &mut impl risingwave_common::array::ListWrite,
26) {
27 let start = std::cmp::max(start, 1) as usize;
28 let end = std::cmp::min(std::cmp::max(0, end), list.len() as i32) as usize;
29 if start <= end {
30 writer.write_iter(list.iter().take(end).skip(start - 1));
31 }
32}