databendlabs/databend · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

The spatial index visitor, when entering a function call, expects the top-level expression to be one of a known set of spatial functions (e.g. ST_Distance, ST_Within variants). If the function call's name maps to none of the expected arms, `_ => unreachable!()` fires, producing internal error 1001 while planning a query against a spatial index.

Solutions

  1. Simplify the WHERE clause so only supported spatial functions (e.g. ST_Distance/ST_Within against indexed geometry columns) appear, putting other predicates in a separate condition
  2. Disable/bypass the spatial index for that query (e.g. drop the spatial index) to confirm the query itself runs
  3. Report the query and version as a bug: the default arm should fall back to visit_function_call rather than unreachable!()

Example fix

// before
_ => unreachable!(),
// after
_ => return Self::visit_function_call(call, self),
Defensive patterns

Strategy: try-catch

Validate before calling

-- keep spatial predicates to supported functions on indexed geometry columns
-- supported: ST_Distance(a, b) <= r, ST_Within(a, b)
-- put other function calls outside the indexed spatial predicate

Try / catch

try {
  runQuery(sql);
} catch (e) {
  if (e.code === 1001 && /spatial|st_/i.test(sql)) {
    // drop the spatial index or rewrite the predicate, then retry
  }
}

Prevention

When it happens

Trigger: Running a query whose WHERE clause contains a function call that the spatial-index rule visits but that is not one of the supported spatial predicates — i.e. the index rewrite rule is applied to a non-spatial function (optimizer scoping bug) or a new ST_* function was added without updating the visitor.

Common situations: Queries mixing spatial predicates with other functions on geometry columns; Databend version where a newly added spatial function isn't handled in spatial_predicate.rs.

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/532faaf68d91df54. Report an issue: GitHub.

Appendix: source

Thrown at src/query/storages/common/index/src/spatial_predicate.rs:210

            }
            "st_within" | "st_coveredby" => {
                if column_is_left {
                    SpatialPredicateOp::Within
                } else {
                    SpatialPredicateOp::Contains
                }
            }
            "st_intersects" => SpatialPredicateOp::Intersects,
            "st_dwithin" => {
                let Some(distance) = Self::extract_constant_arg(&args[2])
                    .and_then(|scalar| scalar.to_distance_threshold())
                else {
                    return Self::visit_function_call(call, self);
                };

                SpatialPredicateOp::Distance(distance)
            }
            _ => unreachable!(),
        };

        let Ok(column_id) = self.schema.column_id_of(&column) else {
            return Self::visit_function_call(call, self);
        };
        if let Some(spatial_index_columns) = self.spatial_index_columns {
            if !spatial_index_columns.contains(&column_id) {
                return Self::visit_function_call(call, self);
            }
        }

        let Some((query_rect, query_srid)) = Self::scalar_to_query(&constant) else {
            return Self::visit_function_call(call, self);
        };

        let placeholder = self.next_placeholder_name();
        self.predicates.push(SpatialPredicate {
            column_id,

View on GitHub (pinned to 288d84d76e)