Skip to main content

mysql_handler/hton/
secondary_engine_kind.rs

1// Copyright (C) 2026 ren-yamanashi
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of this program hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! `SecondaryEngineGraphSimplificationRequest` enum and its
24//! `SecondaryEngineGraphSimplificationRequestParameters` struct from
25//! `sql/handler.h`. Returned by [`Handlerton::secondary_engine_check_optimizer_request`].
26//!
27//! [`Handlerton::secondary_engine_check_optimizer_request`]: crate::hton::Handlerton::secondary_engine_check_optimizer_request
28
29/// What the secondary engine asks the hypergraph optimizer to do next.
30///
31/// Mirrors `enum class SecondaryEngineGraphSimplificationRequest` in
32/// `mysql-server/sql/handler.h`.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[non_exhaustive]
35pub enum SecondaryEngineGraphSimplificationRequest {
36    /// `kContinue = 0`: continue optimization with the current hypergraph.
37    Continue,
38    /// `kRestart = 1`: restart hypergraph with the provided subgraph-pair count.
39    Restart,
40}
41
42impl SecondaryEngineGraphSimplificationRequest {
43    /// Convert to the raw C value the shim packs into the C++ struct.
44    #[must_use]
45    pub const fn to_raw(self) -> i32 {
46        match self {
47            Self::Continue => 0,
48            Self::Restart => 1,
49        }
50    }
51}
52
53/// The pair the secondary engine returns from
54/// [`Handlerton::secondary_engine_check_optimizer_request`].
55///
56/// Mirrors `struct SecondaryEngineGraphSimplificationRequestParameters` in
57/// `mysql-server/sql/handler.h`. Fields are crate-private and the type is
58/// `#[non_exhaustive]` so a future MySQL field can land without a
59/// SemVer-breaking change; engine code constructs values via [`Self::new`] or
60/// [`Self::keep_going`] and reads them via [`Self::request`] /
61/// [`Self::subgraph_pair_limit`].
62///
63/// [`Handlerton::secondary_engine_check_optimizer_request`]: crate::hton::Handlerton::secondary_engine_check_optimizer_request
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65#[non_exhaustive]
66pub struct SecondaryEngineOptimizerRequest {
67    pub(crate) request: SecondaryEngineGraphSimplificationRequest,
68    pub(crate) subgraph_pair_limit: i32,
69}
70
71impl SecondaryEngineOptimizerRequest {
72    /// Construct a request from its two parts.
73    #[must_use]
74    pub const fn new(
75        request: SecondaryEngineGraphSimplificationRequest,
76        subgraph_pair_limit: i32,
77    ) -> Self {
78        Self {
79            request,
80            subgraph_pair_limit,
81        }
82    }
83
84    /// "Keep going" default — what the trait returns when the engine has no
85    /// opinion. Matches the upstream documented default (`kContinue`, 0).
86    #[must_use]
87    pub const fn keep_going() -> Self {
88        Self::new(SecondaryEngineGraphSimplificationRequest::Continue, 0)
89    }
90
91    /// The transition the engine asks the optimizer to make.
92    #[must_use]
93    pub const fn request(&self) -> SecondaryEngineGraphSimplificationRequest {
94        self.request
95    }
96
97    /// Subgraph-pair limit for the (possibly) restarted hypergraph.
98    #[must_use]
99    pub const fn subgraph_pair_limit(&self) -> i32 {
100        self.subgraph_pair_limit
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn simplification_request_raw_round_trip() {
110        assert_eq!(
111            SecondaryEngineGraphSimplificationRequest::Continue.to_raw(),
112            0
113        );
114        assert_eq!(
115            SecondaryEngineGraphSimplificationRequest::Restart.to_raw(),
116            1
117        );
118    }
119
120    #[test]
121    fn keep_going_default() {
122        let r = SecondaryEngineOptimizerRequest::keep_going();
123        assert_eq!(
124            r.request,
125            SecondaryEngineGraphSimplificationRequest::Continue
126        );
127        assert_eq!(r.subgraph_pair_limit, 0);
128    }
129}