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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Definition and helper structs for the [`Cardinality`] attribute.

use std::collections::{BTreeMap, BTreeSet};

use mz_expr::{
    BinaryFunc, Id, JoinImplementation, MirRelationExpr, MirScalarExpr, TableFunc, UnaryFunc,
    VariadicFunc,
};
use mz_ore::cast::CastLossy;
use mz_repr::GlobalId;

use mz_repr::explain::ExprHumanizer;
use ordered_float::OrderedFloat;

use crate::attribute::subtree_size::SubtreeSize;
use crate::attribute::unique_keys::UniqueKeys;
use crate::attribute::{Attribute, DerivedAttributes, DerivedAttributesBuilder, Env};
use crate::symbolic::SymbolicExpression;

use super::Arity;

/// Compute the estimated cardinality of each subtree of a [MirRelationExpr] from the bottom up.
#[allow(missing_debug_implementations)]
pub struct Cardinality {
    /// Environment of computed values for this attribute
    env: Env<Self>,
    /// A vector of results for all nodes in the visited tree in
    /// post-visit order
    pub results: Vec<SymExp>,
    /// A factorizer for generating appropriating scaling factors
    pub factorize: Box<dyn Factorizer + Send + Sync>,
}

impl Default for Cardinality {
    fn default() -> Self {
        Cardinality {
            env: Env::default(),
            results: Vec::new(),
            factorize: Box::new(WorstCaseFactorizer {
                cardinalities: BTreeMap::new(),
            }),
        }
    }
}

/// The variables used in symbolic expressions representing cardinality
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum FactorizerVariable {
    /// The total cardinality of a given global id
    Id(GlobalId),
    /// The inverse of the number of distinct keys in the index held in the given column, i.e., 1/|# of distinct keys|
    ///
    /// TODO(mgree): need to correlate this back to a given global table, to feed in statistics (or have a sub-attribute for collecting this)
    Index(usize),
    /// An unbound local or other unknown quantity
    Unknown,
}

/// SymbolicExpressions specialized to factorizer variables
pub type SymExp = SymbolicExpression<FactorizerVariable>;

/// A `Factorizer` computes selectivity factors
pub trait Factorizer {
    /// Compute selectivity for the flat map of `tf`
    fn flat_map(&self, tf: &TableFunc, input: &SymExp) -> SymExp;
    /// Computes selectivity of the predicate `expr`, given that `unique_columns` are indexed/unique
    ///
    /// The result should be in the range [0, 1.0]
    fn predicate(&self, expr: &MirScalarExpr, unique_columns: &BTreeSet<usize>) -> SymExp;
    /// Computes selectivity for a filter
    fn filter(
        &self,
        predicates: &Vec<MirScalarExpr>,
        keys: &Vec<Vec<usize>>,
        input: &SymExp,
    ) -> SymExp;
    /// Computes selectivity for a join; the cardinality estimate for each input is paired with the keys on that input
    ///
    /// `unique_columns` maps column references (that are indexed/unique) to their relation's index in `inputs`
    fn join(
        &self,
        equivalences: &Vec<Vec<MirScalarExpr>>,
        implementation: &JoinImplementation,
        unique_columns: BTreeMap<usize, usize>,
        inputs: Vec<&SymExp>,
    ) -> SymExp;
    /// Computes selectivity for a reduce
    fn reduce(
        &self,
        group_key: &Vec<MirScalarExpr>,
        expected_group_size: &Option<u64>,
        input: &SymExp,
    ) -> SymExp;
    /// Computes selectivity for a topk
    fn topk(
        &self,
        group_key: &Vec<usize>,
        limit: &Option<MirScalarExpr>,
        expected_group_size: &Option<u64>,
        input: &SymExp,
    ) -> SymExp;
    /// Computes slectivity for a threshold
    fn threshold(&self, input: &SymExp) -> SymExp;
}

/// The simplest possible `Factorizer` that aims to generate worst-case, upper-bound cardinalities
#[derive(Debug)]
pub struct WorstCaseFactorizer {
    /// cardinalities for each `GlobalId` and its unique values
    pub cardinalities: BTreeMap<FactorizerVariable, usize>,
}

