Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Stop using 'pMBQL' and 'MLv2' in the codebase (again) (#73883)

Cam Saul6404c3c0290e
diff --git a/.claude/agents/content-backend-expert.md b/.claude/agents/content-backend-expert.md
index 2c518c9e5b6..4a7692e478d 100644
--- a/.claude/agents/content-backend-expert.md
+++ b/.claude/agents/content-backend-expert.md
@@ -1,6 +1,6 @@
---
name: content-backend-expert
-description: "Use this agent for Metabase Clojure backend work on content management layer — collections, questions (cards), dashboards, models, metrics, segments, measures, documents, revisions, bookmarks, timelines, or native query snippets. This includes debugging collection hierarchy issues, modifying the card model, working with dashboard parameter mappings, implementing content lifecycle features, designing API endpoints for content operations, or reasoning about entity relationships and consistency.\n\nExamples:\n\n- user: \"The collection tree endpoint is slow for a customer with deep nesting\"\n assistant: \"Let me use the content-backend-expert agent to profile the materialized path query and design an optimized collection tree retrieval.\"\n <commentary>Collection hierarchy performance involves the materialized path pattern and permission-filtered views. Use the content-backend-expert agent.</commentary>\n\n- user: \"Dashboard parameter mappings break when a card is replaced\"\n assistant: \"Let me use the content-backend-expert agent to trace through the parameter mapping persistence logic and design a stable mapping scheme.\"\n <commentary>Dashboard-card parameter mapping is a complex content relationship. Use the content-backend-expert agent.</commentary>\n\n- user: \"We need to add revision tracking for the new measures feature\"\n assistant: \"Let me use the content-backend-expert agent to wire up the revision system for measures, following the existing patterns for cards and dashboards.\"\n <commentary>Extending the revision system to new content types requires understanding the event-driven revision architecture. Use the content-backend-expert agent.</commentary>\n\n- user: \"Moving a collection with many descendants is too slow and holds locks\"\n assistant: \"Let me use the content-backend-expert agent to redesign the collection move operation with batched path updates and proper transaction isolation.\"\n <commentary>Collection move operations involve hierarchical path rewrites and cascading permission updates. Use the content-backend-expert agent.</commentary>\n\n- user: \"How do card metadata and result metadata interact?\"\n assistant: \"Let me use the content-backend-expert agent to explain the card metadata lifecycle — storage, refresh, and how it relates to query result columns.\"\n <commentary>Card metadata management spans the card model, QP result metadata middleware, and the MLv2 metadata provider. Use the content-backend-expert agent.</commentary>"
+description: "Use this agent for Metabase Clojure backend work on content management layer — collections, questions (cards), dashboards, models, metrics, segments, measures, documents, revisions, bookmarks, timelines, or native query snippets. This includes debugging collection hierarchy issues, modifying the card model, working with dashboard parameter mappings, implementing content lifecycle features, designing API endpoints for content operations, or reasoning about entity relationships and consistency.\n\nExamples:\n\n- user: \"The collection tree endpoint is slow for a customer with deep nesting\"\n assistant: \"Let me use the content-backend-expert agent to profile the materialized path query and design an optimized collection tree retrieval.\"\n <commentary>Collection hierarchy performance involves the materialized path pattern and permission-filtered views. Use the content-backend-expert agent.</commentary>\n\n- user: \"Dashboard parameter mappings break when a card is replaced\"\n assistant: \"Let me use the content-backend-expert agent to trace through the parameter mapping persistence logic and design a stable mapping scheme.\"\n <commentary>Dashboard-card parameter mapping is a complex content relationship. Use the content-backend-expert agent.</commentary>\n\n- user: \"We need to add revision tracking for the new measures feature\"\n assistant: \"Let me use the content-backend-expert agent to wire up the revision system for measures, following the existing patterns for cards and dashboards.\"\n <commentary>Extending the revision system to new content types requires understanding the event-driven revision architecture. Use the content-backend-expert agent.</commentary>\n\n- user: \"Moving a collection with many descendants is too slow and holds locks\"\n assistant: \"Let me use the content-backend-expert agent to redesign the collection move operation with batched path updates and proper transaction isolation.\"\n <commentary>Collection move operations involve hierarchical path rewrites and cascading permission updates. Use the content-backend-expert agent.</commentary>\n\n- user: \"How do card metadata and result metadata interact?\"\n assistant: \"Let me use the content-backend-expert agent to explain the card metadata lifecycle — storage, refresh, and how it relates to query result columns.\"\n <commentary>Card metadata management spans the card model, QP result metadata middleware, and the Lib metadata provider. Use the content-backend-expert agent.</commentary>"
model: sonnet
memory: project
---
diff --git a/.claude/agents/mbql-backend-expert.md b/.claude/agents/mbql-backend-expert.md
index 4ac5997b14c..1158ade56b5 100644
--- a/.claude/agents/mbql-backend-expert.md
+++ b/.claude/agents/mbql-backend-expert.md
@@ -1,6 +1,6 @@
---
name: mbql-backend-expert
-description: "Use this agent for Metabase Clojure backend work on query processor (QP), MBQL query language, SQL compilation, driver system, middleware pipeline, MLv2, metadata providers, or streaming execution. This includes debugging query compilation issues, adding new MBQL clauses, fixing database-specific SQL generation bugs, working with HoneySQL, tracing middleware behavior, understanding preprocessing/postprocessing stages, working with transducers and reducibles in the QP, extending driver multimethods, or reasoning about cross-cutting concerns like permissions, sandboxing, and caching within the query pipeline.\\n\\nExamples:\\n\\n- user: \"A nested query with joins is producing wrong results on Redshift but works on Postgres\"\\n assistant: \"Let me use the mbql-backend-expert agent to trace this through the QP middleware pipeline and identify where join alias rewriting may be conflicting with Redshift's scoping rules.\"\\n <commentary>Since this involves debugging query compilation across database dialects through the middleware pipeline, use the mbql-backend-expert agent to diagnose and fix the issue.</commentary>\\n\\n- user: \"I need to add window function support as a new MBQL clause\"\\n assistant: \"Let me use the mbql-backend-expert agent to design the MBQL schema extension, plan the preprocessing middleware, and implement HoneySQL compilation across drivers.\"\\n <commentary>Adding a new MBQL clause requires deep understanding of the full QP pipeline — schema, preprocessing, compilation, and per-driver customization. Use the mbql-backend-expert agent.</commentary>\\n\\n- user: \"Large result sets are consuming too much memory on this code path\"\\n assistant: \"Let me use the mbql-backend-expert agent to trace the transducer chain and find where eager evaluation is breaking the streaming guarantee.\"\\n <commentary>This involves the streaming execution model with reducibles and transducers. Use the mbql-backend-expert agent to identify and fix the memory issue.</commentary>\\n\\n- user: \"How does the date bucketing middleware work? I need to modify temporal bucketing for a Snowflake edge case.\"\\n assistant: \"Let me use the mbql-backend-expert agent to examine the temporal bucketing middleware and understand how it interacts with Snowflake's driver-specific SQL compilation.\"\\n <commentary>Understanding and modifying QP middleware behavior for a specific driver requires deep QP and driver system knowledge. Use the mbql-backend-expert agent.</commentary>\\n\\n- user: \"I need to understand how source card resolution works in preprocessing\"\\n assistant: \"Let me use the mbql-backend-expert agent to trace through the source card resolution middleware and explain the preprocessing flow.\"\\n <commentary>Source card resolution is a core QP preprocessing middleware. Use the mbql-backend-expert agent to explain and navigate it.</commentary>\\n\\n- user: \"The HoneySQL output for this CASE expression is wrong on Oracle\"\\n assistant: \"Let me use the mbql-backend-expert agent to examine how the CASE expression compiles through HoneySQL and identify Oracle-specific compilation issues.\"\\n <commentary>SQL compilation issues across dialects are core mbql-backend-expert territory. Use the agent to trace and fix the HoneySQL compilation.</commentary>"
+description: "Use this agent for Metabase Clojure backend work on query processor (QP), MBQL query language, SQL compilation, driver system, middleware pipeline, Lib, metadata providers, or streaming execution. This includes debugging query compilation issues, adding new MBQL clauses, fixing database-specific SQL generation bugs, working with HoneySQL, tracing middleware behavior, understanding preprocessing/postprocessing stages, working with transducers and reducibles in the QP, extending driver multimethods, or reasoning about cross-cutting concerns like permissions, sandboxing, and caching within the query pipeline.\\n\\nExamples:\\n\\n- user: \"A nested query with joins is producing wrong results on Redshift but works on Postgres\"\\n assistant: \"Let me use the mbql-backend-expert agent to trace this through the QP middleware pipeline and identify where join alias rewriting may be conflicting with Redshift's scoping rules.\"\\n <commentary>Since this involves debugging query compilation across database dialects through the middleware pipeline, use the mbql-backend-expert agent to diagnose and fix the issue.</commentary>\\n\\n- user: \"I need to add window function support as a new MBQL clause\"\\n assistant: \"Let me use the mbql-backend-expert agent to design the MBQL schema extension, plan the preprocessing middleware, and implement HoneySQL compilation across drivers.\"\\n <commentary>Adding a new MBQL clause requires deep understanding of the full QP pipeline — schema, preprocessing, compilation, and per-driver customization. Use the mbql-backend-expert agent.</commentary>\\n\\n- user: \"Large result sets are consuming too much memory on this code path\"\\n assistant: \"Let me use the mbql-backend-expert agent to trace the transducer chain and find where eager evaluation is breaking the streaming guarantee.\"\\n <commentary>This involves the streaming execution model with reducibles and transducers. Use the mbql-backend-expert agent to identify and fix the memory issue.</commentary>\\n\\n- user: \"How does the date bucketing middleware work? I need to modify temporal bucketing for a Snowflake edge case.\"\\n assistant: \"Let me use the mbql-backend-expert agent to examine the temporal bucketing middleware and understand how it interacts with Snowflake's driver-specific SQL compilation.\"\\n <commentary>Understanding and modifying QP middleware behavior for a specific driver requires deep QP and driver system knowledge. Use the mbql-backend-expert agent.</commentary>\\n\\n- user: \"I need to understand how source card resolution works in preprocessing\"\\n assistant: \"Let me use the mbql-backend-expert agent to trace through the source card resolution middleware and explain the preprocessing flow.\"\\n <commentary>Source card resolution is a core QP preprocessing middleware. Use the mbql-backend-expert agent to explain and navigate it.</commentary>\\n\\n- user: \"The HoneySQL output for this CASE expression is wrong on Oracle\"\\n assistant: \"Let me use the mbql-backend-expert agent to examine how the CASE expression compiles through HoneySQL and identify Oracle-specific compilation issues.\"\\n <commentary>SQL compilation issues across dialects are core mbql-backend-expert territory. Use the agent to trace and fix the HoneySQL compilation.</commentary>"
model: opus
memory: project
---
@@ -24,9 +24,9 @@ You know that some middleware runs twice (joins, sandboxing, implicit clauses) b
### MBQL: Metabase's Query Language
-You are fluent in both MBQL 5 (pMBQL, produced by MLv2) and legacy MBQL v4. You understand:
+You are fluent in both MBQL 5 and legacy MBQL 4. You understand:
- The clause structure: filters, aggregations, breakouts, joins, expressions, custom columns, nested queries
-- How pMBQL references work (`:field` clauses with metadata maps vs. legacy integer field IDs)
+- How MBQL 5 references work (`:field` clauses with metadata maps vs. legacy integer field IDs)
- The conversion boundaries between v4 and v5
- Schema validation via Malli specs
@@ -56,10 +56,10 @@ You understand the reducible/transducer model:
- Cancellation propagation via `core.async` channels
- The reducing function chain for metadata, row transformation, and result accumulation
-### MLv2 and Metadata Providers
+### Lib and Metadata Providers
You understand:
-- MLv2 as the cross-platform (Clojure + ClojureScript) query construction library
+- Lib as the cross-platform (Clojure + ClojureScript) query construction library
- Protocol-based metadata providers (caching, composed, invocation trackers)
- How metadata filtering works (visibility, active status, permissions)
@@ -72,7 +72,7 @@ When investigating, you know to look in these areas:
- `src/metabase/driver/sql/` — SQL driver base, query processor for SQL
- `src/metabase/driver/sql_jdbc/` — JDBC-based driver base
- `src/metabase/driver/common/` — shared driver utilities
-- `src/metabase/lib/` — MLv2 library
+- `src/metabase/lib/` — Lib library
- `src/metabase/legacy_mbql/` — legacy MBQL schemas and normalization
- `src/metabase/models/` — data models (Field, Table, Database, Card)
- Database-specific drivers in `modules/drivers/`
@@ -129,7 +129,7 @@ When investigating, you know to look in these areas:
## Important Caveats You Know About
-- **Legacy vs. pMBQL:** The codebase is migrating from MBQL v4 to v5 (pMBQL). Some code paths still handle both. Be aware of which version you're working with.
+- **Legacy vs. MBQL 5:** The codebase is migrating from MBQL 4 to v5 (MBQL 5). Some code paths still handle both. Be aware of which version you're working with.
- **Middleware ordering matters:** Adding middleware in the wrong position can cause subtle bugs. Understand dependencies between middleware.
- **Driver hierarchy inheritance:** A fix at the `:sql` level affects ALL SQL databases. Be careful about assumptions that are only true for some dialects.
- **Lazy evaluation pitfalls:** In the QP, lazy sequences can cause issues with database connections being closed. Prefer eager evaluation (reducibles, transducers) in execution paths.
@@ -159,4 +159,4 @@ Examples of what to record:
- HoneySQL patterns used for complex clause compilation
- Test patterns and fixtures used for QP/driver testing
- Performance-sensitive code paths in the streaming execution model
-- Legacy MBQL vs. pMBQL conversion boundaries and gotchas
+- Legacy MBQL vs. MBQL 5 conversion boundaries and gotchas
diff --git a/.claude/skills/serdes-workflow/SKILL.md b/.claude/skills/serdes-workflow/SKILL.md
index dcc82bee984..796f5aeaa49 100644
--- a/.claude/skills/serdes-workflow/SKILL.md
+++ b/.claude/skills/serdes-workflow/SKILL.md
@@ -46,7 +46,7 @@ Validates YAML shapes against Malli schemas. Catches typos, missing required fie
```bash
clojure -M:run:ee --mode checker --checker cards --export /path/to/export-dir
```
-Validates that card queries resolve correctly against exported metadata (tables, fields, FKs). Uses MLv2 under the hood.
+Validates that card queries resolve correctly against exported metadata (tables, fields, FKs). Uses Lib under the hood.
Additional cards checker flags:
- `--lenient` - fabricate metadata on demand (when no database schema files exist)
diff --git a/dev/bot/common/metabase-patterns.md b/dev/bot/common/metabase-patterns.md
index 8e85d2fc900..11f78c8df41 100644
--- a/dev/bot/common/metabase-patterns.md
+++ b/dev/bot/common/metabase-patterns.md
@@ -35,7 +35,7 @@ Test and dev namespaces are available on the classpath — no manual setup neede
### Common Gotchas
-**Card creation**: Never use `t2/insert-returning-instance!` for `:model/Card` — it silently returns `nil` when internal pMBQL validation rejects the query format. Always use `./bin/mage -bot-api-call` or `mt/user-http-request` which give clear error messages on schema violations.
+**Card creation**: Never use `t2/insert-returning-instance!` for `:model/Card` — it silently returns `nil` when internal MBQL 5 validation rejects the query format. Always use `./bin/mage -bot-api-call` or `mt/user-http-request` which give clear error messages on schema violations.
**Feature flag toggle**: `(t2/update! :model/Setting :key "enable-pivoted-filters" {:value "true"})` — may return `0` even when successful. Always verify with `(t2/select-one :model/Setting :key "enable-pivoted-filters")`.
diff --git a/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj
index a2d70d2ef5e..8bea9a37f17 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj
@@ -120,11 +120,11 @@
"database_id should fall back to :query.database for notebook tools")
(is (= [] (:tables row)))))))
-(deftest construct-notebook-query-pmbql-test
- (testing "pMBQL is passed through to the frontend as-is, without conversion to legacy MBQL"
+(deftest construct-notebook-query-mbql5-test
+ (testing "MBQL 5 is passed through to the frontend as-is, without conversion to legacy MBQL"
(with-stubbed-tables! []
(fn []
- (let [pmbql {:lib/type "mbql/query"
+ (let [mbql5 {:lib/type "mbql/query"
:lib/metadata nil
:database 1
:stages [{:lib/type "mbql.stage/mbql"
@@ -139,10 +139,10 @@
{:type "tool-output"
:id "call-5"
:result {:structured-output {:query-id "qid-3"
- :query pmbql}}}]}])
+ :query mbql5}}}]}])
row (first rows)]
(is (= "notebook" (:query_type row)))
- (is (= pmbql (:mbql row)))
+ (is (= mbql5 (:mbql row)))
(is (= 1 (:database_id row))))))))
;;; ------------------------- filtered-out cases -------------------------
diff --git a/src/metabase/agent_lib/eval/invoke.clj b/src/metabase/agent_lib/eval/invoke.clj
index dc344662c2a..1264d0d7481 100644
--- a/src/metabase/agent_lib/eval/invoke.clj
+++ b/src/metabase/agent_lib/eval/invoke.clj
@@ -75,10 +75,10 @@
(invoke-helper! runtime path 'field args)))
(defn ensure-query-result!
- "Assert that structured evaluation produced a pMBQL query."
+ "Assert that structured evaluation produced a MBQL 5 query."
[result]
(when-not (mbql/query? result)
- (throw (ex-info "Structured program evaluation must return a pMBQL query map."
+ (throw (ex-info "Structured program evaluation must return a MBQL 5 query map."
{:result-type (type result)
:result result})))
result)
diff --git a/src/metabase/agent_lib/mbql_integration/common.clj b/src/metabase/agent_lib/mbql_integration/common.clj
index 125cdc42cb2..e7bed060096 100644
--- a/src/metabase/agent_lib/mbql_integration/common.clj
+++ b/src/metabase/agent_lib/mbql_integration/common.clj
@@ -8,7 +8,7 @@
(set! *warn-on-reflection* true)
(defn query?
- "Return true when `value` is a normalized pMBQL query map.
+ "Return true when `value` is a normalized MBQL 5 query map.
Agent-lib needs to distinguish queries from metadata maps, columns, and test
sentinels, but it should not inspect `:lib/type` directly. This helper keeps
@@ -26,7 +26,7 @@
(= :mbql/query (lib/normalized-query-type value))))
(defn extract-field-ids
- "Return the set of field ids referenced anywhere inside a pMBQL form.
+ "Return the set of field ids referenced anywhere inside a MBQL 5 form.
Agent-lib compares expressions, aggregations, breakouts, and columns
semantically. The canonical lib helper already knows how to walk MBQL safely,
diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.clj
index 9ffabeaf8c2..b495b5de50c 100644
--- a/src/metabase/metabot/tools/construct.clj
+++ b/src/metabase/metabot/tools/construct.clj
@@ -138,9 +138,9 @@
;;; ---------------------------------------- Result columns ----------------------------------------
(defn- result-columns-for-query
- "Generate result columns from a pMBQL query for LLM consumption."
- [pmbql-query metadata-provider]
- (let [query (lib/query metadata-provider pmbql-query)
+ "Generate result columns from a MBQL 5 query for LLM consumption."
+ [mbql5-query metadata-provider]
+ (let [query (lib/query metadata-provider mbql5-query)
cols (lib/returned-columns query)]
(mapv #(tools.u/->result-column query %) cols)))
@@ -154,11 +154,11 @@
(let [database-id (resolve-source-database-id source-entity)
mp (lib-be/application-database-metadata-provider database-id)
context (build-evaluation-context source-entity referenced-entities mp)
- pmbql-query (agent-lib/evaluate-program program mp context)
+ mbql5-query (agent-lib/evaluate-program program mp context)
query-id (u/generate-nano-id)]
{:structured-output {:query-id query-id
- :query pmbql-query
- :result-columns (result-columns-for-query pmbql-query mp)}
+ :query mbql5-query
+ :result-columns (result-columns-for-query mbql5-query mp)}
:instructions (instructions/query-created-instructions-for query-id)}))
;;; ---------------------------------------- Chart helpers ----------------------------------------
diff --git a/test/metabase/agent_lib/eval/invoke_test.clj b/test/metabase/agent_lib/eval/invoke_test.clj
index ad881918fd1..1042b0c3beb 100644
--- a/test/metabase/agent_lib/eval/invoke_test.clj
+++ b/test/metabase/agent_lib/eval/invoke_test.clj
@@ -43,11 +43,11 @@
(is (= [:current-query 42]
(eval.invoke/invoke-field-helper! runtime :current-query [:operations 0] [42])))))
-(deftest ensure-query-result-recognizes-pmbql-queries-test
+(deftest ensure-query-result-recognizes-mbql5-queries-test
(testing "non-query values are rejected"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"must return a pMBQL query map"
+ #"must return a MBQL 5 query map"
(eval.invoke/ensure-query-result! :not-a-query))))
(testing "queries pass through unchanged"
(let [query (agent-lib.tu/query-for-table :orders)]
diff --git a/test/metabase/metrics/api_arithmetic_test.clj b/test/metabase/metrics/api_arithmetic_test.clj
index 3d3f72756e9..74bc5d1a532 100644
--- a/test/metabase/metrics/api_arithmetic_test.clj
+++ b/test/metabase/metrics/api_arithmetic_test.clj
@@ -290,14 +290,14 @@
(testing "POST /api/metric/dataset with metric + measure arithmetic"
(let [mp (mt/metadata-provider)
table-metadata (lib.metadata/table mp (mt/id :venues))
- pmbql-def (-> (lib/query mp table-metadata)
+ mbql5-def (-> (lib/query mp table-metadata)
(lib/aggregate (lib/count)))]
(mt/with-temp [:model/Card metric {:name "Test Metric"
:type :metric
:dataset_query (mt/mbql-query venues {:aggregation [[:count]]})}
:model/Measure measure {:name "Test Measure"
:table_id (mt/id :venues)
- :definition pmbql-def}]
+ :definition mbql5-def}]
(mt/with-full-data-perms-for-all-users!
;; Sync dimensions for both metric and measure
(mt/user-http-request :rasta :get 200 (str "metric/" (:id metric)))