{"record":{"id":"660bb8e9f0678246","repo":"risingwavelabs/risingwave","slug":"division-by-zero","errorCode":null,"errorMessage":"Division by zero","messagePattern":"Division by zero","errorType":"exception","errorClass":"ExprError","httpStatus":null,"severity":"error","filePath":"src/expr/core/src/error.rs","lineNumber":67,"sourceCode":"    #[error(\"Unsupported function: {0}\")]\n    UnsupportedFunction(String),\n\n    #[error(\"Unsupported cast: {0} to {1}\")]\n    UnsupportedCast(DataType, DataType),\n\n    #[error(\"Casting to {0} out of range\")]\n    CastOutOfRange(&'static str),\n\n    #[error(\"Numeric out of range\")]\n    NumericOutOfRange,\n\n    #[error(\"Numeric out of range: underflow\")]\n    NumericUnderflow,\n\n    #[error(\"Numeric out of range: overflow\")]\n    NumericOverflow,\n\n    #[error(\"Division by zero\")]\n    DivisionByZero,\n\n    #[error(\"Parse error: {0}\")]\n    // TODO(error-handling): should prefer use error types than strings.\n    Parse(Box<str>),\n\n    #[error(\"Invalid parameter {name}: {reason}\")]\n    // TODO(error-handling): should prefer use error types than strings.\n    InvalidParam {\n        name: &'static str,\n        reason: Box<str>,\n    },\n\n    #[error(\"Array error: {0}\")]\n    Array(\n        #[from]\n        #[backtrace]\n        ArrayError,","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/risingwavelabs/risingwave/blob/6469eb736d691e8e9b8a419a57edd6429ca77417/src/expr/core/src/error.rs#L49-L85","documentation":"ExprError::DivisionByZero is thrown when an expression evaluation attempts to divide a numeric value by zero (or by a value that is effectively zero, e.g. numeric 0). The expression core detects the zero divisor at evaluation time and returns this error instead of panicking or producing an undefined result. It matches Postgres semantics where division by zero raises an error rather than returning NULL.","triggerScenarios":"Evaluating a `/` expression (integer, decimal/numeric, or floating point depending on type semantics) where the right-hand operand evaluates to 0; also via batch/stream expression execution of user SQL like `SELECT 1/0` or `sum(x)/count(x)` when count is 0.","commonSituations":"Aggregate-derived denominators that can be zero (ratio of counts), user-written queries with literal 0 denominators, upstream data changes that make previously non-zero columns zero, percentile/average computations over empty windows.","solutions":["Add an explicit zero check in SQL: `CASE WHEN denom = 0 THEN NULL ELSE num / denom END`.","Use `NULLIF(denom, 0)` as the denominator: `num / NULLIF(denom, 0)` so the result becomes NULL instead of erroring.","If this happens inside a streaming pipeline, guard the denominator expression upstream in the materialized view definition."],"exampleFix":"// before\nCREATE MATERIALIZED VIEW mv AS SELECT clicks / impressions AS ctr FROM stats;\n// after\nCREATE MATERIALIZED VIEW mv AS SELECT clicks / NULLIF(impressions, 0) AS ctr FROM stats;","handlingStrategy":"validation","validationCode":"// SQL-level guard before division\nSELECT num / NULLIF(denom, 0) AS ratio FROM t;","typeGuard":null,"tryCatchPattern":"// Rust caller\nmatch result {\n    Err(e) if matches!(e.downcast_ref::<ExprError>(), Some(ExprError::DivisionByZero)) => Some(f64::NAN), // or None\n    other => other.ok(),\n}","preventionTips":["Always use NULLIF(x, 0) for denominators that derive from data or aggregates.","Never write literal `1/0` in queries even as placeholders.","Unit-test ratio expressions with zero-count windows/empty groups."],"tags":["rust","arithmetic","streaming"],"backgroundTag":"argument-out-of-range","analyzedSha":"6469eb736d691e8e9b8a419a57edd6429ca77417","analyzedAt":"2026-09-11T21:06:21.487Z","contentChangedAt":"2026-09-11T21:06:21.487Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}