MQL — Melodee Query Language
0267c458207082337d9baf1130deeb65f43c9d4a. Version 1.0.1. License MIT. Status: implemented-and-automatically-verified candidate; not published, deployed or production-qualified. This is the component's committed documentation, not proof of a distribution release or production qualification.
MQL is the server-owned query language for canonical Tracks. Clients (CLI, Admin
UI) submit MQL text; the server validates it against the versioned registry
and compiles it to parameterized PostgreSQL. There is no client-side MQL
evaluator, and clients must treat field lists as data from
GET /api/v1/mql/fields.
Version: mql/1 · Contract: native API 1.0.0
Endpoints
| Operation | Purpose |
|---|---|
GET /api/v1/mql/fields | Registry: name, kind (string/int/bool), nullability, description |
POST /api/v1/mql/validate | {"query":"…"} → {valid, version, nodeCount, errors[]}; never touches the database |
POST /api/v1/mql/execute | {"query":"…","limit":25,"cursor":"…"} → {version, items, nextCursor} |
Validation is intentionally 200 OK even when the query is invalid: valid: false plus errors is a normal answer. Execution returns 400 with the standard
error envelope (code: invalid_mql) and the same error codes in error.fields[],
where field is bytes <start>..<end>.
Errors always carry UTF-8 byte offsets:
{"code":"type_mismatch","message":"track.title expects a string value, found \"5\"","start":14,"end":15}
Grammar
query := or
or := and ( "or" and )*
and := unary ( "and" unary )*
unary := "not" unary | primary
primary := "(" query ")" | predicate
predicate := field ( operator value | "is" ["not"] "null" )
| boolean_field -- means field = true
operator := "=" | "!=" | "<" | "<=" | ">" | ">="
| "contains" | "starts_with" | "in" | "not" "in"
value := string | integer | true | false | list
list := "[" ( value ("," value)* )? "]"
- Precedence:
not>and>or. Use parentheses for anything else. - Strings use single or double quotes. Supported escapes:
\\,\",\',\n,\t. All other text (including Unicode) is preserved verbatim. - Keywords are case-insensitive; field names are matched case-insensitively.
containsandstarts_withare case-insensitive and treat%,_and\in the literal as plain characters.is null/is not nullare the only way to match a missing value.=,!=,<, … follow SQL semantics: a comparison with a missing value is unknown and the Track does not match.inlists are bounded to 100 values and cannot be empty.
Bounds
| Limit | Value |
|---|---|
| Query length | 2,000 bytes |
| AST nodes | 100 |
| Nesting depth | 8 |
| List elements | 100 |
| String literal | 200 characters |
Exceeding a bound is a validation error (query_too_long, query_too_complex,
query_too_deep, list_too_long, string_too_long), never an unbounded query.
Field registry (mql/1)
| Field | Kind | Nullable | Notes |
|---|---|---|---|
track.title | string | no | Canonical Track title |
track.duration_ms | int | yes | Preferred asset duration |
release.title | string | no | Any containing Release |
release.year | int | no | Original year of any containing Release |
release.classification | string | no | studio, live, compilation, soundtrack, original_cast, classical, spoken_word, other |
artist.name | string | no | Structured Artist credit display name |
artist.role | string | no | Structured credit role |
genre.name | string | no | Canonical genre name |
quality.score | int | yes | Current combined MQS/AQS |
quality.eligible | bool | yes | Current assessment eligibility |
asset.format | string | yes | Preferred media format |
asset.bitrate_kbps | int | yes | Preferred media bitrate |
preference.favorite | bool | no | Caller's favorite |
preference.rating | int | yes | Caller's 1–5 rating |
preference.disliked | bool | no | Caller's dislike |
history.play_count | int | no | Caller's qualified plays |
history.last_played_ms | int | yes | Caller's last qualified play (epoch ms) |
history.played_total_ms | int | no | Caller's total qualified played ms |
release.*, artist.* and genre.name match when any related row satisfies
the predicate. preference.* and history.* are computed for the authenticated
account only; the account is bound by the server and cannot appear in the query.
Ordering and pagination
Results are ordered by canonical title key, then internal Track identity — stable
across requests and insensitive to catalog changes above the cursor. Pass
nextCursor back verbatim; it is opaque and contains no internal numeric
identifier. limit is 1–100 (default 25).
Examples
artist.name = 'Björk' and preference.rating >= 4
genre.name in ['Rock', 'Pop'] and not preference.disliked
and asset.bitrate_kbps >= 256
release.classification = 'live' and release.year >= 1990
quality.eligible and quality.score is not null
and history.play_count = 0
track.title contains 'Love' or (artist.name starts_with 'The ' and release.year < 2000)
Security model
- The compiler never interpolates user text: every literal and the account identity are bound parameters.
- The account is injected server-side, so a query can only observe the caller's own favorites, ratings, dislikes, preferences and history.
- Queries are bounded and return at most 100 Tracks per page; there is no count or unbounded scan endpoint.
- The compiled SQL is never returned.
- Genre exclusions apply to automatic playback selection, not to explicit search, detail or playback.
Verification
src/mql/parser.rs and src/mql/compile.rs unit tests cover grammar, precedence,
escapes, type/null/operator errors with positions, bounds, LIKE escaping, array
binding, cursor round-trips and placeholder numbering.
tests/api_v1.rs::mql_validates_types_with_positions_and_executes_parameterized_queries
proves registry/validate/execute behaviour, account isolation, null semantics,
genre and artist matching, injection-shaped literals, deterministic pagination,
invalid cursors/limits and complexity rejection against disposable PostgreSQL.