databendlabs/databend · error

not implemented

Error message

not implemented

What it means

Panic from `unimplemented!()` in `Sort::replace_column` (src/query/sql/src/planner/plans/sort.rs:70). When rewriting column references in a sort plan, the case where the sort has a window partition is not handled, so the optimizer pass panics with 'not implemented' instead of remapping those columns.

Solutions

  1. Avoid triggering the rewrite path for plans containing window partitions until supported (or gate the rewrite upstream).
  2. Extend `replace_column` to iterate and remap `window_partition` expressions with the same closure used for `items`.
  3. File/track a Databend issue; replace `unimplemented!()` with a proper internal error for a non-panicking failure.
  4. Reproduce with a minimal ORDER BY + OVER (PARTITION BY) query and include it in the bug report.

Example fix

// before
if self.window_partition.is_some() {
    unimplemented!()
};
// after
if let Some(partition) = self.window_partition.as_mut() {
    for expr in &mut partition.expressions {
        *expr = replace_column_in_expr(expr, &mut replace)?;
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// guard before applying rewrite
fn sort_has_window_partition(sort: &Sort) -> bool { sort.window_partition.is_some() }

Type guard

fn can_rewrite_sort(sort: &Sort) -> bool {
    sort.window_partition.is_none()
}

Try / catch

match sort.replace_column(old, new) {
    Ok(()) => {},
    Err(e) => return Err(e),
    // a panic! here means window_partition is Some; catch_unwind or pre-check
}

Prevention

When it happens

Trigger: An optimizer rewrite calls `replace_column` on a `Sort` plan whose `window_partition` is `Some`, e.g. when substituting column indices in a query containing window functions (OVER PARTITION BY) under a sort node.

Common situations: Queries mixing ORDER BY with window functions during plan normalization/scalar substitution; development-time hits when extending expression rewrites to window plans.

Related errors


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

Appendix: source

Thrown at src/query/sql/src/planner/plans/sort.rs:70

    }

    pub fn replace_column(&mut self, old: Symbol, new: Symbol) {
        for item in &mut self.items {
            if item.index == old {
                item.index = new
            }
        }

        if let Some(projection) = &mut self.pre_projection {
            for i in projection {
                if *i == old {
                    *i = new
                }
            }
        }

        if self.window_partition.is_some() {
            unimplemented!()
        };
    }

    pub fn replace_columns<F>(&mut self, mut replace: F) -> Result<()>
    where F: FnMut(Symbol) -> Result<Symbol> {
        for item in &mut self.items {
            item.index = replace(item.index)?;
        }

        if let Some(projection) = &mut self.pre_projection {
            for index in projection {
                *index = replace(*index)?;
            }
        }

        if self.window_partition.is_some() {
            unimplemented!()
        };

View on GitHub (pinned to 288d84d76e)