QueryBuilder

class lsst.daf.butler.registry.queries.QueryBuilder(summary: QuerySummary, backend: QueryBackend, context: QueryContext | None = None, relation: Relation | None = None)

Bases: object

A builder for potentially complex queries that join tables based on dimension relationships.

Parameters:
summaryQuerySummary

Struct organizing the dimensions involved in the query.

backendQueryBackend

Backend object that represents the Registry implementation.

contextQueryContext, optional

Object that manages relation engines and database-side state (e.g. temporary tables) for the query. Must have been created by backend.context(), which is used if context is not provided.

relationRelation, optional

Initial relation for the query.

Methods Summary

finish([joinMissing])

Finish query constructing, returning a new Query instance.

joinDataset(datasetType, collections, *[, ...])

Add a dataset search or constraint to the query.

Methods Documentation

finish(joinMissing: bool = True) Query

Finish query constructing, returning a new Query instance.

Parameters:
joinMissingbool, optional

If True (default), automatically join any missing dimension element tables (according to the categorization of the QuerySummary the builder was constructed with). False should only be passed if the caller can independently guarantee that all dimension relationships are already captured in non-dimension tables that have been manually included in the query.

Returns:
queryQuery

A Query object that can be executed and used to interpret result rows.

joinDataset(datasetType: DatasetType, collections: Any, *, isResult: bool = True, findFirst: bool = False) bool

Add a dataset search or constraint to the query.

Unlike other QueryBuilder join methods, this must be called directly to search for datasets of a particular type or constrain the query results based on the exists of datasets. However, all dimensions used to identify the dataset type must have already been included in QuerySummary.requested when initializing the QueryBuilder.

Parameters:
datasetTypeDatasetType

The type of datasets to search for.

collectionsAny

An expression that fully or partially identifies the collections to search for datasets, such as a str, re.Pattern, or iterable thereof. can be used to return all collections. See Collection expressions for more information.

isResultbool, optional

If True (default), include the dataset ID column in the result columns of the query, allowing complete DatasetRef instances to be produced from the query results for this dataset type. If False, the existence of datasets of this type is used only to constrain the data IDs returned by the query. joinDataset may be called with isResult=True at most one time on a particular QueryBuilder instance.

findFirstbool, optional

If True (False is default), only include the first match for each data ID, searching the given collections in order. Requires that all entries in collections be regular strings, so there is a clear search order. Ignored if isResult is False.

Returns:
anyRecordsbool

If True, joining the dataset table was successful and the query should proceed. If False, we were able to determine (from the combination of datasetType and collections) that there would be no results joined in from this dataset, and hence (due to the inner join that would normally be present), the full query will return no results.