Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit d732a10

Browse files
authored
Fix: fix for heuristic rule wrapper (#147)
The new expr returned by heuristic rules are not in the original group, which means it never got an OptimizeExpressionTask with exploring as false (OptimizeExpressionTask with exploring=False only be called in OptimizeGroup), it should evoke an OptimizeExpressionTask with exploring=false for itself to apply all the transform rules and implementation rules for itself. Besides, the pr adds checks for whether the original expr equals to the output expr for heuristic rule. If that's the case, it should prompt an error as this breaks the heuristic rule's definition. (Silently accepting it will mark the input expr being as a dead end and there's no more new exprs to replace it) Signed-off-by: AveryQi115 <[email protected]>
1 parent e35d508 commit d732a10

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

optd-core/src/cascades/memo.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,19 @@ impl<T: RelNodeTyp> Memo<T> {
285285
};
286286

287287
// if the new expr already in the memo table, merge the group and remove old expr
288-
if let Some(&expr_id) = self.expr_node_to_expr_id.get(&memo_node) {
289-
let group_id = self.get_group_id_of_expr_id(expr_id);
288+
if let Some(&new_expr_id) = self.expr_node_to_expr_id.get(&memo_node) {
289+
if new_expr_id == expr_id {
290+
// This is not acceptable, as it means the expr returned by a heuristic rule is exactly
291+
// the same as the original expr, which should not happen
292+
// TODO: we can silently ignore this case without marking the original one as a deadend
293+
// But the rule creators should follow the definition of the heuristic rule
294+
// and return an empty vec if their rule does not do the real transformation
295+
unreachable!("replace_group_expr: you're replacing the old expr with the same expr, please check your rules registered as heuristic
296+
and make sure if it does not do any transformation, it should return an empty vec!");
297+
}
298+
let group_id = self.get_group_id_of_expr_id(new_expr_id);
290299
let group_id = self.get_reduced_group_id(group_id);
291300
self.merge_group_inner(replace_group_id, group_id);
292-
293-
// TODO: instead of remove this expr from the old group,
294-
// we mark the expr as all rules have been fired to make it a dead end
295301
return false;
296302
}
297303

optd-core/src/cascades/tasks/apply_rule.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ impl<T: RelNodeTyp> Task<T> for ApplyRuleTask {
233233

234234
trace!(event = "apply_rule replace", expr_id = %self.expr_id, rule_id = %self.rule_id);
235235

236-
// rules registed as heuristics are always logical, exploring its children
237-
tasks.push(
238-
Box::new(OptimizeExpressionTask::new(self.expr_id, self.exploring))
239-
as Box<dyn Task<T>>,
240-
);
236+
// the expr returned by heuristic rule is a brand new one
237+
// so there's no optimizeExpressionTask for it in the original task list
238+
// we should set exploring as false to both envoke tranform rule and impl rule for it
239+
tasks.push(Box::new(OptimizeExpressionTask::new(self.expr_id, false))
240+
as Box<dyn Task<T>>);
241241
}
242242
continue;
243243
}

0 commit comments

Comments
 (0)