query

package
v0.11.608 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QueryTypeMain    = "main"
	QueryTypeColumn  = "column"
	QueryTypeCustom  = "custom"
	QueryTypeSensor  = "sensor"
	QueryTypeQuery   = "query"
	QueryTypeDiff    = "diff"
	QueryTypeImport  = "import"
	QueryTypePatch   = "patch"
	QueryTypeDryRun  = "dryrun"
	QueryTypePing    = "ping"
	QueryTypeSchema  = "schema"
	QueryTypeEnhance = "enhance"
)

Query type labels used by backends (e.g. BigQuery) to tag jobs with a recognizable prefix so users can identify the source of a query in the platform's job/query history. Values are intentionally short and stable — they end up in user-visible identifiers.

Variables

View Source
var DefaultJinjaRenderer = jinja.NewRenderer(jinja.Context{
	"ds":                  time.Now().Format("2006-01-02"),
	"ds_nodash":           time.Now().Format("20060102"),
	"data_interval_start": time.Now().AddDate(0, 0, -1).Format(time.RFC3339),
	"data_interval_end":   time.Now().Format(time.RFC3339),
	"utils": map[string]interface{}{
		"date_add": func(str string, days int) string {
			return str
		},
		"date_format": func(str, inputFormat, outputFormat string) string {
			return str
		},
	},
})

Functions

func FirstSQLKeyword added in v0.11.582

func FirstSQLKeyword(sql string) string

func IsDMLStatement added in v0.11.582

func IsDMLStatement(statementType string) bool

func IsLikelyResultQuery added in v0.11.582

func IsLikelyResultQuery(sql string) bool

func IsNonResultStatement added in v0.11.582

func IsNonResultStatement(summary *QueryExecutionSummary) bool

func LogOrSinkQueryID added in v0.11.558

func LogOrSinkQueryID(ctx context.Context, dbType string, queryID string)

LogOrSinkQueryID either writes the query ID to the sink in the context (if one was set via WithQueryIDSink) or logs it to the console.

func LogQueryID added in v0.11.558

func LogQueryID(ctx context.Context, dbType string, queryID string)

LogQueryID prints a database query ID to the console writer in the context. It is a no-op if the context has no writer or the queryID is empty.

func QueryTypeFromContext added in v0.11.587

func QueryTypeFromContext(ctx context.Context) string

QueryTypeFromContext returns the query type label set via WithQueryType, or the empty string if none was set.

func SQLStatementType added in v0.11.582

func SQLStatementType(sql string) string

func StatementTypeFromCommandTag added in v0.11.582

func StatementTypeFromCommandTag(commandTag string) string

func StripLeadingSQLComments added in v0.11.582

func StripLeadingSQLComments(sql string) string

func WithQueryIDSink added in v0.11.558

func WithQueryIDSink(ctx context.Context, sink *string) context.Context

WithQueryIDSink returns a context that captures query IDs into the given string pointer instead of logging them. This allows callers (e.g. sensors) to control when and whether to log the ID.

func WithQueryType added in v0.11.587

func WithQueryType(ctx context.Context, queryType string) context.Context

WithQueryType returns a context labeled with the kind of work driving the query. Backends may read this label to tag their jobs (e.g. BigQuery uses it as a job ID prefix). Empty types are ignored.

Types

type DMLStatistics added in v0.11.582

type DMLStatistics struct {
	InsertedRowCount int64 `json:"insertedRowCount,omitempty"`
	DeletedRowCount  int64 `json:"deletedRowCount,omitempty"`
	UpdatedRowCount  int64 `json:"updatedRowCount,omitempty"`
}

type DryRunColumn added in v0.11.475

type DryRunColumn struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

DryRunColumn represents a single column in the output schema of a dry-run result.

type DryRunResult added in v0.11.475

type DryRunResult struct {
	// ConnectionType identifies the database backend (e.g., "bigquery", "snowflake", "postgres").
	ConnectionType string `json:"connectionType"`

	// Valid indicates whether the query passed validation without errors.
	Valid bool `json:"valid"`

	// BigQuery-specific fields
	TotalBytesProcessed int64          `json:"totalBytesProcessed,omitempty"`
	EstimatedCostUSD    float64        `json:"estimatedCostUSD,omitempty"`
	StatementType       string         `json:"statementType,omitempty"`
	ReferencedTables    []string       `json:"referencedTables,omitempty"`
	Schema              []DryRunColumn `json:"schema,omitempty"`

	// EXPLAIN-based databases populate this with the query plan output.
	ExplainRows *QueryResult `json:"explainRows,omitempty"`
}

