Skip to content

FEAT: Add arrow fetch support - #354

Merged
gargsaumya merged 35 commits into
microsoft:mainfrom
ffelixg:arrow_fetch
Apr 2, 2026
Merged

gargsaumya merged 35 commits into
microsoft:mainfrom
ffelixg:arrow_fetch

Conversation

@ffelixg

@ffelixg ffelixg commented Nov 30, 2025

Copy link
Copy Markdown
Contributor

Work Item / Issue Reference

GitHub Issue: #130


Summary

Hey, you mentioned in issue #130 that you were willing to consider community contributions for adding Apache Arrow support, so here you go. I have focused only on fetching data into Arrow structures from the Database.

The Function signatures I chose are:

  • arrow_batch(chunk_size=10000): Fetch a single pyarrow.RecordBatch, base for the other two methods.
  • arrow(chunk_size=10000): Fetches the entire result set as a single pyarrow.Table.
  • arrow_reader(chunk_size=10000): Returns a pyarrow.RecordBatchReader for streaming results without loading the entire dataset into RAM.

Using fetch_arrow... instead of just arrow... could also be a good option, but I think the terse version is not too ambiguous.

Technical details

I am not very familiar with C++, but I did have some prior practice for this task from implementing my own ODBC driver in Zig (a very good language for projects like this!). The implementation is written almost entirely in C++ in the FetchArrowBatch_wrap function, which produces PyCapsules that are then consumed by arrow_batch and turned into actual arrow objects.

The function itself is very large. I'm sure it could be factored in a better way, even sharing some code with the other methods of fetching, but my goal was to keep the whole thing as straight forward as possible.

I have also implemented my own loop for SQLGetData for Lob-Columns. Unlike with the python fetch methods, I don't use the result directly, but instead copy it into the same buffer I would use for the case with bound columns. Maybe that's an abstraction that would make sense for that case as well.

Notes on data types

I noticed that you use SQL_C_TYPE_TIME for time(x) columns. The arrow fetch does the same, but I think it would be better to use SQL_C_SS_TIME2, since that supports fractional seconds.

Datetimeoffset is a bit tricky, since SQL Server stores timezone information alongside each cell, while arrow tables expect a fixed timezone for the entire column. I don't really see any solution other than converting everything to UTC and returning a UTC column, so that's what I did.

SQL_C_CHAR columns get copied directly into arrow utf8 arrays. Maybe some encoding options would be useful.

Performance

I think the main performance win to be gained is not interacting with any Python data structures in the hot path. That is satisfied. Further optimizations, which I did not make are:

  • Releasing the GIL for the entire fetch loop
  • Sharing the bound fetch buffer across repeated fetch calls
  • Improve the hot loop switching

Instead of looping over rows and columns and then switching on the data type for each cell, you could

  • Put the row loop inside each switch case (fastest I think, but would bloat the code a lot more)
  • Use function pointers like you recently did for python fetching (has overhead because of the indirect function call I think, also code is more scattered)
  • Replace both loops and the switch with computed gotos. That's what I opted for in my ODBC driver (the Zig equivalent is a labeled switch) and I am quite happy with how it came out. Performance seems very good and it allows you to abstract the fetching process on a row by row basis. I don't know how well that would translate to C++.

Overall the arrow performance seems not too far off from what I achieved with zodbc.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community PR or Issue raised by community members enhancement New feature or request inADO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants