{"record":{"id":"9a4f2b35f98661ca","repo":"databendlabs/databend","slug":"internal-error-entered-unreachable-code-9a4f2b","errorCode":null,"errorMessage":"internal error: entered unreachable code","messagePattern":"internal error: entered unreachable code","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/query/sql/src/planner/optimizer/optimizers/rule/filter_rules/rule_push_down_sort_scan.rs","lineNumber":69,"sourceCode":"                match_op!(TopN -> EvalScalar -> Scan),\n            ],\n        }\n    }\n}\n\nimpl Rule for RulePushDownSortScan {\n    fn id(&self) -> RuleID {\n        self.id\n    }\n\n    fn apply(&self, s_expr: &SExpr, state: &mut TransformResult) -> Result<()> {\n        let (sort_items, sort_limit): (Vec<SortItem>, Option<usize>) = match s_expr.plan() {\n            RelOperator::Sort(sort) => (sort.items.clone(), sort.limit),\n            RelOperator::TopN(top_n) => {\n                let top_n: TopN = top_n.clone();\n                (top_n.items.clone(), Some(top_n.candidate_count()))\n            }\n            _ => unreachable!(),\n        };\n        let child = s_expr.child(0)?;\n\n        let (eval_scalar, mut scan) = match child.plan() {\n            RelOperator::Scan(scan) => (None, scan.clone()),\n            RelOperator::EvalScalar(eval_scalar) => {\n                let grand_child = child.child(0)?;\n                let scan: Scan = grand_child.plan().clone().try_into()?;\n                (Some(eval_scalar.clone()), scan)\n            }\n            _ => unreachable!(),\n        };\n\n        if scan.order_by.is_none() {\n            scan.order_by = Some(sort_items);\n        }\n\n        let can_push_limit = !scan.has_secure_predicates_not_applied_by_prewhere();","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/sql/src/planner/optimizer/optimizers/rule/filter_rules/rule_push_down_sort_scan.rs#L51-L87","documentation":"RulePushDownSortScan::apply panics with `unreachable!()` when the matched SExpr plan node is neither Sort nor TopN, or the child is neither Scan nor EvalScalar. This rewrite rule is registered only for Sort/TopN patterns, so the planner guarantees the shapes; any other shape means a rule-registration or planner bug.","triggerScenarios":"The rule's pattern/matchers in the optimizer are changed or extended (e.g. matching more RelOperator variants) while the match in apply() still only handles Sort/TopN over Scan/EvalScalar children.","commonSituations":"Adding a new relational operator variant or a new push-down rule pattern, rebasing optimizer changes, or a planner bug that hands a malformed SExpr to the rule.","solutions":["Reproduce with EXPLAIN (or the failing query) and inspect the plan shape reaching the rule","Verify the rule's match pattern registration matches the variants handled in apply() at rule_push_down_sort_scan.rs:69","Add arms or return ErrorCode::Internal with the unexpected RelOperator debug output instead of a bare panic","Run planner rule unit tests (cargo test -p databend-sql push_down_sort) after optimizer changes"],"exampleFix":"// before\nlet (sort_items, sort_limit) = match s_expr.plan() {\n    RelOperator::Sort(sort) => (sort.items.clone(), sort.limit),\n    RelOperator::TopN(top_n) => (top_n.items.clone(), Some(top_n.candidate_count())),\n    _ => unreachable!(),\n};\n// after\nlet (sort_items, sort_limit) = match s_expr.plan() {\n    RelOperator::Sort(sort) => (sort.items.clone(), sort.limit),\n    RelOperator::TopN(top_n) => (top_n.items.clone(), Some(top_n.candidate_count())),\n    other => return Err(ErrorCode::Internal(\n        format!(\"RulePushDownSortScan got unexpected plan: {:?}\", other))),\n};","handlingStrategy":"validation","validationCode":"debug_assert!(matches!(s_expr.plan(), RelOperator::Sort(_) | RelOperator::TopN(_)), \"RulePushDownSortScan applied to unexpected plan\");","typeGuard":"fn is_sort_like(op: &RelOperator) -> bool {\n    matches!(op, RelOperator::Sort(_) | RelOperator::TopN(_))\n}","tryCatchPattern":null,"preventionTips":["Keep rule matcher patterns and the match arms in apply() in sync","Prefer returning ErrorCode::Internal with the unexpected operator debug dump over unreachable!()","Add rule unit tests for every pattern the matcher can select","Grep rule registrations when adding RelOperator variants"],"tags":["rust","panic","planner","optimizer-rule"],"backgroundTag":"internal-invariant-violation","analyzedSha":"288d84d76e20a2f8f7173bda9691eb6ece301aa9","analyzedAt":"2026-09-11T11:29:36.208Z","contentChangedAt":"2026-09-11T11:29:36.208Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}