httk.store.query

Define backend-agnostic query protocols and portable query capabilities.

The httk.store.query.optimade_filters module contains OPTIMADE filter-translation machinery for serving layers and is intentionally not lifted here.

Submodules

Attributes

ID_FIELD

The backend field name used for the served entry identifier.

Exceptions

CountUnavailableError

Report that a store cannot provide an exact query count.

MultipleResultsError

Report that a result-set one() operation found multiple results.

NoResultError

Report that a result-set one() operation found no matching result.

PaginationCursorError

Report that a continuation cursor is malformed, expired, or belongs to another result plan.

UnsupportedQueryError

Report that a valid query operation is outside a store's supported profile.

Classes

PortableQueryCapabilities

Describe the query operations guaranteed by one property definition.

ContinuationToken

Carry an opaque URL-safe continuation value.

PageOrder

Order a continuation page by one named scalar result projection.

PageableResultSetLike

Expose optional continuation-page capability on a frozen result set.

ResultPage

Represent an immutable continuation-page result.

ResultRow

Represent one named result row by position, name, or attribute.

ResultRowLike

Require named access to one result row.

ResultSetLike

Require the common operations of a materialized result set.

SearchExpression

Require composable backend search expressions.

SearchField

Expose a queryable field of a search variable.

SearchResult

Represent one match with declared output values and names.

SearchVariable

Bind a query variable to a target type whose attributes yield fields.

Searcher

Build one query and iterate its results.

Store

Require a store that can create a query searcher.

Functions

portable_query_capabilities(definition)

Derive the portable operation subset for definition.

portable_query_fields(entry_type, *[, include, exclude])

Return the ordered portable query fields described by entry_type.

Package Contents

class httk.store.query.PortableQueryCapabilities[source]

Describe the query operations guaranteed by one property definition.

query-support expresses a cross-provider guarantee, not a particular server’s implementation detail. all optional is deliberately fail-closed here: it gives a portable client no operation it can rely on. A server may offer more, but that is not represented by the definition.

Parameters:
  • query_support – The normalized declared query-support level.

  • operations – The portable operation families guaranteed by the definition.

query_support: str | None
operations: frozenset[str]
supports(operation)[source]

Report whether operation is guaranteed by this definition.

Parameters:

operation (str) – The operation family to test.

Returns:

True when the operation is portable.

Return type:

bool

httk.store.query.portable_query_capabilities(definition)[source]

Derive the portable operation subset for definition.

The operation names are "equality", "ordering", "stringmatching", and "set". They intentionally describe the query-language operation families rather than storage implementation. IS [NOT] KNOWN is part of the equality family because it is the NULL spelling of equality/inequality in the OPTIMADE filter language.

Parameters:

definition (httk.core.PropertyDefinition) – The OPTIMADE property definition to inspect.

Returns:

The guaranteed portable query capabilities.

Return type:

PortableQueryCapabilities

httk.store.query.portable_query_fields(entry_type, *, include=(), exclude=())[source]

Return the ordered portable query fields described by entry_type.

By default, this selects scalar fields and flat lists with at least one operation guaranteed by their definition. include is an explicit binding override for named existing properties; it is appended as a second ordered group after the derived fields, in entry-definition order among the included names, but does not manufacture query capabilities absent from that definition. exclude always wins. Both arguments reject unknown or duplicate names so binding mistakes cannot silently broaden a profile.

Parameters:
Returns:

Derived property names followed by explicitly included names.

Raises:

ValueError – If include or exclude contains an unknown or duplicate property name.

Return type:

tuple[str, Ellipsis]

httk.store.query.ID_FIELD: Final = '__id'[source]

The backend field name used for the served entry identifier.

class httk.store.query.ContinuationToken[source]

Bases: str

Carry an opaque URL-safe continuation value.

It is a str subclass so normal JSON serializers preserve it as a scalar value. Applications should pass a token returned by a page back unchanged; data backends validate its version, structure, and result-plan fingerprint before using any decoded value as a bound parameter.

Parameters:

value – The opaque continuation value.

exception httk.store.query.CountUnavailableError[source]

Bases: RuntimeError

Report that a store cannot provide an exact query count.

exception httk.store.query.MultipleResultsError[source]

Bases: LookupError

Report that a result-set one() operation found multiple results.

exception httk.store.query.NoResultError[source]

Bases: LookupError

Report that a result-set one() operation found no matching result.

class httk.store.query.PageOrder[source]

Order a continuation page by one named scalar result projection.

name identifies the name supplied to results() (or Searcher.output()), never a backend column object. The result-set implementation validates that it is a root scalar projection before it generates SQL.

Parameters:
  • name – The declared scalar output name used for ordering.

  • descending – Whether to order this field in descending order.

  • nulls – Whether null values sort first or last.

name: str
descending: bool = False
nulls: Literal['first', 'last'] = 'last'
class httk.store.query.PageableResultSetLike[source]

Bases: Protocol

Expose optional continuation-page capability on a frozen result set.

This deliberately extends neither ResultSetLike nor Searcher: stores that do not support seek pagination remain fully conforming to the required portable contracts.

