Repository navigation
FEAT: Add arrow fetch support - #354
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
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 singlepyarrow.RecordBatch, base for the other two methods.arrow(chunk_size=10000): Fetches the entire result set as a singlepyarrow.Table.arrow_reader(chunk_size=10000): Returns apyarrow.RecordBatchReaderfor streaming results without loading the entire dataset into RAM.Using
fetch_arrow...instead of justarrow...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_wrapfunction, which produces PyCapsules that are then consumed byarrow_batchand 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:
Instead of looping over rows and columns and then switching on the data type for each cell, you could
Overall the arrow performance seems not too far off from what I achieved with zodbc.