query

package
v2.0.0-...-736b758 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package query provides a simplified API for running queries in BigQuery.

This package is EXPERIMENTAL and subject to change without notice.

The query client provides a simplified interface for running queries and retrieving results. It handles the complexities of job management and result pagination.

Example usage:

ctx := context.Background()
client, err := apiv2_client.NewClient(ctx)
if err != nil {
	// TODO: Handle error.
}
defer client.Close()

helper, err := query.NewHelper(client, "my-project")
if err != nil {
	// TODO: Handle error.
}

req := helper.FromSQL("SELECT 1 as foo")
q, err := helper.StartQuery(ctx, req)
if err != nil {
	// TODO: Handle error.
}

if err := q.Wait(ctx); err != nil {
	// TODO: Handle error.
}

it, err := q.Read(ctx)
if err != nil {
	// TODO: Handle error.
}
// TODO: iterate results.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Helper

type Helper struct {
	// contains filtered or unexported fields
}

Helper for running queries in BigQuery. It is a lightweight wrapper around the auto-generated BigQuery v2 client, focused on query operations.

func NewHelper

func NewHelper(c *apiv2_client.Client, projectID string, opts ...option.ClientOption) (*Helper, error)

NewHelper creates a new query helper. This helper should be reused instead of created per-request.

func (*Helper) AttachJob

func (h *Helper) AttachJob(ctx context.Context, jobRef *bigquerypb.JobReference, opts ...gax.CallOption) (*Query, error)

AttachJob attaches to an existing query job. The returned Query object can be used to monitor the job's status, wait for its completion, and retrieve its results.

func (*Helper) FromSQL

func (h *Helper) FromSQL(sql string) *bigquerypb.PostQueryRequest

FromSQL creates a query configuration from a SQL string.

func (*Helper) StartQuery

func (h *Helper) StartQuery(ctx context.Context, req *bigquerypb.PostQueryRequest, opts ...gax.CallOption) (*Query, error)

StartQuery executes a query using the stateless jobs.query RPC. It returns a handle to the running query. The returned Query object can be used to wait for completion and retrieve results.

func (*Helper) StartQueryJob

func (h *Helper) StartQueryJob(ctx context.Context, job *bigquerypb.Job, opts ...gax.CallOption) (*Query, error)

StartQueryJob from a bigquerypb.Job definition. Should have job.Configuration.Query filled out.

type Query

type Query struct {
	// contains filtered or unexported fields
}

Query represents a handle to a query job. Its methods can be used to wait for the job to complete and to iterate over the results.

func (*Query) Complete

func (q *Query) Complete() bool

Complete returns true if the query job has finished execution.

func (*Query) Done

func (q *Query) Done(opts ...gax.CallOption) <-chan struct{}

Done returns a channel that is closed when the query has completed. It can be used in a select statement to perform non-blocking waits.

Example:

	select {
	case <-q.Done():
		if err := q.Err(); err != nil {
			// Handle error.
		}
		// Query is complete.
 case <-time.After(30*time.Second):
	    // Timeout logic
	default:
		// Query is still running.
	}

func (*Query) Err

func (q *Query) Err() error

Err returns the final error state of the query. It is only valid to call Err after the channel returned by Done has been closed. If the query completed successfully, Err returns nil.

func (*Query) JobReference

func (q *Query) JobReference() *bigquerypb.JobReference

JobReference returns a reference to the query job. This will be nil until the query job has been successfully submitted.

func (*Query) QueryID

func (q *Query) QueryID() string

QueryID returns the auto-generated ID for the query. This is only populated for stateless queries (i.e. those started via jobs.query) after the query has been submitted.

func (*Query) Read

func (q *Query) Read(ctx context.Context, opts ...ReadOption) (*RowIterator, error)

Read returns a RowIterator for the query results.

func (*Query) Schema

func (q *Query) Schema() *bigquerypb.TableSchema

Schema returns the schema of the query results. This will be nil until the query has completed and the schema is available.

func (*Query) Wait

func (q *Query) Wait(ctx context.Context) error

Wait blocks until the query has completed. The provided context can be used to cancel the wait. If the query completes successfully, Wait returns nil. Otherwise, it returns the error that caused the query to fail.

Wait is a convenience wrapper around Done and Err.

type ReadOption

type ReadOption func(*readState)

ReadOption is an option for reading query results.

func WithPageToken

func WithPageToken(t string) ReadOption

WithPageToken sets the page token for reading query results.

type Row

type Row struct {
}

Row represents a single row in the query results.

type RowIterator

type RowIterator struct {
}

RowIterator is an iterator over the results of a query.

func (*RowIterator) Next

func (it *RowIterator) Next() (*Row, error)

Next returns the next row from the results.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL