datatypes/vectors/
operations.rs

1// Copyright 2023 Greptime Team
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
15mod cast;
16mod filter;
17mod replicate;
18mod take;
19
20use std::sync::Arc;
21
22use crate::error::{self, Result};
23use crate::types::LogicalPrimitiveType;
24use crate::vectors::constant::ConstantVector;
25use crate::vectors::{
26    BinaryVector, BooleanVector, ConcreteDataType, Decimal128Vector, ListVector, NullVector,
27    PrimitiveVector, StringVector, UInt32Vector, Vector, VectorRef,
28};
29
30/// Vector compute operations.
31pub trait VectorOp {
32    /// Copies each element according `offsets` parameter.
33    /// - `i-th` element should be copied `offsets[i] - offsets[i - 1]` times
34    /// - `0-th` element would be copied `offsets[0]` times
35    ///
36    /// # Panics
37    /// Panics if `offsets.len() != self.len()`.
38    fn replicate(&self, offsets: &[usize]) -> VectorRef;
39
40    /// Filters the vector, returns elements matching the `filter` (i.e. where the values are true).
41    ///
42    /// Note that the nulls of `filter` are interpreted as `false` will lead to these elements being masked out.
43    fn filter(&self, filter: &BooleanVector) -> Result<VectorRef>;
44
45    /// Cast vector to the provided data type and return a new vector with type to_type, if possible.
46    ///
47    /// TODO(dennis) describe behaviors in details.
48    fn cast(&self, to_type: &ConcreteDataType) -> Result<VectorRef>;
49
50    /// Take elements from the vector by the given indices.
51    ///
52    /// # Panics
53    /// Panics if an index is out of bounds.
54    fn take(&self, indices: &UInt32Vector) -> Result<VectorRef>;
55}
56
57macro_rules! impl_scalar_vector_op {
58    ($($VectorType: ident),+) => {$(
59        impl VectorOp for $VectorType {
60            fn replicate(&self, offsets: &[usize]) -> VectorRef {
61                replicate::replicate_scalar(self, offsets)
62            }
63
64            fn filter(&self, filter: &BooleanVector) -> Result<VectorRef> {
65                filter::filter_non_constant!(self, $VectorType, filter)
66            }
67
68            fn cast(&self, to_type: &ConcreteDataType) -> Result<VectorRef> {
69                if let Some(vector) = self.as_any().downcast_ref::<BinaryVector>() {
70                    match to_type {
71                        ConcreteDataType::Json(_) => {
72                            let json_vector = vector.convert_binary_to_json()?;
73                            return Ok(Arc::new(json_vector) as VectorRef);
74                        }
75                        ConcreteDataType::Vector(d) => {
76                            let vector = vector.convert_binary_to_vector(d.dim)?;
77                            return Ok(Arc::new(vector) as VectorRef);
78                        }
79                        _ => {}
80                    }
81                }
82                cast::cast_non_constant!(self, to_type)
83            }
84
85            fn take(&self, indices: &UInt32Vector) -> Result<VectorRef> {
86                take::take_indices!(self, $VectorType, indices)
87            }
88        }
89    )+};
90}
91
92impl_scalar_vector_op!(BinaryVector, BooleanVector, ListVector, StringVector);
93
94impl VectorOp for Decimal128Vector {
95    fn replicate(&self, offsets: &[usize]) -> VectorRef {
96        std::sync::Arc::new(replicate::replicate_decimal128(self, offsets))
97    }
98
99    fn filter(&self, filter: &BooleanVector) -> Result<VectorRef> {
100        filter::filter_non_constant!(self, Decimal128Vector, filter)
101    }
102
103    fn cast(&self, to_type: &ConcreteDataType) -> Result<VectorRef> {
104        cast::cast_non_constant!(self, to_type)
105    }
106
107    fn take(&self, indices: &UInt32Vector) -> Result<VectorRef> {
108        take::take_indices!(self, Decimal128Vector, indices)
109    }
110}
111
112impl<T: LogicalPrimitiveType> VectorOp for PrimitiveVector<T> {
113    fn replicate(&self, offsets: &[usize]) -> VectorRef {
114        std::sync::Arc::new(replicate::replicate_primitive(self, offsets))
115    }
116
117    fn filter(&self, filter: &BooleanVector) -> Result<VectorRef> {
118        filter::filter_non_constant!(self, PrimitiveVector<T>, filter)
119    }
120
121    fn cast(&self, to_type: &ConcreteDataType) -> Result<VectorRef> {
122        cast::cast_non_constant!(self, to_type)
123    }
124
125    fn take(&self, indices: &UInt32Vector) -> Result<VectorRef> {
126        take::take_indices!(self, PrimitiveVector<T>, indices)
127    }
128}
129
130impl VectorOp for NullVector {
131    fn replicate(&self, offsets: &[usize]) -> VectorRef {
132        replicate::replicate_null(self, offsets)
133    }
134
135    fn filter(&self, filter: &BooleanVector) -> Result<VectorRef> {
136        filter::filter_non_constant!(self, NullVector, filter)
137    }
138    fn cast(&self, _to_type: &ConcreteDataType) -> Result<VectorRef> {
139        // TODO(dennis): impl it when NullVector has other datatype.
140        error::UnsupportedOperationSnafu {
141            op: "cast",
142            vector_type: self.vector_type_name(),
143        }
144        .fail()
145    }
146
147    fn take(&self, indices: &UInt32Vector) -> Result<VectorRef> {
148        take::take_indices!(self, NullVector, indices)
149    }
150}
151
152impl VectorOp for ConstantVector {
153    fn replicate(&self, offsets: &[usize]) -> VectorRef {
154        self.replicate_vector(offsets)
155    }
156
157    fn filter(&self, filter: &BooleanVector) -> Result<VectorRef> {
158        self.filter_vector(filter)
159    }
160
161    fn cast(&self, to_type: &ConcreteDataType) -> Result<VectorRef> {
162        self.cast_vector(to_type)
163    }
164
165    fn take(&self, indices: &UInt32Vector) -> Result<VectorRef> {
166        self.take_vector(indices)
167    }
168}