Translates a GBD query string and parameters into a complete SQLite SELECT statement.
A GBD query spans one or more ATTACHed SQLite databases managed by
Database. Each database belongs to a context
(e.g. cnf, wcnf, pbo). Features within the same context are joined
directly; features from a different context require a translator feature - a 1:n
feature named to_{context} that maps hashes between contexts.
Typical usage:
q = GBDQuery(db, "filename like foo%") sql = q.build_query(resolve=["local"], collapse="group_concat") rows = db.query(sql)
| Method | __init__ |
No summary |
| Method | build |
Build the FROM / JOIN clause. |
| Method | build |
Build and return a complete SQL SELECT statement. |
| Method | build |
Build the SELECT clause. |
| Method | build |
Build the WHERE clause body. |
| Method | determine |
Return the default context:hash column used as the GROUP BY key. |
| Method | features |
Raise DatabaseException if any feature in features does not exist in the attached databases. |
| Method | find |
Find the 1:n translator feature that bridges two contexts. |
| Instance Variable | db |
Undocumented |
| Instance Variable | features |
Undocumented |
| Instance Variable | parser |
Undocumented |
| Parameters | |
db:Database | Multi-database instance used for feature resolution. |
query:str|None | GBD filter expression, or None / empty string for an unconditional (match-all) query. |
Build the FROM / JOIN clause.
Three JOIN strategies depending on the feature's context relative to the group-by column:
- Same-context, 1:1 feature (column in features table): {join_type} JOIN db.features ON db.features.hash = base.hash
- Same-context, 1:n feature (separate table): ensures db.features is joined first, then {join_type} JOIN db.{name} ON db.{name}.hash = db.features.{name}
- Cross-context: always INNER JOIN via the translator feature table regardless of join_type (see Issues.md #4).
| Parameters | |
group:str | Feature identifier of the group-by column; its database is the base FROM table. |
features:set[str] | All features that must appear in the clause (filter features + resolved features). |
joinstr | "LEFT" or "INNER" applied to same-context joins. |
| Returns | |
str | SQL FROM / JOIN clause. |
Build and return a complete SQL SELECT statement.
| Parameters | |
hashes:list[str] | Restrict results to these benchmark hashes. |
resolve:list[str] | Features to include as output columns. |
groupNone | Group results by this feature instead of the default context:hash column; inferred from resolve when None. |
joinstr | "LEFT" (default, include unmatched hashes) or "INNER" (exclude them). Cross-context joins are always INNER (see Issues.md #4). |
collapse:str|None | Aggregate function for resolved columns; one of "group_concat", "min", "max", "avg", "count", "sum". None returns one raw row per join result. |
| Returns | |
str | Ready-to-execute SQL query. |
Build the SELECT clause.
When collapse is given, every selected column (including the group-by column) is wrapped with the aggregate function. Without collapse, SELECT DISTINCT deduplicates rows.
The group-by column is also aggregated, which is redundant when GROUP BY is present (see Issues.md #6).
| Parameters | |
groupstr | Primary output column (always first in the SELECT list). |
resolve:list[str] | Additional output columns. |
collapse:str|None | Aggregate function name (e.g. "group_concat"), or None for no aggregation. |
| Returns | |
str |
|
Build the WHERE clause body.
Combines three conditions with AND:
- group_column != 'None' excludes the sentinel null-hash row present in every 1:n feature table (see Issues.md #7 - sentinel design).
- The SQL fragment compiled from the GBD filter expression.
- An optional hash IN (...) restriction when hashes is non-empty.
| Parameters | |
hashes:list[str] | Benchmark hashes to restrict results to; empty list means no hash restriction. |
groupstr | Feature identifier of the group-by column. |
| Returns | |
str | SQL WHERE clause body (without the WHERE keyword). |
Return the default context:hash column used as the GROUP BY key.
Uses the database of the first feature in resolve, or the primary hash feature when resolve is empty.
When resolve spans multiple contexts, only the first feature's context is considered (see Issues.md #5).
| Parameters | |
resolve:list[str] | Features that will be resolved in the query. |
| Returns | |
str | Feature identifier of the form "context:hash". |
Raise DatabaseException if any feature in
features does not exist in the attached databases.
| Parameters | |
features:list[str] | Feature identifiers to validate. |
Find the 1:n translator feature that bridges two contexts.
A translator feature is a 1:n feature named to_{target_context} stored in a
database of source_context, or to_{source_context} stored in a database of
target_context. It provides a hash mapping used by build_from to
construct cross-context JOINs.
| Parameters | |
sourcestr | Context of the primary (group-by) table. |
targetstr | Context of the feature being resolved. |
| Returns | |
FeatureInfo | Info object for the translator feature. |
| Raises | |
DatabaseException | If no translator feature exists for the context pair. |