{"record":{"id":"261911650dd7225d","repo":"diesel-rs/diesel","slug":"using-window-functions-in-where-clauses-is-not-sup","errorCode":null,"errorMessage":"Using window functions in WHERE clauses is not supported","messagePattern":"Using window functions in WHERE clauses is not supported","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel/src/query_builder/where_clause.rs","lineNumber":90,"sourceCode":"\nimpl<DB> From<NoWhereClause> for BoxedCloneWhereClause<'_, DB> {\n    fn from(_: NoWhereClause) -> Self {\n        BoxedCloneWhereClause::None\n    }\n}\n\n/// The `WHERE` clause of a query.\n#[derive(Debug, Clone, Copy)]\npub struct WhereClause<Expr>(Expr);\n\nimpl<Expr: diesel::query_builder::QueryId> diesel::query_builder::QueryId for WhereClause<Expr> {\n    type QueryId = WhereClause<<Expr as diesel::query_builder::QueryId>::QueryId>;\n    const HAS_STATIC_QUERY_ID: bool =\n        <Expr as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID && true;\n\n    const IS_WINDOW_FUNCTION: bool = const {\n        if Expr::IS_WINDOW_FUNCTION {\n            panic!(\"Using window functions in WHERE clauses is not supported\");\n        }\n        false\n    };\n}\n\nimpl<DB, Expr> QueryFragment<DB> for WhereClause<Expr>\nwhere\n    DB: Backend + DieselReserveSpecialization,\n    Expr: QueryFragment<DB>,\n{\n    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {\n        out.push_sql(\" WHERE \");\n        self.0.walk_ast(out.reborrow())?;\n        Ok(())\n    }\n}\n\nimpl<Expr, Predicate> WhereAnd<Predicate> for WhereClause<Expr>","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel/src/query_builder/where_clause.rs#L72-L108","documentation":"This is a compile-time panic enforced by diesel's QueryId trait impl for WhereClause. Window functions (OVER clauses) are only valid in SELECT/ORDER BY contexts; SQL forbids them in WHERE. Diesel evaluates `Expr::IS_WINDOW_FUNCTION` in a const block and fails compilation when a query tries to place a window function inside a WHERE clause.","triggerScenarios":"Building a query where an expression marked IS_WINDOW_FUNCTION (e.g. row_number().over(...) or any .over() call) is passed to .filter() / into a WHERE clause, e.g. `users.filter(row_number().over(...).eq(1))` — the const assertion fires at compile time.","commonSituations":"Developers trying to select the first row per group filter on row_number(), rank(), dense_rank(), or lag/lead results inside .filter(). SQL requires wrapping the window expression in a subquery/CTE and filtering on the derived column instead.","solutions":["Wrap the window function in a subquery or CTE (WITH ...) and apply .filter() on the aliased result column in the outer query.","Replace the window-function filter with an equivalent aggregate/GROUP BY + HAVING formulation when possible.","Use diesel's `select_with`/raw SQL escape hatch (sql_query) for databases whose dialect allows the pattern, only if truly needed.","Check diesel documentation on window functions for supported placement contexts (SELECT list, ORDER BY)."],"exampleFix":"// before\nusers.filter(row_number().over(order_by(users::id)).eq(1))\n\n// after\n// filter via subquery/CTE on the window result instead\nsql_query(\"SELECT * FROM (SELECT *, row_number() OVER (ORDER BY id) AS rn FROM users) t WHERE rn = 1\")","handlingStrategy":"validation","validationCode":"// Compile-time: this error IS the guard. Pattern-check before writing the query:\n// any Expr with IS_WINDOW_FUNCTION (e.g. x.over(...)) must never be passed to .filter().","typeGuard":"// Cannot be caught at runtime; enforce via code review:\n// assert no `.over(` expression appears inside `.filter(` in diesel query code.","tryCatchPattern":null,"preventionTips":["Treat .filter() as WHERE-only: never pass window functions (over()) into it.","Filter window results via subquery/CTE on the projected column.","Prefer GROUP BY + HAVING for per-group filtering needs."],"tags":["compile-time","sql","window-functions","diesel"],"backgroundTag":"unsupported-operation","analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}