Query¶
- final class lsst.daf.butler.queries.Query(driver: QueryDriver, tree: QueryTree | None = None)¶
- Bases: - QueryBase- A method-chaining builder for butler queries. - Parameters:
- driverQueryDriver
- Implementation object that knows how to actually execute queries. 
- treeQueryTree, optional
- Description of the query as a tree of joins and column expressions. Defaults to the result of a call to - make_identity_query_tree.
 
- driver
 - Notes - Queryobjects should never be constructed directly by users; use- Butler.queryinstead.- A - Queryobject represents the first stage of query construction, in which constraints and joins are defined (roughly corresponding to the WHERE and FROM clauses in SQL). The various “results” objects represent the second (and final) stage, where the columns returned are specified and any sorting or integer slicing can be applied. Result objects are obtained from the- data_ids,- datasets, and- dimension_recordsmethods.- Queryand query-result objects are always immutable (except for caching information fetched from the database or server), so modifier methods always return a new object without modifying the current one.- Attributes Summary - The names of all dataset types joined into the query. - Dimensions currently present in the query, either directly or indirectly. - A factory for column expressions using overloaded operators. - Methods Summary - any(*[, execute, exact])- Test whether the query would return any rows. - data_ids([dimensions])- Return a result object that is a - DataCoordinateiterable.- datasets(dataset_type[, collections, find_first])- Return a result object that is a - DatasetRefiterable.- dimension_records(element)- Return a result object that is a - DimensionRecorditerable.- explain_no_results([execute])- Return human-readable messages that may help explain why the query yields no results. - general(dimensions, *names[, ...])- Execute query returning general result. - join_data_coordinates(iterable)- Return a new query that joins in an explicit table of data IDs. - join_dataset_search(dataset_type[, collections])- Return a new query with a search for a dataset joined in. - join_dimensions(dimensions)- Return a new query that joins the logical tables for additional dimensions. - materialize(*[, dimensions, datasets])- Execute the query, save its results to a temporary location, and return a new query that represents fetching or joining against those saved results. - where(*args[, bind])- Return a query with a boolean-expression filter on its rows. - Attributes Documentation - constraint_dataset_types¶
- The names of all dataset types joined into the query. - The existence of datasets of these types constrains the data IDs of any type of result. Fields for these dataset types are also usable in ‘where’ expressions. 
 - constraint_dimensions¶
- Dimensions currently present in the query, either directly or indirectly. - This includes dimensions that are present in any joined subquery (such as a dataset search, materialization, or data ID upload) or - whereargument, as well as any required or implied dependency of those dimensions.
 - expression_factory¶
- A factory for column expressions using overloaded operators. ( - ExpressionFactory).- Notes - Typically this attribute will be assigned to a single-character local variable, and then its (dynamic) attributes can be used to obtain references to columns that can be included in a query: - with butler.query() as query: x = query.expression_factory query = query.where( x.instrument == "LSSTCam", x.visit.day_obs > 20240701, x.any(x.band == "u", x.band == "y"), ) - As shown above, the returned object also has an - anymethod to create combine expressions with logical OR (as well as- not_and- all, though the latter is rarely necessary since- wherealready combines its arguments with AND).- Proxies for fields associated with individual datasets but not dimension records ( - dataset_id,- ingest_date,- run,- collection, as well as- timespanfor- CALIBRATIONcollection searches) can be obtained with dict-like access instead:- with butler.query() as query: query = query.order_by(x["raw"].ingest_date) - Expression proxy objects that correspond to scalar columns overload the standard comparison operators ( - ==,- !=,- <,- >,- <=,- >=) and provide- in_range,- in_iterable, and- in_querymethods for membership tests. For- order_bycontexts, they also have a- descproperty to indicate that the sort order for that expression should be reversed.- Proxy objects for - regionand- timespanfields have an- overlapsmethod, and timespans also have- beginand- endproperties to access scalar expression proxies for the bounds.- All proxy objects also have an - is_nullproperty.- Literal values can be created by calling - ExpressionFactory.literal, but can almost always be created implicitly via overloaded operators instead.
 - Methods Documentation - any(*, execute: bool = True, exact: bool = True) bool¶
