Skip to content

Fix wrong values and lost parallelism in the pandas, numpy and Arrow input scans - #700

Merged
evertlammerts merged 18 commits into
duckdb:v2.0-cyanopterafrom
evertlammerts:input-scan-fixes
Oct 20, 2026
Merged

evertlammerts merged 18 commits into
duckdb:v2.0-cyanopterafrom
evertlammerts:input-scan-fixes

Conversation

@evertlammerts

Copy link
Copy Markdown
Member

Input scan fixes for pandas, numpy and Arrow, found by comparing the 2.0 client with neo.

This PR makes duckdb read each pandas or numpy column as the array it holds at bind: its buffer, length, dtype and mask. Some fixes change behavior.

Currently:

  • a sliced nullable column moves its NULLs, and masked numpy elements read as values.
  • a registered dict whose array shrank is read past its end, and a non str key fails with std::bad_cast.
  • count(*) converts the first column, which can fail the query.
  • reading back a fetchnumpy result with NULL strings hangs.
  • a duckdb.Value in an object column invalidates the database.
  • pyarrow string columns fail on pandas 2, and on pandas 3 become Python objects at every bind.
  • a one chunk Arrow source scans on one thread.

With this PR, bind reads each column once and checks it against one row count: Column 'b' has 2 rows where 'a' has 5. Masked elements read NULL, count(*) reads no column, pyarrow strings are read as Arrow without the GIL, and the Arrow scan splits batches into 122880 row ranges, so one chunk uses every thread. Unordered results from such a source can now vary.

Benchmarks (instructions): suite geomean 0.69x, 0 slower; a 2M row str column 0.30x; count(*) over a str frame 0.01x. A 10 percent masked array costs 1.63x: the base ignored masks.

Notes:

  • input that misreports itself, like an ndarray subclass whose shape lies, is out of scope.
  • a duckdb.Value with a str type name can still hang against a bind on the default connection, as on the base (strict xfail).

Next PR:

  • exact conversion of object values outside the analyzer's sample. it needs declared types for con.append and Spark, so it waits for the relational API rework.

@evertlammerts
evertlammerts added this pull request to the merge queue Oct 20, 2026
@evertlammerts
evertlammerts removed this pull request from the merge queue due to a manual request Oct 20, 2026
@evertlammerts
evertlammerts merged commit f7826ad into duckdb:v2.0-cyanoptera Oct 20, 2026
30 checks passed
@evertlammerts
evertlammerts deleted the input-scan-fixes branch October 20, 2026 12:53
evertlammerts added a commit that referenced this pull request Oct 20, 2026
Fixes the cp311 Windows wheel job of the Release workflow, which fails
every run since #700 in
`test_a_dictionary_is_decoded_once_per_large_batch` before any DuckDB
code runs:

    pyarrow.lib.ArrowIndexError: Index -1074 out of bounds

The test builds its dictionary indices as `np.arange(2_000_000) *
1_000_000 // 2_000_000`. On Windows with numpy below 2, `np.arange`
defaults to int32 (the C `long`), so the product overflows and about
half the indices come out negative. The cp311 job is the only Windows
job that installs numpy below 2 (the tensorflow pin), which is why
neither the PR workflow nor the other wheel jobs saw it. An int32
simulation gives the same minimum of -1074.

The arange now carries `dtype=np.int64`. Same class as #696; the other
`np.arange` calls in the tests are small, float, or typed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant