Query language (dhis2w_ql)¶
dhis2w_ql is the engine behind d2ql and d2path: a
pipeline query/transform language with an embedded expression language (d2path) that navigates and
computes over data with collection semantics. It is a pure, dependency-light package (pydantic only)
— the DHIS2 binding (data sources,
pushdown compiler, CLI, MCP) lives in the query plugin in dhis2w-core.
When to reach for it¶
- Parse a d2ql program or d2path expression to a typed AST (
parse,parse_pipeline,parse_expression). - Evaluate d2path over your own data (
Evaluator) — DHIS2 models, FHIR JSON, fixtures. - Run a parsed program against any source via the engine (
QueryEngine+ aResourceBinder); split pushdown from local work withplan_pipeline. - Browse or generate examples (
SAMPLES,generate).
Worked example — evaluate d2path over JSON¶
from dhis2w_ql import Evaluator, parse_expression
patient = {"name": [{"use": "official", "given": ["Ada"], "family": "King"}]}
result = Evaluator().evaluate(parse_expression('name.where(use = "official").family'), [patient])
assert result == ["King"]
Worked example — run a program over an in-memory source¶
import asyncio
from dhis2w_ql import InMemoryBinder, QueryEngine, parse
rows = [{"id": "a1", "name": "ANC", "domainType": "AGGREGATE"}]
engine = QueryEngine(parse('dataElements | where domainType = "AGGREGATE" | transform { code: id }'),
InMemoryBinder({"dataElements": rows}))
print(asyncio.run(engine.run_terminal()).rows) # [{'code': 'a1'}]
Reference¶
dhis2w_ql
¶
d2qlis the pipeline language (source | where | select | transform | order | limit >> sink), withdefine/define functionlibrary definitions.d2path(indhis2w_ql.d2path) is the embedded expression language: path navigation, operators, and functions with collection semantics.dhis2w_ql.engineruns a parsed library against aResourceBinder, pushing supported work down to the source and evaluating the rest locally.
Classes¶
AggregateStage
¶
Bases: _Node
Group rows by a key expression and reduce each group with aggregate expressions.
group by <group> { total: sum(value), n: count() } — each aggregation expression is
evaluated against the group's rows, so value gathers that field across the group.
Source code in packages/dhis2w-ql/src/dhis2w_ql/ast.py
CallSource
¶
Bases: _Node
A source written as a named call with keyword arguments (e.g. analytics(dx: "...", pe: "...")).
Source code in packages/dhis2w-ql/src/dhis2w_ql/ast.py
Define
¶
DefineFunction
¶
Bases: _Node
A named function definition: define function NAME(params): <expr>.
Source code in packages/dhis2w-ql/src/dhis2w_ql/ast.py
FoldStage
¶
Bases: _Node
Collapse the whole stream into a single object (e.g. a FHIR Bundle or GeoJSON FeatureCollection).
fold { resourceType: "Bundle", entry: $rows } — the template is built once with the entire
stream in focus; $rows is the stream as a list, and aggregate/select functions see all rows.
Source code in packages/dhis2w-ql/src/dhis2w_ql/ast.py
Library
¶
Bases: _Node
A whole d2ql program: zero or more definitions plus an optional terminal pipeline.
Source code in packages/dhis2w-ql/src/dhis2w_ql/ast.py
Pipeline
¶
Bases: _Node
A source feeding a chain of stages, optionally ending in a sink.
Source code in packages/dhis2w-ql/src/dhis2w_ql/ast.py
EvalContext
¶
Bases: BaseModel
Lexical scope for one evaluation: $this, $index, and any bound function parameters.
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
Evaluator
¶
Evaluates d2ql expressions against a focus collection.
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
Functions¶
__init__(resolver=None)
¶
Bind an optional resolver for library-level variable/function references.
evaluate(expr, focus, context=None)
¶
Evaluate expr against focus, returning a collection (list of values).
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
call_function(definition, args, focus, context)
¶
Invoke a user-defined function, overlaying its params on the caller's scope.
The caller context carries through so $this/$index stay visible inside an extracted
helper; the function name is tracked to reject recursive calls instead of crashing.
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
per_item(expr, item, index, context)
¶
Evaluate expr against a single item, binding $this/$index for that item.
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
build_object(expr, focus, context)
¶
Build a single dict from an object constructor, collapsing each field value.
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
Resolver
¶
Bases: Protocol
Resolves library-level names referenced from inside expressions.
Source code in packages/dhis2w-ql/src/dhis2w_ql/d2path/evaluator.py
DataSource
¶
Bases: Protocol
A fetchable collection of rows that may satisfy part of a query natively.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/datasource.py
InMemoryBinder
¶
Binds resource names to in-memory row lists (test/fixture binding).
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/datasource.py
InMemoryDataSource
¶
A data source backed by an in-memory list; declares no native capabilities (all-local).
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/datasource.py
NativeFilter
¶
Bases: BaseModel
A single field filter pushed to the source (property OPERATOR value).
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/plan.py
NativeOrder
¶
NativeQuery
¶
Bases: BaseModel
The pushed-down portion of a pipeline against a named resource.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/plan.py
QueryEngine
¶
Executes d2ql pipelines and definitions against a resource binder.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
Functions¶
__init__(library, binder)
¶
Index the library's definitions and bind the source resolver.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
resolve_variable(name)
¶
Resolve a $name reference to a scalar define's value, or None when not scalar.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
resolve_function(name)
¶
run_terminal()
async
¶
Run the library's terminal pipeline, raising when there is none.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
run_define(name)
async
¶
Run a named define's pipeline.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
run(pipeline)
async
¶
Execute a pipeline and apply its sink, returning the rows and sink metadata.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
QueryPlan
¶
Bases: BaseModel
A pushed-down NativeQuery plus the stages the engine still runs locally.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/plan.py
QueryResult
¶
Bases: BaseModel
The outcome of running a pipeline: the produced rows plus sink/shape metadata.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
ResourceBinder
¶
Bases: Protocol
Resolves a pipeline source name to a DataSource, or None when it is not a known resource.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/datasource.py
SourceCapabilities
¶
Bases: BaseModel
Declares which pushdown operations a source can satisfy natively.
non_pushable_paths lists field roots the source cannot filter or order on natively (e.g. a
DHIS2 embedded GeoJSON geometry object): predicates touching them stay local instead of being
pushed down to an endpoint that would reject them.
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/plan.py
D2qlError
¶
EvaluationError
¶
LexError
¶
Bases: D2qlError
Raised when the tokenizer hits a character it cannot start a token with.
Source code in packages/dhis2w-ql/src/dhis2w_ql/errors.py
ParseError
¶
Bases: D2qlError
Raised when the parser cannot build an AST from the token stream.
Source code in packages/dhis2w-ql/src/dhis2w_ql/errors.py
SemanticError
¶
GeneratedExample
¶
Bases: BaseModel
One generated d2ql pipeline or d2path expression.
Source code in packages/dhis2w-ql/src/dhis2w_ql/generate.py
SchemaSpec
¶
Bases: BaseModel
A set of resources the generator expands into example programs.
Source code in packages/dhis2w-ql/src/dhis2w_ql/generate.py
Sample
¶
Bases: BaseModel
One illustrative d2ql pipeline or d2path expression with metadata for display and testing.
Source code in packages/dhis2w-ql/src/dhis2w_ql/samples.py
Token
¶
Bases: BaseModel
A single lexed token with its source position.
Source code in packages/dhis2w-ql/src/dhis2w_ql/tokenizer.py
TokenKind
¶
Bases: StrEnum
The lexical category of a token.
Source code in packages/dhis2w-ql/src/dhis2w_ql/tokenizer.py
Functions¶
plan_pipeline(resource, capabilities, stages)
¶
Split stages into a NativeQuery (pushed down) and a residual list (run locally).
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/planner.py
to_jsonable(value)
¶
Convert a result value (pydantic models, dicts, lists, scalars) to JSON-serialisable data.
write_rows(path, rows, fmt=None, *, scalar=False)
¶
Write rows to path as json/ndjson/csv (inferring the format from the extension when None).
Source code in packages/dhis2w-ql/src/dhis2w_ql/engine/executor.py
parse(source)
¶
parse_expression(source)
¶
Parse a single bare expression (the d2path expression surface).
parse_pipeline(source)
¶
Parse a single pipeline (no definitions) into a Pipeline AST.
Source code in packages/dhis2w-ql/src/dhis2w_ql/parser.py
samples_by_category()
¶
Group the catalog by category, preserving definition order within each group.