Skip to content

perf: build result rows without per-row Python calls - #1078

Merged
aakash-saravanan-db merged 5 commits into
mainfrom
aakash-saravanan-db/perf/direct-row-construction
Nov 24, 2026
Merged

aakash-saravanan-db merged 5 commits into
mainfrom
aakash-saravanan-db/perf/direct-row-construction

Conversation

@aakash-saravanan-db

@aakash-saravanan-db aakash-saravanan-db commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • Refactor
  • Feature
  • Bug Fix
  • Other (performance)

Description

Every fetchone(), fetchmany() and fetchall() builds one Row per result row by calling the row factory (ResultRow(*values)). Each call goes through Row.__call__, _create_row, Row.__new__ and Row.__setattr__: four Python calls per row, all made while holding the GIL.

This adds _create_rows in types.py, which builds the same Row objects directly: the values as a Row tuple, with __fields__ stored where Row.__setattr__ stores it. A row with more values than fields raises the same ValueError as the factory. The four places that build rows now use it:

  • ResultSet._convert_arrow_table, both the pandas and _disable_pandas branches (Thrift Arrow, SEA Arrow and kernel results)
  • ThriftResultSet._convert_columnar_table
  • SeaResultSet._create_json_table

The 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):

main this PR
_convert_arrow_table (pandas) 37–38 ms 18–20 ms
_convert_arrow_table (_disable_pandas) 67–69 ms 58–62 ms

On a staging Real-Time warehouse, building rows directly in _convert_arrow_table (before the tolist() chunking) raised fetchall() throughput 10.8% for the 22 TPC-H SF1 queries with 80 concurrent cursors in one process.

How is this tested?

  • Unit tests
  • E2E Tests
  • Manually
  • N/A

tests/unit/test_create_rows.py compares _create_rows with 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-values ValueError message, 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", and tests/unit/test_kernel_async_readers.py -m realkernel passes with kernel 1.5.0.

Related Tickets & Documents

N/A

This pull request and its description were written by Isaac.

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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 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.

Comment thread src/databricks/sql/result_set.py Resolved
_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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

Comment thread src/databricks/sql/result_set.py Resolved
aakash-saravanan-db and others added 2 commits November 24, 2026 01:08
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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 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 because Row.__call__ raises when len(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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 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_rows raises _too_many_values_error itself 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)

@aakash-saravanan-db
aakash-saravanan-db added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit 75a0d5d Nov 24, 2026
52 checks passed
@aakash-saravanan-db
aakash-saravanan-db deleted the aakash-saravanan-db/perf/direct-row-construction branch November 24, 2026 23:14

This branch was previously deployed

1 inactive deployment
azure-prod — 5ab974da Deployed Nov 24, 2026 by aakash-saravanan-db via test-with-coverage #1378
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.

2 participants