databendlabs/databend · error
internal error: entered unreachable code
Error message
internal error: entered unreachable code
What it means
A `unreachable!()` in `create_window_function` (called from `create_window_spec`) when building a LAG/LEAD window function description. The code expects the lag/lead `default` argument to be either absent or a `BoundColumnRef`; any other scalar shape panics. This is an internal assumption about how the expression binder normalizes LAG/LEAD default values.
Solutions
- Avoid a default argument, or wrap the literal in a subquery/column so the default resolves as a column reference, e.g., CROSS JOIN a one-row derived table providing the default
- Upgrade Databend to a version where LAG/LEAD default handling supports literals and arbitrary expressions
- Rewrite LAG/LEAD with default as COALESCE on a shifted expression, or compute the default in the outer query
- File the failing query with Databend maintainers — the builder should bind literal defaults rather than panicking
Example fix
// before -- LAG(x, 1, 0) OVER (ORDER BY id) // after SELECT COALESCE(LAG(x) OVER (ORDER BY id), 0) FROM t;
Defensive patterns
Strategy: validation
Validate before calling
// Ensure the LAG/LEAD default is a column reference before building
if let Some(d) = &lag_lead.default {
if !matches!(d.as_ref(), ScalarExpr::BoundColumnRef(_)) {
return Err(ErrorCode::Unimplemented("LAG/LEAD default must be a column reference"));
}
} Type guard
fn is_column_default(d: &Option<Box<ScalarExpr>>) -> bool {
matches!(d.as_deref(), None | Some(ScalarExpr::BoundColumnRef(_)))
} Try / catch
catch_unwind around create_window_function; surface a user-facing error suggesting COALESCE rewrite
Prevention
- Write LAG/LEAD defaults as COALESCE(LAG(x) OVER (...), default) on versions lacking literal-default support
- Keep binder normalization and window-function builders in sync regarding accepted default shapes
- Add tests for LAG/LEAD with literal and expression defaults
- Replace unreachable!() with a descriptive planning error
When it happens
Trigger: Creating a LAG or LEAD window function whose third (default) argument is a literal or complex expression instead of a plain column reference, e.g., `LAG(x, 1, 0) OVER (...)` or `LAG(x, 1, x+1) OVER (...)`.
Common situations: Using LAG/LEAD with constant default values (very common in SQL) on a version where non-column defaults are not supported by the window-function builder; queries migrated from other databases using literal defaults.
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
- internal error: entered unreachable code
- internal error: entered unreachable code
- internal error: entered unreachable code
AI-assisted analysis of databendlabs/databend@288d84d76e (2026-09-11).
Data as JSON: /api/errors/52ccc57ab3fdb7aa.
Report an issue: GitHub.
Appendix: source
Thrown at src/query/service/src/physical_plans/physical_window.rs:852
"Aggregate function sort description must be a BoundColumnRef"
.to_string(),
))
}
})
.collect::<Result<_>>()?,
display: ScalarExpr::AggregateFunction(agg.clone())
.as_expr()?
.sql_display(),
}))
}
WindowFuncType::LagLead(lag_lead) => {
let new_default = match &lag_lead.default {
None => LagLeadDefault::Null,
Some(d) => match d {
box ScalarExpr::BoundColumnRef(col) => {
LagLeadDefault::Index(col.column.index)
}
_ => unreachable!(),
},
};
Ok(WindowFunction::LagLead(LagLeadFunctionDesc {
is_lag: lag_lead.is_lag,
offset: lag_lead.offset,
return_type: *lag_lead.return_type.clone(),
arg: if let ScalarExpr::BoundColumnRef(col) = *lag_lead.arg.clone() {
Ok(col.column.index)
} else {
Err(ErrorCode::Internal(
"Window's lag function argument must be a BoundColumnRef".to_string(),
))
}?,
default: new_default,
}))
}
WindowFuncType::NthValue(func) => Ok(WindowFunction::NthValue(NthValueFunctionDesc {
n: func.n,View on GitHub (pinned to 288d84d76e)