databendlabs/databend · error
internal error: entered unreachable code
Error message
internal error: entered unreachable code
What it means
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.
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.
Example fix
// before
} else {
unreachable!()
};
// after
} else {
return Ok(false);
}; Defensive patterns
Strategy: try-catch
Try / catch
// Detect MV rewrite panics and fall back to base-table execution
if err.contains("unreachable code") && uses_materialized_view(query) {
exec(query_without_mv(query));
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- internal error: entered unreachable code
- internal error: entered unreachable code
- plan in InsertInputSource::Stag must be CopyIntoTable
- internal error: entered unreachable code
- {}
AI-assisted analysis of databendlabs/databend@288d84d76e (2026-09-11).
Data as JSON: /api/errors/e631d344dad7ab57.
Report an issue: GitHub.
Appendix: source
Thrown at src/query/sql/src/planner/semantic/materialized_view_rewriter.rs:281
original_targets.iter().enumerate().find(|(_, target)| {
matches!(target, SelectTarget::AliasedExpr { alias: Some(alias), .. }
if matches!(group, Expr::ColumnRef { column, .. }
if column.column.name() == alias.name))
})
});
let Some((first_output_index, selected_group)) = selected_group else {
return Err(ErrorCode::InvalidMaterializedView(format!(
"GROUP BY key '{}' is not in the view definition's select list",
group
)));
};
let SelectTarget::AliasedExpr {
expr: selected_expr,
..
} = selected_group
else {
unreachable!()
};
let selected_expr_text = selected_expr.to_string();
// Store a repeated GROUP BY expression only once, under the first matching output
// name. Every logical output of that expression must reference this same physical
// column; otherwise later outputs would retain a source expression that does not
// exist in the MV storage schema.
let name = output_names[first_output_index].clone();
for (output_index, target) in original_targets.iter().enumerate() {
if matches!(target, SelectTarget::AliasedExpr { expr, .. }
if expr.to_string() == selected_expr_text)
{
self.logical_define_exprs[output_index] = name.clone();
}
}
aggregate_targets.push(AggregateExprRewriter::target(
selected_expr.as_ref().clone(),
&name,
));View on GitHub (pinned to 288d84d76e)