Skip to content

impl(bigquery): expose query metadata on complete query handle#5925

Draft
alvarowolfx wants to merge 2 commits into
googleapis:mainfrom
alvarowolfx:impl-bq-query-md
Draft

impl(bigquery): expose query metadata on complete query handle#5925
alvarowolfx wants to merge 2 commits into
googleapis:mainfrom
alvarowolfx:impl-bq-query-md

Conversation

@alvarowolfx

Copy link
Copy Markdown
Contributor

Towards #5844

@product-auto-label product-auto-label Bot added the api: bigquery Issues related to the BigQuery API. label Jun 22, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request implements caching of rows, schema, page tokens, and query metadata in CompleteQuery, resolving a pending TODO, and refactors the Schema struct. The review feedback suggests several optimizations to avoid expensive clones of potentially large response data by utilizing std::mem::take and .as_ref(), as well as simplifying the Schema::new constructor to directly store the schema instead of manually reconstructing it.

Comment thread src/bigquery/src/query/query_handle.rs Outdated
Comment on lines 106 to 126
pub(crate) fn from_get_query_results_response(q: &Query, res: GetQueryResultsResponse) -> Self {
let schema = res
.schema
.clone()
.expect("complete query should have schema");
let schema = Arc::new(Schema::new(schema));
let page_token = if res.page_token.is_empty() {
None
} else {
Some(res.page_token.clone())
};
let cached_rows = VecDeque::from(res.rows.clone());
Self {
job_service: q.job_service.clone(),
job_ref: q.job_ref.clone(),
cached_rows,
page_token,
schema,
metadata: QueryMetadata::from(res),
}
}

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.

high

The res parameter is passed by value, meaning we own it. Instead of cloning res.rows (which can be very large and expensive to clone), we can move it into cached_rows using std::mem::take. By converting res to metadata first, we can also avoid cloning schema and page_token multiple times.

    pub(crate) fn from_get_query_results_response(q: &Query, mut res: GetQueryResultsResponse) -> Self {
        let cached_rows = VecDeque::from(std::mem::take(&mut res.rows));
        let metadata = QueryMetadata::from(res);
        let schema = metadata
            .schema
            .clone()
            .expect("complete query should have schema");
        let schema = Arc::new(Schema::new(schema));
        let page_token = if metadata.page_token.is_empty() {
            None
        } else {
            Some(metadata.page_token.clone())
        };
        Self {
            job_service: q.job_service.clone(),
            job_ref: q.job_ref.clone(),
            cached_rows,
            page_token,
            schema,
            metadata,
        }
    }
References
  1. Scrutinize expensive uses of clone(). Is it necessary to copy the data? Can we move the data instead? (link)

Comment thread src/bigquery/src/query/query_handle.rs Outdated
Comment on lines 128 to 148
pub(crate) fn from_query_response(q: &Query, res: &QueryResponse) -> Self {
let schema = res
.schema
.clone()
.expect("complete query should have schema");
let schema = Arc::new(Schema::new(schema));
let page_token = if res.page_token.is_empty() {
None
} else {
Some(res.page_token.clone())
};
let cached_rows = VecDeque::from(res.rows.clone());
Self {
job_service: q.job_service.clone(),
job_ref: q.job_ref.clone(),
cached_rows,
page_token,
schema,
metadata: QueryMetadata::from(res.clone()),
}
}

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.

high

Since res is a reference, we must clone it. However, by cloning res once up front, we can move res.rows into cached_rows using std::mem::take and move the rest of res into metadata. This avoids cloning the potentially large rows vector twice.

    pub(crate) fn from_query_response(q: &Query, res: &QueryResponse) -> Self {
        let mut res = res.clone();
        let cached_rows = VecDeque::from(std::mem::take(&mut res.rows));
        let metadata = QueryMetadata::from(res);
        let schema = metadata
            .schema
            .clone()
            .expect("complete query should have schema");
        let schema = Arc::new(Schema::new(schema));
        let page_token = if metadata.page_token.is_empty() {
            None
        } else {
            Some(metadata.page_token.clone())
        };
        Self {
            job_service: q.job_service.clone(),
            job_ref: q.job_ref.clone(),
            cached_rows,
            page_token,
            schema,
            metadata,
        }
    }
References
  1. Scrutinize expensive uses of clone(). Is it necessary to copy the data? Can we move the data instead? (link)

Comment thread src/bigquery/src/query/query_handle.rs Outdated

let completed = query.until_done().await?;
assert_eq!(completed.job_ref.unwrap().job_id, "some_job_id");
assert_eq!(completed.job_ref.clone().unwrap().job_id, "some_job_id");

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.

medium

Use .as_ref() instead of .clone() to inspect the Option without moving or cloning. This is more idiomatic and consistent with the other test at line 306.

Suggested change
assert_eq!(completed.job_ref.clone().unwrap().job_id, "some_job_id");
assert_eq!(completed.job_ref.as_ref().unwrap().job_id, "some_job_id");
References
  1. Scrutinize expensive uses of clone(). Is it necessary to copy the data? Can we move the data instead? (link)

Comment thread src/bigquery/src/query/schema.rs
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.69072% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.88%. Comparing base (75c4db2) to head (73c4e6c).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/bigquery/src/query/query_handle.rs 89.36% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5925      +/-   ##
==========================================
- Coverage   97.90%   97.88%   -0.02%     
==========================================
  Files         234      235       +1     
  Lines       59940    60019      +79     
==========================================
+ Hits        58683    58752      +69     
- Misses       1257     1267      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

api: bigquery Issues related to the BigQuery API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant