{"record":{"id":"d3cb57767c421f43","repo":"pola-rs/polars","slug":"addition-overflow-self-other","errorCode":null,"errorMessage":"addition overflow: {self:?} + {other:?}","messagePattern":"addition overflow: (.+?) \\+ (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-utils/src/row_counter.rs","lineNumber":97,"sourceCode":"    pub fn add(self, other: Self) -> Self {\n        (|| {\n            let physical_rows = self.physical_rows.checked_add(other.physical_rows);\n            let deleted_rows = self.deleted_rows.checked_add(other.deleted_rows)?;\n\n            let physical_rows = if deleted_rows == 0 {\n                physical_rows.unwrap_or(usize::MAX)\n            } else {\n                // If there are row deletions we cannot saturate the position properly (the\n                // `num_rows()` will start to decrease).\n                physical_rows?\n            };\n\n            Some(Self {\n                physical_rows,\n                deleted_rows,\n            })\n        })()\n        .unwrap_or_else(|| panic!(\"addition overflow: {self:?} + {other:?}\"))\n    }\n\n    /// # Panics\n    /// Panics if subtraction overflows.\n    #[allow(clippy::should_implement_trait)]\n    pub fn sub(self, other: Self) -> Self {\n        let func = |a: usize, b: usize| {\n            a.checked_sub(b)\n                .unwrap_or_else(|| panic!(\"subtraction overflow: {self:?} - {other:?}\"))\n        };\n\n        Self {\n            physical_rows: func(self.physical_rows, other.physical_rows),\n            deleted_rows: func(self.deleted_rows, other.deleted_rows),\n        }\n    }\n\n    /// Returns the number of rows after applying deletions. This returns an","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-utils/src/row_counter.rs#L79-L115","documentation":"RowCounter::add panics with 'addition overflow: {self:?} + {other:?}' when combining two counters overflows (checked_add returns None). RowCounter tracks physical_rows and deleted_rows as usize, so very large counts or pathological subtraction-produced values can overflow usize.","triggerScenarios":"Calling add() (directly or via apply_to_df, resolve_negative_slice, or run) with counters whose row counts sum beyond usize::MAX, or adding to a counter previously built from negative-slice resolution edge cases.","commonSituations":"Resolution of negative slices in extreme sparse data; a usize::MAX-sized count produced by an earlier bug; 32-bit targets overflowing far sooner than 64-bit ones.","solutions":["Trace which code path produced the overflowing counter (the Debug output in the message shows both operands) and fix the upstream count","Avoid resolving slices/counters near usize::MAX; check intermediate values","Ensure subtraction (sub) results stay non-negative and sane before adding","On 32-bit platforms, reduce dataset chunking so row counts stay in range"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"def safe_add(a, b, limit):\n    if a + b > limit:\n        raise OverflowError(f\"addition overflow: {a} + {b}\")\n    return a + b","typeGuard":null,"tryCatchPattern":"import polars as pl\ntry:\n    out = df.slice(offset, length)  # path that resolves negative slices\nexcept pl.exceptions.PolarsPanicError as e:\n    if \"addition overflow\" in str(e):\n        out = handle_overflow(df)\n    else:\n        raise","preventionTips":["Keep intermediate row counts well below platform usize::MAX","Fix upstream sub() results that may be negative-corrupted","Recheck the code path on 32-bit targets, which overflow sooner","Report pathological inputs upstream with the Debug output from the panic message"],"tags":["rust","panic","integer-overflow","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}