{"record":{"id":"403b5d22a8d4c24e","repo":"databendlabs/databend","slug":"internal-error-entered-unreachable-code-403b5d","errorCode":null,"errorMessage":"internal error: entered unreachable code","messagePattern":"internal error: entered unreachable code","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/query/sql/src/planner/semantic/type_check/lambda.rs","lineNumber":323,"sourceCode":"                        .to_string(),\n                )\n                .set_span(span));\n            }\n        };\n\n        let inner_tys = if func_name == \"array_reduce\" {\n            let max_ty = self.transform_to_max_type(&inner_ty)?;\n            vec![max_ty.clone(), max_ty.clone()]\n        } else if func_name == \"map_filter\"\n            || func_name == \"map_transform_keys\"\n            || func_name == \"map_transform_values\"\n        {\n            match &inner_ty {\n                DataType::Null => {\n                    vec![DataType::Null, DataType::Null]\n                }\n                DataType::Tuple(t) => t.clone(),\n                _ => unreachable!(),\n            }\n        } else {\n            vec![inner_ty.clone()]\n        };\n\n        let lambda_columns = params\n            .iter()\n            .zip(inner_tys.iter())\n            .map(|(col, ty)| (col.clone(), ty.clone()))\n            .collect::<Vec<_>>();\n\n        let mut lambda_context = self.bind_context.clone();\n        let box (lambda_expr, lambda_type) = self.resolve_core_lambda_expr(\n            arena,\n            &mut lambda_context,\n            &lambda_columns,\n            lambda_expr,\n        )?;","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/sql/src/planner/semantic/type_check/lambda.rs#L305-L341","documentation":"This is an internal invariant panic inside Databend's lambda-function type checker. When resolving a lambda for a map function (map_filter, map_transform_keys, map_transform_values), the code assumes the map's inner type is a Tuple (key, value); any other inner type (e.g. Variant or Array left over from a JSON auto-cast) hits `unreachable!()` and panics with \"entered unreachable code\". It signals a type-checking gap, not a user-facing semantic error.","triggerScenarios":"Calling a map_* lambda function (e.g. map_filter(m, (k,v) -> ...)) where the argument type was auto-cast from Variant and its inner type resolves to something other than Tuple or Null — e.g. the argument is actually an array or a bare variant that got cast to Map(NonTupleType).","commonSituations":"Querying JSON/Variant columns with map lambda functions when the JSON value is not an object (e.g. it is a JSON array); mismatched function/argument families such as map_* lambdas over arrays; Databend version upgrades that change auto-cast behavior for Variant arguments.","solutions":["Inspect the actual data type of the lambda argument with SELECT typeof(col) and ensure it is a MAP whose inner type is TUPLE(key, value)","If the column is Variant, first cast it explicitly to the intended shape: CAST(col AS MAP(VARCHAR, VARIANT)) or ARRAY(...) before applying the map lambda","If the JSON value is an array, use array_* lambda functions (array_filter, array_transform) instead of map_*","If the type is genuinely a Map but the panic still occurs, this is a planner bug — capture the full query and EXPLAIN output and report it to Databend"],"exampleFix":"// before: map_filter on a Variant holding a JSON array\nSELECT map_filter(v, (k, v2) -> k = 'a') FROM t;\n// after: cast to the correct type or use the array lambda\nSELECT array_filter(v, x -> x['k'] = 'a') FROM t; -- if v is a JSON array","handlingStrategy":"validation","validationCode":"-- run before the lambda query\nSELECT typeof(arg_col) FROM t LIMIT 1;\n-- expect MAP(VARCHAR NULL, VARIANT NULL) or similar tuple-backed Map","typeGuard":"fn is_map_type(ty: &str) -> bool { ty.starts_with(\"MAP(\") }","tryCatchPattern":null,"preventionTips":["Match function family to argument shape: map_* lambdas only on Map columns, array_* lambdas only on Array columns","Cast VARIANT columns explicitly to MAP/ARRAY before applying lambdas","Use typeof()/DESCRIBE to confirm column types before writing lambda queries"],"tags":["internal-invariant","type-checking","lambda","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"}