Skip to content

improve(bq_driver): Accelerated Data Fetching and Post-Processing for Large Queries - #1673

Closed
Anshu6250 wants to merge 3 commits into
googleapis:mainfrom
Anshu6250:perf_improvement
Closed

Anshu6250 wants to merge 3 commits into
googleapis:mainfrom
Anshu6250:perf_improvement

Conversation

@Anshu6250

@Anshu6250 Anshu6250 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR significantly improves the data fetching and post-processing performance of the driver for both the JSON REST API (small datasets) and the Arrow HTAPI (large datasets). It addresses severe performance bottlenecks observed during SQLBindCol benchmarks on 1 Million+ row queries and optimizes memory allocations during JSON response parsing.

Changes

  • Optimized SQLBindCol Fetch Performance (odbc_sql_fetch.cc):
    • Extracted bound column descriptor information into a new GetBoundColumns helper function, allowing WriteRowset to pre-compute a cached BoundColInfo vector before iterating over rows.
    • Previously, the tight inner loop executed ard.HasDescriptorRecord() and ard.GetDescriptorRecord() (which are O(log N) std::map lookups) for every single cell. For a 1 Million row query with 50 columns, this eliminated over 100 million redundant map lookups, bringing SQLBindCol performance on par with SQLGetData.
    • Removed the now obsolete WriteDSRow function.
  • Optimized JSON REST Processing (odbc_internal_commons.cc):
    • Refactored ProcessResultSetRows to utilize std::move() when emplacing DSValue variants into the row vector, preventing millions of deep buffer copies.
    • Replaced by-value std::string copies with std::string const& inside the cell iteration loop.
    • Hoisted BQDataType schema lookups outside the row iteration loop.
    • Added vector::reserve() calls to pre-allocate vector memory based on schema and result set sizes.
  • Intelligent HTAPI Routing:
    • Replaced hardcoded max_results = 1 with a balanced threshold to correctly route small results (<100k rows) to the ultra-fast JSON REST parser without a page_token, while securely deferring massive datasets to Arrow HTAPI without triggering data truncation test failures.

benchmark_summary_linux.txt

@Anshu6250 Anshu6250 changed the title test:perf benchmark improvemen test: perf benchmark improvement Sep 28, 2026
@Anshu6250
Anshu6250 force-pushed the perf_improvement branch 4 times, most recently from 37ef3e8 to 3d2c0eb Compare September 29, 2026 07:29
// associated serialization and download overhead. (Note: max_results = 0
// is avoided because google-cloud-cpp's CreateKeysToFilterOut strips
// max_results from the JSON request if the value is <= 0).
query_request.set_max_results(1);

@sachinpro sachinpro Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please run TEST_P(HTAPIParameterizedTest, SQLExecDirect_with_pagination) with 1M rows and make sure all the values are returned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. I ran the test with 1M rows and our driver successfully fetched and validated all values for both standard and HTAPI runs.

Note: I had to remove the ORDER BY index clause to bypass a BigQuery memory limit error, and I updated the validation loop to extract row IDs dynamically. Notably, at this scale, the existing driver fails on the HTAPI run with a Stream removed error, but our driver handles it perfectly.

@Anshu6250 Anshu6250 changed the title test: perf benchmark improvement improve(bq_driver): Accelerated Data Fetching and Post-Processing for Large Queries Oct 4, 2026
bool bool_val = false;
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
if (data == "1" || data == "true" || data == "yes") {
std::string lower_data = data;

@shivamd-gpartner shivamd-gpartner Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why this change?, we are simply copying the lower_data = data and rest all is the same, isn't the output of bool same as the existing driver?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The outer loop now uses a const std::string& to eliminate deep string copies for every cell. Because data is now immutable, we must make a local mutable copy (lower_data) specifically for booleans to use std::transform. This small tradeoff allows us to skip string copies entirely for all other data types.

std::vector<BoundColInfo> bound_cols;
bound_cols.reserve(result_set.row_schema.size());

SQLINTEGER bind_type = ard.GetHeaderRecord().bind_type;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you please make this logic a function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@Anshu6250
Anshu6250 marked this pull request as ready for review October 5, 2026 09:56
@Anshu6250
Anshu6250 requested a review from a team as a code owner October 5, 2026 09:56
@sachinpro

Copy link
Copy Markdown
Collaborator

Please rebase this with main and run the linux pipeline once.

// (or 10MB), returning a page_token to trigger HTAPI. This prevents
// BigQuery from spending 15+ seconds formatting massive JSON payloads,
// slashing TTFB.
query_request.set_max_results(100000);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unless there is a clear justification wrt performance, we shouldn't hardcode this. I don't see us setting set_max_results = 1 on the driver before this. The page sizes depend on the number of columns. We shouldn't optimize based on he current behaviour of BQ services since the backend may change.

@sachinpro

Copy link
Copy Markdown
Collaborator

@Anshu6250 Please split this PR for SQLBindCol optimization and JSON Post processing optimization, if any of those are relevant now. In the future raise PRs focused on a single optimization so we can understand the impact of these changes.

@Anshu6250

Copy link
Copy Markdown
Contributor Author

Closing this PR as it is no longer relevant now. The updated optimizations and benchmarks have been addressed in #1714 (#1714). Thanks @sachinpro!

@Anshu6250 Anshu6250 closed this Nov 25, 2026
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.

3 participants