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
// 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.

//! Placeholder module for an [crate::plan::transform::Transform] that infers
//! physical monotonicity.

use std::marker::PhantomData;

use crate::plan::interpret::{PhysicallyMonotonic, SingleTimeMonotonic};
use crate::plan::reduce::{HierarchicalPlan, MonotonicPlan};
use crate::plan::top_k::{MonotonicTop1Plan, MonotonicTopKPlan, TopKPlan};
use crate::plan::transform::{BottomUpTransform, TransformConfig};
use crate::plan::{Plan, ReducePlan};

/// A transformation that takes the result of single-time physical monotonicity
/// analysis and refines, as appropriate, the setting of the `must_consolidate`
/// flag in monotonic `Plan` nodes with forced consolidation.
#[derive(Debug)]
pub struct RelaxMustConsolidate<T = mz_repr::Timestamp> {
    _phantom: PhantomData<T>,
}

impl<T> RelaxMustConsolidate<T> {
    /// TODO(#25239): Add documentation.
    pub fn new() -> Self {
        RelaxMustConsolidate {
            _phantom: Default::default(),
        }
    }
}

impl<T> BottomUpTransform<T> for RelaxMustConsolidate<T> {
    type Info = PhysicallyMonotonic;

    type Interpreter<'a> = SingleTimeMonotonic<'a, T>;

    fn name(&self) -> &'static str {
        "must_consolidate relaxation"
    }

    fn interpreter(config: &TransformConfig) -> Self::Interpreter<'_> {
        SingleTimeMonotonic::new(&config.monotonic_ids)
    }

    fn action(plan: &mut Plan<T>, _plan_info: &Self::Info, input_infos: &[Self::Info]) {
        // Look at `input_infos` and type of `Plan` node and refine the `must_consolidate` flag.
        // Note that the LIR nodes we care about have a single input.
        match (plan, input_infos) {
            (
                Plan::Reduce {
                    plan:
                        ReducePlan::Hierarchical(HierarchicalPlan::Monotonic(MonotonicPlan {
                            must_consolidate,
                            ..
                        })),
                    ..
                }
                | Plan::TopK {
                    top_k_plan:
                        TopKPlan::MonotonicTop1(MonotonicTop1Plan {
                            must_consolidate, ..
                        }),
                    ..
                }
                | Plan::TopK {
                    top_k_plan:
                        TopKPlan::MonotonicTopK(MonotonicTopKPlan {
                            must_consolidate, ..
                        }),
                    ..
                },
                [PhysicallyMonotonic(true)],
            ) => *must_consolidate = false,
            _ => (),
        }
    }
}