/// The default selectivity for predicates we know nothing about.
///
/// It is safe to use this instead of `FactorizerVariable::Index(col)`.
pub const WORST_CASE_SELECTIVITY: f64 = 0.1;

impl Factorizer for WorstCaseFactorizer {
    fn flat_map(&self, tf: &TableFunc, input: &SymExp) -> SymExp {
        match tf {
            TableFunc::Wrap { types, width } => {
                input * (f64::cast_lossy(types.len()) / f64::cast_lossy(*width))
            }
            _ => {
                // TODO(mgree) what explosion factor should we make up?
                input * &SymExp::from(4.0)
            }
        }
    }

    fn predicate(&self, expr: &MirScalarExpr, unique_columns: &BTreeSet<usize>) -> SymExp {
        let index_cardinality = |expr: &MirScalarExpr| -> Option<SymExp> {
            match expr {
                MirScalarExpr::Column(col) => {
                    if unique_columns.contains(col) {
                        Some(SymbolicExpression::symbolic(FactorizerVariable::Index(
                            *col,
                        )))
                    } else {
                        None
                    }
                }
                _ => None,
            }
        };

        match expr {
            MirScalarExpr::Column(_)
            | MirScalarExpr::Literal(_, _)
            | MirScalarExpr::CallUnmaterializable(_) => SymExp::from(1.0),
            MirScalarExpr::CallUnary { func, expr } => match func {
                UnaryFunc::Not(_) => 1.0 - self.predicate(expr, unique_columns),
                UnaryFunc::IsTrue(_) | UnaryFunc::IsFalse(_) => SymExp::from(0.5),
                UnaryFunc::IsNull(_) => {
                    if let Some(icard) = index_cardinality(expr) {
                        icard
                    } else {
                        SymExp::from(WORST_CASE_SELECTIVITY)
                    }
                }
                _ => SymExp::from(WORST_CASE_SELECTIVITY),
            },
            MirScalarExpr::CallBinary { func, expr1, expr2 } => {
                match func {
                    BinaryFunc::Eq => match (index_cardinality(expr1), index_cardinality(expr2)) {
                        (Some(icard1), Some(icard2)) => SymbolicExpression::max(icard1, icard2),
                        (Some(icard), None) | (None, Some(icard)) => icard,
                        (None, None) => SymExp::from(WORST_CASE_SELECTIVITY),
                    },
                    // 1.0 - the Eq case
                    BinaryFunc::NotEq => match (index_cardinality(expr1), index_cardinality(expr2))
                    {
                        (Some(icard1), Some(icard2)) => {
                            1.0 - SymbolicExpression::max(icard1, icard2)
                        }
                        (Some(icard), None) | (None, Some(icard)) => 1.0 - icard,
                        (None, None) => SymExp::from(1.0 - WORST_CASE_SELECTIVITY),
                    },
                    BinaryFunc::Lt | BinaryFunc::Lte | BinaryFunc::Gt | BinaryFunc::Gte => {
                        // TODO(mgree) if we have high/low key values and one of the columns is an index, we can do better
                        SymExp::from(0.33)
                    }
                    _ => SymExp::from(1.0), // TOOD(mgree): are there other interesting cases?
                }
            }
            MirScalarExpr::CallVariadic { func, exprs } => match func {
                VariadicFunc::And => {
                    // can't use SymExp::product because it expects a vector of references :/
                    let mut factor = SymExp::from(1.0);

                    for expr in exprs {
                        factor = factor * self.predicate(expr, unique_columns);
                    }

                    factor
                }
                VariadicFunc::Or => {
                    // TODO(mgree): BETWEEN will get compiled down to an OR of appropriate bounds---we could try to detect it and be clever

                    // F(expr1 OR expr2) = F(expr1) + F(expr2) - F(expr1) * F(expr2), but generalized
                    let mut exprs = exprs.into_iter();

                    let mut expr1;

                    if let Some(first) = exprs.next() {
                        expr1 = self.predicate(first, unique_columns);
                    } else {
                        return SymExp::from(1.0);
                    }

                    for expr2 in exprs {
                        let expr2 = self.predicate(expr2, unique_columns);

                        // TODO(mgree) a big expression! two things could help: hash-consing and simplification
                        expr1 = expr1.clone() + expr2.clone() - expr1.clone() * expr2;
                    }
                    expr1
                }
                _ => SymExp::from(1.0),
            },
            MirScalarExpr::If { cond: _, then, els } => SymExp::max(
                self.predicate(then, unique_columns),
                self.predicate(els, unique_columns),
            ),
        }
    }

