Skip to main content

MQL — Melodee Query Language

Imported component documentation. Source: melodee-project/melodee-api at commit 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​

OperationPurpose
GET /api/v1/mql/fieldsRegistry: 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.
  • contains and starts_with are case-insensitive and treat %, _ and \ in the literal as plain characters.
  • is null / is not null are 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.
  • in lists are bounded to 100 values and cannot be empty.

Bounds​

LimitValue
Query length2,000 bytes
AST nodes100
Nesting depth8
List elements100
String literal200 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)​

FieldKindNullableNotes
track.titlestringnoCanonical Track title
track.duration_msintyesPreferred asset duration
release.titlestringnoAny containing Release
release.yearintnoOriginal year of any containing Release
release.classificationstringnostudio, live, compilation, soundtrack, original_cast, classical, spoken_word, other
artist.namestringnoStructured Artist credit display name
artist.rolestringnoStructured credit role
genre.namestringnoCanonical genre name
quality.scoreintyesCurrent combined MQS/AQS
quality.eligibleboolyesCurrent assessment eligibility
asset.formatstringyesPreferred media format
asset.bitrate_kbpsintyesPreferred media bitrate
preference.favoriteboolnoCaller's favorite
preference.ratingintyesCaller's 1–5 rating
preference.dislikedboolnoCaller's dislike
history.play_countintnoCaller's qualified plays
history.last_played_msintyesCaller's last qualified play (epoch ms)
history.played_total_msintnoCaller'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.