risingwave_common/types/
scalar_impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::hash::Hasher;

use super::*;
use crate::{dispatch_scalar_ref_variants, dispatch_scalar_variants, for_all_native_types};

/// `ScalarPartialOrd` allows comparison between `Scalar` and `ScalarRef`.
///
/// TODO: see if it is possible to implement this trait directly on `ScalarRef`.
pub trait ScalarPartialOrd: Scalar {
    fn scalar_cmp(&self, other: Self::ScalarRefType<'_>) -> Option<std::cmp::Ordering>;
}

/// Implement `Scalar` and `ScalarRef` for native type.
/// For `PrimitiveArrayItemType`, clone is trivial, so `T` is both `Scalar` and `ScalarRef`.
macro_rules! impl_all_native_scalar {
    ($({ $scalar_type:ty, $_variant_name:ident, $_read_fn:ident } ),*) => {
        $(
            impl Scalar for $scalar_type {
                type ScalarRefType<'a> = Self;

                fn as_scalar_ref(&self) -> Self {
                    *self
                }
            }

            impl<'scalar> ScalarRef<'scalar> for $scalar_type {
                type ScalarType = Self;

                fn to_owned_scalar(&self) -> Self {
                    *self
                }

                fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
                    self.hash(state)
                }
            }
        )*
    };
}

for_all_native_types! { impl_all_native_scalar }

/// Implement `Scalar` for `Box<str>`.
/// `Box<str>` could be converted to `&str`.
impl Scalar for Box<str> {
    type ScalarRefType<'a> = &'a str;

    fn as_scalar_ref(&self) -> &str {
        self.as_ref()
    }
}

/// Implement `Scalar` for `Bytes`.
impl Scalar for Box<[u8]> {
    type ScalarRefType<'a> = &'a [u8];

    fn as_scalar_ref(&self) -> &[u8] {
        self
    }
}

/// Implement `Scalar` for `StructValue`.
impl Scalar for StructValue {
    type ScalarRefType<'a> = StructRef<'a>;

    fn as_scalar_ref(&self) -> StructRef<'_> {
        StructRef::ValueRef { val: self }
    }
}

/// Implement `Scalar` for `ListValue`.
impl Scalar for ListValue {
    type ScalarRefType<'a> = ListRef<'a>;

    fn as_scalar_ref(&self) -> ListRef<'_> {
        self.into()
    }
}

/// Implement `ScalarRef` for `Box<str>`.
/// `Box<str>` could be converted to `&str`.
impl<'a> ScalarRef<'a> for &'a str {
    type ScalarType = Box<str>;

    fn to_owned_scalar(&self) -> Box<str> {
        (*self).into()
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

impl<'a> ScalarRef<'a> for &'a [u8] {
    type ScalarType = Box<[u8]>;

    fn to_owned_scalar(&self) -> Box<[u8]> {
        self.to_vec().into()
    }

    fn hash_scalar<H: Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

impl ScalarPartialOrd for Box<str> {
    fn scalar_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
        self.as_ref().partial_cmp(other)
    }
}

impl<T: PrimitiveArrayItemType + Scalar> ScalarPartialOrd for T {
    fn scalar_cmp(&self, other: Self) -> Option<std::cmp::Ordering> {
        self.partial_cmp(&other)
    }
}

impl ScalarPartialOrd for bool {
    fn scalar_cmp(&self, other: Self) -> Option<std::cmp::Ordering> {
        self.partial_cmp(&other)
    }
}

/// Implement `Scalar` for `bool`.
impl Scalar for bool {
    type ScalarRefType<'a> = bool;

    fn as_scalar_ref(&self) -> bool {
        *self
    }
}

/// Implement `ScalarRef` for `bool`.
impl ScalarRef<'_> for bool {
    type ScalarType = bool;

    fn to_owned_scalar(&self) -> bool {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

/// Implement `Scalar` for `Decimal`.
impl Scalar for Decimal {
    type ScalarRefType<'a> = Decimal;

    fn as_scalar_ref(&self) -> Decimal {
        *self
    }
}

/// Implement `ScalarRef` for `Decimal`.
impl ScalarRef<'_> for Decimal {
    type ScalarType = Decimal;

    fn to_owned_scalar(&self) -> Decimal {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.normalize().hash(state)
    }
}

/// Implement `Scalar` for `Interval`.
impl Scalar for Interval {
    type ScalarRefType<'a> = Interval;

    fn as_scalar_ref(&self) -> Interval {
        *self
    }
}

/// Implement `ScalarRef` for `Interval`.
impl ScalarRef<'_> for Interval {
    type ScalarType = Interval;

    fn to_owned_scalar(&self) -> Interval {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

/// Implement `Scalar` for `Date`.
impl Scalar for Date {
    type ScalarRefType<'a> = Date;

    fn as_scalar_ref(&self) -> Date {
        *self
    }
}

/// Implement `ScalarRef` for `Date`.
impl ScalarRef<'_> for Date {
    type ScalarType = Date;

    fn to_owned_scalar(&self) -> Date {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

/// Implement `Scalar` for `Timestamp`.
impl Scalar for Timestamp {
    type ScalarRefType<'a> = Timestamp;

    fn as_scalar_ref(&self) -> Timestamp {
        *self
    }
}

/// Implement `ScalarRef` for `Timestamp`.
impl ScalarRef<'_> for Timestamp {
    type ScalarType = Timestamp;

    fn to_owned_scalar(&self) -> Timestamp {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

/// Implement `Scalar` for `Time`.
impl Scalar for Time {
    type ScalarRefType<'a> = Time;

    fn as_scalar_ref(&self) -> Time {
        *self
    }
}

/// Implement `ScalarRef` for `Time`.
impl ScalarRef<'_> for Time {
    type ScalarType = Time;

    fn to_owned_scalar(&self) -> Time {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

/// Implement `Scalar` for `Timestamptz`.
impl Scalar for Timestamptz {
    type ScalarRefType<'a> = Timestamptz;

    fn as_scalar_ref(&self) -> Timestamptz {
        *self
    }
}

/// Implement `ScalarRef` for `Timestamptz`.
impl ScalarRef<'_> for Timestamptz {
    type ScalarType = Timestamptz;

    fn to_owned_scalar(&self) -> Timestamptz {
        *self
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash(state)
    }
}

/// Implement `Scalar` for `StructValue`.
impl<'a> ScalarRef<'a> for StructRef<'a> {
    type ScalarType = StructValue;

    fn to_owned_scalar(&self) -> StructValue {
        let fields = self.iter_fields_ref().map(|f| f.to_owned_datum()).collect();
        StructValue::new(fields)
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash_scalar_inner(state)
    }
}

/// Implement `Scalar` for `ListValue`.
impl<'a> ScalarRef<'a> for ListRef<'a> {
    type ScalarType = ListValue;

    fn to_owned_scalar(&self) -> ListValue {
        (*self).into()
    }

    fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
        self.hash_scalar_inner(state)
    }
}

impl ScalarImpl {
    pub fn get_ident(&self) -> &'static str {
        dispatch_scalar_variants!(self, [I = VARIANT_NAME], { I })
    }
}

impl ScalarRefImpl<'_> {
    pub fn get_ident(&self) -> &'static str {
        dispatch_scalar_ref_variants!(self, [I = VARIANT_NAME], { I })
    }
}