databendlabs/databend · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

`unreachable!()` in `TransformWindow::advance_frame_start` (transform_window.rs): the frame-start bound is asserted to never be a `FrameBound::Following` at this point, because a FOLLOWING start would make the frame start 'after' the current row — the state machine processes start bounds only as Preceding/CurrentRow. Hitting it means a window frame with a FOLLOWING start bound reached the start-advancing code path.

Solutions

  1. Rewrite the query to use a valid frame: start bounds must be PRECEDING or CURRENT ROW (e.g. `ROWS BETWEEN UNBOUNDED PRECEDING AND 5 FOLLOWING`).
  2. Validate/reject FOLLOWING start bounds in the planner/binder so the query fails with a clear semantic error instead of a panic.
  3. Upgrade Databend if a newer version adds proper validation or support for such frames.
  4. Reproduce with the exact SQL and file an issue including the window spec.

Example fix

-- before (panics)
SELECT sum(x) OVER (ORDER BY ts ROWS BETWEEN 2 FOLLOWING AND 5 FOLLOWING) FROM t;
-- after
SELECT sum(x) OVER (ORDER BY ts ROWS BETWEEN 2 PRECEDING AND 5 FOLLOWING) FROM t;
Defensive patterns

Strategy: validation

Validate before calling

// validate the frame before running the window query
-- start bound must be PRECEDING or CURRENT ROW
SELECT ... OVER (ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND 5 FOLLOWING);

Prevention

When it happens

Trigger: Running a window query whose frame definition has a FOLLOWING start, e.g. `ROWS BETWEEN 2 FOLLOWING AND 5 FOLLOWING` or `RANGE BETWEEN UNBOUNDED FOLLOWING AND ...`, so `advance_frame_start` matches `FrameBound::Following(_)` and panics.

Common situations: Users writing non-standard frames like `BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING` that the planner passes through to the executor unvalidated; older clients/scripts generating frames programmatically.

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


AI-assisted analysis of databendlabs/databend@288d84d76e (2026-09-11). Data as JSON: /api/errors/615b4588218d3ca3. Report an issue: GitHub.

Appendix: source

Thrown at src/query/pipeline/transforms/src/processors/transforms/window/transform_window.rs:938

            FrameBound::Preceding(_) => {
                self.frame_started = true;
            }
            FrameBound::Following(Some(n)) => {
                debug_assert!(self.frame_unit.is_rows() || self.order_by.len() == 1);

                if self.is_null_frame {
                    self.frame_started = true;
                    self.frame_start = self.peer_group_start;
                } else if self.frame_unit.is_rows() {
                    self.advance_frame_start_rows_following(self.rows_start_bound);
                } else if self.order_by[0].is_nullable {
                    self.advance_frame_nullable_range::<FrameStart>(n.clone(), false)?;
                } else {
                    self.advance_frame_range::<FrameStart>(n.clone(), false)?;
                }
            }
            FrameBound::Following(_) => {
                unreachable!()
            }
        }
        Ok(())
    }

    fn advance_frame_end(&mut self) -> Result<()> {
        debug_assert!(!self.frame_ended);

        match &self.end_bound {
            FrameBound::CurrentRow => {
                self.advance_frame_end_current_row();
            }
            FrameBound::Preceding(Some(n)) => {
                debug_assert!(self.frame_unit.is_rows() || self.order_by.len() == 1);

                if self.is_null_frame {
                    self.advance_frame_end_current_row();
                } else if self.frame_unit.is_rows() {

View on GitHub (pinned to 288d84d76e)