DryRunResult holds the output of a dry-run operation. Different databases populate different fields depending on their capabilities.

type FileQuerySplitterExtractor

type FileQuerySplitterExtractor struct {
	Fs       afero.Fs
	Renderer jinja.RendererInterface
}

FileQuerySplitterExtractor is a regular file extractor, but it splits the queries in the given file into multiple instances. For usecases that require EXPLAIN statements, such as validating Snowflake queries, it is not possible to use a single query with multiple statements, so we need to split them into multiple queries.

func (FileQuerySplitterExtractor) CloneForAsset added in v0.11.230

func (FileQuerySplitterExtractor) ExtractQueriesFromString added in v0.7.1

func (f FileQuerySplitterExtractor) ExtractQueriesFromString(content string) ([]*Query, error)

func (FileQuerySplitterExtractor) ReextractQueriesFromSlice added in v0.11.230

func (f FileQuerySplitterExtractor) ReextractQueriesFromSlice(content []string) ([]string, error)

type Query

type Query struct {
	VariableDefinitions []string
	Query               string
	Args                []any
}

func (Query) String

func (q Query) String() string

func (Query) ToDryRunQuery

func (q Query) ToDryRunQuery() string

func (Query) ToExplainQuery

func (q Query) ToExplainQuery() string

type QueryDryRunner added in v0.11.475

type QueryDryRunner interface {
	DryRunQuery(ctx context.Context, q *Query) (*DryRunResult, error)
}

QueryDryRunner is an optional interface that database connections can implement to support dry-run / explain functionality for the "bruin query --dry-run" command.

type QueryExecutionSummary added in v0.11.582

type QueryExecutionSummary struct {
	ConnectionType        string         `json:"connectionType,omitempty"`
	JobID                 string         `json:"jobId,omitempty"`
	StatementType         string         `json:"statementType,omitempty"`
	TotalBytesProcessed   int64          `json:"totalBytesProcessed,omitempty"`
	TotalBytesBilled      int64          `json:"totalBytesBilled,omitempty"`
	SlotMillis            int64          `json:"slotMillis,omitempty"`
	DMLAffectedRows       *int64         `json:"dmlAffectedRows,omitempty"`
	DMLStats              *DMLStatistics `json:"dmlStats,omitempty"`
	DDLOperationPerformed string         `json:"ddlOperationPerformed,omitempty"`
	DDLTargetTable        string         `json:"ddlTargetTable,omitempty"`
	DDLTargetRoutine      string         `json:"ddlTargetRoutine,omitempty"`
}

QueryExecutionSummary holds metadata for a successfully executed statement. Databases populate this when a statement does not return a result set.

func NewExecutionSummaryFromStatement added in v0.11.582

func NewExecutionSummaryFromStatement(connectionType, statementType string, rowsAffected *int64) *QueryExecutionSummary

type QueryExtractor added in v0.11.182

type QueryExtractor interface {
	ExtractQueriesFromString(filepath string) ([]*Query, error)
	CloneForAsset(ctx context.Context, pipeline *pipeline.Pipeline, asset *pipeline.Asset) (QueryExtractor, error)
	ReextractQueriesFromSlice(content []string) ([]string, error)
}

type QueryResult added in v0.11.53

type QueryResult struct {
	Columns     []string
	Rows        [][]interface{}
	ColumnTypes []string
	Execution   *QueryExecutionSummary
}

type Renderer

type Renderer struct {
	Args map[string]string
}

func (Renderer) Render

func (r Renderer) Render(query string) string

type WholeFileExtractor

type WholeFileExtractor struct {
	Fs       afero.Fs
	Renderer jinja.RendererInterface
}

WholeFileExtractor is a regular file extractor that returns the whole file content as the query string. It is useful for cases where the whole file content can be treated as a single query, such as validating GoogleCloudPlatform queries via dry-run.

func (*WholeFileExtractor) CloneForAsset added in v0.11.182

func (*WholeFileExtractor) ExtractQueriesFromString added in v0.7.1

func (f *WholeFileExtractor) ExtractQueriesFromString(content string) ([]*Query, error)

func (*WholeFileExtractor) ReextractQueriesFromSlice added in v0.11.165

func (f *WholeFileExtractor) ReextractQueriesFromSlice(content []string) ([]string, error)

Jump to

Keyboard shortcuts

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