glaredb_core 25.6.3

Core functionality for GlareDB
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use std::fmt;

use glaredb_error::Result;

use super::Signature;
use super::cast::DEFAULT_IMPLICIT_CAST_SCORES;
use super::implicit::{NO_CAST_SCORE, implicit_cast_score};
use crate::arrays::datatype::{DataType, DataTypeId};
use crate::arrays::scalar::ScalarValue;
use crate::expr::Expression;
use crate::expr::literal_expr::LiteralExpr;

#[derive(Debug, Clone, PartialEq)]
pub enum CastType {
    /// Need to cast the type to this one.
    Cast { to: DataTypeId, score: u32 },
    /// Casting isn't needed, the original data type works.
    NoCastNeeded,
    /// The literal value was refined, we can create a literal expression
    /// directly from the refined value.
    RefinedLiteral { refined: RefinedLiteral, score: u32 },
}

impl CastType {
    fn score(&self) -> u32 {
        match self {
            Self::Cast { score, .. } => *score,
            Self::NoCastNeeded => NO_CAST_SCORE,
            Self::RefinedLiteral { score, .. } => *score,
        }
    }
}

/// Bonus applied to the cast score when we successfully refine a literal.
pub const REFINED_LITERAL_SCORE_BONUS: u32 = 100;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RefinedLiteral {
    Int8(i8),
    Int16(i16),
    Int32(i32),
    Int64(i64),
}

/// Extract the literal value from an expression.
///
/// This is used since we will only read integer literals in queries as i32 or
/// i64, but sometimes that literal may cause unwanted casts if the expression
/// contains smaller integer sizes.
///
/// By extracting the literals, we can check to see if the literal can actually
/// fit in the smaller size, which may reduce the number of casts in a query.
#[derive(Debug, Clone, Copy)]
pub enum InputLiteral {
    Int32(i32),
    Int64(i64),
    /// Either not a literal, or a literal we don't care to handle.
    Unhandled,
}

/// A datatype with some additional metadata around if we're dealing with
/// literals.
#[derive(Debug)]
pub struct InputDataType {
    pub datatype: DataType,
    pub literal: InputLiteral,
}

impl InputDataType {
    pub fn try_from_expr(expr: &Expression) -> Result<Self> {
        let (datatype, literal) = match expr {
            Expression::Literal(LiteralExpr(ScalarValue::Int32(v))) => {
                (DataType::int32(), InputLiteral::Int32(*v))
            }
            Expression::Literal(LiteralExpr(ScalarValue::Int64(v))) => {
                (DataType::int64(), InputLiteral::Int64(*v))
            }
            other => (other.datatype()?, InputLiteral::Unhandled),
        };

        Ok(InputDataType { datatype, literal })
    }
}

impl From<DataType> for InputDataType {
    fn from(value: DataType) -> Self {
        InputDataType {
            datatype: value,
            literal: InputLiteral::Unhandled,
        }
    }
}

// Since it's used in the "no function found" error.
impl fmt::Display for InputDataType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.datatype)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct CandidateSignature {
    /// Index of the signature
    pub signature_idx: usize,
    /// Casts that would need to be applied in order to satisfy the signature.
    pub casts: Vec<CastType>,
}

