Skip to main content

feldera_sqllib/
lib.rs

1#![allow(non_snake_case)]
2
3#[doc(hidden)]
4pub mod aggregates;
5pub use aggregates::*;
6pub mod array;
7pub use array::*;
8#[doc(hidden)]
9pub mod binary;
10pub use binary::*;
11#[doc(hidden)]
12pub mod casts;
13pub use casts::*;
14#[doc(hidden)]
15pub mod decimal;
16pub use decimal::*;
17#[doc(hidden)]
18pub mod error;
19pub use error::*;
20#[doc(hidden)]
21pub mod float;
22pub use float::*;
23#[doc(hidden)]
24pub mod geopoint;
25pub use geopoint::*;
26#[doc(hidden)]
27pub mod interval;
28pub use interval::*;
29#[doc(hidden)]
30pub mod map;
31pub use map::*;
32#[doc(hidden)]
33pub mod operators;
34pub use operators::*;
35#[doc(hidden)]
36pub mod source;
37pub use source::*;
38#[doc(hidden)]
39pub mod string;
40pub use string::*;
41#[doc(hidden)]
42pub mod timestamp;
43pub use timestamp::*;
44#[doc(hidden)]
45pub mod uuid;
46pub use uuid::*;
47#[doc(hidden)]
48pub mod variant;
49pub use variant::*;
50#[doc(hidden)]
51pub mod rfc3339;
52#[doc(hidden)]
53pub use num_traits::Float;
54pub use regex::Regex;
55#[doc(hidden)]
56pub use source::{SourcePosition, SourcePositionRange};
57
58mod string_interner;
59pub use string_interner::{build_string_interner, intern_string, unintern_string};
60
61// Re-export these types, so they can be used in UDFs without having to import the dbsp crate directly.
62// Perhaps they should be defined in sqllib in the first place?
63pub use dbsp::algebra::{F32, F64};
64use dbsp::{
65    DBData, MapHandle, OrdIndexedZSet, OrdZSet, OutputHandle, ZSetHandle, ZWeight,
66    algebra::{
67        AddByRef, HasOne, HasZero, NegByRef, OrdIndexedZSetFactories, OrdZSetFactories, Semigroup,
68        SemigroupValue, ZRingValue,
69    },
70    circuit::metrics::TOTAL_LATE_RECORDS,
71    dynamic::{DowncastTrait, DynData, Erase},
72    operator::Update,
73    trace::{
74        BatchReader, BatchReaderFactories, Builder, Cursor,
75        ord::{OrdIndexedWSetBuilder, OrdWSetBuilder},
76    },
77    typed_batch::{SpineSnapshot, TypedBatch},
78    utils::*,
79};
80use num::PrimInt;
81use num_traits::Pow;
82use std::marker::PhantomData;
83use std::ops::{Deref, Neg};
84use std::sync::OnceLock;
85use std::{fmt::Debug, sync::atomic::Ordering};
86
87/// Convert a value of a SQL data type to an integer
88/// that preserves ordering.  Used for partitioned_rolling_aggregates
89#[doc(hidden)]
90pub trait ToInteger<T>
91where
92    T: PrimInt,
93{
94    #[doc(hidden)]
95    fn to_integer(&self) -> T;
96}
97
98/// Trait that provides the inverse functionality of the ToInteger trait.
99/// Used for partitioned_rolling_aggregates
100#[doc(hidden)]
101pub trait FromInteger<T>
102where
103    T: PrimInt,
104{
105    #[doc(hidden)]
106    fn from_integer(value: &T) -> Self;
107}
108
109#[doc(hidden)]
110impl<T> ToInteger<T> for T
111where
112    T: PrimInt,
113{
114    #[doc(hidden)]
115    fn to_integer(&self) -> T {
116        *self
117    }
118}
119
120#[doc(hidden)]
121impl<T> FromInteger<T> for T
122where
123    T: PrimInt,
124{
125    #[doc(hidden)]
126    fn from_integer(value: &T) -> Self {
127        *value
128    }
129}
130
131#[doc(hidden)]
132pub type Weight = ZWeight;
133#[doc(hidden)]
134pub type WSet<D> = OrdZSet<D>;
135#[doc(hidden)]
136pub type IndexedWSet<K, D> = OrdIndexedZSet<K, D>;
137
138// Macro to create variants of a function with 1 argument
139// If there exists a function is f_(x: T) -> S, this creates a function
140// fN(x: Option<T>) -> Option<S>, defined as
141// fN(x) { let x = x?; Some(f_(x)) }.
142macro_rules! some_function1 {
143    ($func_name:ident, $arg_type:ty, $ret_type:ty) => {
144        ::paste::paste! {
145            #[doc(hidden)]
146            pub fn [<$func_name N>]( arg: Option<$arg_type> ) -> Option<$ret_type> {
147                let arg = arg?;
148                Some([<$func_name _>](arg))
149            }
150        }
151    };
152}
153
154pub(crate) use some_function1;
155
156// Macro to create variants of a function with 1 argument
157// If there exists a function is f_type(x: T) -> S, this creates a function
158// f_typeN(x: Option<T>) -> Option<S>
159// { let x = x?; Some(f_type(x)) }.
160macro_rules! some_polymorphic_function1 {
161    ($func_name:ident $(< $( const $var : ident : $ty: ty),* >)?, $type_name: ident, $arg_type:ty, $ret_type:ty) => {
162        ::paste::paste! {
163            #[doc(hidden)]
164            pub fn [<$func_name _ $type_name N>] $(< $( const $var : $ty ),* >)? ( arg: Option<$arg_type> ) -> Option<$ret_type> {
165                let arg = arg?;
166                Some([<$func_name _ $type_name >] $(:: < $($var),* >)? (arg))
167            }
168        }
169    };
170}
171
172pub(crate) use some_polymorphic_function1;
173
174// Macro to create variants of a function with 2 arguments
175// If there exists a function is f__(x: T, y: S) -> U, this creates
176// three functions:
177// - f_N(x: T, y: Option<S>) -> Option<U>
178// - fN_(x: Option<T>, y: S) -> Option<U>
179// - fNN(x: Option<T>, y: Option<S>) -> Option<U>
180// The resulting functions return Some only if all arguments are 'Some'.
181macro_rules! some_function2 {
182    ($func_name:ident, $arg_type0:ty, $arg_type1:ty, $ret_type:ty) => {
183        ::paste::paste! {
184            #[doc(hidden)]
185            pub fn [<$func_name NN>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1> ) -> Option<$ret_type> {
186                let arg0 = arg0?;
187                let arg1 = arg1?;
188                Some([<$func_name __>](arg0, arg1))
189            }
190
191            #[doc(hidden)]
192            pub fn [<$func_name _N>]( arg0: $arg_type0, arg1: Option<$arg_type1> ) -> Option<$ret_type> {
193                let arg1 = arg1?;
194                Some([<$func_name __>](arg0, arg1))
195            }
196
197            #[doc(hidden)]
198            pub fn [<$func_name N_>]( arg0: Option<$arg_type0>, arg1: $arg_type1 ) -> Option<$ret_type> {
199                let arg0 = arg0?;
200                Some([<$func_name __>](arg0, arg1))
201            }
202        }
203    }
204}
205
206pub(crate) use some_function2;
207
208// Macro to create variants of a polymorphic function with 2 arguments
209// If there exists a function is f_type1_type2(x: T, y: S) -> U, this
210// creates three functions:
211// - f_type1_type2N(x: T, y: Option<S>) -> Option<U>
212// - f_type1N_type2(x: Option<T>, y: S) -> Option<U>
213// - f_type1N_type2N(x: Option<T>, y: Option<S>) -> Option<U>
214// The resulting functions return Some only if all arguments are 'Some'.
215macro_rules! some_polymorphic_function2 {
216    ($func_name:ident  $(< $( const $var:ident : $ty: ty),* >)?, $type_name0: ident, $arg_type0:ty, $type_name1: ident, $arg_type1:ty, $ret_type:ty) => {
217        ::paste::paste! {
218            #[doc(hidden)]
219            pub fn [<$func_name _$type_name0 _ $type_name1 N>] $(< $( const $var : $ty),* >)? ( arg0: $arg_type0, arg1: Option<$arg_type1> ) -> Option<$ret_type> {
220                let arg1 = arg1?;
221                Some([<$func_name _ $type_name0 _ $type_name1>] $(:: < $($var),* >)? (arg0, arg1))
222            }
223
224            #[doc(hidden)]
225            pub fn [<$func_name _ $type_name0 N _ $type_name1>] $(< $( const $var : $ty),* >)? ( arg0: Option<$arg_type0>, arg1: $arg_type1 ) -> Option<$ret_type> {
226                let arg0 = arg0?;
227                Some([<$func_name _ $type_name0 _ $type_name1>] $(:: < $($var),* >)? (arg0, arg1))
228            }
229
230            #[doc(hidden)]
231            pub fn [<$func_name _ $type_name0 N _ $type_name1 N>] $(< $( const $var : $ty),* >)? ( arg0: Option<$arg_type0>, arg1: Option<$arg_type1> ) -> Option<$ret_type> {
232                let arg0 = arg0?;
233                let arg1 = arg1?;
234                Some([<$func_name _ $type_name0 _ $type_name1>] $(:: < $($var),* >)? (arg0, arg1))
235            }
236        }
237    }
238}
239
240pub(crate) use some_polymorphic_function2;
241
242// If there exists a function is f_t1_t2_t3(x: T, y: S, z: V) -> U, this creates
243// seven functions:
244// - f_t1_t2_t3N(x: T, y: S, z: Option<V>) -> Option<U>
245// - f_t1_t2N_t2(x: T, y: Option<S>, z: V) -> Option<U>
246// - etc.
247// The resulting functions return Some only if all arguments are 'Some'.
248macro_rules! some_polymorphic_function3 {
249    ($func_name:ident,
250     $type_name0: ident, $arg_type0:ty,
251     $type_name1: ident, $arg_type1:ty,
252     $type_name2: ident, $arg_type2: ty,
253     $ret_type:ty) => {
254        ::paste::paste! {
255            #[doc(hidden)]
256            pub fn [<$func_name _ $type_name0 _ $type_name1 _ $type_name2 N>](
257                arg0: $arg_type0,
258                arg1: $arg_type1,
259                arg2: Option<$arg_type2>
260            ) -> Option<$ret_type> {
261                let arg2 = arg2?;
262                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
263            }
264
265            #[doc(hidden)]
266            pub fn [<$func_name _ $type_name0 _ $type_name1 N _ $type_name2>](
267                arg0: $arg_type0,
268                arg1: Option<$arg_type1>,
269                arg2: $arg_type2
270            ) -> Option<$ret_type> {
271                let arg1 = arg1?;
272                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
273            }
274
275            #[doc(hidden)]
276            pub fn [<$func_name _ $type_name0 _ $type_name1 N _ $type_name2 N>](
277                arg0: $arg_type0,
278                arg1: Option<$arg_type1>,
279                arg2: Option<$arg_type2>
280            ) -> Option<$ret_type> {
281                let arg1 = arg1?;
282                let arg2 = arg2?;
283                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
284            }
285
286            #[doc(hidden)]
287            pub fn [<$func_name _ $type_name0 N _ $type_name1 _ $type_name2>](
288                arg0: Option<$arg_type0>,
289                arg1: $arg_type1,
290                arg2: $arg_type2
291            ) -> Option<$ret_type> {
292                let arg0 = arg0?;
293                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
294            }
295
296            #[doc(hidden)]
297            pub fn [<$func_name _ $type_name0 N _ $type_name1 _ $type_name2 N>](
298                arg0: Option<$arg_type0>,
299                arg1: $arg_type1,
300                arg2: Option<$arg_type2>
301            ) -> Option<$ret_type> {
302                let arg0 = arg0?;
303                let arg2 = arg2?;
304                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
305            }
306
307            #[doc(hidden)]
308            pub fn [<$func_name _ $type_name0 N _ $type_name1 N _ $type_name2>](
309                arg0: Option<$arg_type0>,
310                arg1: Option<$arg_type1>,
311                arg2: $arg_type2
312            ) -> Option<$ret_type> {
313                let arg0 = arg0?;
314                let arg1 = arg1?;
315                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
316            }
317
318            #[doc(hidden)]
319            pub fn [<$func_name _ $type_name0 N _ $type_name1 N _ $type_name2 N>](
320                arg0: Option<$arg_type0>,
321                arg1: Option<$arg_type1>,
322                arg2: Option<$arg_type2>
323            ) -> Option<$ret_type> {
324                let arg0 = arg0?;
325                let arg1 = arg1?;
326                let arg2 = arg2?;
327                Some([<$func_name _ $type_name0 _ $type_name1 _ $type_name2>](arg0, arg1, arg2))
328            }
329        }
330    };
331}
332
333pub(crate) use some_polymorphic_function3;
334
335// If there exists a function is f___(x: T, y: S, z: V) -> U, this creates
336// seven functions:
337// - f__N(x: T, y: S, z: Option<V>) -> Option<U>
338// - f_N_(x: T, y: Option<S>, z: V) -> Option<U>
339// - etc.
340// The resulting functions return Some only if all arguments are 'Some'.
341macro_rules! some_function3 {
342    ($func_name:ident, $arg_type0:ty, $arg_type1:ty, $arg_type2: ty, $ret_type:ty) => {
343        ::paste::paste! {
344            #[doc(hidden)]
345            pub fn [<$func_name __N>]( arg0: $arg_type0, arg1: $arg_type1, arg2: Option<$arg_type2> ) -> Option<$ret_type> {
346                let arg2 = arg2?;
347                Some([<$func_name ___>](arg0, arg1, arg2))
348            }
349
350            #[doc(hidden)]
351            pub fn [<$func_name _N_>]( arg0: $arg_type0, arg1: Option<$arg_type1>, arg2: $arg_type2 ) -> Option<$ret_type> {
352                let arg1 = arg1?;
353                Some([<$func_name ___>](arg0, arg1, arg2))
354            }
355
356            #[doc(hidden)]
357            pub fn [<$func_name _NN>]( arg0: $arg_type0, arg1: Option<$arg_type1>, arg2: Option<$arg_type2> ) -> Option<$ret_type> {
358                let arg1 = arg1?;
359                let arg2 = arg2?;
360                Some([<$func_name ___>](arg0, arg1, arg2))
361            }
362
363            #[doc(hidden)]
364            pub fn [<$func_name N__>]( arg0: Option<$arg_type0>, arg1: $arg_type1, arg2: $arg_type2 ) -> Option<$ret_type> {
365                let arg0 = arg0?;
366                Some([<$func_name ___>](arg0, arg1, arg2))
367            }
368
369            #[doc(hidden)]
370            pub fn [<$func_name N_N>]( arg0: Option<$arg_type0>, arg1: $arg_type1, arg2: Option<$arg_type2> ) -> Option<$ret_type> {
371                let arg0 = arg0?;
372                let arg2 = arg2?;
373                Some([<$func_name ___>](arg0, arg1, arg2))
374            }
375
376            #[doc(hidden)]
377            pub fn [<$func_name NN_>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1>, arg2: $arg_type2 ) -> Option<$ret_type> {
378                let arg0 = arg0?;
379                let arg1 = arg1?;
380                Some([<$func_name ___>](arg0, arg1, arg2))
381            }
382
383            #[doc(hidden)]
384            pub fn [<$func_name NNN>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1>, arg2: Option<$arg_type2> ) -> Option<$ret_type> {
385                let arg0 = arg0?;
386                let arg1 = arg1?;
387                let arg2 = arg2?;
388                Some([<$func_name ___>](arg0, arg1, arg2))
389            }
390        }
391    }
392}
393
394pub(crate) use some_function3;
395
396// Macro to create variants of a function with 4 arguments
397// If there exists a function is f____(x: T, y: S, z: V, w: W) -> U, this
398// creates fifteen functions:
399// - f___N(x: T, y: S, z: V, w: Option<W>) -> Option<U>
400// - f__N_(x: T, y: S, z: Option<V>, w: W) -> Option<U>
401// - etc.
402// The resulting functions return Some only if all arguments are 'Some'.
403macro_rules! some_function4 {
404    ($func_name:ident, $arg_type0:ty, $arg_type1:ty, $arg_type2: ty, $arg_type3: ty, $ret_type:ty) => {
405        ::paste::paste! {
406            #[doc(hidden)]
407            pub fn [<$func_name ___N>]( arg0: $arg_type0, arg1: $arg_type1, arg2: $arg_type2, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
408                let arg3 = arg3?;
409                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
410            }
411
412            #[doc(hidden)]
413            pub fn [<$func_name __N_>]( arg0: $arg_type0, arg1: $arg_type1, arg2: Option<$arg_type2>, arg3: $arg_type3 ) -> Option<$ret_type> {
414                let arg2 = arg2?;
415                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
416            }
417
418            #[doc(hidden)]
419            pub fn [<$func_name __NN>]( arg0: $arg_type0, arg1: $arg_type1, arg2: Option<$arg_type2>, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
420                let arg2 = arg2?;
421                let arg3 = arg3?;
422                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
423            }
424
425            #[doc(hidden)]
426            pub fn [<$func_name _N__>]( arg0: $arg_type0, arg1: Option<$arg_type1>, arg2: $arg_type2, arg3: $arg_type3 ) -> Option<$ret_type> {
427                let arg1 = arg1?;
428                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
429            }
430
431            #[doc(hidden)]
432            pub fn [<$func_name _N_N>]( arg0: $arg_type0, arg1: Option<$arg_type1>, arg2: $arg_type2, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
433                let arg1 = arg1?;
434                let arg3 = arg3?;
435                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
436            }
437
438            #[doc(hidden)]
439            pub fn [<$func_name _NN_>]( arg0: $arg_type0, arg1: Option<$arg_type1>, arg2: Option<$arg_type2>, arg3: $arg_type3 ) -> Option<$ret_type> {
440                let arg1 = arg1?;
441                let arg2 = arg2?;
442                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
443            }
444
445            #[doc(hidden)]
446            pub fn [<$func_name _NNN>]( arg0: $arg_type0, arg1: Option<$arg_type1>, arg2: Option<$arg_type2>, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
447                let arg1 = arg1?;
448                let arg2 = arg2?;
449                let arg3 = arg3?;
450                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
451            }
452
453            #[doc(hidden)]
454            pub fn [<$func_name N___>]( arg0: Option<$arg_type0>, arg1: $arg_type1, arg2: $arg_type2, arg3: $arg_type3 ) -> Option<$ret_type> {
455                let arg0 = arg0?;
456                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
457            }
458
459            #[doc(hidden)]
460            pub fn [<$func_name N__N>]( arg0: Option<$arg_type0>, arg1: $arg_type1, arg2: $arg_type2, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
461                let arg0 = arg0?;
462                let arg3 = arg3?;
463                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
464            }
465
466            #[doc(hidden)]
467            pub fn [<$func_name N_N_>]( arg0: Option<$arg_type0>, arg1: $arg_type1, arg2: Option<$arg_type2>, arg3: $arg_type3 ) -> Option<$ret_type> {
468                let arg0 = arg0?;
469                let arg2 = arg2?;
470                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
471            }
472
473            #[doc(hidden)]
474            pub fn [<$func_name N_NN>]( arg0: Option<$arg_type0>, arg1: $arg_type1, arg2: Option<$arg_type2>, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
475                let arg0 = arg0?;
476                let arg2 = arg2?;
477                let arg3 = arg3?;
478                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
479            }
480
481            #[doc(hidden)]
482            pub fn [<$func_name NN__>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1>, arg2: $arg_type2, arg3: $arg_type3 ) -> Option<$ret_type> {
483                let arg0 = arg0?;
484                let arg1 = arg1?;
485                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
486            }
487
488            #[doc(hidden)]
489            pub fn [<$func_name NN_N>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1>, arg2: $arg_type2, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
490                let arg0 = arg0?;
491                let arg1 = arg1?;
492                let arg3 = arg3?;
493                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
494            }
495
496            #[doc(hidden)]
497            pub fn [<$func_name NNN_>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1>, arg2: Option<$arg_type2>, arg3: $arg_type3 ) -> Option<$ret_type> {
498                let arg0 = arg0?;
499                let arg1 = arg1?;
500                let arg2 = arg2?;
501                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
502            }
503
504            #[doc(hidden)]
505            pub fn [<$func_name NNNN>]( arg0: Option<$arg_type0>, arg1: Option<$arg_type1>, arg2: Option<$arg_type2>, arg3: Option<$arg_type3> ) -> Option<$ret_type> {
506                let arg0 = arg0?;
507                let arg1 = arg1?;
508                let arg2 = arg2?;
509                let arg3 = arg3?;
510                Some([<$func_name ____>](arg0, arg1, arg2, arg3))
511            }
512        }
513    }
514}
515
516pub(crate) use some_function4;
517
518// Macro to create variants of a function with 2 arguments
519// optimized for the implementation of arithmetic operators.
520// Assuming there exists a function is f__(x: T, y: T) -> U, this creates
521// three functions:
522// - f_tN_t(x: T, y: Option<T>) -> Option<U>
523// - f_t_tN(x: Option<T>, y: T) -> Option<U>
524// - f_tN_tN(x: Option<T>, y: Option<T>) -> Option<U>
525// The resulting functions return Some only if all arguments are 'Some'.
526macro_rules! some_existing_operator {
527    ($func_name: ident $(< $( const $var:ident : $ty: ty),* >)?, $short_name: ident, $arg_type: ty, $ret_type: ty) => {
528        ::paste::paste! {
529            #[inline(always)]
530            #[doc(hidden)]
531            pub fn [<$func_name _ $short_name N _ $short_name N>] $(< $( const $var : $ty ),* >)? ( arg0: Option<$arg_type>, arg1: Option<$arg_type> ) -> Option<$ret_type> {
532                let arg0 = arg0?;
533                let arg1 = arg1?;
534                Some([<$func_name _ $short_name _ $short_name>] $(:: < $($var),* >)? (arg0, arg1))
535            }
536
537            #[inline(always)]
538            #[doc(hidden)]
539            pub fn [<$func_name _ $short_name _ $short_name N>] $(< $( const $var : $ty ),* >)? ( arg0: $arg_type, arg1: Option<$arg_type> ) -> Option<$ret_type> {
540                let arg1 = arg1?;
541                Some([<$func_name _ $short_name _ $short_name>] $(:: < $($var),* >)? (arg0, arg1))
542            }
543
544            #[inline(always)]
545            #[doc(hidden)]
546            pub fn [<$func_name _ $short_name N _ $short_name>] $(< $( const $var : $ty ),* >)? ( arg0: Option<$arg_type>, arg1: $arg_type ) -> Option<$ret_type> {
547                let arg0 = arg0?;
548                Some([<$func_name _ $short_name _ $short_name>] $(:: < $($var),* >)? (arg0, arg1))
549            }
550        }
551    }
552}
553
554pub(crate) use some_existing_operator;
555
556// Macro to create variants of a function with 2 arguments
557// optimized for the implementation of arithmetic operators.
558// Assuming there exists a function is f(x: T, y: T) -> U, this creates
559// four functions:
560// - f_t_t(x: T, y: T) -> U
561// - f_tN_t(x: T, y: Option<T>) -> Option<U>
562// - f_t_tN(x: Option<T>, y: T) -> Option<U>
563// - f_tN_tN(x: Option<T>, y: Option<T>) -> Option<U>
564// The resulting functions return Some only if all arguments are 'Some'.
565//
566// Has two variants:
567// - Takes the name of the existing function, the generated functions will have
568// this prefix
569// - Takes the name of the existing function, and the prefix for the generated
570// functions
571macro_rules! some_operator {
572    ($func_name: ident $(< $( const $var:ident : $ty: ty),* >)?, $short_name: ident, $arg_type: ty, $ret_type: ty) => {
573        ::paste::paste! {
574            #[doc(hidden)]
575            #[inline(always)]
576            pub fn [<$func_name _ $short_name _ $short_name >] $(< $(const $var : $ty),* >)?  ( arg0: $arg_type, arg1: $arg_type ) -> $ret_type {
577                $func_name $(:: < $($var),* >)? (arg0, arg1)
578            }
579
580            some_existing_operator!($func_name $(< $( const $var : $ty),* >)?, $short_name, $arg_type, $ret_type);
581        }
582    };
583
584    ($func_name: ident $(< $( const $var:ident : $ty: ty),* >)?, $new_func_name: ident, $short_name: ident, $arg_type: ty, $ret_type: ty) => {
585        ::paste::paste! {
586            #[doc(hidden)]
587            #[inline(always)]
588            pub fn [<$new_func_name _ $short_name _ $short_name >] $(< $(const $var : $ty ),* >)? ( arg0: $arg_type, arg1: $arg_type ) -> $ret_type {
589                $func_name $(:: < $($var),* >)? (arg0, arg1)
590            }
591
592            some_existing_operator!($new_func_name $(< $( const $var : $ty),* >)?, $short_name, $arg_type, $ret_type);
593        }
594    }
595}
596
597pub(crate) use some_operator;
598
599#[doc(hidden)]
600#[inline(always)]
601pub fn wrap_bool(b: Option<bool>) -> bool {
602    b.unwrap_or_default()
603}
604
605#[doc(hidden)]
606#[inline(always)]
607pub fn or_b_b<F>(left: bool, right: F) -> bool
608where
609    F: Fn() -> bool,
610{
611    left || right()
612}
613
614#[doc(hidden)]
615#[inline(always)]
616pub fn or_bN_b<F>(left: Option<bool>, right: F) -> Option<bool>
617where
618    F: Fn() -> bool,
619{
620    match left {
621        Some(l) => Some(l || right()),
622        None => match right() {
623            true => Some(true),
624            _ => None,
625        },
626    }
627}
628
629#[doc(hidden)]
630#[inline(always)]
631pub fn or_b_bN<F>(left: bool, right: F) -> Option<bool>
632where
633    F: Fn() -> Option<bool>,
634{
635    match left {
636        false => right(),
637        true => Some(true),
638    }
639}
640
641#[doc(hidden)]
642#[inline(always)]
643pub fn or_bN_bN<F>(left: Option<bool>, right: F) -> Option<bool>
644where
645    F: Fn() -> Option<bool>,
646{
647    match left {
648        None => match right() {
649            Some(true) => Some(true),
650            _ => None,
651        },
652        Some(false) => right(),
653        Some(true) => Some(true),
654    }
655}
656
657// OR and AND are special, they can't be generated by rules
658
659#[doc(hidden)]
660#[inline(always)]
661pub fn and_b_b<F>(left: bool, right: F) -> bool
662where
663    F: Fn() -> bool,
664{
665    left && right()
666}
667
668#[doc(hidden)]
669#[inline(always)]
670pub fn and_bN_b<F>(left: Option<bool>, right: F) -> Option<bool>
671where
672    F: Fn() -> bool,
673{
674    match left {
675        Some(false) => Some(false),
676        Some(true) => Some(right()),
677        None => match right() {
678            false => Some(false),
679            _ => None,
680        },
681    }
682}
683
684#[doc(hidden)]
685#[inline(always)]
686pub fn and_b_bN<F>(left: bool, right: F) -> Option<bool>
687where
688    F: Fn() -> Option<bool>,
689{
690    match left {
691        false => Some(false),
692        true => right(),
693    }
694}
695
696#[doc(hidden)]
697#[inline(always)]
698pub fn and_bN_bN<F>(left: Option<bool>, right: F) -> Option<bool>
699where
700    F: Fn() -> Option<bool>,
701{
702    match left {
703        Some(false) => Some(false),
704        Some(true) => right(),
705        None => match right() {
706            Some(false) => Some(false),
707            _ => None,
708        },
709    }
710}
711
712#[doc(hidden)]
713#[inline(always)]
714pub fn is_null<T>(value: Option<T>) -> bool {
715    value.is_none()
716}
717
718#[doc(hidden)]
719#[inline(always)]
720pub fn indicator<T, R>(value: &Option<T>) -> R
721where
722    R: HasZero + HasOne,
723{
724    match value {
725        None => R::zero(),
726        Some(_) => R::one(),
727    }
728}
729
730//////////////////////////////////////////
731
732#[doc(hidden)]
733#[inline(always)]
734pub fn abs_i8(left: i8) -> i8 {
735    left.abs()
736}
737
738#[doc(hidden)]
739#[inline(always)]
740pub fn abs_i16(left: i16) -> i16 {
741    left.abs()
742}
743
744#[doc(hidden)]
745#[inline(always)]
746pub fn abs_i32(left: i32) -> i32 {
747    left.abs()
748}
749
750#[doc(hidden)]
751#[inline(always)]
752pub fn abs_i64(left: i64) -> i64 {
753    left.abs()
754}
755
756#[doc(hidden)]
757#[inline(always)]
758pub fn abs_i128(left: i128) -> i128 {
759    left.abs()
760}
761
762#[doc(hidden)]
763#[inline(always)]
764pub fn abs_f(left: F32) -> F32 {
765    left.abs()
766}
767
768#[doc(hidden)]
769#[inline(always)]
770pub fn abs_d(left: F64) -> F64 {
771    left.abs()
772}
773
774#[doc(hidden)]
775#[inline(always)]
776pub fn abs_u8(left: u8) -> u8 {
777    left
778}
779
780#[doc(hidden)]
781#[inline(always)]
782pub fn abs_u16(left: u16) -> u16 {
783    left
784}
785
786#[doc(hidden)]
787#[inline(always)]
788pub fn abs_u32(left: u32) -> u32 {
789    left
790}
791
792#[doc(hidden)]
793#[inline(always)]
794pub fn abs_u64(left: u64) -> u64 {
795    left
796}
797
798#[doc(hidden)]
799#[inline(always)]
800pub fn abs_u128(left: u128) -> u128 {
801    left
802}
803
804some_polymorphic_function1!(abs, i8, i8, i8);
805some_polymorphic_function1!(abs, i16, i16, i16);
806some_polymorphic_function1!(abs, i32, i32, i32);
807some_polymorphic_function1!(abs, i64, i64, i64);
808some_polymorphic_function1!(abs, i128, i128, i128);
809some_polymorphic_function1!(abs, f, F32, F32);
810some_polymorphic_function1!(abs, d, F64, F64);
811some_polymorphic_function1!(abs, u8, u8, u8);
812some_polymorphic_function1!(abs, u16, u16, u16);
813some_polymorphic_function1!(abs, u32, u32, u32);
814some_polymorphic_function1!(abs, u64, u64, u64);
815some_polymorphic_function1!(abs, u128, u128, u128);
816
817#[inline(always)]
818#[doc(hidden)]
819pub fn sign_i8(x: i8) -> i8 {
820    if x == 0 { x } else { x.signum() }
821}
822
823#[inline(always)]
824#[doc(hidden)]
825pub fn sign_i16(x: i16) -> i16 {
826    if x == 0 { x } else { x.signum() }
827}
828
829#[inline(always)]
830#[doc(hidden)]
831pub fn sign_i32(x: i32) -> i32 {
832    if x == 0 { x } else { x.signum() }
833}
834
835#[inline(always)]
836#[doc(hidden)]
837pub fn sign_i64(x: i64) -> i64 {
838    if x == 0 { x } else { x.signum() }
839}
840
841#[inline(always)]
842#[doc(hidden)]
843pub fn sign_i128(x: i128) -> i128 {
844    if x == 0 { x } else { x.signum() }
845}
846
847#[inline(always)]
848#[doc(hidden)]
849pub fn sign_u8(x: u8) -> u8 {
850    if x == 0 { x } else { 1 }
851}
852
853#[inline(always)]
854#[doc(hidden)]
855pub fn sign_u16(x: u16) -> u16 {
856    if x == 0 { x } else { 1 }
857}
858
859#[inline(always)]
860#[doc(hidden)]
861pub fn sign_u32(x: u32) -> u32 {
862    if x == 0 { x } else { 1 }
863}
864
865#[inline(always)]
866#[doc(hidden)]
867pub fn sign_u64(x: u64) -> u64 {
868    if x == 0 { x } else { 1 }
869}
870
871#[inline(always)]
872#[doc(hidden)]
873pub fn sign_u128(x: u128) -> u128 {
874    if x == 0 { x } else { 1 }
875}
876
877some_polymorphic_function1!(sign, i8, i8, i8);
878some_polymorphic_function1!(sign, i16, i16, i16);
879some_polymorphic_function1!(sign, i32, i32, i32);
880some_polymorphic_function1!(sign, i64, i64, i64);
881some_polymorphic_function1!(sign, i128, i128, i128);
882some_polymorphic_function1!(sign, u8, u8, u8);
883some_polymorphic_function1!(sign, u16, u16, u16);
884some_polymorphic_function1!(sign, u32, u32, u32);
885some_polymorphic_function1!(sign, u64, u64, u64);
886some_polymorphic_function1!(sign, u128, u128, u128);
887
888#[doc(hidden)]
889#[inline(always)]
890pub const fn is_true_b_(left: bool) -> bool {
891    left
892}
893
894#[doc(hidden)]
895#[inline(always)]
896pub const fn is_true_bN_(left: Option<bool>) -> bool {
897    matches!(left, Some(true))
898}
899
900#[doc(hidden)]
901#[inline(always)]
902pub fn is_false_b_(left: bool) -> bool {
903    !left
904}
905
906#[doc(hidden)]
907#[inline(always)]
908pub const fn is_false_bN_(left: Option<bool>) -> bool {
909    matches!(left, Some(false))
910}
911
912#[doc(hidden)]
913#[inline(always)]
914pub const fn is_not_true_b_(left: bool) -> bool {
915    !left
916}
917
918#[doc(hidden)]
919#[inline(always)]
920pub const fn is_not_true_bN_(left: Option<bool>) -> bool {
921    match left {
922        Some(true) => false,
923        Some(false) => true,
924        _ => true,
925    }
926}
927
928#[doc(hidden)]
929#[inline(always)]
930pub const fn is_not_false_b_(left: bool) -> bool {
931    left
932}
933
934#[doc(hidden)]
935#[inline(always)]
936pub const fn is_not_false_bN_(left: Option<bool>) -> bool {
937    match left {
938        Some(true) => true,
939        Some(false) => false,
940        _ => true,
941    }
942}
943
944#[doc(hidden)]
945#[inline(always)]
946pub fn is_distinct__<T>(left: T, right: T) -> bool
947where
948    T: Eq,
949{
950    left != right
951}
952
953#[doc(hidden)]
954#[inline(always)]
955pub fn is_distinct_N_N<T>(left: Option<T>, right: Option<T>) -> bool
956where
957    T: Eq,
958{
959    left != right
960}
961
962#[doc(hidden)]
963#[inline(always)]
964pub fn is_distinct__N<T>(left: T, right: Option<T>) -> bool
965where
966    T: Eq,
967{
968    match right {
969        Some(b) => left != b,
970        None => true,
971    }
972}
973
974#[doc(hidden)]
975#[inline(always)]
976pub fn is_distinct_N_<T>(left: Option<T>, right: T) -> bool
977where
978    T: Eq,
979{
980    match left {
981        Some(a) => a != right,
982        None => true,
983    }
984}
985
986#[doc(hidden)]
987pub fn weighted_push<T, W>(vec: &mut Vec<T>, value: &T, weight: W)
988where
989    W: ZRingValue,
990    T: Clone,
991{
992    let mut w = weight;
993    let negone = W::one().neg();
994    while w != W::zero() {
995        vec.push(value.clone());
996        w = w.add_by_ref(&negone);
997    }
998}
999
1000#[doc(hidden)]
1001pub fn power_i32_i32(left: i32, right: i32) -> F64 {
1002    (left as f64).pow(right).into()
1003}
1004
1005some_polymorphic_function2!(power, i32, i32, i32, i32, F64);
1006
1007////////////////////////////////////////////////
1008
1009// Functions called by 'writelog'
1010#[doc(hidden)]
1011pub fn dump<T>(prefix: SqlString, data: &T) -> T
1012where
1013    T: Debug + Clone,
1014{
1015    println!("{}: {:?}", prefix.str(), data);
1016    data.clone()
1017}
1018
1019#[doc(hidden)]
1020pub fn print(str: SqlString) {
1021    print!("{}", str.str())
1022}
1023
1024#[doc(hidden)]
1025pub fn print_opt(str: Option<SqlString>) {
1026    match str {
1027        None => print!("NULL"),
1028        Some(x) => print!("{}", x.str()),
1029    }
1030}
1031
1032#[doc(hidden)]
1033pub fn zset_map<D, T, F>(data: &WSet<D>, mapper: F) -> WSet<T>
1034where
1035    D: DBData + 'static,
1036    T: DBData + 'static,
1037    F: Fn(&D) -> T,
1038{
1039    let mut tuples = Vec::new();
1040    let mut cursor = data.cursor();
1041    while cursor.key_valid() {
1042        let item = unsafe { cursor.key().downcast::<D>() };
1043        let data = mapper(item);
1044        let weight = unsafe { *cursor.weight().downcast::<ZWeight>() };
1045        tuples.push(Tup2(Tup2(data, ()), weight));
1046        cursor.step_key();
1047    }
1048    WSet::from_tuples((), tuples)
1049}
1050
1051#[doc(hidden)]
1052pub fn late() {
1053    TOTAL_LATE_RECORDS.fetch_add(1, Ordering::Relaxed);
1054}
1055
1056#[doc(hidden)]
1057pub fn zset_filter_comparator<D, T, F>(data: &WSet<D>, value: &T, comparator: F) -> WSet<D>
1058where
1059    D: DBData + 'static,
1060    T: 'static + Debug,
1061    F: Fn(&D, &T) -> bool,
1062{
1063    let factories = OrdZSetFactories::new::<D, (), ZWeight>();
1064
1065    let mut builder = OrdWSetBuilder::with_capacity(&factories, data.len(), data.len());
1066
1067    let mut cursor = data.cursor();
1068    while cursor.key_valid() {
1069        let item = unsafe { cursor.key().downcast::<D>() };
1070        if comparator(item, value) {
1071            // println!("controlled_filter accepts={:?}", item);
1072            builder.push_val_diff(().erase(), cursor.weight());
1073            builder.push_key(cursor.key());
1074        } else {
1075            late();
1076        }
1077        cursor.step_key();
1078    }
1079    TypedBatch::new(builder.done())
1080}
1081
1082#[doc(hidden)]
1083pub fn indexed_zset_filter_comparator<K, D, T, F>(
1084    data: &IndexedWSet<K, D>,
1085    value: &T,
1086    comparator: F,
1087) -> IndexedWSet<K, D>
1088where
1089    K: DBData + Erase<DynData>,
1090    D: DBData + Erase<DynData>,
1091    T: 'static,
1092    F: Fn((&K, &D), &T) -> bool,
1093{
1094    let factories = OrdIndexedZSetFactories::new::<K, D, ZWeight>();
1095    let mut builder =
1096        OrdIndexedWSetBuilder::with_capacity(&factories, data.key_count(), data.len());
1097
1098    let mut cursor = data.cursor();
1099    while cursor.key_valid() {
1100        let key = unsafe { cursor.key().downcast::<K>() }.clone();
1101        let mut any_values = false;
1102        while cursor.val_valid() {
1103            let w = *cursor.weight().deref();
1104            let item = unsafe { cursor.val().downcast::<D>() };
1105            if comparator((&key, item), value) {
1106                builder.push_val_diff(item.erase(), w.erase());
1107                any_values = true;
1108            } else {
1109                late();
1110            }
1111            cursor.step_val();
1112        }
1113        if any_values {
1114            builder.push_key(cursor.key());
1115        }
1116        cursor.step_key();
1117    }
1118    TypedBatch::new(builder.done())
1119}
1120
1121#[doc(hidden)]
1122pub fn append_to_map_handle<K, V, U>(
1123    data: &WSet<V>,
1124    handle: &MapHandle<K, V, U>,
1125    key_f: fn(&V) -> K,
1126) where
1127    K: DBData,
1128    V: DBData,
1129    U: DBData,
1130{
1131    let mut cursor = data.cursor();
1132    while cursor.key_valid() {
1133        let w = *cursor.weight().deref();
1134        if w.is_zero() {
1135            continue;
1136        }
1137        if !w.ge0() {
1138            let key = unsafe { cursor.key().downcast::<V>() };
1139            handle.push(key_f(&key.clone()), Update::Delete);
1140        } else {
1141            let key = unsafe { cursor.key().downcast::<V>() };
1142            handle.push(key_f(&key.clone()), Update::Insert(key.clone()));
1143        }
1144        cursor.step_key();
1145    }
1146}
1147
1148#[doc(hidden)]
1149pub fn append_to_collection_handle<K>(data: &WSet<K>, handle: &ZSetHandle<K>)
1150where
1151    K: DBData,
1152{
1153    let mut cursor = data.cursor();
1154    while cursor.key_valid() {
1155        handle.push(
1156            unsafe { cursor.key().downcast::<K>() }.clone(),
1157            *cursor.weight().deref(),
1158        );
1159        cursor.step_key();
1160    }
1161}
1162
1163#[doc(hidden)]
1164pub fn read_output_handle<K>(handle: &OutputHandle<WSet<K>>) -> WSet<K>
1165where
1166    K: DBData,
1167{
1168    handle.consolidate()
1169}
1170
1171#[doc(hidden)]
1172pub fn read_output_spine<K>(handle: &OutputHandle<SpineSnapshot<WSet<K>>>) -> WSet<K>
1173where
1174    K: DBData,
1175{
1176    handle.concat().consolidate()
1177}
1178
1179// Check that two zsets are equal.  If yes, returns true.
1180// If not, print a diff of the zsets and returns false.
1181// Assumes that the zsets are positive (all weights are positive).
1182#[doc(hidden)]
1183pub fn must_equal<K>(left: &WSet<K>, right: &WSet<K>) -> bool
1184where
1185    K: DBData + Clone,
1186{
1187    // println!("L={:?}\nR={:?}\n", left, right);
1188    let diff = left.add_by_ref(&right.neg_by_ref());
1189    if diff.is_zero() {
1190        return true;
1191    }
1192    let mut cursor = diff.cursor();
1193    let mut shown = 0;
1194    let mut left = 0;
1195    let mut right = 0;
1196    while cursor.key_valid() {
1197        let weight = **cursor.weight();
1198        let key = cursor.key();
1199        if shown < 50 {
1200            if weight.le0() {
1201                println!("R: {:?}x{:?}", key, weight.neg());
1202            } else {
1203                println!("L: {:?}x{:?}", key, weight);
1204            }
1205        } else if weight.le0() {
1206            right += weight.neg();
1207        } else {
1208            left += weight;
1209        }
1210        cursor.step_key();
1211        shown += 1;
1212    }
1213    if left > 0 || right > 0 {
1214        println!("Additional L:{left} and R:{right} rows not shown");
1215    }
1216    false
1217}
1218
1219#[doc(hidden)]
1220pub fn zset_size<K>(set: &WSet<K>) -> i64 {
1221    let mut w = 0;
1222    let mut cursor = set.cursor();
1223    while cursor.key_valid() {
1224        let weight = **cursor.weight();
1225        w += weight;
1226        cursor.step_key();
1227    }
1228    w
1229}
1230
1231//////////////////////// Semigroup implementations
1232
1233#[derive(Clone)]
1234#[doc(hidden)]
1235pub struct DefaultOptSemigroup<T>(PhantomData<T>);
1236
1237#[doc(hidden)]
1238impl<T> Semigroup<Option<T>> for DefaultOptSemigroup<T>
1239where
1240    T: SemigroupValue,
1241{
1242    #[doc(hidden)]
1243    fn combine(left: &Option<T>, right: &Option<T>) -> Option<T> {
1244        match (left, right) {
1245            (None, _) => None,
1246            (_, None) => None,
1247            (Some(x), Some(y)) => Some(x.add_by_ref(y)),
1248        }
1249    }
1250}
1251
1252#[derive(Clone)]
1253#[doc(hidden)]
1254pub struct PairSemigroup<T, R, TS, RS>(PhantomData<(T, R, TS, RS)>);
1255
1256#[doc(hidden)]
1257impl<T, R, TS, RS> Semigroup<Tup2<T, R>> for PairSemigroup<T, R, TS, RS>
1258where
1259    TS: Semigroup<T>,
1260    RS: Semigroup<R>,
1261{
1262    #[doc(hidden)]
1263    fn combine(left: &Tup2<T, R>, right: &Tup2<T, R>) -> Tup2<T, R> {
1264        Tup2::new(
1265            TS::combine(&left.0, &right.0),
1266            RS::combine(&left.1, &right.1),
1267        )
1268    }
1269}
1270
1271#[derive(Clone)]
1272#[doc(hidden)]
1273pub struct TripleSemigroup<T, R, V, TS, RS, VS>(PhantomData<(T, R, V, TS, RS, VS)>);
1274
1275#[doc(hidden)]
1276impl<T, R, V, TS, RS, VS> Semigroup<Tup3<T, R, V>> for TripleSemigroup<T, R, V, TS, RS, VS>
1277where
1278    TS: Semigroup<T>,
1279    RS: Semigroup<R>,
1280    VS: Semigroup<V>,
1281{
1282    #[doc(hidden)]
1283    fn combine(left: &Tup3<T, R, V>, right: &Tup3<T, R, V>) -> Tup3<T, R, V> {
1284        Tup3::new(
1285            TS::combine(&left.0, &right.0),
1286            RS::combine(&left.1, &right.1),
1287            VS::combine(&left.2, &right.2),
1288        )
1289    }
1290}
1291
1292#[doc(hidden)]
1293#[derive(Clone)]
1294pub struct ConcatSemigroup<V>(PhantomData<V>);
1295
1296// Semigroup for the SINGLE_VALUE aggregate
1297#[derive(Clone)]
1298#[doc(hidden)]
1299pub struct SingleSemigroup<T>(PhantomData<T>);
1300
1301#[doc(hidden)]
1302impl<T> Semigroup<Tup2<bool, T>> for SingleSemigroup<Tup2<bool, T>>
1303where
1304    T: Clone,
1305{
1306    #[doc(hidden)]
1307    fn combine(left: &Tup2<bool, T>, right: &Tup2<bool, T>) -> Tup2<bool, T> {
1308        if left.0 && right.0 {
1309            panic!("More than one value in subquery");
1310        }
1311        Tup2::new(
1312            left.0 || right.0,
1313            if left.0 {
1314                left.1.clone()
1315            } else {
1316                right.1.clone()
1317            },
1318        )
1319    }
1320}
1321
1322// Semigroup for the an aggregate which computes nothing
1323// Useful when the compiler removes all aggregates from an aggregate operator
1324// (This is currently never used, because the compiler generates a linear aggregate
1325// for this case)
1326#[derive(Clone)]
1327#[doc(hidden)]
1328pub struct EmptySemigroup;
1329
1330#[doc(hidden)]
1331impl Semigroup<Tup0> for EmptySemigroup {
1332    #[doc(hidden)]
1333    fn combine(_left: &Tup0, _right: &Tup0) -> Tup0 {
1334        Tup0::new()
1335    }
1336}
1337
1338/// A lazily initialized static value, where the initialized can be a capturing
1339/// closure.
1340///
1341/// This struct is supposed to do the same thing as some other lazy
1342/// Rust types, like std::sync::Lazy, or std::cell::LazyCell: it holds
1343/// a constant value, but which needs to be initialized only on demand
1344/// (because it could panic).  Unfortunately I could not use an
1345/// off-the-shelf struct like the ones mentioned, because the
1346/// initializer is not always `const`.  This implementation separates
1347/// the creation (`new`) from the initialization (`init`).  This
1348/// enables us to use this class to declare `static` Rust values,
1349/// which nevertheless are initialized on first use.  In the
1350/// initializers we actually always use static constant values to, but
1351/// they are sometimes passed through function arguments, so the Rust
1352/// compiler cannot recognize them as such.  For example:
1353/// ```ignore
1354/// // In the main crate:
1355/// pub static SOURCE_MAP_STATIC: OnceLock<SourceMap> = OnceLock::new();
1356/// SOURCE_MAP_STATIC.get_or_init(|| {
1357///      let mut m = SourceMap::new();
1358///           m.insert("0ee9907f", 0, SourcePosition::new(222, 2));
1359///           m.insert("0ee9907f", 1, SourcePosition::new(223, 2));
1360///           m
1361///       });
1362/// let sourceMap = SOURCE_MAP_STATIC.get().unwrap();
1363/// let s3 = create_operator_x(&circuit, Some("0ee9907f"), &sourceMap, &mut catalog, s2);
1364/// ...
1365/// // In the operator crate
1366/// pub fn create_operator_x(
1367///         circuit: &RootCircuit,
1368///         hash: Option<&'static str>,
1369///         sourceMap: &'static SourceMap,  // static lifetime, but not const
1370///         catalog: &mut Catalog,
1371///         i0: &Stream<RootCircuit, WSet<...>>) -> Stream<RootCircuit, ...> {
1372///    static STATIC_4dbc5aebe9c4edea: StaticLazy<Option<SqlDecimal<38, 6>>> = StaticLazy::new();
1373///    STATIC_4dbc5aebe9c4edea.init(move || handle_error_with_position(
1374///               "global", 25, &sourceMap, cast_to_SqlDecimalN_i32N::<38, 6>(Some(1i32));
1375///    ...
1376/// };
1377/// ```
1378/// The function `handle_error_with_position` may panic, so it needs to be lazily evaluated.
1379#[derive(Default)]
1380#[doc(hidden)]
1381pub struct StaticLazy<T: 'static> {
1382    cell: OnceLock<T>,
1383    init: OnceLock<&'static (dyn Fn() -> T + Send + Sync)>,
1384}
1385
1386#[doc(hidden)]
1387impl<T> StaticLazy<T> {
1388    #[doc(hidden)]
1389    pub const fn new() -> Self {
1390        Self {
1391            cell: OnceLock::new(),
1392            init: OnceLock::new(),
1393        }
1394    }
1395
1396    #[doc(hidden)]
1397    pub fn init<F>(&'static self, f: F)
1398    where
1399        F: Fn() -> T + Send + Sync + 'static,
1400    {
1401        if self.cell.get().is_some() {
1402            return;
1403        }
1404        let leaked: &'static (dyn Fn() -> T + Send + Sync) = Box::leak(Box::new(f));
1405        self.init.set(leaked).unwrap_or(());
1406    }
1407
1408    #[doc(hidden)]
1409    fn get(&self) -> &T {
1410        self.cell.get_or_init(|| {
1411            let f = self
1412                .init
1413                .get()
1414                .unwrap_or_else(|| panic!("Initializer not set"));
1415            f()
1416        })
1417    }
1418}
1419
1420#[doc(hidden)]
1421impl<T> Deref for StaticLazy<T> {
1422    type Target = T;
1423
1424    fn deref(&self) -> &Self::Target {
1425        self.get()
1426    }
1427}
1428
1429#[doc(hidden)]
1430// If the data is Ok(None), convert it to Err, other leave it unchanged
1431pub fn unwrap_sql_result<T>(data: SqlResult<Option<T>>) -> SqlResult<T> {
1432    match data {
1433        Err(e) => Err(e),
1434        Ok(None) => Err(SqlRuntimeError::from_strng("NULL result produced")),
1435        Ok(Some(data)) => Ok(data),
1436    }
1437}
1438
1439#[doc(hidden)]
1440pub fn wrap_sql_result<T>(data: T) -> SqlResult<T> {
1441    Ok(data)
1442}