    fn filter(
        &self,
        predicates: &Vec<MirScalarExpr>,
        keys: &Vec<Vec<usize>>,
        input: &SymExp,
    ) -> SymExp {
        // TODO(mgree): should we try to do something for indices built on multiple columns?
        let mut unique_columns = BTreeSet::new();
        for key in keys {
            if key.len() == 1 {
                unique_columns.insert(key[0]);
            }
        }

        // worst case scaling factor is 1
        let mut factor = SymExp::from(1.0);

        for expr in predicates {
            let predicate_scaling_factor = self.predicate(expr, &unique_columns);

            // constant scaling factors should be in [0,1]
            debug_assert!(
                match predicate_scaling_factor {
                    SymExp::Constant(OrderedFloat(n)) => 0.0 <= n && n <= 1.0,
                    _ => true,
                },
                "predicate scaling factor {predicate_scaling_factor} should be in the range [0,1]"
            );

            factor = factor * predicate_scaling_factor;
        }

        input.clone() * factor
    }

    fn join(
        &self,
        equivalences: &Vec<Vec<MirScalarExpr>>,
        _implementation: &JoinImplementation,
        unique_columns: BTreeMap<usize, usize>,
        inputs: Vec<&SymExp>,
    ) -> SymExp {
        let mut inputs = inputs.into_iter().cloned().collect::<Vec<_>>();

        for equiv in equivalences {
            // those sources which have a unique key
            let mut unique_sources = BTreeSet::new();
            let mut all_unique = true;

            for expr in equiv {
                if let MirScalarExpr::Column(col) = expr {
                    if let Some(idx) = unique_columns.get(col) {
                        unique_sources.insert(*idx);
                    } else {
                        all_unique = false;
                    }
                } else {
                    all_unique = false;
                }
            }

            // no unique columns in this equivalence
            if unique_sources.is_empty() {
                continue;
            }

            // ALL unique columns in this equivalence
            if all_unique {
                // these inputs have unique keys for _all_ of the equivalence, so they're a bound on how many rows we'll get from those sources
                // we'll find the leftmost such input and use it to hold the minimum; the other sources we set to 1.0 (so they have no effect)
                let mut sources = unique_sources.iter();

                let lhs_idx = *sources.next().unwrap();
                let mut lhs = std::mem::replace(&mut inputs[lhs_idx], SymExp::f64(1.0));
                for &rhs_idx in sources {
                    let rhs = std::mem::replace(&mut inputs[rhs_idx], SymExp::f64(1.0));
                    lhs = SymExp::min(lhs, rhs);
                }

                inputs[lhs_idx] = lhs;

                // best option! go look at the next equivalence
                continue;
            }

            // some unique columns in this equivalence
            for idx in unique_sources {
                // when joining R and S on R.x = S.x, if R.x is unique and S.x is not, we're bounded above by the cardinality of S
                inputs[idx] = SymExp::f64(1.0);
            }
        }

        SymbolicExpression::product(inputs)
    }

    fn reduce(
        &self,
        group_key: &Vec<MirScalarExpr>,
        expected_group_size: &Option<u64>,
        input: &SymExp,
    ) -> SymExp {
        // TODO(mgree): if no `group_key` is present, we can do way better

        if let Some(group_size) = expected_group_size {
            input / f64::cast_lossy(*group_size)
        } else if group_key.is_empty() {
            SymExp::from(1.0)
        } else {
            // in the worst case, every row is its own group
            input.clone()
        }
    }