- Test whether the query would return any rows. - Parameters:
- executebool, optional
- If - True, execute at least a- LIMIT 1query if it cannot be determined prior to execution that the query would return no rows.
- exactbool, optional
- If - True, run the full query and perform post-query filtering if needed, until at least one result row is found. If- False, the returned result does not account for post-query filtering, and hence may be- Trueeven when all result rows would be filtered out.
 
- execute
- Returns:
 
 - data_ids(dimensions: DimensionGroup | Iterable[str] | str | None = None) DataCoordinateQueryResults¶
- Return a result object that is a - DataCoordinateiterable.- Parameters:
- dimensionsDimensionGroup,str, orIterable[str], optional
- The dimensions of the data IDs to yield, as either - DimensionGroupinstances or- strnames. Will be automatically expanded to a complete- DimensionGroup. These dimensions do not need to match the query’s current dimensions. Default is- constraint_dimensions.
 
- dimensions
- Returns:
- data_idsDataCoordinateQueryResults
- Data IDs matching the given query parameters. These are guaranteed to identify all dimensions ( - DataCoordinate.hasFullreturns- True), but will not contain- DimensionRecordobjects (- DataCoordinate.hasRecordsreturns- False). Call- with_dimension_recordson the returned object to include dimension records as well.
 
- data_ids
 
 - datasets(dataset_type: str | DatasetType, collections: str | Iterable[str] | None = None, *, find_first: bool = True) DatasetRefQueryResults¶
- Return a result object that is a - DatasetRefiterable.- Parameters:
- dataset_typestrorDatasetType
- The dataset type to search for. 
- collectionsstrorIterable[str], optional
- The collection or collections to search, in order. If not provided or - None, and the dataset has not already been joined into the query, the default collection search path for this butler is used.
- find_firstbool, optional
- If - True(default), for each result data ID, only yield one- DatasetRefof each- DatasetType, from the first collection in which a dataset of that dataset type appears (according to the order of- collectionspassed in). If- True,- collectionsmust not be- ....
 
- dataset_type
- Returns:
- refslsst.daf.butler.queries.DatasetRefQueryResults
- Dataset references matching the given query criteria. Nested data IDs are guaranteed to include values for all implied dimensions (i.e. - DataCoordinate.hasFullwill return- True), but will not include dimension records (- DataCoordinate.hasRecordswill be- False) unless- with_dimension_recordsis called on the result object (which returns a new one).
 
- refs
- Raises:
- lsst.daf.butler.registry.DatasetTypeExpressionError
- Raised when the - dataset_typeexpression is invalid.
- lsst.daf.butler.registry.NoDefaultCollectionError
- Raised when - collectionsis- Noneand default butler collections are not defined.
- TypeError
- Raised when the arguments are incompatible, such as when a collection wildcard is passed when - find_firstis- True
 
 
 - dimension_records(element: str) DimensionRecordQueryResults¶
- Return a result object that is a - DimensionRecorditerable.- Parameters:
- elementstr
- The name of a dimension element to obtain records for. 
 
- element
- Returns:
- recordslsst.daf.butler.queries.DimensionRecordQueryResults
- Data IDs matching the given query parameters. 
 
- records
 
 - explain_no_results(execute: bool = True) Iterable[str]¶
- Return human-readable messages that may help explain why the query yields no results. 
 - general(dimensions: DimensionGroup | Iterable[str], *names: str, dimension_fields: Mapping[str, Set[str]] | None = None, dataset_fields: Mapping[str, Set[Literal['dataset_id', 'ingest_date', 'run', 'collection', 'timespan']] | ellipsis] | None = None, find_first: bool | None = None) GeneralQueryResults¶
- Execute query returning general result. - This is an experimental interface and may change at any time. - Parameters:
- dimensionsDimensionGrouporIterable[str]
- The dimensions that span all fields returned by this query. 
- *namesstr
- Names of dimensions fields (in “dimension.field” format), dataset fields (in “dataset_type.field” format) to include in this query. 
- dimension_fieldsMapping[str,Set[str]], optional
- Dimension record fields included in this query, keyed by dimension element name. 
- dataset_fieldsMapping[str,Set|...], optional
- Dataset fields included in this query, the key in the mapping is dataset type name. Ellipsis ( - ...) can be used for value to include all dataset fields needed to extract- DatasetRefinstances later.
- find_firstbool, optional
- Whether this query requires find-first resolution for a dataset. This is ignored and can be omitted if the query has no dataset fields. It must be explicitly set to - Falseif there are multiple dataset types with fields, or if any dataset’s- collectionsor- timespanfields are included in the results.
 