impl CandidateSignature {
    /// Find candidate signatures for the given dataypes.
    ///
    /// This will return a sorted vec where the first element is the candidate
    /// with the highest score.
    pub fn find_candidates<'a>(
        inputs: &[InputDataType],
        sigs: impl IntoIterator<Item = &'a Signature>,
    ) -> Vec<Self> {
        // TODO: Track only top 'k' candidates to reduce some work being done
        // here. Whatever 'k' is should match the constant in
        // 'NoFunctionMatches'.

        let mut candidates = Vec::new();

        let mut buf = Vec::new();
        for (idx, sig) in sigs.into_iter().enumerate() {
            if !Self::compare_and_fill_types(
                inputs,
                sig.positional_args,
                sig.variadic_arg,
                &mut buf,
            ) {
                continue;
            }

            candidates.push(CandidateSignature {
                signature_idx: idx,
                casts: std::mem::take(&mut buf),
            })
        }

        candidates.sort_unstable_by(|a, b| {
            let a_score: u32 = a.casts.iter().map(|c| c.score()).sum();
            let b_score: u32 = b.casts.iter().map(|c| c.score()).sum();

            a_score.cmp(&b_score).reverse() // Higher score should be first.
        });

        candidates
    }

    /// Compare the types we have with the types we want, filling the provided
    /// buffer with the cast type.
    ///
    /// Returns true if everything is able to be implicitly cast, false otherwise.
    fn compare_and_fill_types(
        have: &[InputDataType],
        want: &[DataTypeId],
        variadic: Option<DataTypeId>,
        buf: &mut Vec<CastType>,
    ) -> bool {
        // TODO: This logic is duplicated with candidate exact match.
        if variadic.is_some() {
            if have.len() < want.len() {
                return false;
            }
        } else if have.len() != want.len() {
            return false;
        }

        buf.clear();

        for (have, &want) in have.iter().zip(want.iter()) {
            if Self::no_cast_needed(&have.datatype, want) {
                buf.push(CastType::NoCastNeeded);
                continue;
            }

            if let Some((refined, score)) = Self::try_refine_literal(have, want) {
                buf.push(CastType::RefinedLiteral { refined, score });
                continue;
            }

            let score = implicit_cast_score(have.datatype.id, want);
            if let Some(score) = score {
                buf.push(CastType::Cast { to: want, score });
                continue;
            }

            return false;
        }

        // Check variadic.
        let remaining = &have[want.len()..];
        match variadic {
            Some(expected) if !remaining.is_empty() => {
                let expected = if expected == DataTypeId::Any {
                    // Find a common data type to use in place of Any.
                    match Self::best_datatype_for_variadic_any(remaining) {
                        Some(typ) => typ,
                        None => return false, // No common data type for all remaining args.
                    }
                } else {
                    expected
                };

                for have in remaining {
                    if Self::no_cast_needed(&have.datatype, expected) {
                        buf.push(CastType::NoCastNeeded);
                        continue;
                    }

                    if let Some((refined, score)) = Self::try_refine_literal(have, expected) {
                        buf.push(CastType::RefinedLiteral { refined, score });
                        continue;
                    }

                    let score = implicit_cast_score(have.datatype.id, expected);
                    if let Some(score) = score {
                        buf.push(CastType::Cast {
                            to: expected,
                            score,
                        });
                        continue;
                    }

                    return false;
                }

                // Everything's valid, casts have been pushed to the buffer.
                true
            }
            _ => {
                // No variadics to check. If we got this far, everything's valid
                // for this signature.
                true
            }
        }
    }

    /// Try to refine an integer literal based on the type we want.
    ///
    /// This will only return a refined literal if the we can safely fit the
    /// literal in the new type, as well as the score for the cast.
    fn try_refine_literal(have: &InputDataType, want: DataTypeId) -> Option<(RefinedLiteral, u32)> {
        match (have.literal, want) {
            // Int32
            (InputLiteral::Int32(v), DataTypeId::Int8) => match i8::try_from(v) {
                Ok(v) => Some((
                    RefinedLiteral::Int8(v),
                    DEFAULT_IMPLICIT_CAST_SCORES.i8 + REFINED_LITERAL_SCORE_BONUS,
                )),
                Err(_) => None,
            },
            (InputLiteral::Int32(v), DataTypeId::Int16) => match i16::try_from(v) {
                Ok(v) => Some((
                    RefinedLiteral::Int16(v),
                    DEFAULT_IMPLICIT_CAST_SCORES.i16 + REFINED_LITERAL_SCORE_BONUS,
                )),
                Err(_) => None,
            },
            (InputLiteral::Int32(v), DataTypeId::Int64) => Some((
                RefinedLiteral::Int64(v as i64),
                DEFAULT_IMPLICIT_CAST_SCORES.i64 + REFINED_LITERAL_SCORE_BONUS,
            )),
            // Int64
            (InputLiteral::Int64(v), DataTypeId::Int8) => match i8::try_from(v) {
                Ok(v) => Some((
                    RefinedLiteral::Int8(v),
                    DEFAULT_IMPLICIT_CAST_SCORES.i8 + REFINED_LITERAL_SCORE_BONUS,
                )),
                Err(_) => None,
            },
            (InputLiteral::Int64(v), DataTypeId::Int16) => match i16::try_from(v) {
                Ok(v) => Some((
                    RefinedLiteral::Int16(v),
                    DEFAULT_IMPLICIT_CAST_SCORES.i16 + REFINED_LITERAL_SCORE_BONUS,
                )),
                Err(_) => None,
            },
            (InputLiteral::Int64(v), DataTypeId::Int32) => match i32::try_from(v) {
                Ok(v) => Some((
                    RefinedLiteral::Int32(v),
                    DEFAULT_IMPLICIT_CAST_SCORES.i32 + REFINED_LITERAL_SCORE_BONUS,
                )),
                Err(_) => None,
            },
            _ => None,
        }
    }

    fn no_cast_needed(have: &DataType, want: DataTypeId) -> bool {
        match (have.id, want) {
            (_, DataTypeId::Any) => true,
            (a, b) => a == b,
        }
    }

    /// Get the best common data type that we can cast to for the given inputs. Returns None
    /// if there isn't a common data type.
    fn best_datatype_for_variadic_any(inputs: &[InputDataType]) -> Option<DataTypeId> {
        let mut best_type = None;
        let mut best_total_score = 0;

        for input in inputs {
            let test_type = input.datatype.id;
            let mut total_score = 0;
            let mut valid = true;

            for input in inputs {
                if input.datatype.id == test_type {
                    // Arbitrary.
                    total_score += 200;
                    continue;
                }

                let score = implicit_cast_score(input.datatype.id, test_type);
                match score {
                    Some(score) => total_score += score,
                    None => {
                        // Test type is not a valid cast for this input.
                        valid = false;
                    }
                }
            }

            if total_score > best_total_score && valid {
                best_type = Some(test_type);
                best_total_score = total_score;
            }
        }

        best_type
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn find_candidate_no_match() {
        let inputs = &[DataType::int64().into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::List],
            variadic_arg: None,
            return_type: DataTypeId::Int64,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        let expected: Vec<CandidateSignature> = Vec::new();
        assert_eq!(expected, candidates);
    }

    #[test]
    fn find_candidate_simple_no_variadic() {
        let inputs = &[DataType::int64().into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::Int64],
            variadic_arg: None,
            return_type: DataTypeId::Int64,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        let expected = vec![CandidateSignature {
            signature_idx: 0,
            casts: vec![CastType::NoCastNeeded],
        }];

        assert_eq!(expected, candidates);
    }

    #[test]
    fn find_candidate_match_list_i64() {
        let inputs = &[DataType::list(DataType::int64()).into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::List],
            variadic_arg: None,
            return_type: DataTypeId::Int64,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        let expected = vec![CandidateSignature {
            signature_idx: 0,
            casts: vec![CastType::NoCastNeeded],
        }];

        assert_eq!(expected, candidates);
    }

    #[test]
    fn find_candidate_match_list_any() {
        let inputs = &[DataType::list(DataType::int64()).into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::List],
            variadic_arg: None,
            return_type: DataTypeId::Any,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        let expected = vec![CandidateSignature {
            signature_idx: 0,
            casts: vec![CastType::NoCastNeeded],
        }];

        assert_eq!(expected, candidates);
    }

    // #[test]
    // fn find_candidate_no_match_list_inner_types_different() {
    //     let inputs = &[DataType::list(DataType::int64())];
    //     let sigs = &[Signature {
    //         positional_args: &[DataTypeId::List],
    //         variadic_arg: None,
    //         return_type: DataTypeId::Float64,
    //     }];

    //     let candidates = CandidateSignature::find_candidates(inputs, sigs);
    //     assert!(candidates.is_empty());
    // }

    #[test]
    fn find_candidate_match_list_with_null_input() {
        // SELECT * FROM unnest(NULL);
        //
        // NULL should be castable to a list.
        //
        // TDB if this is desired behavior.
        //
        // Postgres with null without types:
        // ```
        // select * from unnest(null);
        // ERROR:  function unnest(unknown) is not unique
        // ```
        //
        // However postgres executes it fine when provided `null::int[]` (and
        // produces zero rows).
        let inputs = &[DataType::null().into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::List],
            variadic_arg: None,
            return_type: DataTypeId::Table,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        assert_eq!(1, candidates.len(), "candidates: {candidates:?}");
    }

    #[test]
    fn find_candidate_simple_with_variadic() {
        let inputs = &[
            DataType::int64().into(),
            DataType::int64().into(),
            DataType::int64().into(),
        ];
        let sigs = &[Signature {
            positional_args: &[],
            variadic_arg: Some(DataTypeId::Any),
            return_type: DataTypeId::List,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        let expected = vec![CandidateSignature {
            signature_idx: 0,
            casts: vec![
                CastType::NoCastNeeded,
                CastType::NoCastNeeded,
                CastType::NoCastNeeded,
            ],
        }];

        assert_eq!(expected, candidates);
    }

    #[test]
    fn find_candidate_utf8_positional_and_variadic() {
        let inputs = &[DataType::utf8().into(), DataType::utf8().into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::Utf8],
            variadic_arg: Some(DataTypeId::Utf8),
            return_type: DataTypeId::Utf8,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        let expected = vec![CandidateSignature {
            signature_idx: 0,
            casts: vec![CastType::NoCastNeeded, CastType::NoCastNeeded],
        }];

        assert_eq!(expected, candidates);
    }

    #[test]
    fn find_candidate_utf8_positional_and_variadic_cast_needed() {
        // SELECT concat('a', 1)
        //
        // Should want to cast Int32 to Utf8.

        let inputs = &[DataType::utf8().into(), DataType::int32().into()];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::Utf8],
            variadic_arg: Some(DataTypeId::Utf8),
            return_type: DataTypeId::Utf8,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        assert_eq!(1, candidates.len());

        assert!(matches!(candidates[0].casts[0], CastType::NoCastNeeded));
        assert!(matches!(
            candidates[0].casts[1],
            CastType::Cast {
                to: DataTypeId::Utf8,
                ..
            }
        ));
    }

    #[test]
    fn find_candidate_utf8_positional_and_variadic_no_args_no_match() {
        let inputs = &[];
        let sigs = &[Signature {
            positional_args: &[DataTypeId::Utf8],
            variadic_arg: Some(DataTypeId::Utf8),
            return_type: DataTypeId::Utf8,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        assert!(candidates.is_empty());
    }

    #[test]
    fn find_candidate_float32_and_string() {
        // SELECT f32_col + '5.0'
        let inputs = &[DataType::float32().into(), DataType::utf8().into()];

        let sigs = &[Signature {
            positional_args: &[DataTypeId::Float32, DataTypeId::Float32],
            variadic_arg: None,
            return_type: DataTypeId::Float32,
        }];

        let candidates = CandidateSignature::find_candidates(inputs, sigs);
        assert_eq!(1, candidates.len());
    }

    #[test]
    fn best_datatype_for_ints_and_floats() {
        let inputs = &[
            DataType::int64().into(),
            DataType::float64().into(),
            DataType::int64().into(),
        ];
        let best = CandidateSignature::best_datatype_for_variadic_any(inputs);
        assert_eq!(Some(DataTypeId::Float64), best);
    }

    #[test]
    fn best_datatype_for_floats() {
        let inputs = &[
            DataType::float64().into(),
            DataType::float64().into(),
            DataType::float64().into(),
        ];
        let best = CandidateSignature::best_datatype_for_variadic_any(inputs);
        assert_eq!(Some(DataTypeId::Float64), best);
    }
}