    fn topk(
        &self,
        group_key: &Vec<usize>,
        limit: &Option<MirScalarExpr>,
        expected_group_size: &Option<u64>,
        input: &SymExp,
    ) -> SymExp {
        // TODO: support simple arithmetic expressions
        let k = limit
            .as_ref()
            .and_then(|l| l.as_literal_int64())
            .map_or(1, |l| std::cmp::max(0, l));

        if let Some(group_size) = expected_group_size {
            input * (f64::cast_lossy(k) / f64::cast_lossy(*group_size))
        } else if group_key.is_empty() {
            SymExp::from(k)
        } else {
            // in the worst case, every row is its own group
            input.clone()
        }
    }

    fn threshold(&self, input: &SymExp) -> SymExp {
        // worst case scaling factor is 1
        input.clone()
    }
}

impl Attribute for Cardinality {
    type Value = SymExp;

    fn derive(&mut self, expr: &MirRelationExpr, deps: &DerivedAttributes) {
        use MirRelationExpr::*;
        let n = self.results.len();

        match expr {
            Constant { rows, .. } => self
                .results
                .push(SymExp::from(rows.as_ref().map_or_else(|_| 0, |v| v.len()))),
            Get { id, .. } => match id {
                Id::Local(id) => match self.env.get(id) {
                    Some(value) => self.results.push(value.clone()),
                    None => {
                        // TODO(mgree) when will we meet an unbound local?
                        self.results
                            .push(SymExp::symbolic(FactorizerVariable::Unknown));
                    }
                },
                Id::Global(id) => self
                    .results
                    .push(SymbolicExpression::symbolic(FactorizerVariable::Id(*id))),
            },
            Let { .. } | Project { .. } | Map { .. } | ArrangeBy { .. } | Negate { .. } => {
                let input = self.results[n - 1].clone();
                self.results.push(input);
            }
            LetRec { .. } =>
            // TODO(mgree): implement a recurrence-based approach (or at least identify common idioms, e.g. transitive closure)
            {
                self.results
                    .push(SymbolicExpression::symbolic(FactorizerVariable::Unknown));
            }
            Union { base: _, inputs } => {
                let mut branches = Vec::with_capacity(inputs.len() + 1);
                let mut offset = 1;
                for _ in 0..inputs.len() {
                    branches.push(self.results[n - offset].clone());
                    offset += deps.get_results::<SubtreeSize>()[n - offset];
                }
                branches.push(self.results[n - offset].clone());

                self.results.push(SymbolicExpression::sum(branches));
            }
            FlatMap { func, .. } => {
                let input = &self.results[n - 1];
                self.results.push(self.factorize.flat_map(func, input));
            }
            Filter { predicates, .. } => {
                let input = &self.results[n - 1];
                let keys = &deps.get_results::<UniqueKeys>()[n - 1];
                self.results
                    .push(self.factorize.filter(predicates, keys, input));
            }
            Join {
                equivalences,
                implementation,
                inputs,
                ..
            } => {
                let mut input_results = Vec::with_capacity(inputs.len());

                // maps a column to the index in `inputs` that it belongs to
                let mut unique_columns = BTreeMap::new();
                let mut key_offset = 0;

                let mut offset = 1;
                for idx in 0..inputs.len() {
                    let input = &self.results[n - offset];
                    input_results.push(input);

                    let arity = deps.get_results::<Arity>()[n - offset];
                    let keys = &deps.get_results::<UniqueKeys>()[n - offset];
                    for key in keys {
                        if key.len() == 1 {
                            unique_columns.insert(key_offset + key[0], idx);
                        }
                    }
                    key_offset += arity;

                    offset += &deps.get_results::<SubtreeSize>()[n - offset];
                }

                self.results.push(self.factorize.join(
                    equivalences,
                    implementation,
                    unique_columns,
                    input_results,
                ));
            }
            Reduce {
                group_key,
                expected_group_size,
                ..
            } => {
                let input = &self.results[n - 1];
                self.results
                    .push(self.factorize.reduce(group_key, expected_group_size, input));
            }
            TopK {
                group_key,
                limit,
                expected_group_size,
                ..
            } => {
                let input = &self.results[n - 1];
                self.results.push(self.factorize.topk(
                    group_key,
                    limit,
                    expected_group_size,
                    input,
                ));
            }
            Threshold { .. } => {
                let input = &self.results[n - 1];
                self.results.push(self.factorize.threshold(input));
            }
        }
    }

