{"record":{"id":"e631d344dad7ab57","repo":"databendlabs/databend","slug":"internal-error-entered-unreachable-code-e631d3","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/semantic/materialized_view_rewriter.rs","lineNumber":281,"sourceCode":"                        original_targets.iter().enumerate().find(|(_, target)| {\n                            matches!(target, SelectTarget::AliasedExpr { alias: Some(alias), .. }\n                                if matches!(group, Expr::ColumnRef { column, .. }\n                                    if column.column.name() == alias.name))\n                        })\n                    });\n\n                let Some((first_output_index, selected_group)) = selected_group else {\n                    return Err(ErrorCode::InvalidMaterializedView(format!(\n                        \"GROUP BY key '{}' is not in the view definition's select list\",\n                        group\n                    )));\n                };\n                let SelectTarget::AliasedExpr {\n                    expr: selected_expr,\n                    ..\n                } = selected_group\n                else {\n                    unreachable!()\n                };\n                let selected_expr_text = selected_expr.to_string();\n                // Store a repeated GROUP BY expression only once, under the first matching output\n                // name. Every logical output of that expression must reference this same physical\n                // column; otherwise later outputs would retain a source expression that does not\n                // exist in the MV storage schema.\n                let name = output_names[first_output_index].clone();\n                for (output_index, target) in original_targets.iter().enumerate() {\n                    if matches!(target, SelectTarget::AliasedExpr { expr, .. }\n                        if expr.to_string() == selected_expr_text)\n                    {\n                        self.logical_define_exprs[output_index] = name.clone();\n                    }\n                }\n                aggregate_targets.push(AggregateExprRewriter::target(\n                    selected_expr.as_ref().clone(),\n                    &name,\n                ));","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/sql/src/planner/semantic/materialized_view_rewriter.rs#L263-L299","documentation":"In the materialized-view rewriter's rewrite_aggregate, the code first matched a select item as an aggregate-shaped SelectTarget and then re-matches it as SelectTarget::AliasedExpr; the second match is assumed infallible, so the else branch is `unreachable!()`. If the matched item was actually a SelectStmt::aliased_items / non-aliased target, the assumption is wrong and the rewriter panics.","triggerScenarios":"Rewriting a query against a materialized view whose GROUP BY matching logic selects an output item that is not SelectTarget::AliasedExpr — e.g. MV definitions with unusual select-item forms (expressions without alias, set-returning items) that slip past the earlier filter.","commonSituations":"Hit by users querying an aggregate materialized view where the MV's SELECT list contains GROUP BY expressions the rewriter maps by alias; typically after changes to SelectTarget representation or MV binding.","solutions":["Check the preceding filter that selected `selected_group`; extend it to guarantee the item is AliasedExpr.","Log the offending select target when the invariant breaks to identify which MV/query shape triggers it.","Return None/Ok(false) from rewrite_aggregate for non-aliased targets, falling back to normal (non-MV) query execution.","Recreate the MV with explicit aliases for every GROUP BY expression as a workaround."],"exampleFix":"// before\n} else {\n    unreachable!()\n};\n// after\n} else {\n    return Ok(false);\n};","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Detect MV rewrite panics and fall back to base-table execution\nif err.contains(\"unreachable code\") && uses_materialized_view(query) {\n    exec(query_without_mv(query));\n}","preventionTips":["Define MVs with explicit aliases for every GROUP BY expression.","Avoid unusual select-item forms in aggregate MVs while on affected versions.","Test MV-hit queries after planner upgrades.","Temporarily disable the affected MV (ALTER ... SUSPEND) if queries panic."],"tags":["rust","planner","materialized-view","panic"],"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"}