- dimensions
- Returns:
- resultGeneralQueryResults
- Query result that can be iterated over. 
 
- result
 - Notes - The dimensions of the returned query are automatically expanded to include those associated with all dimension and dataset fields; the - dimensionsargument is just the minimal dimensions to return.
 - join_data_coordinates(iterable: Iterable[DataCoordinate]) Query¶
- Return a new query that joins in an explicit table of data IDs. - Parameters:
- iterableIterable[DataCoordinate]
- Iterable of - DataCoordinate. All items must have the same dimensions. Must have at least one item.
 
- iterable
- Returns:
- queryQuery
- A new query object with the data IDs joined in. 
 
- query
 
 - join_dataset_search(dataset_type: str | DatasetType, collections: Iterable[str] | None = None) Query¶
- Return a new query with a search for a dataset joined in. - Parameters:
- dataset_typestrorDatasetType
- Dataset type or name. May not refer to a dataset component. 
- collectionsIterable[str], optional
- Iterable of collections to search. Order is preserved, but will not matter if the dataset search is only used as a constraint on dimensions or if - find_first=Falsewhen requesting results. If not present or- None, the default collection search path will be used.
 
- dataset_type
- Returns:
- queryQuery
- A new query object with dataset columns available and rows restricted to those consistent with the found data IDs. 
 
- query
- Raises:
- DatasetTypeError
- Raised if given dataset type is inconsistent with the registered dataset type. 
- MissingDatasetTypeError
- Raised if the dataset type has not been registered and only a - strdataset type name was given.
 
 - Notes - This method may require communication with the server unless the dataset type and collections have already been referenced by the same query context. 
 - join_dimensions(dimensions: Iterable[str] | DimensionGroup) Query¶
- Return a new query that joins the logical tables for additional dimensions. - Parameters:
- dimensionsIterable[str] orDimensionGroup
- Names of dimensions to join in. 
 
- dimensions
- Returns:
- queryQuery
- A new query object with the dimensions joined in. 
 
- query
 - Notes - Dimensions are automatically joined in whenever needed, so this method should rarely need to be called directly. 
 - materialize(*, dimensions: Iterable[str] | DimensionGroup | None = None, datasets: Iterable[str] | None = None) Query¶
- Execute the query, save its results to a temporary location, and return a new query that represents fetching or joining against those saved results. - Parameters:
- dimensionsIterable[str] orDimensionGroup, optional
- Dimensions to include in the temporary results. Default is to include all dimensions in the query. 
- datasetsIterable[str], optional
- Names of dataset types that should be included in the new query; default is to include - constraint_dataset_types.
 
- dimensions
- Returns:
- queryQuery
- A new query object whose that represents the materialized rows. 
 
- query
 - Notes - Only dimension key columns and (at the discretion of the implementation) certain dataset columns are actually materialized, since at this stage we do not know which dataset or dimension record fields are actually needed in result rows, and these can be joined back in on the materialized dimension keys. But all constraints on those dimension keys (including dataset existence) are applied to the materialized rows. 
 - where(*args: str | Predicate | DataCoordinate | Mapping[str, Any], bind: Mapping[str, Any] | None = None, **kwargs: Any) Query¶
- Return a query with a boolean-expression filter on its rows. - Parameters:
- *args
- Constraints to apply, combined with logical AND. Arguments may be - strexpressions to parse,- Predicateobjects (these are typically constructed via- expression_factory) or data IDs.
- bindMapping
- Mapping from string identifier appearing in a string expression to a literal value that should be substituted for it. This is recommended instead of embedding literals directly into the expression, especially for strings, timespans, or other types where quoting or formatting is nontrivial. 
- **kwargs
- Data ID key value pairs that extend and override any present in - *args.
 
- Returns:
- queryQuery
- A new query object with the given row filters (as well as any already present in - self). All row filters are combined with logical AND.
 
- query
 - Notes - Expressions referring to dimensions or dimension elements are resolved automatically. References to dataset fields (see - expression_factoryfor the distinction) cannot be resolved by default; they must either be preceded by a call to- join_dataset_searchor must be passed to- DatasetRefQueryResults.whereinstead.- Data ID values are not checked for consistency; they are extracted from - argsand then- kwargsand combined, with later values overriding earlier ones.