    fn schedule_env_tasks(&mut self, expr: &MirRelationExpr) {
        self.env.schedule_tasks(expr);
    }

    fn handle_env_tasks(&mut self) {
        self.env.handle_tasks(&self.results);
    }

    fn add_dependencies(builder: &mut DerivedAttributesBuilder)
    where
        Self: Sized,
    {
        builder.require(SubtreeSize::default());
        builder.require(Arity::default());
        builder.require(UniqueKeys::default());
    }

    fn get_results(&self) -> &Vec<Self::Value> {
        &self.results
    }

    fn get_results_mut(&mut self) -> &mut Vec<Self::Value> {
        &mut self.results
    }

    fn take(self) -> Vec<Self::Value> {
        self.results
    }
}

impl SymExp {
    /// Render a symbolic expression nicely
    pub fn humanize(
        &self,
        h: &dyn ExprHumanizer,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        self.humanize_factor(h, f)
    }

    fn humanize_factor(
        &self,
        h: &dyn ExprHumanizer,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        use SymbolicExpression::*;
        match self {
            Sum(ss) => {
                assert!(ss.len() >= 2);

                let mut ss = ss.iter();
                ss.next().unwrap().humanize_factor(h, f)?;
                for s in ss {
                    write!(f, " + ")?;
                    s.humanize_factor(h, f)?;
                }
                Ok(())
            }
            _ => self.humanize_term(h, f),
        }
    }

    fn humanize_term(
        &self,
        h: &dyn ExprHumanizer,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        use SymbolicExpression::*;
        match self {
            Product(ps) => {
                assert!(ps.len() >= 2);

                let mut ps = ps.iter();
                ps.next().unwrap().humanize_term(h, f)?;
                for p in ps {
                    write!(f, " * ")?;
                    p.humanize_term(h, f)?;
                }
                Ok(())
            }
            _ => self.humanize_atom(h, f),
        }
    }

    fn humanize_atom(
        &self,
        h: &dyn ExprHumanizer,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        use SymbolicExpression::*;
        match self {
            Constant(OrderedFloat::<f64>(n)) => write!(f, "{n}"),
            Symbolic(FactorizerVariable::Id(v), n) => {
                let id = h.humanize_id(*v).unwrap_or_else(|| format!("{v:?}"));
                write!(f, "{id}")?;

                if *n > 1 {
                    write!(f, "^{n}")?;
                }

                Ok(())
            }
            Symbolic(FactorizerVariable::Index(col), n) => {
                write!(f, "icard(#{col})^{n}")
            }
            Symbolic(FactorizerVariable::Unknown, n) => {
                write!(f, "unknown^{n}")
            }
            Max(e1, e2) => {
                write!(f, "max(")?;
                e1.humanize_factor(h, f)?;
                write!(f, ", ")?;
                e2.humanize_factor(h, f)?;
                write!(f, ")")
            }
            Min(e1, e2) => {
                write!(f, "min(")?;
                e1.humanize_factor(h, f)?;
                write!(f, ", ")?;
                e2.humanize_factor(h, f)?;
                write!(f, ")")
            }
            Sum(_) | Product(_) => {
                write!(f, "(")?;
                self.humanize_factor(h, f)?;
                write!(f, ")")
            }
        }
    }
}

impl std::fmt::Display for SymExp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.humanize(&mz_repr::explain::DummyHumanizer, f)
    }
}

/// Wrapping struct for pretty printing of symbolic expressions
#[allow(missing_debug_implementations)]
pub struct HumanizedSymbolicExpression<'a, 'b> {
    expr: &'a SymExp,
    humanizer: &'b dyn ExprHumanizer,
}

impl<'a, 'b> HumanizedSymbolicExpression<'a, 'b> {
    /// Pairs a symbolic expression with a way to render GlobalIds
    pub fn new(expr: &'a SymExp, humanizer: &'b dyn ExprHumanizer) -> Self {
        Self { expr, humanizer }
    }
}

impl<'a, 'b> std::fmt::Display for HumanizedSymbolicExpression<'a, 'b> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.expr.normalize().humanize(self.humanizer, f)
    }
}