{"record":{"id":"52ccc57ab3fdb7aa","repo":"databendlabs/databend","slug":"internal-error-entered-unreachable-code-52ccc5","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/service/src/physical_plans/physical_window.rs","lineNumber":852,"sourceCode":"                                    \"Aggregate function sort description must be a BoundColumnRef\"\n                                        .to_string(),\n                                ))\n                            }\n                        })\n                        .collect::<Result<_>>()?,\n                    display: ScalarExpr::AggregateFunction(agg.clone())\n                        .as_expr()?\n                        .sql_display(),\n                }))\n            }\n            WindowFuncType::LagLead(lag_lead) => {\n                let new_default = match &lag_lead.default {\n                    None => LagLeadDefault::Null,\n                    Some(d) => match d {\n                        box ScalarExpr::BoundColumnRef(col) => {\n                            LagLeadDefault::Index(col.column.index)\n                        }\n                        _ => unreachable!(),\n                    },\n                };\n                Ok(WindowFunction::LagLead(LagLeadFunctionDesc {\n                    is_lag: lag_lead.is_lag,\n                    offset: lag_lead.offset,\n                    return_type: *lag_lead.return_type.clone(),\n                    arg: if let ScalarExpr::BoundColumnRef(col) = *lag_lead.arg.clone() {\n                        Ok(col.column.index)\n                    } else {\n                        Err(ErrorCode::Internal(\n                            \"Window's lag function argument must be a BoundColumnRef\".to_string(),\n                        ))\n                    }?,\n                    default: new_default,\n                }))\n            }\n            WindowFuncType::NthValue(func) => Ok(WindowFunction::NthValue(NthValueFunctionDesc {\n                n: func.n,","sourceCodeStart":834,"sourceCodeEnd":870,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/service/src/physical_plans/physical_window.rs#L834-L870","documentation":"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.","triggerScenarios":"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 (...)`.","commonSituations":"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.","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"],"exampleFix":"// before\n-- LAG(x, 1, 0) OVER (ORDER BY id)\n// after\nSELECT COALESCE(LAG(x) OVER (ORDER BY id), 0) FROM t;","handlingStrategy":"validation","validationCode":"// Ensure the LAG/LEAD default is a column reference before building\nif let Some(d) = &lag_lead.default {\n    if !matches!(d.as_ref(), ScalarExpr::BoundColumnRef(_)) {\n        return Err(ErrorCode::Unimplemented(\"LAG/LEAD default must be a column reference\"));\n    }\n}","typeGuard":"fn is_column_default(d: &Option<Box<ScalarExpr>>) -> bool {\n    matches!(d.as_deref(), None | Some(ScalarExpr::BoundColumnRef(_)))\n}","tryCatchPattern":"catch_unwind around create_window_function; surface a user-facing error suggesting COALESCE rewrite","preventionTips":["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"],"tags":["rust","panic","unreachable","window-function","lag-lead","sql"],"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"}