{"record":{"id":"ee92b4b8aef88843","repo":"databendlabs/databend","slug":"internal-error-entered-unreachable-code-ee92b4","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/type_check/window.rs","lineNumber":730,"sourceCode":"                _ => {\n                    return Err(ErrorCode::InvalidArgument(format!(\n                        \"The second argument to the function {:?} must be a constant\",\n                        func_name\n                    )));\n                }\n            }\n        } else {\n            None\n        };\n\n        let offset = offset.unwrap_or(1);\n\n        let is_lag = match func_name {\n            \"lag\" if offset < 0 => false,\n            \"lead\" if offset < 0 => true,\n            \"lag\" => true,\n            \"lead\" => false,\n            _ => unreachable!(),\n        };\n\n        let (default, return_type) = if args.len() == 3 {\n            (Some(args[2].clone()), arg_types[0].clone())\n        } else {\n            (None, arg_types[0].wrap_nullable())\n        };\n\n        let cast_default = default.map(|d| {\n            Box::new(ScalarExpr::CastExpr(CastExpr {\n                span: d.span(),\n                is_try: false,\n                argument: Box::new(d),\n                target_type: Box::new(return_type.clone()),\n            }))\n        });\n\n        Ok(WindowFuncType::LagLead(LagLeadFunction {","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/sql/src/planner/semantic/type_check/window.rs#L712-L748","documentation":"When type-checking LAG/LEAD window functions, the planner derives `is_lag` from the function name and offset sign; the match assumes the offset is negative only for lag/lead (offset is already normalized negated earlier). A function name or offset combination outside the four listed arms triggers `unreachable!()`, i.e. internal error 1001.","triggerScenarios":"Calling LAG/LEAD with a non-constant or unusual offset expression that the resolver failed to normalize to a known constant sign, so `func_name`/`offset` no longer matches any arm (e.g. other window function names routed into this resolver).","commonSituations":"Queries like `lag(x) over (...)` with negative offsets via `lead`, or planner regressions where a non-lag/lead name reaches resolve_lag_lead_window_function.","solutions":["Express the offset positively on the right function: use `lag(x, -n)` semantics via `lead(x, n)` or `lag(x, n)`","Constant-fold the offset argument (use an integer literal, not an expression) so the resolver can determine its sign","Report as a bug with the query and version; the default arm should return an Internal ErrorCode instead of unreachable!()"],"exampleFix":"// before (window.rs:725-730)\nlet is_lag = match func_name {\n    \"lag\" if offset < 0 => false,\n    \"lead\" if offset < 0 => true,\n    \"lag\" => true,\n    \"lead\" => false,\n    _ => unreachable!(),\n};\n// after\nlet is_lag = match func_name {\n    \"lag\" if offset < 0 => false,\n    \"lead\" if offset < 0 => true,\n    \"lag\" => true,\n    \"lead\" => false,\n    _ => return Err(ErrorCode::Internal(\n        format!(\"unexpected window func {} with offset {}\", func_name, offset))),\n};","handlingStrategy":"validation","validationCode":"-- use literal non-negative offsets on the right function\n-- good: lag(x, 3) / lead(x, 3)\n-- avoid: lag(x, -3) (express it as lead(x, 3))","typeGuard":null,"tryCatchPattern":"try {\n  runQuery(sql);\n} catch (e) {\n  if (e.code === 1001 && /lag|lead/i.test(sql)) {\n    // normalize offset sign and function name, then retry\n  }\n}","preventionTips":["Always pass an integer literal offset to lag/lead","Express negative-offset lag as lead (and vice versa)","Avoid calling lag/lead through wrappers that may alter the function name"],"tags":["rust","planner","internal-error","window-functions"],"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"}