-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(optimizer): use Cell
instead of RefCell
for interior mutability
#19883
Merged
+51
−40
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
// limitations under the License. | ||
|
||
use core::fmt::Formatter; | ||
use std::cell::{RefCell, RefMut}; | ||
use std::cell::{Cell, RefCell, RefMut}; | ||
use std::collections::HashMap; | ||
use std::marker::PhantomData; | ||
use std::rc::Rc; | ||
|
@@ -35,8 +35,6 @@ type PhantomUnsend = PhantomData<Rc<()>>; | |
|
||
pub struct OptimizerContext { | ||
session_ctx: Arc<SessionImpl>, | ||
/// Store plan node id | ||
next_plan_node_id: RefCell<i32>, | ||
/// The original SQL string, used for debugging. | ||
sql: Arc<str>, | ||
/// Normalized SQL string. See [`HandlerArgs::normalize_sql`]. | ||
|
@@ -47,14 +45,10 @@ pub struct OptimizerContext { | |
optimizer_trace: RefCell<Vec<String>>, | ||
/// Store the optimized logical plan of optimizer | ||
logical_explain: RefCell<Option<String>>, | ||
/// Store correlated id | ||
next_correlated_id: RefCell<u32>, | ||
/// Store options or properties from the `with` clause | ||
with_options: WithOptions, | ||
/// Store the Session Timezone and whether it was used. | ||
session_timezone: RefCell<SessionTimezone>, | ||
/// Store expr display id. | ||
next_expr_display_id: RefCell<usize>, | ||
/// Total number of optimization rules have been applied. | ||
total_rule_applied: RefCell<usize>, | ||
/// Store the configs can be overwritten in with clause | ||
|
@@ -64,9 +58,22 @@ pub struct OptimizerContext { | |
/// `PlanRef`, used by rcte's planning. (e.g., in `LogicalCteRef`) | ||
rcte_cache: RefCell<HashMap<ShareId, PlanRef>>, | ||
|
||
/// Last assigned plan node ID. | ||
last_plan_node_id: Cell<i32>, | ||
/// Last assigned correlated ID. | ||
last_correlated_id: Cell<u32>, | ||
/// Last assigned expr display ID. | ||
last_expr_display_id: Cell<usize>, | ||
|
||
_phantom: PhantomUnsend, | ||
} | ||
|
||
pub(in crate::optimizer) struct LastAssignedIds { | ||
last_plan_node_id: i32, | ||
last_correlated_id: u32, | ||
last_expr_display_id: usize, | ||
} | ||
|
||
pub type OptimizerContextRef = Rc<OptimizerContext>; | ||
|
||
impl OptimizerContext { | ||
|
@@ -84,19 +91,21 @@ impl OptimizerContext { | |
let overwrite_options = OverwriteOptions::new(&mut handler_args); | ||
Self { | ||
session_ctx: handler_args.session, | ||
next_plan_node_id: RefCell::new(RESERVED_ID_NUM.into()), | ||
sql: handler_args.sql, | ||
normalized_sql: handler_args.normalized_sql, | ||
explain_options, | ||
optimizer_trace: RefCell::new(vec![]), | ||
logical_explain: RefCell::new(None), | ||
next_correlated_id: RefCell::new(0), | ||
with_options: handler_args.with_options, | ||
session_timezone, | ||
next_expr_display_id: RefCell::new(RESERVED_ID_NUM.into()), | ||
total_rule_applied: RefCell::new(0), | ||
overwrite_options, | ||
rcte_cache: RefCell::new(HashMap::new()), | ||
|
||
last_plan_node_id: Cell::new(RESERVED_ID_NUM.into()), | ||
last_correlated_id: Cell::new(0), | ||
last_expr_display_id: Cell::new(RESERVED_ID_NUM.into()), | ||
|
||
_phantom: Default::default(), | ||
} | ||
} | ||
|
@@ -107,53 +116,57 @@ impl OptimizerContext { | |
pub async fn mock() -> OptimizerContextRef { | ||
Self { | ||
session_ctx: Arc::new(SessionImpl::mock()), | ||
next_plan_node_id: RefCell::new(0), | ||
sql: Arc::from(""), | ||
normalized_sql: "".to_owned(), | ||
explain_options: ExplainOptions::default(), | ||
optimizer_trace: RefCell::new(vec![]), | ||
logical_explain: RefCell::new(None), | ||
next_correlated_id: RefCell::new(0), | ||
with_options: Default::default(), | ||
session_timezone: RefCell::new(SessionTimezone::new("UTC".into())), | ||
next_expr_display_id: RefCell::new(0), | ||
total_rule_applied: RefCell::new(0), | ||
overwrite_options: OverwriteOptions::default(), | ||
rcte_cache: RefCell::new(HashMap::new()), | ||
|
||
last_plan_node_id: Cell::new(0), | ||
last_correlated_id: Cell::new(0), | ||
last_expr_display_id: Cell::new(0), | ||
|
||
_phantom: Default::default(), | ||
} | ||
.into() | ||
} | ||
|
||
pub fn next_plan_node_id(&self) -> PlanNodeId { | ||
*self.next_plan_node_id.borrow_mut() += 1; | ||
PlanNodeId(*self.next_plan_node_id.borrow()) | ||
} | ||
|
||
pub fn get_plan_node_id(&self) -> i32 { | ||
*self.next_plan_node_id.borrow() | ||
PlanNodeId(self.last_plan_node_id.update(|id| id + 1)) | ||
} | ||
|
||
pub fn set_plan_node_id(&self, next_plan_node_id: i32) { | ||
*self.next_plan_node_id.borrow_mut() = next_plan_node_id; | ||
pub fn next_correlated_id(&self) -> CorrelatedId { | ||
self.last_correlated_id.update(|id| id + 1) | ||
} | ||
|
||
pub fn next_expr_display_id(&self) -> usize { | ||
*self.next_expr_display_id.borrow_mut() += 1; | ||
*self.next_expr_display_id.borrow() | ||
self.last_expr_display_id.update(|id| id + 1) | ||
} | ||
|
||
pub fn get_expr_display_id(&self) -> usize { | ||
*self.next_expr_display_id.borrow() | ||
pub(in crate::optimizer) fn backup_elem_ids(&self) -> LastAssignedIds { | ||
LastAssignedIds { | ||
last_plan_node_id: self.last_plan_node_id.get(), | ||
last_correlated_id: self.last_correlated_id.get(), | ||
last_expr_display_id: self.last_expr_display_id.get(), | ||
} | ||
} | ||
|
||
pub fn set_expr_display_id(&self, expr_display_id: usize) { | ||
*self.next_expr_display_id.borrow_mut() = expr_display_id; | ||
/// This should only be called in [`crate::optimizer::plan_node::reorganize_elements_id`]. | ||
pub(in crate::optimizer) fn reset_elem_ids(&self) { | ||
self.last_plan_node_id.set(0); | ||
self.last_correlated_id.set(0); | ||
self.last_expr_display_id.set(0); | ||
} | ||
|
||
pub fn next_correlated_id(&self) -> CorrelatedId { | ||
*self.next_correlated_id.borrow_mut() += 1; | ||
*self.next_correlated_id.borrow() | ||
pub(in crate::optimizer) fn restore_elem_ids(&self, backup: LastAssignedIds) { | ||
self.last_plan_node_id.set(backup.last_plan_node_id); | ||
self.last_correlated_id.set(backup.last_correlated_id); | ||
self.last_expr_display_id.set(backup.last_expr_display_id); | ||
} | ||
|
||
pub fn add_rule_applied(&self, num: usize) { | ||
|
@@ -255,12 +268,12 @@ impl std::fmt::Debug for OptimizerContext { | |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
f, | ||
"QueryContext {{ next_plan_node_id = {}, sql = {}, explain_options = {}, next_correlated_id = {}, with_options = {:?} }}", | ||
self.next_plan_node_id.borrow(), | ||
"QueryContext {{ sql = {}, explain_options = {}, with_options = {:?}, last_plan_node_id = {}, last_correlated_id = {} }}", | ||
self.sql, | ||
self.explain_options, | ||
self.next_correlated_id.borrow(), | ||
&self.with_options | ||
self.with_options, | ||
self.last_plan_node_id.get(), | ||
self.last_correlated_id.get(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about just inlining these functions info
reorganize_elements_id
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When adding a new
id
later, we can hardly remember that there's areorganize_elements_id
far away need to be updated. So I decided to hide all the ids insideLastAssignedIds
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get you. We can still add an
id
inOptimizerContext
but not inLastAssignedIds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you should replace the fields to
LastAssignedIds
inOptimizerContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact is that we missed
last_correlated_id
inreorganize_elements_id
before. I want to reduce the possibility of future error, so I moved them toLastAssignedIds
and put it right next toOptimizerContext
. I also definitely considered directly usingLastAssignedIds
as a field inOptimizerContext
, but IMO it's too annoying to access these ID fields via a longself.last_assigned_ids.last_plan_node_id
. After all forgetting to add new id field toLastAssignedIds
is not a critical issue, we just need to prevent it to a proper degree.This PR doesn't aim to completely refactor the optimizer element IDs, otherwise I would extract an
IdAllocator
struct to allocate IDs instead ofself.last_plan_node_id.update
inOptimizerContext
. Also I don't think use interior mutability here inOptimizerContext
is a good idea. But I don't want to do that in this PR. It's just a quick fix to unblock my future work on other things.