class_docstr: Used for building Physical Plan from Logical Plan.
class_docstr_tok: ["Used","for","building","Physical","Plan","from","Logical","Plan","."]
code: def optimize(self, logical_plan: Operator):
      optimizer_context = OptimizerContext(
          self.db, self.cost_model, self.rules_manager
      )
      memo = optimizer_context.memo
      grp_expr = optimizer_context.add_opr_to_group(opr=logical_plan)
      root_grp_id = grp_expr.group_id
      root_expr = memo.groups[root_grp_id].logical_exprs[0]
  
      # TopDown Rewrite
      # We specify rules that should be applied initially to prevent any interference
      # from other rules. For instance, if we apply the PushDownFilterThroughJoin
      # rule first, it can prevent the XformLateralJoinToLinearFlow rule from being
      # executed because the filter will be pushed to the right child.
  
      optimizer_context.task_stack.push(
          TopDownRewrite(
              root_expr,
              self.rules_manager.stage_one_rewrite_rules,
              optimizer_context,
          )
      )
      self.execute_task_stack(optimizer_context.task_stack)
  
      # BottomUp Rewrite
      root_expr = memo.groups[root_grp_id].logical_exprs[0]
      optimizer_context.task_stack.push(
          BottomUpRewrite(
              root_expr,
              self.rules_manager.stage_two_rewrite_rules,
              optimizer_context,
          )
      )
      self.execute_task_stack(optimizer_context.task_stack)
  
      # Optimize Expression (logical -> physical transformation)
      root_group = memo.get_group_by_id(root_grp_id)
      optimizer_context.task_stack.push(
          OptimizeGroup(root_group, optimizer_context)
      )
      self.execute_task_stack(optimizer_context.task_stack)
  
      # Build Optimal Tree
      optimal_plan = self.build_optimal_physical_plan(
          root_grp_id, optimizer_context
      )
      return optimal_plan
  
file_path: evadb/evadb/optimizer/plan_generator.py
imports: from evadb.database import EvaDBDatabase
  from evadb.optimizer.cost_model import CostModel
  from evadb.optimizer.operators import Operator
  from evadb.optimizer.optimizer_context import OptimizerContext
  from evadb.optimizer.optimizer_task_stack import OptimizerTaskStack
  from evadb.optimizer.optimizer_tasks import BottomUpRewrite, OptimizeGroup, TopDownRewrite
  from evadb.optimizer.property import PropertyType
  from evadb.optimizer.rules.rules_manager import RulesManager
label_desc: Use image node_id 4 for calling the PlanGenerator obj's underlying member method code with example usage: obj.optimize(logical_plan) and returns: optimal_plan