pola-rs/polars · error

Expected Statistics to be

Error message

Expected Statistics to be {}, found {} instead

What it means

Generated accessor (e.g. expect_binary, expect_int) on the parquet Statistics enum panics when the actual statistics variant differs from the expected one. This is a caller-side type assertion; the message names the expected struct and the found variant.

Solutions

  1. Match on the Statistics enum and handle each variant explicitly
  2. Check the column's physical/logical type before calling expect_*
  3. Use the variant_str()/display API instead of asserting a fixed variant

Example fix

// before
let s = stats.expect_int().min;
// after
if let Statistics::Int(s) = &stats { use s.min } else { handle other variant }
Defensive patterns

Strategy: type-guard

Type guard

// Rust narrowing
fn as_int_stats(s: &Statistics) -> Option<&IntStatistics> {
    match s { Statistics::Int(i) => Some(i), _ => None }
}

Try / catch

std::panic::catch_unwind(|| stats.expect_int());

Prevention

When it happens

Trigger: Calling stats.expect_int() (or similar expect_* / owned expect_*) on Statistics holding e.g. BooleanStatistics instead.

Common situations: Code assuming uniform column types in a parquet file; reading metadata for a column whose logical type differs from what the handler expects; refactors that changed column types.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/6d0deac905426426. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-parquet/src/parquet/statistics/mod.rs:188

                }

                #[doc = concat!("Try to take [`Statistics`] as [`", stringify!($struct), "`]")]
                #[inline]
                pub fn $into_ident(self) -> Option<$struct> {
                    match self {
                        Self::$variant(s) => Some(s),
                        _ => None,
                    }
                }

                #[doc = concat!("Interpret [`Statistics`] to be [`", stringify!($struct), "`]")]
                ///
                /// Panics if it is not the correct variant.
                #[track_caller]
                #[inline]
                pub fn $expect_ident(&self) -> &$struct {
                    let Self::$variant(s) = self else {
                        panic!("Expected Statistics to be {}, found {} instead", stringify!($struct), self.variant_str());
                    };

                    s
                }

                #[doc = concat!("Interpret [`Statistics`] to be [`", stringify!($struct), "`]")]
                ///
                /// Panics if it is not the correct variant.
                #[track_caller]
                #[inline]
                pub fn $owned_expect_ident(self) -> $struct {
                    let Self::$variant(s) = self else {
                        panic!("Expected Statistics to be {}, found {} instead", stringify!($struct), self.variant_str());
                    };

                    s
                }
            )+

View on GitHub (pinned to fe841f959e)