Skip to content

arrow-cast numeric parsers fail to parse whitespace-padded strings #9538

Closed

Description

@aryan-212

Describe the bug

The Parser::parse implementations for numeric types fail to parse strings that contain leading or trailing whitespace.

In practice, this happens quite often when reading data from CSVs or other text-based sources where values may be padded with spaces, tabs, or newline characters. Instead of parsing successfully, these inputs currently return None.

To Reproduce

use arrow_array::types::*;
use arrow_cast::parse::Parser;

// they return None instead of the parsed number
assert_eq!(Float32Type::parse(" 1.5 "), None);   // expected Some(1.5)
assert_eq!(Int32Type::parse(" 42 "), None);      // expected Some(42)
assert_eq!(Int64Type::parse("\t100\n"), None);   // expected Some(100)
assert_eq!(UInt64Type::parse(" 7 "), None);      // expected Some(7)

Expected behavior

Numeric parsers should ignore leading and trailing whitespace before parsing. For example, " 42 " should parse successfully to Some(42) rather than returning None.

This behavior is consistent with how most data ingestion systems handle text-to-number conversion.

Additional context

The issue originates in arrow-cast/src/parse.rs. The float parsers pass string.as_bytes() directly to lexical_core::parse, and the parser_primitive! macro (used for integers and durations) similarly operates on the input without trimming.

A simple fix would be to call .trim() on the input string before attempting to parse.

Activity

  1. added
    bug
    on Mar 11, 2026
  2. added
    enhancement
    and removed
    bug
    on Mar 13, 2026
  3. alamb commented on Mar 19, 2026

    @alamb
    Contributor

    Copying some comments from #9537

    I guess question comes down to expectations

    Numeric parsers should ignore leading and trailing whitespace before parsing. For example, " 42 " should parse successfully to Some(42) rather than returning None.

    This behavior is consistent with how most data ingestion systems handle text-to-number conversion.

    I did some brief checking of postgres and duckdb and they indeed do trim spaces

    Interestingly DuckDB does automatically trim

    andrewlamb@Andrews-MacBook-Pro-3:~/Software/influxdb_pro/ent$ duckdb
    DuckDB v1.5.0 (Variegata)
    Enter ".help" for usage hints.
    memory D select '  4.5'::float;
    ┌────────────────────────┐
    │ CAST('  4.5' AS FLOAT) │
    │         float          │
    ├────────────────────────┤
    │                    4.5 │
    └────────────────────────┘

    As does postgres 🤔

    andrewlamb@Andrews-MacBook-Pro-3:~/Software/influxdb_pro/ent$ psql -h localhost -U postgres
    psql (14.22 (Homebrew), server 11.16 (Debian 11.16-1.pgdg90+1))
    Type "help" for help.
    
    postgres=#  select '  4.5'::float;
     float8
    --------
        4.5
    (1 row)
  4. Rich-T-kid commented on Jul 16, 2026

    @Rich-T-kid
    Contributor

    take

  5. added a commit that references this issue on Jul 26, 2026
    cd47d4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions