agentleFS
malloydatamalloydata/publisher/skills/malloy-gotchas-queries/SKILL.md

malloy-gotchas-queries

Skill116 starsChanged 2 months agoMITView on GitHub

Common Malloy query and view mistakes, for writing views, queries or notebooks - chart constraints, aggregate filters, joined field aliasing, method syntax, time truncation vs extraction.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Check the field's type in the get_context result before aggregating it.

    line 95
  2. Very specific:

    Place # bar_chart / # line_chart on the nested view definition, not on nest: itself.

    line 144
  3. Very specific:

    Use .month for time series charts (proper date ordering).

    line 164
  4. Distinctive:

    When combining an alternation filter with other filters, use a comma:

    line 172

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- Copyright (c) Credible Data Inc. SPDX-License-Identifier: MIT -->

Query & View Gotchas

Read this before writing views or queries. These patterns cause most query errors.

Charts: ONE Aggregate Per View

Charts render only the first aggregate. Use exactly one aggregate per # bar_chart / # line_chart view.

// WRONG: revenue is ignored
# bar_chart
view: x is { group_by: status, aggregate: order_count, revenue }
// RIGHT: single aggregate
# bar_chart
view: x is { group_by: status, aggregate: revenue }

For multiple metrics: nest separate chart views in a # dashboard, or use y=['revenue','cost'] for multi-measure series.

Joined Fields in order_by: Must Alias First

// WRONG: compile error
view: x is { group_by: races.season_year, aggregate: pts, order_by: races.season_year }
// RIGHT: alias then reference
view: x is { group_by: yr is races.season_year, aggregate: pts, order_by: yr }

Any time you group_by a joined field, create an alias and use it in order_by.

having: vs where:: Aggregate Filters

// WRONG: "Aggregate expressions not allowed in where"
view: x is { group_by: cat, aggregate: n is count(), where: n > 10 }
// RIGHT
view: x is { group_by: cat, aggregate: n is count(), having: n > 10 }
  • where: filters rows BEFORE aggregation (dimensions/raw columns)
  • having: filters AFTER aggregation (measures)

Aggregating Joined Fields: Method Syntax

// WRONG: compile error: "Join path is required for this calculation; use 'inventory_items.item_cost.sum()'"
measure: cogs is sum(inventory_items.item_cost)
// RIGHT: method syntax
measure: cogs is inventory_items.item_cost.sum()

sum, avg, min, and max over a dotted joined path all produce that compile error; the diagnostic message even tells you the exact fix. Don't worry about catching this in code review; the compiler does it for you.

Method syntax is for aggregates over a path. Scalar functions never take it.

// WRONG: "something is missing before 'round'"
aggregate: avg_price_r is avg(price).round(2)
aggregate: avg_price_r is price.avg().round(2)
// WRONG: "Cannot call function round(number, number) with source"
aggregate: avg_price_r is avg_price.round(2)
dimension: rounded is price.round(2)
// RIGHT: scalar functions are always call form
aggregate: avg_price_r is round(avg(price), 2)
dimension: rounded is round(price, 2)

Two separate rules produce those errors:

  • No method call chains onto the result of a function call. avg(price).round(2) and price.avg().round(2) are both parse errors. The message names round without saying it is unsupported in that position, so it reads like a typo somewhere else. .floor() and .ceil() fail identically.
  • Scalar functions have no method form. round, floor, and ceil are always round(x, 2), never x.round(2), whether x is a named measure or a plain column.

price.avg() and inventory_items.item_cost.sum() are correct because avg and sum are aggregate functions over a field path, which is exactly what method syntax is for.

Exception: count(joined.field) is correct, not a bug. count(joined.field) is the canonical Malloy idiom for distinct-count through a join. Keep it as-is even when nearby sum/avg/min/max calls have to use method syntax. The closest method-syntax form joined.count() counts rows in the joined source (different semantics, differs from the distinct count when the joined field has duplicates within the joined table). The Malloy docs example joined.count(field) does NOT compile against current Malloy (error: Expression illegal inside path.count()); it only works for double-nested paths like aircraft.count(aircraft_models.code).

sum/avg Need a Numeric Field

// WRONG: "Can't use type string" - status is a string column
aggregate: avg_status is avg(status)
// RIGHT: aggregate a numeric field; count a string one
aggregate: avg_price is avg(price), statuses is count(status)

Check the field's type in the get_context result before aggregating it. A name that reads numeric (order_number, zip, account_id) is very often typed string.

A Measure Is Already an Aggregate