page(*, size, order_by, cursor=None, include_total=False)[source]

Return one ordered continuation page.

exception httk.store.query.PaginationCursorError[source]

Bases: ValueError

Report that a continuation cursor is malformed, expired, or belongs to another result plan.

class httk.store.query.ResultPage[source]

Represent an immutable continuation-page result.

rows is always a tuple. Returned rows are ordinary persistent result rows, not the expiring proxies produced by SqlResultSet.cursor(). total is populated only when the caller explicitly asks for it.

Parameters:
  • rows – The persistent rows returned by the page.

  • next – The token for the next page, if one exists.

  • previous – The token for the previous page, if one exists.

  • total – The exact result count when requested, otherwise None.

rows: tuple[ResultRowLike, Ellipsis]
next: ContinuationToken | None
previous: ContinuationToken | None
total: int | None = None
class httk.store.query.ResultRow(values, names, resolver=None, guard=None)[source]

Represent one named result row by position, name, or attribute.

Parameters:
  • values (tuple[Any, Ellipsis]) – The row values in declaration order.

  • names (tuple[str, Ellipsis]) – The corresponding output names.

  • resolver (Any) – An optional lazy value resolver.

  • guard (Any) – An optional callback that rejects access to expired values.

property names: tuple[str, Ellipsis]

Return the declared output names.

property values: tuple[Any, Ellipsis]

Return the row values in declaration order.

class httk.store.query.ResultRowLike[source]

Bases: Protocol

Require named access to one result row.

property names: tuple[str, Ellipsis]

Return the row’s declared output names.

class httk.store.query.ResultSetLike[source]

Bases: Protocol

Require the common operations of a materialized result set.

first()[source]

Return the first row, or None when no row matches.

one()[source]

Return the only row, or raise when the count is not one.

scalars(name=None)[source]

Iterate over one named scalar output.

class httk.store.query.SearchExpression[source]

Bases: Protocol

Require composable backend search expressions.

class httk.store.query.SearchField[source]

Bases: Protocol

Expose a queryable field of a search variable.

In addition to the methods below, fields support the rich comparison operators (==, !=, <, <=, >, >=), returning SearchExpression. The handlers invoke those via getattr(field, '__eq__')(value) since the comparison dunders cannot be typed as expression-returning.

The three string-matching methods take literal text: no wildcard or pattern syntax whatsoever crosses this contract, so % and _ (and any other metacharacter) match themselves. A backend is therefore free to implement them with SQL LIKE over an escaped pattern, with a regular expression, or with a full-text index — the choice is invisible here.

has(value)[source]

Match a list field containing value.

has_any(*values)[source]

Match a list field containing any of values.

has_only(*values)[source]

Match a list field containing no values outside values.

is_in(*values)[source]

Match a root scalar field whose value is one of values.

None is an explicit member: it matches a null field value, and its negation excludes nulls rather than inheriting SQL’s three-valued NOT IN (..., NULL) behavior.

Backends define the corresponding semantics for child or set fields; for example, a backend may use the existing has_only-style all-values reading for a child field.

contains(text)[source]

Match values containing text as a literal substring.

startswith(prefix)[source]

Match values beginning with the literal prefix.

endswith(suffix)[source]

Match values ending with the literal suffix.

class httk.store.query.SearchResult[source]

Bases: NamedTuple

Represent one match with declared output values and names.

values holds one entry per Searcher.output() call in declaration order; it is a tuple, so values, names = result and result[0][0] both work.

values: tuple[Any, Ellipsis]
names: tuple[str, Ellipsis]
class httk.store.query.SearchVariable[source]

Bases: Protocol

Bind a query variable to a target type whose attributes yield fields.

always_true/always_false are reserved names: they are real methods of the variable, never stored fields resolved through __getattr__. They exist so a translation layer can express a constant truth value without inventing a probe field. A field == field probe is NULL-unsound, since it yields NULL (not true) for a NULL field.

always_true()[source]

An expression that matches every row.

always_false()[source]

An expression that matches no row.

class httk.store.query.Searcher[source]

Bases: Protocol

Build one query and iterate its results.

Iteration yields one SearchResult per match, so item[0][0] is the first declared output of the match (typically the matched row object). The expressions received by add are always ones produced by this same backend’s search variables, so implementations may type them as their own expression class; a backend that needs a second (post-filter) evaluation position decides that from the expression itself, not from the caller.

offset: int
variable(target)[source]

Bind a query variable to target.

output(variable, name)[source]

Declare variable as a named result output.

add(expression)[source]

Add a filter expression to the query.

count()[source]

Return the exact count of the current query.

set_limit(limit)[source]

Set the query limit.

add_offset(offset)[source]

Add an offset to the query.

add_sort(field, descending)[source]

Add a field sort to the query.

results(**outputs)[source]

Return a result set for the requested named outputs.

class httk.store.query.Store[source]

Bases: Protocol

Require a store that can create a query searcher.

searcher()[source]

Create an empty searcher.

exception httk.store.query.UnsupportedQueryError[source]

Bases: ValueError

Report that a valid query operation is outside a store’s supported profile.