-
Notifications
You must be signed in to change notification settings - Fork 137
docs(bigquery): improve Query handles docs #6212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alvarowolfx
wants to merge
3
commits into
googleapis:main
Choose a base branch
from
alvarowolfx:docs-bq-query-handles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,33 @@ use google_cloud_gax::polling_state::PollingState; | |
| use std::collections::VecDeque; | ||
| use std::sync::Arc; | ||
|
|
||
| /// A handle representing a running query. | ||
| /// A handle representing a running or completed SQL query execution. | ||
| /// | ||
| /// An instance of `Query` is returned by [`RunQuery::run()`](crate::query::RunQuery::run). | ||
| /// Depending on how the query was routed and executed, this handle may represent an asynchronous | ||
| /// background job currently executing on BigQuery, or a fast-path query that has already completed. | ||
| /// | ||
| /// To obtain the final result set, call [`until_done()`](Query::until_done), which will check the | ||
| /// execution status and automatically poll the service if the job is still running in the background. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let query_handle = client | ||
| /// .query("SELECT 42 AS answer") | ||
| /// .with_project_id("my-project-id") | ||
| /// .run() | ||
| /// .await?; | ||
| /// | ||
| /// // Poll until execution completes. | ||
| /// let completed = query_handle.until_done().await?; | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| #[derive(Clone, Debug)] | ||
| pub struct Query { | ||
| pub(crate) job_service: Arc<JobService>, | ||
|
|
@@ -39,13 +65,37 @@ pub struct Query { | |
| } | ||
|
|
||
| impl Query { | ||
| /// Returns the [`QueryReference`] for this query. | ||
| /// Returns the [`QueryReference`](crate::model::QueryReference) identifying this query execution. | ||
| /// | ||
| /// The reference will be [`QueryReference::Job`] with a query [job reference], | ||
| /// or [`QueryReference::Stateless`] with an opaque query ID if job creation | ||
| /// was skipped. | ||
| /// The reference will be [`QueryReference::Job`](crate::model::QueryReference::Job) containing a BigQuery Query [job reference] | ||
| /// if a job was created, or [`QueryReference::Stateless`](crate::model::QueryReference::Stateless) with an opaque | ||
| /// query ID if the execution ran statelessly via [jobs.query]. | ||
| /// | ||
| /// [job reference]: https://docs.cloud.google.com/bigquery/docs/reference/rest/v2/JobReference | ||
| /// [jobs.query]: https://docs.cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// use google_cloud_bigquery::model::QueryReference; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let query_handle = client | ||
| /// .query("SELECT 'hello' AS msg") | ||
| /// .with_project_id("my-project-id") | ||
| /// .run() | ||
| /// .await?; | ||
| /// | ||
| /// match query_handle.query_reference() { | ||
| /// QueryReference::Job(job_ref) => println!("Running job ID: {}", job_ref.job_id), | ||
| /// QueryReference::Stateless { query_id } => println!("Stateless query ID: {query_id}"), | ||
| /// _ => println!("Other query reference"), | ||
| /// } | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| pub fn query_reference(&self) -> QueryReference { | ||
| let from_query_id = self | ||
| .initial_response | ||
|
|
@@ -60,8 +110,34 @@ impl Query { | |
| .expect("query must have either a job reference or query id") | ||
| } | ||
|
|
||
| /// Periodically checks the status of the background job until it finishes. | ||
| /// Returns an error if a remote service or connection failure happens during polling. | ||
| /// Periodically polls the background job status until query execution finishes. | ||
| /// | ||
| /// If the query was executed via the fast path and already completed during the initial request, | ||
| /// this method immediately returns a [`CompleteQuery`](crate::query::CompleteQuery) without making additional network calls. | ||
| /// Otherwise, it implements a backoff loop querying the job status until it succeeds or fails. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if a remote service or network failure happens during polling, or if the BigQuery job | ||
| /// fails due to runtime execution errors. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let complete = client | ||
| /// .query("SELECT 1 + 1 AS result") | ||
| /// .with_project_id("my-project-id") | ||
| /// .run() | ||
| /// .await? | ||
| /// .until_done() | ||
| /// .await?; | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| pub async fn until_done(self) -> Result<CompleteQuery> { | ||
| let Query { | ||
| job_service, | ||
|
|
@@ -100,7 +176,32 @@ impl Query { | |
| } | ||
| } | ||
|
|
||
| /// A handle representing a successfully completed query ready for reading. | ||
| /// A handle representing a successfully completed query ready for reading results. | ||
| /// | ||
| /// An instance of `CompleteQuery` is returned by [`Query::until_done()`](crate::query::Query::until_done). | ||
| /// | ||
| /// This handle provides access to cached execution metadata, schema definitions, and a | ||
| /// row iterator via [`read()`](CompleteQuery::read). | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let complete = client | ||
| /// .query("SELECT 'done' AS status") | ||
| /// .with_project_id("my-project-id") | ||
| /// .run() | ||
| /// .await? | ||
| /// .until_done() | ||
| /// .await?; | ||
| /// | ||
| /// println!("Cache hit: {:?}", complete.metadata().cache_hit); | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| #[derive(Clone)] | ||
| pub struct CompleteQuery { | ||
| pub(crate) job_service: Arc<JobService>, | ||
|
|
@@ -178,19 +279,99 @@ impl CompleteQuery { | |
| } | ||
| } | ||
|
|
||
| /// Returns a row iterator for the query result. | ||
| /// Returns a row iterator for reading the query result set. | ||
| /// | ||
| /// Consumes the `CompleteQuery` handle and initializes a [`RowIterator`](crate::query::RowIterator) that | ||
| /// iterates over initial cached rows in memory before automatically fetching subsequent pages from the API. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let mut rows = client | ||
| /// .query("SELECT 100 AS score") | ||
| /// .with_project_id("my-project-id") | ||
| /// .run() | ||
| /// .await? | ||
| /// .until_done() | ||
| /// .await? | ||
| /// .read(); | ||
| /// | ||
| /// while let Some(row) = rows.next().await.transpose()? { | ||
| /// let score: i64 = row.get("score"); | ||
| /// println!("Score: {score}"); | ||
| /// } | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| pub fn read(self) -> RowIterator { | ||
| RowIterator::new(self) | ||
| } | ||
|
|
||
| /// Returns the cached metadata for this query. | ||
| /// Returns a reference to the cached summary metadata for this query. | ||
| /// | ||
| /// The returned [`QueryMetadata`](crate::model::QueryMetadata) contains useful summary statistics such as | ||
| /// total rows, schema details, cache hit indicators, and estimated bytes processed without making additional RPC calls. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let completed = client | ||
| /// .query("SELECT 'metadata_check'") | ||
| /// .with_project_id("my-project-id") | ||
| /// .run() | ||
| /// .await? | ||
| /// .until_done() | ||
| /// .await?; | ||
| /// | ||
| /// let meta = completed.metadata(); | ||
| /// println!("Total rows: {:?}", meta.total_rows); | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| pub fn metadata(&self) -> &QueryMetadata { | ||
| &self.metadata | ||
| } | ||
|
|
||
| /// Fetches the full `Job` information for the given query. | ||
| /// Fetches full [Job] execution metadata from the service for this query. | ||
| /// | ||
| /// Unlike [`metadata()`](CompleteQuery::metadata), this method executes an RPC request (`jobs.get`) to | ||
| /// retrieve complete job details, including user email, execution timelines, billing tier estimates, and detailed error summaries. | ||
| /// | ||
| /// > [!IMPORTANT] | ||
| /// > Calling this method on a stateless query will return [`QueryError::StatelessQuery`](crate::error::QueryError::StatelessQuery). | ||
| /// | ||
| /// [Job]: https://docs.cloud.google.com/bigquery/docs/reference/rest/v2/Job | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # async fn sample() -> anyhow::Result<()> { | ||
| /// use google_cloud_bigquery::client::BigQuery; | ||
| /// use google_cloud_bigquery::model::query_request::JobCreationMode; | ||
| /// | ||
| /// let client = BigQuery::builder().build().await?; | ||
| /// let completed = client | ||
| /// .query("SELECT 1") | ||
| /// .with_project_id("my-project-id") | ||
| /// .set_job_creation_mode(JobCreationMode::JobCreationRequired) // Forces a job to be created | ||
| /// .run() | ||
| /// .await? | ||
| /// .until_done() | ||
| /// .await?; | ||
| /// | ||
| /// Stateless queries will return `QueryError::StatelessQuery`. | ||
| /// let job_info = completed.job_metadata().await?; | ||
| /// println!("Executed by user: {}", job_info.user_email); | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| pub async fn job_metadata(&self) -> Result<Job> { | ||
| let job_ref = self.job_ref.as_ref().ok_or(QueryError::StatelessQuery)?; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
QueryReferencetype is imported fromcrate::query::QueryReference(defined insrc/bigquery/src/query/query_reference.rs), notcrate::model::QueryReference. Using the incorrect path will result in broken intra-doc links and rustdoc warnings, which can fail the CI build.References
cargo doc) before merging, because rustdoc warnings (such as bare URLs or unresolved links) will fail CI builds that enforce-D warnings.