Repository navigation
perf: build result rows without per-row Python calls - #1078
Conversation
Every fetch builds one Row per result row, and each one goes through Row.__call__, _create_row, Row.__new__ and Row.__setattr__. Add _create_rows, which builds the same Row objects directly, and use it in the Arrow (pandas and disable_pandas), Thrift columnar and SEA JSON conversions. A row with more values than fields still raises the same ValueError. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).
Verdict: 1 Low
Looks good, no findings. _create_rows produces the same objects as Row.__call__ → _create_row: same Row tuple, same shared __fields__ in __dict__, and the same ValueError for rows with too many values. All four call sites pass sequences, and the tests cover equivalence, pickling and each call site.
This overview is Claude Opus 5.5's initial take; GPT-6.1 Sol added findings it doesn't mention — see the findings below.
_convert_columnar_table collected every per-row value list before passing them to _create_rows, so all the lists and all the rows were alive at once. Pass a generator so each list is freed once its row is built, which keeps peak memory where it was before _create_rows. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
Iterating the object array from to_numpy() creates a numpy view per row. Convert 1024 rows at a time with tolist() instead, which returns the same value objects as plain lists and keeps peak memory where it was. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com> Co-authored-by: vuanhphung <313058935+vuanhphung@users.noreply.github.com> Co-authored-by: Isaac <no-reply@databricks.com>
Resolve the CHANGELOG conflict from the 4.10.0 release by starting a new Unreleased section with this change's entry. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).
Verdict: 1 Nit
Looks good, no blocking issues. I checked _create_rows against Row.__new__, __setattr__, __reduce__ and __call__: it builds the same rows, including __fields__, pickling and the ValueError for too many values. With an object dtype, tolist() returns the same value objects.
Other findings
- ⚪ Nit — The too-many-values branch calls
fields(*values)and discards the result, so it only works becauseRow.__call__raises whenlen(args) > len(self). The code is correct today, but the intent is implicit. Raise the ValueError explicitly, or add a comment saying the call is made only to raise. (Found by Claude Opus 5.5 · confirmed by the judge)
_create_rows called fields(*values) only so that Row.__call__ would raise. Build the ValueError in one helper that both Row.__call__ and _create_rows raise, so the message stays identical. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).
Verdict: 1 Nit
Looks good, no blocking issues. _create_rows builds the same rows as Row.__call__/_create_row (same tuple values, __fields__ stored in __dict__, same error), and tolist() on the object array returns the same value objects. All four call sites have tests.
Other findings
- ⚪ Nit — The PR description says a row with too many values "still goes through the factory", but
_create_rowsraises_too_many_values_erroritself and never calls the factory. The error message is the same, so only the description needs fixing. (Found by Claude Opus 5.5 · the judge disputed it but couldn't back that up, so it's kept)
What type of PR is this?
Description
Every
fetchone(),fetchmany()andfetchall()builds oneRowper result row by calling the row factory (ResultRow(*values)). Each call goes throughRow.__call__,_create_row,Row.__new__andRow.__setattr__: four Python calls per row, all made while holding the GIL.This adds
_create_rowsintypes.py, which builds the sameRowobjects directly: the values as aRowtuple, with__fields__stored whereRow.__setattr__stores it. A row with more values than fields raises the sameValueErroras the factory. The four places that build rows now use it:ResultSet._convert_arrow_table, both the pandas and_disable_pandasbranches (Thrift Arrow, SEA Arrow and kernel results)ThriftResultSet._convert_columnar_tableSeaResultSet._create_json_tableThe pandas branch also converts the object array to lists 1,024 rows at a time with
tolist()instead of iterating it row by row, which creates a numpy view per row.tolist()returns the same value objects, and chunking keeps peak memory unchanged.How values are produced is unchanged.
For an 18,314-row, 4-column result (TPC-H SF1 Q16), best of 41 runs over 4 interleaved rounds (pyarrow 26.0.1, pandas 3.1.1, Python 3.12):
_convert_arrow_table(pandas)_convert_arrow_table(_disable_pandas)On a staging Real-Time warehouse, building rows directly in
_convert_arrow_table(before thetolist()chunking) raisedfetchall()throughput 10.8% for the 22 TPC-H SF1 queries with 80 concurrent cursors in one process.How is this tested?
tests/unit/test_create_rows.pycompares_create_rowswith the row factory for tuples, lists, numpy object rows, generators, rows with fewer values than fields, and no rows. It checks the row type, values and value types,__fields__,repr,asDict, equality, attribute and key access,in, and the pickled bytes. It also checks the too-many-valuesValueErrormessage, that rows stay read-only, each of the four call sites, and pandas conversion across the 1,024-row chunk boundaries. The full unit suite passes with-m "not realkernel", andtests/unit/test_kernel_async_readers.py -m realkernelpasses with kernel 1.5.0.Related Tickets & Documents
N/A
This pull request and its description were written by Isaac.