|
| 1 | +use crate::StorageResult; |
| 2 | + |
| 3 | +/// A trait representing an implementation of a memoization table. |
| 4 | +/// |
| 5 | +/// Note that we use [`trait_variant`] here in order to add bounds on every method. |
| 6 | +/// See this [blog post]( |
| 7 | +/// https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#async-fn-in-public-traits) |
| 8 | +/// for more information. |
| 9 | +/// |
| 10 | +/// TODO Figure out for each when to get the ID of a record or the entire record itself. |
| 11 | +#[trait_variant::make(Send)] |
| 12 | +pub trait Memo { |
| 13 | + /// A type representing a group in the Cascades framework. |
| 14 | + type Group; |
| 15 | + /// A type representing a unique identifier for a group. |
| 16 | + type GroupId; |
| 17 | + /// A type representing a logical expression. |
| 18 | + type LogicalExpression; |
| 19 | + /// A type representing a unique identifier for a logical expression. |
| 20 | + type LogicalExpressionId; |
| 21 | + /// A type representing a physical expression. |
| 22 | + type PhysicalExpression; |
| 23 | + /// A type representing a unique identifier for a physical expression. |
| 24 | + type PhysicalExpressionId; |
| 25 | + |
| 26 | + /// Retrieves a [`Self::Group`] given a [`Self::GroupId`]. |
| 27 | + /// |
| 28 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 29 | + async fn get_group(&self, group_id: Self::GroupId) -> StorageResult<Self::Group>; |
| 30 | + |
| 31 | + /// Retrieves all group IDs that are stored in the memo table. |
| 32 | + async fn get_all_groups(&self) -> StorageResult<Vec<Self::Group>>; |
| 33 | + |
| 34 | + /// Retrieves a [`Self::LogicalExpression`] given a [`Self::LogicalExpressionId`]. |
| 35 | + /// |
| 36 | + /// If the logical expression does not exist, returns a [`MemoError::UnknownLogicalExpression`] |
| 37 | + /// error. |
| 38 | + async fn get_logical_expression( |
| 39 | + &self, |
| 40 | + logical_expression_id: Self::LogicalExpressionId, |
| 41 | + ) -> StorageResult<Self::LogicalExpression>; |
| 42 | + |
| 43 | + /// Retrieves a [`Self::PhysicalExpression`] given a [`Self::PhysicalExpressionId`]. |
| 44 | + /// |
| 45 | + /// If the physical expression does not exist, returns a |
| 46 | + /// [`MemoError::UnknownPhysicalExpression`] error. |
| 47 | + async fn get_physical_expression( |
| 48 | + &self, |
| 49 | + physical_expression_id: Self::PhysicalExpressionId, |
| 50 | + ) -> StorageResult<Self::PhysicalExpression>; |
| 51 | + |
| 52 | + /// Retrieves the parent group ID of a logical expression given its expression ID. |
| 53 | + /// |
| 54 | + /// If the logical expression does not exist, returns a [`MemoError::UnknownLogicalExpression`] |
| 55 | + /// error. |
| 56 | + async fn get_group_from_logical_expression( |
| 57 | + &self, |
| 58 | + logical_expression_id: Self::LogicalExpressionId, |
| 59 | + ) -> StorageResult<Self::GroupId>; |
| 60 | + |
| 61 | + /// Retrieves the parent group ID of a logical expression given its expression ID. |
| 62 | + /// |
| 63 | + /// If the physical expression does not exist, returns a |
| 64 | + /// [`MemoError::UnknownPhysicalExpression`] error. |
| 65 | + async fn get_group_from_physical_expression( |
| 66 | + &self, |
| 67 | + physical_expression_id: Self::PhysicalExpressionId, |
| 68 | + ) -> StorageResult<Self::GroupId>; |
| 69 | + |
| 70 | + /// Retrieves all of the logical expression "children" of a group. |
| 71 | + /// |
| 72 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 73 | + async fn get_group_logical_expressions( |
| 74 | + &self, |
| 75 | + group_id: Self::GroupId, |
| 76 | + ) -> StorageResult<Vec<Self::LogicalExpression>>; |
| 77 | + |
| 78 | + /// Retrieves all of the physical expression "children" of a group. |
| 79 | + /// |
| 80 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 81 | + async fn get_group_physical_expressions( |
| 82 | + &self, |
| 83 | + group_id: Self::GroupId, |
| 84 | + ) -> StorageResult<Vec<Self::PhysicalExpression>>; |
| 85 | + |
| 86 | + /// Retrieves the best physical query plan (winner) for a given group. |
| 87 | + /// |
| 88 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 89 | + async fn get_winner( |
| 90 | + &self, |
| 91 | + group_id: Self::GroupId, |
| 92 | + ) -> StorageResult<Option<Self::PhysicalExpressionId>>; |
| 93 | + |
| 94 | + /// Updates / replaces a group's best physical plan (winner). Optionally returns the previous |
| 95 | + /// winner's physical expression ID. |
| 96 | + /// |
| 97 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 98 | + async fn update_group_winner( |
| 99 | + &self, |
| 100 | + group_id: Self::GroupId, |
| 101 | + physical_expression_id: Self::PhysicalExpressionId, |
| 102 | + ) -> StorageResult<Option<Self::PhysicalExpressionId>>; |
| 103 | + |
| 104 | + /// Adds a logical expression to an existing group via its [`Self::GroupId`]. |
| 105 | + /// |
| 106 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 107 | + async fn add_logical_expression_to_group( |
| 108 | + &self, |
| 109 | + group_id: Self::GroupId, |
| 110 | + logical_expression: Self::LogicalExpression, |
| 111 | + children: Vec<Self::LogicalExpressionId>, |
| 112 | + ) -> StorageResult<()>; |
| 113 | + |
| 114 | + /// Adds a physical expression to an existing group via its [`Self::GroupId`]. |
| 115 | + /// |
| 116 | + /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error. |
| 117 | + async fn add_physical_expression_to_group( |
| 118 | + &self, |
| 119 | + group_id: Self::GroupId, |
| 120 | + physical_expression: Self::PhysicalExpression, |
| 121 | + children: Vec<Self::LogicalExpressionId>, |
| 122 | + ) -> StorageResult<()>; |
| 123 | + |
| 124 | + /// Adds a new logical expression into the memo table, creating a new group if the expression |
| 125 | + /// does not already exist. |
| 126 | + /// |
| 127 | + /// The [`Self::LogicalExpression`] type should have some sort of mechanism for checking if |
| 128 | + /// the expression has been seen before, and if it has already been created, then the parent |
| 129 | + /// group ID should also be retrievable. |
| 130 | + /// |
| 131 | + /// If the expression already exists, then this function will return the [`Self::GroupId`] of |
| 132 | + /// the parent group and the corresponding (already existing) [`Self::LogicalExpressionId`]. |
| 133 | + /// |
| 134 | + /// If the expression does not exist, this function will create a new group and a new |
| 135 | + /// expression, returning brand new IDs for both. |
| 136 | + async fn add_logical_expression( |
| 137 | + &self, |
| 138 | + expression: Self::LogicalExpression, |
| 139 | + children: Vec<Self::LogicalExpressionId>, |
| 140 | + ) -> StorageResult<(Self::GroupId, Self::LogicalExpressionId)>; |
| 141 | +} |
0 commit comments