// WRONG: "Aggregate expression cannot be aggregate" - flight_count is a measure
run: flights -> { aggregate: busiest is max(flight_count) }
// RIGHT: aggregate per group, then take the max in a second stage
run: flights -> { group_by: carrier, aggregate: n is flight_count } -> { aggregate: busiest is max(n) }

The message does not name the field. When it appears, look for a model measure you wrapped in another aggregate. If you only need the top row, skip the second stage: order_by: n desc with limit: 1.

Window Braces Bind to the Function

// WRONG: "`partition_by` is not supported for this kind of expression"
calculate: share is sum_cumulative(n) / region_total { partition_by: region, order_by: n desc }
// RIGHT: the brace goes directly after the window function call
calculate: share is sum_cumulative(n) { partition_by: region, order_by: n desc } / region_total

For a within-group share, compute the denominator in aggregate: (region_total is all(count(), region)). A window's { partition_by } does not apply to all().

Dotted Paths Must Name a Declared Join

// WRONG: the source declares the join as `carrier`, so this fails with
//   "'carriers.name' is not a source or join"
run: flights -> { group_by: carriers.name }
// RIGHT: use the join name the source actually declares
run: flights -> { group_by: carrier.nickname }

A dotted path resolves only against a join declared on the source you are running. Confirm both the join name and the field under it in a get_context result; do not infer either from a table name or a plural/singular guess.

order_by: Can Only Name an Output Column

// WRONG: "Unknown field total in output space" - total is never emitted
run: orders -> { group_by: state, aggregate: revenue is sum(total), order_by: total }
// RIGHT: order by a column the query actually outputs
run: orders -> { group_by: state, aggregate: revenue is sum(total), order_by: revenue }

order_by: resolves against the query's output columns, not the source's fields. To order by something, group_by or aggregate it first - and if it comes through a join, alias it (see above).

Chart Annotation Placement

Place # bar_chart / # line_chart on the nested view definition, not on nest: itself. Putting it on nest: causes "not a repeated record" errors.

DRY: Define in Source, Reference in View

// WRONG: inline in view
view: summary is { aggregate: revenue is sum(total) }
// RIGHT: reference existing measure
view: summary is { aggregate: revenue }

Time Truncation vs Extraction

SyntaxWhat it doesReturns
ts.monthTruncates to start of monthTimestamp (@2024-03-01)
month(ts)Extracts month numberInteger (1-12)
ts.yearTruncates to start of yearTimestamp (@2024-01-01)
year(ts)Extracts year numberInteger (2024)

Use .month for time series charts (proper date ordering). Use month() for cross-year comparison.

Year integers render with commas. year(ts) displays as 2,018. Tag with # number=id to suppress commas. Same for zip codes, IDs.

? Alternation: Use Commas to Combine Filters

The ? operator is Malloy's alternation operator: a shorthand for "match any of these values." party ? 'Democrat' | 'Republican' means party = 'Democrat' OR party = 'Republican'. The | separates the alternatives.

When combining an alternation filter with other filters, use a comma:

// CANONICAL: commas separate independent filter conditions
where: is_us = true, party ? 'Democrat' | 'Republican'

and works in some arrangements (when the alternation is the second operand) but produces a confusing 'logical operator' Can't use type string compile error when the alternation comes first. The comma form is unambiguous in every position, so just use it.

Strings: Apostrophes and Concatenation

// WRONG: "no viable alternative at input 's'" - the apostrophe ends the literal
where: name = 'Mac's Diner'
// RIGHT
where: name = "Mac's Diner"

// WRONG: "unexpected '+'" / "no viable alternative at input '||'"
group_by: route is origin ++ '-' ++ destination
// RIGHT: there is no concatenation operator
group_by: route is concat(origin, '-', destination)

A Semicolon Inside a Clause Ends It

Between clauses a newline, comma or ; all work. Within one clause, separate fields with commas or newlines: a ; closes the clause, and the next field is orphaned.

// WRONG: "no viable alternative at input 'charters'"
run: schools -> { aggregate: total is count(); charters is count() { where: is_charter } }
// RIGHT
run: schools -> { aggregate: total is count(), charters is count() { where: is_charter } }

The same message appears for an unnamed aggregate after the first: aggregate: n, max(x) fails at max. Name every entry.

Compiles, Then Refused by the Warehouse

Query execution failed: ... means the Malloy compiled and the warehouse rejected the SQL, often over a type. The position it quotes is in the generated SQL, not in your query. BigQuery, for example, will not partition a window function on a FLOAT64 column. partition_by: takes only a field name, so cast in group_by: (season_key is season::string) and partition on season_key, or fix the type in the model.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.