{"record":{"id":"a21c2c9d17c3908d","repo":"databendlabs/databend","slug":"internal-error-entered-unreachable-code-a21c2c","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/optimizer/optimizers/rule/join_rules/push_down_filter_join/mark_join_to_semi_join.rs","lineNumber":68,"sourceCode":"            if col.column.index == mark_index {\n                find_mark_index = true;\n                filter.predicates.remove(idx);\n                break;\n            }\n        }\n        // Check if the predicate used mark, if so, we won't convert it to semi join\n        return Ok((s_expr.clone(), false));\n    }\n\n    if !find_mark_index {\n        // To be conservative, we do not convert\n        return Ok((s_expr.clone(), false));\n    }\n\n    join.join_type = match join.join_type {\n        JoinType::LeftMark => JoinType::RightSemi,\n        JoinType::RightMark => JoinType::LeftSemi,\n        _ => unreachable!(),\n    };\n\n    metadata.write().add_removed_mark_index(mark_index);\n\n    // clear is null equal sign\n    join.equi_conditions.iter_mut().for_each(|c| {\n        c.is_null_equal = false;\n    });\n\n    let s_join_expr = s_expr.child(0)?;\n    let mut result = SExpr::create_binary(\n        Arc::new(join.into()),\n        Arc::new(s_join_expr.child(0)?.clone()),\n        Arc::new(s_join_expr.child(1)?.clone()),\n    );\n\n    if !filter.predicates.is_empty() {\n        result = SExpr::create_unary(Arc::new(filter.into()), Arc::new(result));","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/sql/src/planner/optimizer/optimizers/rule/join_rules/push_down_filter_join/mark_join_to_semi_join.rs#L50-L86","documentation":"In the mark-join-to-semi-join rewrite, the code has already established (via mark_index checks) that the join is a LeftMark or RightMark join before this match; any other JoinType reaching the match violates that precondition and hits `unreachable!()`, panicking with \"internal error: entered unreachable code\". The invariant lives in the caller's filtering logic (mark_index lookup and outer-join type checks), not in this match itself.","triggerScenarios":"Calling convert_mark_to_semi_join on a Join whose mark_index resolves but whose join_type is not LeftMark/RightMark — e.g. the wrong column index was extracted from the mark join cache, or the join was rewritten by another rule between selection and conversion.","commonSituations":"Occurs during optimization of queries with scalar subqueries / EXISTS rewritten into mark joins, typically after partial rule application or a bug in index bookkeeping in push_down_filter_join passes.","solutions":["Verify the mark_index used to locate the join actually corresponds to this join's output column (check metadata mark join index registration).","Confirm the rule's matchers only select MarkJoin S-expressions.","Replace `_ => unreachable!()` with `_ => return Ok((s_expr.clone(), false))` to skip ineligible joins defensively.","Add a debug assertion/log of join_type before the match to ease diagnosis."],"exampleFix":"// before\n_ => unreachable!(),\n// after\n_ => return Ok((s_expr.clone(), false)),","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Session-level guard: treat internal errors as planner bugs\nif err.message().contains(\"internal error: entered unreachable code\") {\n    retry_with_optimizer_disabled_or_simplified_query(err);\n}","preventionTips":["Avoid exotic scalar-subquery shapes that depend on mark-join rewrites while on affected versions.","Rewrite EXISTS/IN subqueries explicitly (e.g. as joins) if you hit planner panics.","Test MV/subquery-heavy workloads on staging before upgrading.","Include the full query text when reporting; rule ordering bugs are query-shape specific."],"tags":["rust","optimizer","join","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"}