Skip to content

Commit 8056a4b

Browse files
authored
feat: stabilize memo goals (#33)
## Problem Due to the need to handle physical property, the physical expression is now organized to be memoized based on an optimization _goal_, which represents a (group, required_physical_properties) pair instead of a group alone. Admittedly, we could share expressions among the different goals for the same group. However, it is hard to consider sharing expressions for all potential goals (a different combination of properties). It is likely in practice we will share an expression only with the default goal (with no required properties). ## Summary of changes **Memo** - stabilize Memo API `create_or_get_goal`, `add_physical_expr_to_goal`, `get_goal`, and `get_winner_physical_expr_in_goal`. **Storage** - A new `goals` table that mimics the `relation_groups` table for physical optimizations. _Misc_ - fix some clippy lints for `optd-dsl` --------- Signed-off-by: Yuchen Liang <yuchenl3@andrew.cmu.edu>
1 parent 1b7f31a commit 8056a4b

28 files changed

+446
-247
lines changed

optd-core/src/cascades/expressions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use crate::operators::scalar::ScalarOperator;
55
use crate::{operators::relational::logical::LogicalOperator, values::OptdValue};
66
use serde::Deserialize;
77

8+
use super::goal::GoalId;
89
use super::groups::{RelationalGroupId, ScalarGroupId};
910

1011
/// A logical expression in the memo table.
1112
pub type LogicalExpression = LogicalOperator<OptdValue, RelationalGroupId, ScalarGroupId>;
1213

1314
/// A physical expression in the memo table.
14-
pub type PhysicalExpression = PhysicalOperator<OptdValue, RelationalGroupId, ScalarGroupId>;
15+
pub type PhysicalExpression = PhysicalOperator<OptdValue, GoalId, ScalarGroupId>;
1516

1617
/// A scalar expression in the memo table.
1718
pub type ScalarExpression = ScalarOperator<OptdValue, ScalarGroupId>;

optd-core/src/cascades/goal.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use serde::Deserialize;
22

3+
use super::{groups::RelationalGroupId, properties::PhysicalProperties};
4+
35
/// A unique identifier for a goal in the memo table.
46
#[repr(transparent)]
57
#[derive(
@@ -20,7 +22,6 @@ pub struct GoalId(pub i64);
2022

2123
/// The optimization status of a group or a physical expression with a goal in the memo table.
2224
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, sqlx::Type)]
23-
#[repr(i32)]
2425
pub enum OptimizationStatus {
2526
/// The group or the physical expression has not been explored.
2627
Unoptimized,
@@ -32,6 +33,13 @@ pub enum OptimizationStatus {
3233

3334
#[derive(Debug, Clone, PartialEq, sqlx::FromRow)]
3435
pub struct Goal {
35-
pub id: GoalId,
36+
/// The identifier of the goal.
37+
pub representative_goal_id: GoalId,
38+
/// The identifier of the group that the goal is associated with.
39+
pub group_id: RelationalGroupId,
40+
/// The required physical properties for the goal.
41+
#[sqlx(json)]
42+
pub required_physical_properties: PhysicalProperties,
43+
/// The optimization status of the goal.
3644
pub optimization_status: OptimizationStatus,
3745
}

optd-core/src/cascades/groups.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use serde::Deserialize;
2-
31
/// A unique identifier for a group of relational expressions in the memo table.
42
#[repr(transparent)]
53
#[derive(
@@ -13,7 +11,7 @@ use serde::Deserialize;
1311
Hash,
1412
sqlx::Type,
1513
serde::Serialize,
16-
Deserialize,
14+
serde::Deserialize,
1715
)]
1816
#[sqlx(transparent)]
1917
pub struct RelationalGroupId(pub i64);
@@ -31,14 +29,13 @@ pub struct RelationalGroupId(pub i64);
3129
Hash,
3230
sqlx::Type,
3331
serde::Serialize,
34-
Deserialize,
32+
serde::Deserialize,
3533
)]
3634
#[sqlx(transparent)]
3735
pub struct ScalarGroupId(pub i64);
3836

3937
/// The exploration status of a group or a logical expression in the memo table.
4038
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, sqlx::Type)]
41-
#[repr(i32)]
4239
pub enum ExplorationStatus {
4340
/// The group or the logical expression has not been explored.
4441
Unexplored,

optd-core/src/cascades/memo.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,30 @@
99
1010
use std::sync::Arc;
1111

12+
use crate::cost_model::Cost;
13+
1214
use super::{
1315
expressions::{
1416
LogicalExpression, LogicalExpressionId, PhysicalExpression, PhysicalExpressionId,
1517
ScalarExpression, ScalarExpressionId,
1618
},
17-
goal::Goal,
19+
goal::{Goal, GoalId},
1820
groups::{RelationalGroupId, ScalarGroupId},
19-
properties::PhysicalProperty,
21+
properties::PhysicalProperties,
2022
};
2123
use anyhow::Result;
2224

2325
#[trait_variant::make(Send)]
2426
pub trait Memoize: Send + Sync + 'static {
2527
/// Creates or get an optimization goal for a group with some required physical properties.
26-
async fn create_or_get_relation_group_goal(
28+
async fn create_or_get_goal(
2729
&self,
2830
group_id: RelationalGroupId,
29-
required_physical_props: Vec<PhysicalProperty>,
30-
) -> Result<Goal>;
31+
required_physical_props: PhysicalProperties,
32+
) -> Result<GoalId>;
33+
34+
/// Gets the metadata that describes a goal.
35+
async fn get_goal(&self, goal_id: GoalId) -> Result<Arc<Goal>>;
3136

3237
/// Gets all logical expressions in a group.
3338
async fn get_all_logical_exprs_in_group(
@@ -80,16 +85,26 @@ pub trait Memoize: Send + Sync + 'static {
8085
to: ScalarGroupId,
8186
) -> Result<ScalarGroupId>;
8287

83-
/// Gets all logical expressions in a group.
84-
async fn get_all_physical_exprs_in_group(
88+
async fn merge_goal(&self, from: GoalId, to: GoalId) -> Result<GoalId>;
89+
90+
/// Gets the winner physical expression.
91+
async fn get_winner_physical_expr_in_goal(
8592
&self,
86-
group_id: RelationalGroupId,
93+
goal_id: GoalId,
94+
) -> Result<Option<(PhysicalExpressionId, Arc<PhysicalExpression>, Cost)>>;
95+
96+
/// Gets all physical expressions in a goal.
97+
async fn get_all_physical_exprs_in_goal(
98+
&self,
99+
goal_id: GoalId,
87100
) -> Result<Vec<(PhysicalExpressionId, Arc<PhysicalExpression>)>>;
88101

89-
/// Adds a physical expression to an existing group in the memo table.
90-
async fn add_physical_expr_to_group(
102+
/// Adds a physical expression to a goal in the memo table.
103+
// TODO: cost and statistics probably is also added here.
104+
async fn add_physical_expr_to_goal(
91105
&self,
92106
physical_expr: &PhysicalExpression,
93-
group_id: RelationalGroupId,
94-
) -> Result<RelationalGroupId>;
107+
cost: Cost,
108+
goal_id: GoalId,
109+
) -> Result<GoalId>;
95110
}

0 commit comments

Comments
 (0)