Fix the review rule for a query tuning plan

Review the AI's codeEasy12 min

Your task

You are a database engineer. An AI told you to run SET enable_seqscan = off in every session because it "forces the planner to use the faster index". Turning off a planner method hides the real problem and can make other queries much slower, so your team reviews AI tuning plans with a rule before anyone runs them.

An assistant wrote review_query_tuning_plan(steps). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • steps: the plan's steps, in order, as short tokens such as explain_analyze or add_composite_index.
  • Return one of "reject_planner_override", "diagnose_first", "incomplete_needs_fix" and "accept".

Rules the tests check:

  • Trim whitespace from both ends of each step and lowercase it. A step that is empty after trimming is ignored.
  • These six steps override the planner: set_enable_seqscan_off, set_enable_indexscan_off, set_enable_bitmapscan_off, set_enable_hashjoin_off, set_random_page_cost_zero, force_index_hint. If any step is one of them, return "reject_planner_override", whatever else the plan holds.
  • These four steps are diagnostics: explain_analyze, check_row_estimates, inspect_join_order, review_index_usage. Every other step is a change.
  • Otherwise return the first that applies:
    1. "diagnose_first" when there is no diagnostic, or when the first change comes before the first diagnostic.
    2. "incomplete_needs_fix" when there is no change.
    3. "accept" otherwise.

Examples

review_query_tuning_plan(steps: list[str]) -> str
Example 1
Inputreview_query_tuning_plan(["set_enable_seqscan_off"])
Output"reject_planner_override"
Example 2
Inputreview_query_tuning_plan(["explain_analyze", "check_row_estimates", "add_composite_index"])
Output"accept"
Example 3
Inputreview_query_tuning_plan(["add_composite_index", "explain_analyze"])
Output"diagnose_first"