diff --git a/doc/user/content/sql/system-catalog/mz_internal.md b/doc/user/content/sql/system-catalog/mz_internal.md index b395ec32b0fe0..31d1670c2f11c 100644 --- a/doc/user/content/sql/system-catalog/mz_internal.md +++ b/doc/user/content/sql/system-catalog/mz_internal.md @@ -657,6 +657,30 @@ has no replicas — needs operator action." | `schema` | [`jsonb`]| JSON Schema describing the object's columns and types. | | `hydration` | [`jsonb`]| Readiness summary as a JSON object with `hydrated` (bool), `replica_count` (int), and `hydrated_replica_count` (int). `hydrated` is true only when the cluster has at least one replica and the dataflow is hydrated on every replica. Reads against a non-hydrated data product block until the dataflow catches up (they never return partial data). Check this before reading: if `hydrated` is false and `replica_count > 0`, wait and retry; if `replica_count` is 0, the cluster has no replicas and that needs operator action, not a retry. A null `cluster` means your role lacks USAGE on that cluster, and only materialized views appear that way, since a view is listed only with an index on a cluster you can use. A read of such a row runs on another cluster and returns the materialized view's stored results, so `hydration` then reports the materialized view's own cluster, the one that keeps those results current. | +## `mz_mcp_data_product_reads` + +The `mz_mcp_data_product_reads` view lists each data product from +[`mz_mcp_data_products`](#mz_mcp_data_products) once, with its columns and the +one place the MCP server picks to read it. The server picks the cluster your +role can use with an index on the object, preferring an index that is +hydrated, then one that is hydrating. A materialized view with no such index, +or whose only usable index is on a cluster with no replicas, reads its stored +results from any cluster. Ties break by cluster name. The `/api/mcp` route's +`list_data_products` and `get_data_product_details` tools read this view. + + +| Field | Type | Meaning | +| ----------------------- | ------------- | ------- | +| `object_name` | [`text`] | Fully qualified object name (database.schema.name). | +| `object_type` | [`text`] | `materialized-view` or `view`. | +| `description` | [`text`] | Object comment, otherwise the comment of an index on `cluster`. | +| `columns` | [`jsonb`] | Each column's JSON Schema type and comment, keyed by column name. | +| `cluster` | [`text`] | Cluster to read the object on: the cluster you can use with an index on the object, preferring a hydrated index. Null for a materialized view with no such index: run the read on any cluster, and it returns the stored results. | +| `uses_index` | [`boolean`] | Whether a read on `cluster` is served from an index. | +| `indexed_columns` | [`jsonb`] | Key columns of the indexes on `cluster`, as a JSON array. Empty when `uses_index` is false. | +| `hydration` | [`jsonb`] | Readiness of the read as a JSON object with `hydrated` (bool), `replica_count` (int), and `hydrated_replica_count` (int). It describes the indexes on `cluster`, or, when `cluster` is null, the materialized view's own cluster, which keeps the stored results current. `hydrated` is true only when that cluster has at least one replica and every replica is hydrated. A read that is not hydrated blocks until it is. If `replica_count` is 0, the read cannot make progress until a replica is added. | +| `faster_read_available` | [`boolean`] | True when the read does not use an index, but an index on the object exists on a cluster your role cannot use. | + ## `mz_object_dependencies` The `mz_object_dependencies` materialized view describes the dependency diff --git a/src/catalog/src/builtin.rs b/src/catalog/src/builtin.rs index 49b9504de623b..e93da9bf996e6 100644 --- a/src/catalog/src/builtin.rs +++ b/src/catalog/src/builtin.rs @@ -1555,6 +1555,7 @@ pub static BUILTINS_STATIC: LazyLock>> = LazyLock::ne Builtin::View(&MZ_INDEX_ADVICE), Builtin::View(&MZ_MCP_DATA_PRODUCTS), Builtin::View(&MZ_MCP_DATA_PRODUCT_DETAILS), + Builtin::View(&MZ_MCP_DATA_PRODUCT_READS), Builtin::Table(&MZ_OBJECT_HYDRATION_HISTORY), Builtin::Table(&MZ_REPLICA_HYDRATION_HISTORY), ]; diff --git a/src/catalog/src/builtin/mz_internal.rs b/src/catalog/src/builtin/mz_internal.rs index 7b8292aebfbc9..4fec445d61ef9 100644 --- a/src/catalog/src/builtin/mz_internal.rs +++ b/src/catalog/src/builtin/mz_internal.rs @@ -7374,6 +7374,262 @@ LEFT JOIN hydration h ontology: None, }); +/// One row per MCP data product, with the single place the server picks to +/// read it from. Used by the `/api/mcp` route's `list_data_products` and +/// `get_data_product_details` tools. +pub static MZ_MCP_DATA_PRODUCT_READS: LazyLock = LazyLock::new(|| BuiltinView { + name: "mz_mcp_data_product_reads", + schema: MZ_INTERNAL_SCHEMA, + oid: oid::VIEW_MZ_MCP_DATA_PRODUCT_READS_OID, + desc: RelationDesc::builder() + .with_column("object_name", SqlScalarType::String.nullable(false)) + .with_column("object_type", SqlScalarType::String.nullable(false)) + .with_column("description", SqlScalarType::String.nullable(true)) + .with_column("columns", SqlScalarType::Jsonb.nullable(false)) + .with_column("cluster", SqlScalarType::String.nullable(true)) + .with_column("uses_index", SqlScalarType::Bool.nullable(false)) + .with_column("indexed_columns", SqlScalarType::Jsonb.nullable(false)) + .with_column("hydration", SqlScalarType::Jsonb.nullable(false)) + .with_column("faster_read_available", SqlScalarType::Bool.nullable(false)) + .finish(), + column_comments: BTreeMap::from_iter([ + ( + "object_name", + "Fully qualified object name (database.schema.name).", + ), + ("object_type", "`materialized-view` or `view`."), + ( + "description", + "Object comment, otherwise the comment of an index on `cluster`.", + ), + ( + "columns", + "Each column's JSON Schema type and comment, keyed by column name.", + ), + ( + "cluster", + "Cluster to read the object on: the cluster you can use with an index on the object, preferring a hydrated index. Null for a materialized view with no such index: run the read on any cluster, and it returns the stored results.", + ), + ( + "uses_index", + "Whether a read on `cluster` is served from an index.", + ), + ( + "indexed_columns", + "Key columns of the indexes on `cluster`, as a JSON array. Empty when `uses_index` is false.", + ), + ( + "hydration", + "Readiness of the read as a JSON object with `hydrated` (bool), `replica_count` (int), and `hydrated_replica_count` (int). It describes the indexes on `cluster`, or, when `cluster` is null, the materialized view's own cluster, which keeps the stored results current. `hydrated` is true only when that cluster has at least one replica and every replica is hydrated. A read that is not hydrated blocks until it is. If `replica_count` is 0, the read cannot make progress until a replica is added.", + ), + ( + "faster_read_available", + "True when the read does not use an index, but an index on the object exists on a cluster your role cannot use.", + ), + ]), + // The object set and privilege checks match `mz_mcp_data_products`. A view + // is readable only through an index on a cluster the role can use, since a + // read anywhere else recomputes it. A materialized view also reads its + // stored results from any cluster. + sql: r#" +WITH products AS ( + SELECT DISTINCT + '"' || op.database || '"."' || op.schema || '"."' || op.name || '"' AS object_name, + o.id AS object_id, + o.type AS object_type, + o.cluster_id AS object_cluster_id + FROM mz_internal.mz_show_my_object_privileges op + JOIN mz_objects o ON op.name = o.name AND op.object_type = o.type + JOIN mz_schemas s ON s.name = op.schema AND s.id = o.schema_id + JOIN mz_databases d ON d.name = op.database AND d.id = s.database_id + WHERE op.privilege_type = 'SELECT' + AND o.type IN ('materialized-view', 'view') + AND s.name NOT IN ('mz_catalog', 'mz_internal', 'pg_catalog', 'information_schema', 'mz_introspection') +), +indexes AS ( + SELECT + p.object_id, + i.id AS index_id, + i.cluster_id, + EXISTS ( + SELECT 1 FROM mz_internal.mz_show_my_cluster_privileges cp + WHERE cp.name = c.name AND cp.privilege_type = 'USAGE' + ) AS usable + FROM products p + JOIN mz_indexes i ON i.on_id = p.object_id + JOIN mz_clusters c ON c.id = i.cluster_id +), +-- Each place a read can run, keyed by (object_id, uses_index, cluster_id), +-- with the dataflows the read waits on there. +read_dataflows AS ( + SELECT object_id, true AS uses_index, cluster_id, index_id AS dataflow_id + FROM indexes + WHERE usable + UNION ALL + SELECT object_id, false, object_cluster_id, object_id + FROM products + WHERE object_type = 'materialized-view' +), +read_replicas AS ( + SELECT + rd.object_id, + rd.uses_index, + rd.cluster_id, + r.id AS replica_id, + bool_and(COALESCE(h.hydrated, false)) AS hydrated + FROM read_dataflows rd + JOIN mz_cluster_replicas r ON r.cluster_id = rd.cluster_id + LEFT JOIN mz_internal.mz_hydration_statuses h + ON h.object_id = rd.dataflow_id AND h.replica_id = r.id + GROUP BY 1, 2, 3, 4 +), +reads AS ( + SELECT + rd.object_id, + rd.uses_index, + rd.cluster_id, + COUNT(rr.replica_id)::int AS replica_count, + COUNT(rr.replica_id) FILTER (WHERE rr.hydrated)::int AS hydrated_replica_count + FROM (SELECT DISTINCT object_id, uses_index, cluster_id FROM read_dataflows) rd + LEFT JOIN read_replicas rr + ON rr.object_id = rd.object_id + AND rr.uses_index = rd.uses_index + AND rr.cluster_id = rd.cluster_id + GROUP BY 1, 2, 3 +), +-- A hydrated index beats a hydrating one, and either beats the stored +-- results of a materialized view. An index on a cluster with no replicas +-- never makes progress, so it comes last. Ties break by cluster name. +picked AS ( + SELECT DISTINCT ON (r.object_id) + r.object_id, + r.uses_index, + r.cluster_id, + CASE WHEN r.uses_index THEN c.name END AS cluster, + r.replica_count, + r.hydrated_replica_count + FROM reads r + JOIN mz_clusters c ON c.id = r.cluster_id + ORDER BY + r.object_id, + CASE + WHEN r.uses_index AND r.replica_count > 0 AND r.hydrated_replica_count = r.replica_count THEN 1 + WHEN r.uses_index AND r.replica_count > 0 THEN 2 + WHEN NOT r.uses_index THEN 3 + ELSE 4 + END, + c.name +), +indexed_columns AS ( + SELECT object_id, cluster_id, jsonb_agg(name ORDER BY position, name) AS indexed_columns + FROM ( + SELECT i.object_id, i.cluster_id, col.name, min(ic.index_position) AS position + FROM indexes i + JOIN mz_index_columns ic ON ic.index_id = i.index_id + JOIN mz_columns col ON col.id = i.object_id AND col.position = ic.on_position + WHERE i.usable + GROUP BY 1, 2, 3 + ) + GROUP BY 1, 2 +), +index_comments AS ( + SELECT i.object_id, i.cluster_id, min(cm.comment) AS comment + FROM indexes i + JOIN mz_internal.mz_comments cm + ON cm.id = i.index_id AND cm.object_type = 'index' AND cm.object_sub_id IS NULL + WHERE i.usable + GROUP BY 1, 2 +), +columns AS ( + SELECT + p.object_id, + jsonb_strip_nulls(jsonb_object_agg( + col.name, + CASE + WHEN col.type IN ( + 'uint2', 'uint4','uint8', 'int', 'integer', 'smallint', + 'double', 'double precision', 'bigint', 'float', + 'numeric', 'real' + ) THEN jsonb_build_object( + 'type', 'number', + 'description', cm.comment + ) + WHEN col.type = 'boolean' THEN jsonb_build_object( + 'type', 'boolean', + 'description', cm.comment + ) + WHEN col.type = 'bytea' THEN jsonb_build_object( + 'type', 'string', + 'description', cm.comment, + 'contentEncoding', 'base64', + 'contentMediaType', 'application/octet-stream' + ) + WHEN col.type = 'date' THEN jsonb_build_object( + 'type', 'string', + 'format', 'date', + 'description', cm.comment + ) + WHEN col.type = 'time' THEN jsonb_build_object( + 'type', 'string', + 'format', 'time', + 'description', cm.comment + ) + WHEN col.type ilike 'timestamp%' THEN jsonb_build_object( + 'type', 'string', + 'format', 'date-time', + 'description', cm.comment + ) + WHEN col.type = 'jsonb' THEN jsonb_build_object( + 'type', 'object', + 'description', cm.comment + ) + WHEN col.type = 'uuid' THEN jsonb_build_object( + 'type', 'string', + 'format', 'uuid', + 'description', cm.comment + ) + ELSE jsonb_build_object( + 'type', 'string', + 'description', cm.comment + ) + END + )) AS columns + FROM products p + JOIN mz_columns col ON col.id = p.object_id + LEFT JOIN mz_internal.mz_comments cm + ON cm.id = p.object_id AND cm.object_type = p.object_type AND cm.object_sub_id = col.position + GROUP BY 1 +) +SELECT + p.object_name, + p.object_type, + COALESCE(oc.comment, ixc.comment) AS description, + COALESCE(cols.columns, '{}'::jsonb) AS columns, + pk.cluster, + pk.uses_index, + COALESCE(ixcol.indexed_columns, '[]'::jsonb) AS indexed_columns, + jsonb_build_object( + 'hydrated', pk.replica_count > 0 AND pk.hydrated_replica_count = pk.replica_count, + 'replica_count', pk.replica_count, + 'hydrated_replica_count', pk.hydrated_replica_count + ) AS hydration, + NOT pk.uses_index AND EXISTS ( + SELECT 1 FROM indexes i WHERE i.object_id = p.object_id AND NOT i.usable + ) AS faster_read_available +FROM products p +JOIN picked pk ON pk.object_id = p.object_id +LEFT JOIN mz_internal.mz_comments oc + ON oc.id = p.object_id AND oc.object_type = p.object_type AND oc.object_sub_id IS NULL +LEFT JOIN columns cols ON cols.object_id = p.object_id +LEFT JOIN indexed_columns ixcol + ON pk.uses_index AND ixcol.object_id = pk.object_id AND ixcol.cluster_id = pk.cluster_id +LEFT JOIN index_comments ixc + ON pk.uses_index AND ixc.object_id = pk.object_id AND ixc.cluster_id = pk.cluster_id +"#, + access: vec![PUBLIC_SELECT], + ontology: None, +}); + pub static MZ_SHOW_ROLE_MEMBERS: LazyLock = LazyLock::new(|| BuiltinView { name: "mz_show_role_members", schema: MZ_INTERNAL_SCHEMA, diff --git a/src/environmentd/src/http/mcp.rs b/src/environmentd/src/http/mcp.rs index c857ed89e1a8a..956342efaf6e7 100644 --- a/src/environmentd/src/http/mcp.rs +++ b/src/environmentd/src/http/mcp.rs @@ -27,7 +27,8 @@ //! dyncfg-gated. `list_data_products` and `get_data_products` are one tool, and //! either name works on the agent route and on `/api/mcp`. //! -//! Data products are discovered via `mz_internal.mz_mcp_data_products` system view. +//! The agent route discovers data products through `mz_internal.mz_mcp_data_products`, +//! and `/api/mcp` through `mz_internal.mz_mcp_data_product_reads`. use std::borrow::Cow; use std::collections::BTreeSet; @@ -316,6 +317,12 @@ fn decode_mcp_name(value: &str) -> Result, McpRequestError> { const DISCOVERY_QUERY: &str = "SELECT * FROM mz_internal.mz_mcp_data_products"; const DETAILS_QUERY_PREFIX: &str = "SELECT * FROM mz_internal.mz_mcp_data_product_details WHERE object_name = "; +const UNIFIED_DISCOVERY_QUERY: &str = "SELECT object_name, cluster, description \ + FROM mz_internal.mz_mcp_data_product_reads ORDER BY object_name"; +/// Selects the columns of [`DataProductReadRow`], in its field order. +const UNIFIED_DETAILS_QUERY_PREFIX: &str = "SELECT object_name, object_type, description, \ + columns, cluster, uses_index, indexed_columns, hydration, faster_read_available \ + FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = "; /// MCP request errors, mapped to JSON-RPC error codes. #[derive(Debug, Error)] @@ -1524,13 +1531,14 @@ You are connected to the Materialize MCP server. It reads data products and the Tools: - list_data_products: the data products your role can read. Start here. -- get_data_product_details: one data product's columns, its cluster, and its `hydration`. +- get_data_product_details: one data product's columns and how to read it. - query: read-only SELECT, SHOW, or EXPLAIN against anything your role can read, data products and system catalog alike. Reading data products: -- Pass the `cluster` from get_data_product_details to query, so indexed reads hit the arrangement. Prefer indexed objects (served from memory) over unindexed materialized views (read from persistent storage). -- A null `cluster` means your role lacks USAGE on that cluster. Omit `cluster`, and the query runs on your session's default cluster. -- Check `hydration` before reading. Reads never return partial data: a read of a product that is not hydrated blocks until the dataflow catches up, and may hit the request timeout. If `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the cluster has no replicas and the read cannot make progress until one is added. +- Pass `read.cluster` from get_data_product_details to query. It is the cluster where the read uses an index. +- A null `read.cluster` means the product is a materialized view with no index your role can use. Omit `cluster`, and the query returns its stored results. +- Check `read.hydration` before reading. Reads never return partial data: a read of a product that is not hydrated blocks until the dataflow catches up, and may hit the request timeout. If `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the cluster has no replicas and the read cannot make progress until one is added. +- `read.faster_read_available` means an index on the product exists on a cluster your role cannot use. If reads are slow, an admin can grant access to it. System catalog: - Write system object names with their schema, for example mz_internal.mz_source_statuses. query does not add the system schemas to the search path. @@ -1589,6 +1597,8 @@ fn handle_discover( const DATA_PRODUCTS_DESCRIPTION: &str = "Discover all available real-time data views (data products) that represent business entities like customers, orders, products, etc. Each data product provides fresh, queryable data with defined schemas. Use this first to see what data is available before querying specific information."; +const UNIFIED_DATA_PRODUCT_DETAILS_DESCRIPTION: &str = "Get one data product's columns and how to read it. `read.cluster` is the cluster to pass to `query`: the cluster your role can use with an index on the product, preferring a hydrated index. A null `read.cluster` means the product is a materialized view with no index your role can use: omit `cluster`, and the query returns its stored results. `read.uses_index` says whether the read is served from an index. `read.hydration` (`{hydrated, replica_count, hydrated_replica_count}`) describes that read. Reads never return partial data: if `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the read cannot make progress until a replica is added. `read.faster_read_available` is true when an index on the product exists on a cluster your role cannot use."; + const DATA_PRODUCT_DETAILS_DESCRIPTION: &str = "Get the complete schema and structure of a specific data product, plus a `hydration` object reporting whether the dataflow is ready across the cluster's replicas (`{hydrated, replica_count, hydrated_replica_count}`). This shows you exactly what fields are available, their types, and what data you can query. Reads never return partial data, so check `hydration` before reading: if `hydrated` is false and `replica_count` is greater than 0 the dataflow is still warming up (a read would block until it catches up, possibly hitting the request timeout), so wait and retry; if `replica_count` is 0 the cluster has no replicas and the read cannot make progress until one is added."; fn handle_tools_list( @@ -1699,7 +1709,7 @@ fn handle_tools_list( ToolDefinition { name: "get_data_product_details".to_string(), title: Some("Get Data Product Details".to_string()), - description: DATA_PRODUCT_DETAILS_DESCRIPTION.to_string(), + description: UNIFIED_DATA_PRODUCT_DETAILS_DESCRIPTION.to_string(), input_schema: json!({ "type": "object", "properties": { @@ -1809,14 +1819,18 @@ async fn handle_tools_call( let mut guard = ToolCallGuard::new(metrics, endpoint_type.as_label(), tool_name.to_string()); let result = match (endpoint_type, params) { - ( - McpEndpointType::Agent | McpEndpointType::Unified, - ToolsCallParams::GetDataProducts(_), - ) => get_data_products(client, max_response_size).await, - ( - McpEndpointType::Agent | McpEndpointType::Unified, - ToolsCallParams::GetDataProductDetails(p), - ) => get_data_product_details(client, &p.name, max_response_size).await, + (McpEndpointType::Agent, ToolsCallParams::GetDataProducts(_)) => { + get_data_products(client, max_response_size).await + } + (McpEndpointType::Agent, ToolsCallParams::GetDataProductDetails(p)) => { + get_data_product_details(client, &p.name, max_response_size).await + } + (McpEndpointType::Unified, ToolsCallParams::GetDataProducts(_)) => { + list_data_products(client, max_response_size).await + } + (McpEndpointType::Unified, ToolsCallParams::GetDataProductDetails(p)) => { + get_data_product_read_details(client, &p.name, max_response_size).await + } (McpEndpointType::Agent, ToolsCallParams::ReadDataProduct(_)) if !read_data_product_tool_enabled => { @@ -1955,7 +1969,11 @@ fn format_rows_response( // verbatim, so the outer array is pretty-printed while rows stay compact. let text = serde_json::to_string_pretty(&rows).map_err(|e| McpRequestError::Internal(anyhow!(e)))?; + text_response(text, max_size) +} +/// Wraps `text` as tool content, failing when it exceeds `max_size` bytes. +fn text_response(text: String, max_size: usize) -> Result { if text.len() > max_size { return Err(McpRequestError::QueryExecutionFailed(format!( "Response size ({} bytes) exceeds the {} byte limit. \ @@ -2003,6 +2021,106 @@ async fn get_data_product_details( format_rows_response(rows, max_response_size) } +async fn list_data_products( + client: &mut AuthedClient, + max_response_size: usize, +) -> Result { + let rows = execute_sql(client, UNIFIED_DISCOVERY_QUERY).await?; + format_rows_response(rows, max_response_size) +} + +/// A row of [`UNIFIED_DETAILS_QUERY_PREFIX`]. +#[derive(Deserialize)] +struct DataProductReadRow( + String, + String, + Option, + serde_json::Value, + Option, + bool, + serde_json::Value, + serde_json::Value, + bool, +); + +/// The `/api/mcp` shape of `get_data_product_details`. +#[derive(Serialize)] +struct DataProductReadDetails { + name: String, + #[serde(rename = "type")] + object_type: String, + #[serde(skip_serializing_if = "Option::is_none")] + description: Option, + columns: serde_json::Value, + read: DataProductRead, +} + +#[derive(Serialize)] +struct DataProductRead { + cluster: Option, + uses_index: bool, + indexed_columns: serde_json::Value, + hydration: serde_json::Value, + faster_read_available: bool, +} + +impl From for DataProductReadDetails { + fn from(row: DataProductReadRow) -> Self { + let DataProductReadRow( + name, + object_type, + description, + columns, + cluster, + uses_index, + indexed_columns, + hydration, + faster_read_available, + ) = row; + DataProductReadDetails { + name, + object_type, + description, + columns, + read: DataProductRead { + cluster, + uses_index, + indexed_columns, + hydration, + faster_read_available, + }, + } + } +} + +async fn get_data_product_read_details( + client: &mut AuthedClient, + name: &str, + max_response_size: usize, +) -> Result { + let query = format!( + "{}{}", + UNIFIED_DETAILS_QUERY_PREFIX, + escaped_string_literal(name) + ); + let rows = execute_sql(client, &query).await?; + let row = match rows.as_slice() { + [] => return Err(McpRequestError::DataProductNotFound(name.to_string())), + [row] => row, + _ => { + return Err(McpRequestError::Internal(anyhow!( + "mz_mcp_data_product_reads returned {} rows for {name}", + rows.len() + ))); + } + }; + let row: DataProductReadRow = + serde_json::from_str(row.get()).map_err(|e| McpRequestError::Internal(anyhow!(e)))?; + let text = serde_json::to_string_pretty(&DataProductReadDetails::from(row)) + .map_err(|e| McpRequestError::Internal(anyhow!(e)))?; + text_response(text, max_response_size) +} + /// Parses a data product name and returns it safely quoted for SQL interpolation. /// /// Uses the SQL parser to validate the name as an `UnresolvedItemName`, then diff --git a/src/environmentd/tests/testdata/mcp/unified b/src/environmentd/tests/testdata/mcp/unified index 9db3dfddb0258..a642485c1a932 100644 --- a/src/environmentd/tests/testdata/mcp/unified +++ b/src/environmentd/tests/testdata/mcp/unified @@ -12,7 +12,7 @@ mcp-unified {"jsonrpc":"2.0","id":1,"method":"tools/list"} ---- 200 OK -{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"list_data_products","title":"List Data Products","description":"Discover all available real-time data views (data products) that represent business entities like customers, orders, products, etc. Each data product provides fresh, queryable data with defined schemas. Use this first to see what data is available before querying specific information.","inputSchema":{"type":"object","properties":{},"required":[]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}},{"name":"get_data_product_details","title":"Get Data Product Details","description":"Get the complete schema and structure of a specific data product, plus a `hydration` object reporting whether the dataflow is ready across the cluster's replicas (`{hydrated, replica_count, hydrated_replica_count}`). This shows you exactly what fields are available, their types, and what data you can query. Reads never return partial data, so check `hydration` before reading: if `hydrated` is false and `replica_count` is greater than 0 the dataflow is still warming up (a read would block until it catches up, possibly hitting the request timeout), so wait and retry; if `replica_count` is 0 the cluster has no replicas and the read cannot make progress until one is added.","inputSchema":{"type":"object","properties":{"name":{"type":"string","description":"Exact name of the data product from list_data_products() list"}},"required":["name"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}},{"name":"query","title":"Query","description":"Execute a read-only SQL query (SELECT, SHOW, or EXPLAIN) against any object the role can access, including data products and the system catalog. Pass the cluster from the data product details so indexed reads hit the arrangement. Omit `cluster` to run on your session's default cluster, for example when the data product's cluster is null. Write system object names in full, such as `mz_catalog.mz_clusters`. Response limit: 1.0 MB.","inputSchema":{"type":"object","properties":{"cluster":{"type":"string","description":"Exact cluster name the query should run on, such as the cluster from the data product details. Omit it to run on your session's default cluster. EXPLAIN ANALYZE needs a cluster."},"cluster_replica":{"type":"string","description":"Optional replica name (e.g. 'r1') to target one replica of `cluster`. Requires `cluster`. Required for EXPLAIN ANALYZE on clusters with more than one replica. Find replica names in mz_catalog.mz_cluster_replicas."},"sql_query":{"type":"string","description":"PostgreSQL-compatible SELECT, SHOW, or EXPLAIN statement. Use the fully qualified data product name exactly as provided (with double quotes). Multi-statement queries are rejected."}},"required":["sql_query"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}}]}} +{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"list_data_products","title":"List Data Products","description":"Discover all available real-time data views (data products) that represent business entities like customers, orders, products, etc. Each data product provides fresh, queryable data with defined schemas. Use this first to see what data is available before querying specific information.","inputSchema":{"type":"object","properties":{},"required":[]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}},{"name":"get_data_product_details","title":"Get Data Product Details","description":"Get one data product's columns and how to read it. `read.cluster` is the cluster to pass to `query`: the cluster your role can use with an index on the product, preferring a hydrated index. A null `read.cluster` means the product is a materialized view with no index your role can use: omit `cluster`, and the query returns its stored results. `read.uses_index` says whether the read is served from an index. `read.hydration` (`{hydrated, replica_count, hydrated_replica_count}`) describes that read. Reads never return partial data: if `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the read cannot make progress until a replica is added. `read.faster_read_available` is true when an index on the product exists on a cluster your role cannot use.","inputSchema":{"type":"object","properties":{"name":{"type":"string","description":"Exact name of the data product from list_data_products() list"}},"required":["name"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}},{"name":"query","title":"Query","description":"Execute a read-only SQL query (SELECT, SHOW, or EXPLAIN) against any object the role can access, including data products and the system catalog. Pass the cluster from the data product details so indexed reads hit the arrangement. Omit `cluster` to run on your session's default cluster, for example when the data product's cluster is null. Write system object names in full, such as `mz_catalog.mz_clusters`. Response limit: 1.0 MB.","inputSchema":{"type":"object","properties":{"cluster":{"type":"string","description":"Exact cluster name the query should run on, such as the cluster from the data product details. Omit it to run on your session's default cluster. EXPLAIN ANALYZE needs a cluster."},"cluster_replica":{"type":"string","description":"Optional replica name (e.g. 'r1') to target one replica of `cluster`. Requires `cluster`. Required for EXPLAIN ANALYZE on clusters with more than one replica. Find replica names in mz_catalog.mz_cluster_replicas."},"sql_query":{"type":"string","description":"PostgreSQL-compatible SELECT, SHOW, or EXPLAIN statement. Use the fully qualified data product name exactly as provided (with double quotes). Multi-statement queries are rejected."}},"required":["sql_query"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}}]}} mcp-unified {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_data_products"}} @@ -102,7 +102,7 @@ mcp-unified {"jsonrpc":"2.0","id":14,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"test","version":"0"}}} ---- 200 OK -{"jsonrpc":"2.0","id":14,"result":{"protocolVersion":"2025-11-25","capabilities":{"tools":{}},"serverInfo":{"name":"materialize-mcp","version":""},"instructions":"You are connected to the Materialize MCP server. It reads data products and the system catalog.\n\nTools:\n- list_data_products: the data products your role can read. Start here.\n- get_data_product_details: one data product's columns, its cluster, and its `hydration`.\n- query: read-only SELECT, SHOW, or EXPLAIN against anything your role can read, data products and system catalog alike.\n\nReading data products:\n- Pass the `cluster` from get_data_product_details to query, so indexed reads hit the arrangement. Prefer indexed objects (served from memory) over unindexed materialized views (read from persistent storage).\n- A null `cluster` means your role lacks USAGE on that cluster. Omit `cluster`, and the query runs on your session's default cluster.\n- Check `hydration` before reading. Reads never return partial data: a read of a product that is not hydrated blocks until the dataflow catches up, and may hit the request timeout. If `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the cluster has no replicas and the read cannot make progress until one is added.\n\nSystem catalog:\n- Write system object names with their schema, for example mz_internal.mz_source_statuses. query does not add the system schemas to the search path.\n- Before writing catalog queries, find tables, columns, and join paths in mz_internal.mz_ontology_entity_types, mz_internal.mz_ontology_link_types, mz_internal.mz_ontology_properties, and mz_internal.mz_ontology_semantic_types.\n- mz_introspection relations (for example mz_introspection.mz_dataflow_arrangement_sizes) are cluster-scoped: query them with `cluster` set, and `cluster_replica` on a cluster with more than one replica.\n- mz_internal.mz_source_statuses and mz_internal.mz_sink_statuses use `last_status_change_at`, not `updated_at`.\n- mz_internal.mz_cluster_replica_utilization only has `replica_id`: join mz_catalog.mz_cluster_replicas and mz_catalog.mz_clusters for names.\n- mz_introspection.mz_dataflow_arrangement_sizes.id and mz_introspection.mz_dataflows.id are dataflow ids (uint8), not catalog ids: join through mz_introspection.mz_compute_exports (dataflow_id to export_id), or match the name column, which reads Dataflow: ..."}} +{"jsonrpc":"2.0","id":14,"result":{"protocolVersion":"2025-11-25","capabilities":{"tools":{}},"serverInfo":{"name":"materialize-mcp","version":""},"instructions":"You are connected to the Materialize MCP server. It reads data products and the system catalog.\n\nTools:\n- list_data_products: the data products your role can read. Start here.\n- get_data_product_details: one data product's columns and how to read it.\n- query: read-only SELECT, SHOW, or EXPLAIN against anything your role can read, data products and system catalog alike.\n\nReading data products:\n- Pass `read.cluster` from get_data_product_details to query. It is the cluster where the read uses an index.\n- A null `read.cluster` means the product is a materialized view with no index your role can use. Omit `cluster`, and the query returns its stored results.\n- Check `read.hydration` before reading. Reads never return partial data: a read of a product that is not hydrated blocks until the dataflow catches up, and may hit the request timeout. If `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the cluster has no replicas and the read cannot make progress until one is added.\n- `read.faster_read_available` means an index on the product exists on a cluster your role cannot use. If reads are slow, an admin can grant access to it.\n\nSystem catalog:\n- Write system object names with their schema, for example mz_internal.mz_source_statuses. query does not add the system schemas to the search path.\n- Before writing catalog queries, find tables, columns, and join paths in mz_internal.mz_ontology_entity_types, mz_internal.mz_ontology_link_types, mz_internal.mz_ontology_properties, and mz_internal.mz_ontology_semantic_types.\n- mz_introspection relations (for example mz_introspection.mz_dataflow_arrangement_sizes) are cluster-scoped: query them with `cluster` set, and `cluster_replica` on a cluster with more than one replica.\n- mz_internal.mz_source_statuses and mz_internal.mz_sink_statuses use `last_status_change_at`, not `updated_at`.\n- mz_internal.mz_cluster_replica_utilization only has `replica_id`: join mz_catalog.mz_cluster_replicas and mz_catalog.mz_clusters for names.\n- mz_introspection.mz_dataflow_arrangement_sizes.id and mz_introspection.mz_dataflows.id are dataflow ids (uint8), not catalog ids: join through mz_introspection.mz_compute_exports (dataflow_id to export_id), or match the name column, which reads Dataflow: ..."}} mcp-unified MCP-Protocol-Version: 2026-07-28 @@ -110,7 +110,7 @@ Mcp-Method: server/discover {"jsonrpc":"2.0","id":15,"method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}} ---- 200 OK -{"jsonrpc":"2.0","id":15,"result":{"supportedVersions":["2026-07-28","2025-11-25"],"capabilities":{"tools":{}},"instructions":"You are connected to the Materialize MCP server. It reads data products and the system catalog.\n\nTools:\n- list_data_products: the data products your role can read. Start here.\n- get_data_product_details: one data product's columns, its cluster, and its `hydration`.\n- query: read-only SELECT, SHOW, or EXPLAIN against anything your role can read, data products and system catalog alike.\n\nReading data products:\n- Pass the `cluster` from get_data_product_details to query, so indexed reads hit the arrangement. Prefer indexed objects (served from memory) over unindexed materialized views (read from persistent storage).\n- A null `cluster` means your role lacks USAGE on that cluster. Omit `cluster`, and the query runs on your session's default cluster.\n- Check `hydration` before reading. Reads never return partial data: a read of a product that is not hydrated blocks until the dataflow catches up, and may hit the request timeout. If `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the cluster has no replicas and the read cannot make progress until one is added.\n\nSystem catalog:\n- Write system object names with their schema, for example mz_internal.mz_source_statuses. query does not add the system schemas to the search path.\n- Before writing catalog queries, find tables, columns, and join paths in mz_internal.mz_ontology_entity_types, mz_internal.mz_ontology_link_types, mz_internal.mz_ontology_properties, and mz_internal.mz_ontology_semantic_types.\n- mz_introspection relations (for example mz_introspection.mz_dataflow_arrangement_sizes) are cluster-scoped: query them with `cluster` set, and `cluster_replica` on a cluster with more than one replica.\n- mz_internal.mz_source_statuses and mz_internal.mz_sink_statuses use `last_status_change_at`, not `updated_at`.\n- mz_internal.mz_cluster_replica_utilization only has `replica_id`: join mz_catalog.mz_cluster_replicas and mz_catalog.mz_clusters for names.\n- mz_introspection.mz_dataflow_arrangement_sizes.id and mz_introspection.mz_dataflows.id are dataflow ids (uint8), not catalog ids: join through mz_introspection.mz_compute_exports (dataflow_id to export_id), or match the name column, which reads Dataflow: ...","resultType":"complete","ttlMs":60000,"cacheScope":"private","_meta":{"io.modelcontextprotocol/serverInfo":{"name":"materialize-mcp","version":""}}}} +{"jsonrpc":"2.0","id":15,"result":{"supportedVersions":["2026-07-28","2025-11-25"],"capabilities":{"tools":{}},"instructions":"You are connected to the Materialize MCP server. It reads data products and the system catalog.\n\nTools:\n- list_data_products: the data products your role can read. Start here.\n- get_data_product_details: one data product's columns and how to read it.\n- query: read-only SELECT, SHOW, or EXPLAIN against anything your role can read, data products and system catalog alike.\n\nReading data products:\n- Pass `read.cluster` from get_data_product_details to query. It is the cluster where the read uses an index.\n- A null `read.cluster` means the product is a materialized view with no index your role can use. Omit `cluster`, and the query returns its stored results.\n- Check `read.hydration` before reading. Reads never return partial data: a read of a product that is not hydrated blocks until the dataflow catches up, and may hit the request timeout. If `hydrated` is false and `replica_count` is greater than 0, wait and retry. If `replica_count` is 0, the cluster has no replicas and the read cannot make progress until one is added.\n- `read.faster_read_available` means an index on the product exists on a cluster your role cannot use. If reads are slow, an admin can grant access to it.\n\nSystem catalog:\n- Write system object names with their schema, for example mz_internal.mz_source_statuses. query does not add the system schemas to the search path.\n- Before writing catalog queries, find tables, columns, and join paths in mz_internal.mz_ontology_entity_types, mz_internal.mz_ontology_link_types, mz_internal.mz_ontology_properties, and mz_internal.mz_ontology_semantic_types.\n- mz_introspection relations (for example mz_introspection.mz_dataflow_arrangement_sizes) are cluster-scoped: query them with `cluster` set, and `cluster_replica` on a cluster with more than one replica.\n- mz_internal.mz_source_statuses and mz_internal.mz_sink_statuses use `last_status_change_at`, not `updated_at`.\n- mz_internal.mz_cluster_replica_utilization only has `replica_id`: join mz_catalog.mz_cluster_replicas and mz_catalog.mz_clusters for names.\n- mz_introspection.mz_dataflow_arrangement_sizes.id and mz_introspection.mz_dataflows.id are dataflow ids (uint8), not catalog ids: join through mz_introspection.mz_compute_exports (dataflow_id to export_id), or match the name column, which reads Dataflow: ...","resultType":"complete","ttlMs":60000,"cacheScope":"private","_meta":{"io.modelcontextprotocol/serverInfo":{"name":"materialize-mcp","version":""}}}} # The system relations the instructions name, under the names they give. mcp-unified @@ -127,6 +127,50 @@ mcp-unified 200 OK {"jsonrpc":"2.0","id":17,"result":{"content":[{"type":"text","text":"[\n [true,true,true,true,true,true]\n]"}],"isError":false}} +# Data products for the details shape. The cluster has no replicas, so +# hydration does not depend on timing. +sql +CREATE CLUSTER no_replicas REPLICAS (); +CREATE MATERIALIZED VIEW orders_mv IN CLUSTER no_replicas AS SELECT 1 AS id, 'open' AS status; +COMMENT ON MATERIALIZED VIEW orders_mv IS 'Open orders.'; +COMMENT ON COLUMN orders_mv.id IS 'Order id.'; +CREATE INDEX orders_mv_idx IN CLUSTER no_replicas ON orders_mv (id); +CREATE VIEW orders_view AS SELECT 1 AS id, 'open' AS status; +CREATE INDEX orders_view_idx IN CLUSTER no_replicas ON orders_view (id); +COMMENT ON INDEX orders_view_idx IS 'Orders by id.' +---- +ok + +# Each product is listed once, with the cluster get_data_product_details picks. +mcp-unified +{"jsonrpc":"2.0","id":18,"method":"tools/call","params":{"name":"list_data_products"}} +---- +200 OK +{"jsonrpc":"2.0","id":18,"result":{"content":[{"type":"text","text":"[\n [\"\\\"materialize\\\".\\\"public\\\".\\\"orders_mv\\\"\",null,\"Open orders.\"],\n [\"\\\"materialize\\\".\\\"public\\\".\\\"orders_view\\\"\",\"no_replicas\",\"Orders by id.\"]\n]"}],"isError":false}} + +# A materialized view reads its stored results rather than an index on a +# cluster with no replicas, so `read.cluster` is null. +mcp-unified +{"jsonrpc":"2.0","id":19,"method":"tools/call","params":{"name":"get_data_product_details","arguments":{"name":"\"materialize\".\"public\".\"orders_mv\""}}} +---- +200 OK +{"jsonrpc":"2.0","id":19,"result":{"content":[{"type":"text","text":"{\n \"name\": \"\\\"materialize\\\".\\\"public\\\".\\\"orders_mv\\\"\",\n \"type\": \"materialized-view\",\n \"description\": \"Open orders.\",\n \"columns\": {\n \"id\": {\n \"description\": \"Order id.\",\n \"type\": \"number\"\n },\n \"status\": {\n \"type\": \"string\"\n }\n },\n \"read\": {\n \"cluster\": null,\n \"uses_index\": false,\n \"indexed_columns\": [],\n \"hydration\": {\n \"hydrated\": false,\n \"hydrated_replica_count\": 0,\n \"replica_count\": 0\n },\n \"faster_read_available\": false\n }\n}"}],"isError":false}} + +# A view reads only through its index. With no comment on the view, the +# index comment describes it. +mcp-unified +{"jsonrpc":"2.0","id":20,"method":"tools/call","params":{"name":"get_data_product_details","arguments":{"name":"\"materialize\".\"public\".\"orders_view\""}}} +---- +200 OK +{"jsonrpc":"2.0","id":20,"result":{"content":[{"type":"text","text":"{\n \"name\": \"\\\"materialize\\\".\\\"public\\\".\\\"orders_view\\\"\",\n \"type\": \"view\",\n \"description\": \"Orders by id.\",\n \"columns\": {\n \"id\": {\n \"type\": \"number\"\n },\n \"status\": {\n \"type\": \"string\"\n }\n },\n \"read\": {\n \"cluster\": \"no_replicas\",\n \"uses_index\": true,\n \"indexed_columns\": [\n \"id\"\n ],\n \"hydration\": {\n \"hydrated\": false,\n \"hydrated_replica_count\": 0,\n \"replica_count\": 0\n },\n \"faster_read_available\": false\n }\n}"}],"isError":false}} + +# The agent route keeps its row shape. +mcp-agent +{"jsonrpc":"2.0","id":21,"method":"tools/call","params":{"name":"get_data_product_details","arguments":{"name":"\"materialize\".\"public\".\"orders_view\""}}} +---- +200 OK +{"jsonrpc":"2.0","id":21,"result":{"content":[{"type":"text","text":"[\n [\"\\\"materialize\\\".\\\"public\\\".\\\"orders_view\\\"\",\"no_replicas\",\"Orders by id.\",{\"indexedColumns\":[\"id\"],\"properties\":{\"id\":{\"type\":\"number\"},\"status\":{\"type\":\"string\"}},\"type\":\"object\"},{\"hydrated\":false,\"hydrated_replica_count\":0,\"replica_count\":0}]\n]"}],"isError":false}} + # GET on the POST-only endpoint returns 405, as on the agent and developer routes. mcp-unified get ---- @@ -135,6 +179,6 @@ mcp-unified get # An Origin header not on the CORS allowlist (empty in this test harness) is # rejected, as on the agent and developer routes. mcp-unified origin -{"jsonrpc":"2.0","id":18,"method":"tools/list"} +{"jsonrpc":"2.0","id":22,"method":"tools/list"} ---- 403 Forbidden diff --git a/src/pgrepr-consts/src/oid.rs b/src/pgrepr-consts/src/oid.rs index 919bf45816c76..aec991e70ee5f 100644 --- a/src/pgrepr-consts/src/oid.rs +++ b/src/pgrepr-consts/src/oid.rs @@ -857,3 +857,4 @@ pub const VIEW_MZ_CATALOG_ITEM_METADATA_OID: u32 = 17147; pub const INDEX_MZ_CATALOG_ITEM_METADATA_IND_OID: u32 = 17148; pub const VIEW_MZ_BUILTIN_OBJECT_MAPPINGS_OID: u32 = 17150; pub const INDEX_MZ_BUILTIN_OBJECT_MAPPINGS_IND_OID: u32 = 17151; +pub const VIEW_MZ_MCP_DATA_PRODUCT_READS_OID: u32 = 17152; diff --git a/src/sql/src/rbac.rs b/src/sql/src/rbac.rs index 56ba5bb24dc43..4c3213a6598c0 100644 --- a/src/sql/src/rbac.rs +++ b/src/sql/src/rbac.rs @@ -123,6 +123,7 @@ static RESTRICT_TO_USER_OBJECTS_ALLOWED_OIDS: LazyLock> = LazyLock btreeset! { oid::VIEW_MZ_MCP_DATA_PRODUCTS_OID, oid::VIEW_MZ_MCP_DATA_PRODUCT_DETAILS_OID, + oid::VIEW_MZ_MCP_DATA_PRODUCT_READS_OID, oid::VIEW_MZ_SHOW_MY_CLUSTER_PRIVILEGES_OID, } }); diff --git a/test/sqllogictest/autogenerated/mz_internal.slt b/test/sqllogictest/autogenerated/mz_internal.slt index 7037d0d1443c7..13b6f4069e0e6 100644 --- a/test/sqllogictest/autogenerated/mz_internal.slt +++ b/test/sqllogictest/autogenerated/mz_internal.slt @@ -350,6 +350,19 @@ description text Index␠comment␠if␠available,␠otherwise␠object␠comm schema jsonb JSON␠Schema␠describing␠the␠object's␠columns␠and␠types. hydration jsonb Readiness␠summary␠as␠a␠JSON␠object␠with␠`hydrated`␠(bool),␠`replica_count`␠(int),␠and␠`hydrated_replica_count`␠(int).␠`hydrated`␠is␠true␠only␠when␠the␠cluster␠has␠at␠least␠one␠replica␠and␠the␠dataflow␠is␠hydrated␠on␠every␠replica.␠Reads␠against␠a␠non-hydrated␠data␠product␠block␠until␠the␠dataflow␠catches␠up␠(they␠never␠return␠partial␠data).␠Check␠this␠before␠reading:␠if␠`hydrated`␠is␠false␠and␠`replica_count␠>␠0`,␠wait␠and␠retry;␠if␠`replica_count`␠is␠0,␠the␠cluster␠has␠no␠replicas␠and␠that␠needs␠operator␠action,␠not␠a␠retry.␠A␠null␠`cluster`␠means␠your␠role␠lacks␠USAGE␠on␠that␠cluster,␠and␠only␠materialized␠views␠appear␠that␠way,␠since␠a␠view␠is␠listed␠only␠with␠an␠index␠on␠a␠cluster␠you␠can␠use.␠A␠read␠of␠such␠a␠row␠runs␠on␠another␠cluster␠and␠returns␠the␠materialized␠view's␠stored␠results,␠so␠`hydration`␠then␠reports␠the␠materialized␠view's␠own␠cluster,␠the␠one␠that␠keeps␠those␠results␠current. +query TTT +SELECT name, type, comment FROM objects WHERE schema = 'mz_internal' AND object = 'mz_mcp_data_product_reads' ORDER BY position +---- +object_name text Fully␠qualified␠object␠name␠(database.schema.name). +object_type text `materialized-view`␠or␠`view`. +description text Object␠comment,␠otherwise␠the␠comment␠of␠an␠index␠on␠`cluster`. +columns jsonb Each␠column's␠JSON␠Schema␠type␠and␠comment,␠keyed␠by␠column␠name. +cluster text Cluster␠to␠read␠the␠object␠on:␠the␠cluster␠you␠can␠use␠with␠an␠index␠on␠the␠object,␠preferring␠a␠hydrated␠index.␠Null␠for␠a␠materialized␠view␠with␠no␠such␠index:␠run␠the␠read␠on␠any␠cluster,␠and␠it␠returns␠the␠stored␠results. +uses_index boolean Whether␠a␠read␠on␠`cluster`␠is␠served␠from␠an␠index. +indexed_columns jsonb Key␠columns␠of␠the␠indexes␠on␠`cluster`,␠as␠a␠JSON␠array.␠Empty␠when␠`uses_index`␠is␠false. +hydration jsonb Readiness␠of␠the␠read␠as␠a␠JSON␠object␠with␠`hydrated`␠(bool),␠`replica_count`␠(int),␠and␠`hydrated_replica_count`␠(int).␠It␠describes␠the␠indexes␠on␠`cluster`,␠or,␠when␠`cluster`␠is␠null,␠the␠materialized␠view's␠own␠cluster,␠which␠keeps␠the␠stored␠results␠current.␠`hydrated`␠is␠true␠only␠when␠that␠cluster␠has␠at␠least␠one␠replica␠and␠every␠replica␠is␠hydrated.␠A␠read␠that␠is␠not␠hydrated␠blocks␠until␠it␠is.␠If␠`replica_count`␠is␠0,␠the␠read␠cannot␠make␠progress␠until␠a␠replica␠is␠added. +faster_read_available boolean True␠when␠the␠read␠does␠not␠use␠an␠index,␠but␠an␠index␠on␠the␠object␠exists␠on␠a␠cluster␠your␠role␠cannot␠use. + query TTT SELECT name, type, comment FROM objects WHERE schema = 'mz_internal' AND object = 'mz_object_dependencies' ORDER BY position ---- @@ -888,6 +901,7 @@ mz_materialization_lag mz_materialized_view_refresh_strategies mz_materialized_view_refreshes mz_mcp_data_product_details +mz_mcp_data_product_reads mz_mcp_data_products mz_metric_sinks mz_mysql_source_tables diff --git a/test/sqllogictest/catalog_server_explain.slt b/test/sqllogictest/catalog_server_explain.slt index 3929141b227b9..6650f132216df 100644 --- a/test/sqllogictest/catalog_server_explain.slt +++ b/test/sqllogictest/catalog_server_explain.slt @@ -4986,7 +4986,7 @@ mz_catalog.mz_columns: Project: #6, #0..=#3, #7, #4, #5 Map: null →Arrange (#0{schema_name}, #1{relation_name}) - →Constant (1985 rows) + →Constant (1994 rows) →Arrange (#1{schema_name}, #2{name}) →Fused with Child Map/Filter/Project Project: #6, #4, #3 @@ -5799,7 +5799,7 @@ mz_catalog.mz_views: Project: #5, #0, #6, #1, #2, #8, #3, #4, #7 Map: "s1" →Arrange (#1{schema_name}, #2{name}) - →Constant (201 rows) + →Constant (202 rows) →Arrange (#0{schema_name}) (#0{schema_name}, #1{name}) →Fused with Child Map/Filter/Project Project: #5, #4, #6 @@ -6316,7 +6316,7 @@ mz_internal.mz_object_dependencies: Project: #4..=#6, #2 Filter: case when (#7{ref_kind} = "func") then (#3{object_type} = "8") else case when (#7{ref_kind} = "type") then (#3{object_type} = "7") else ((#3{object_type} != "7") AND (#3{object_type} != "8")) end end →Arrange (#0{object_schema}..=#2{object_type}) (#3{ref_schema}, #4{ref_name}) - →Constant (1118 rows) + →Constant (1139 rows) →Arrange (#1{schema_name}..=#3{object_type}) →Fused with Child Map/Filter/Project Project: #5, #4, #2, #3 @@ -8929,7 +8929,7 @@ query T multiline EXPLAIN SELECT * FROM "mz_internal"."mz_builtin_columns"; ---- Explained Query (fast path): - →Constant (1985 rows) + →Constant (1994 rows) Target cluster: mz_catalog_server @@ -9013,7 +9013,7 @@ query T multiline EXPLAIN SELECT * FROM "mz_internal"."mz_builtin_views"; ---- Explained Query (fast path): - →Constant (201 rows) + →Constant (202 rows) Target cluster: mz_catalog_server @@ -10517,6 +10517,547 @@ Target cluster: mz_catalog_server EOF +query T multiline +EXPLAIN SELECT * FROM "mz_internal"."mz_mcp_data_product_reads"; +---- +Explained Query: + →With + cte l0 = + →Fused with Child Map/Filter/Project + Project: #0, #2..=#4 + →Table Function mz_aclexplode(#1{privileges}) + →Fused with Child Map/Filter/Project + Project: #1, #3 + Filter: NOT(like["s%"](#0{id})) + →Arranged mz_catalog.mz_clusters + Key: (#0{id}) + cte l1 = + →Fused with Child Map/Filter/Project + Project: #0 + →Arranged mz_catalog.mz_roles + Key: (#0{id}) + cte l2 = + →Negate Diffs + →Stream l1 + cte l3 = + →Negate Diffs + →Fused with Child Map/Filter/Project + Project: #0 + Filter: NOT(like["s%"](#0)) + →Arranged mz_catalog.mz_roles + Key: (#0{id}) + cte l4 = + →Fused with Child Map/Filter/Project + Project: #0, #2, #6 + Filter: NOT(like["s%"](#0)) + Map: true + →Arranged mz_catalog.mz_roles + Key: (#0{id}) + cte l5 = + →One-Shot Delta Join [%0:l0 » %1[#0] » %2[#0]] + path %0: + after %2: + Project: #1, #4 + Map: case when (#0{grantee} = "p") then "PUBLIC" else case when (#3) IS NULL then null else #2 end end + →Unarranged Raw Stream + →Fused with Child Map/Filter/Project + Project: #0..=#2 + Filter: NOT(like["s%"](#2{grantee})) AND (#3{privilege_type} = "USAGE") + →Read l0 + →Arrange (#0) + →Union + →Stream l1 + →Unarranged Raw Stream + →Threshold Diffs #0 + →Arrange (#0) + →Union + →Stream l2 + →Unarranged Raw Stream + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #1 + →Read l0 + →Arrange (#0) + →Union + →Stream l4 + →Map/Filter/Project + Project: #0..=#2 + Map: null, null + →Threshold Diffs #0 + →Arrange (#0) + →Union + →Stream l3 + →Unarranged Raw Stream + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #2 + Filter: NOT(like["s%"](#2{grantee})) + →Read l0 + cte l6 = + →Fused with Child Map/Filter/Project + Project: #0..=#2, #4..=#6 + →Table Function mz_aclexplode(#3{privileges}) + →Fused with Child Map/Filter/Project + Project: #0, #3, #4, #7 + Filter: NOT(like["s%"](#1{id})) + →Arranged mz_catalog.mz_objects + Key: (#2{schema_id}) + cte l7 = + →Fused with Child Map/Filter/Project + Project: #1, #0 + →Arranged mz_catalog.mz_databases + Key: (#2{name}) + cte l8 = + →One-Shot Delta Join [%0:l6 » %3:mz_schemas[#0] » %4:l7[#0] » %1[#0] » %2[#0]] + path %0: + after %2: + Project: #1..=#4, #7 + Map: case when (#0{grantee} = "p") then "PUBLIC" else case when (#6) IS NULL then null else #5 end end + →Unarranged Raw Stream + →Fused with Child Map/Filter/Project + Project: #0..=#4 + Filter: ((#2{object_type} = "view") OR (#2{object_type} = "materialized-view")) AND NOT(like["s%"](#4{grantee})) AND (#5{privilege_type} = "SELECT") + →Read l6 + →Arrange (#0) + →Union + →Stream l1 + →Unarranged Raw Stream + →Threshold Diffs #0 + →Arrange (#0) + →Union + →Stream l2 + →Unarranged Raw Stream + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #3 + →Read l6 + →Arrange (#0) + →Union + →Stream l4 + →Map/Filter/Project + Project: #0..=#2 + Map: null, null + →Threshold Diffs #0 + →Arrange (#0) + →Union + →Stream l3 + →Unarranged Raw Stream + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #4 + Filter: NOT(like["s%"](#4{grantee})) + →Read l6 + →Arrange (#0) (#1{database_id}) + →Fused with Child Map/Filter/Project + Project: #1, #0, #3 + Filter: (#3 != "mz_catalog") AND (#3 != "pg_catalog") AND (#3 != "mz_internal") AND (#3 != "mz_introspection") AND (#3 != "information_schema") + →Arranged mz_catalog.mz_schemas + Key: (#2{database_id}) + →Arrange (#0) + →Stream l7 + cte l9 = + →Distinct GroupAggregate + Key: + Project: #6, #4, #1, #5 + Map: (((((("\"" || #3{database}) || "\".\"") || #2{schema}) || "\".\"") || #0{name}) || "\"") + →Differential Join %0[#0{name}, #1{object_type}] » %1:mz_objects[#2{name}, #3{type}] » %2:mz_schemas[#0{id}, #2{name}] » %3:l7[#0{id}, #1{name}] + →Arrange (#0{name}, #1{object_type}) + →Union + →Fused with Child Map/Filter/Project + Project: #0..=#3 + Filter: (#4{grantee} = "PUBLIC") + →Read l8 + →Fused with Child Map/Filter/Project + Project: #0..=#3 + Filter: (#4 = "materialize") AND (false = (#4{grantee} = "PUBLIC")) + →Read l8 + →Arrange (#2{name}, #3{type}) + →Fused with Child Map/Filter/Project + Project: #1, #0, #3, #4, #6 + Filter: ((#4{type} = "view") OR (#4{type} = "materialized-view")) + →Arranged mz_catalog.mz_objects + Key: (#2{schema_id}) + →Arrange (#0{id}, #2{name}) + →Fused with Child Map/Filter/Project + Project: #1, #0, #3 + Filter: (#0{database_id}) IS NOT NULL AND (#3{name} != "mz_catalog") AND (#3{name} != "pg_catalog") AND (#3{name} != "mz_internal") AND (#3{name} != "mz_introspection") AND (#3{name} != "information_schema") + →Arranged mz_catalog.mz_schemas + Key: (#2{database_id}) + →Arrange (#0{id}, #1{name}) + →Stream l7 + cte l10 = + →Arranged mz_catalog.mz_clusters + cte l11 = + →One-Shot Delta Join [%0:l9 » %1:mz_indexes[#1{on_id}] » %2:l10[#0{id}]] + →Unarranged Raw Stream + →Fused with Child Map/Filter/Project + Project: #1 + →Arranged l9 + Key: (#0..=#3) + →Arrange (#1{on_id}) (#2{cluster_id}) + →Fused with Child Map/Filter/Project + Project: #0, #3, #4 + →Arranged mz_catalog.mz_indexes + Key: (#0{id}) + →Arranged l10 + cte l12 = + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #3 + →Read l11 + cte l13 = + →Differential Join %0:l12[#0] » %1[#0] + →Arranged l12 + →Distinct GroupAggregate + →Union + →Fused with Child Map/Filter/Project + Project: #0 + Filter: (#1{grantee} = "PUBLIC") + →Read l5 + →Fused with Child Map/Filter/Project + Project: #0 + Filter: (#1 = "materialize") AND (false = (#1{grantee} = "PUBLIC")) + →Read l5 + cte l14 = + →Differential Join %0:l11[#3] » %1[#0] + →Arrange (#3) + →Stream l11 + →Arrange (#0) + →Union + →Fused with Child Map/Filter/Project + Project: #0, #1 + Map: true + →Read l13 + →Map/Filter/Project + Project: #0, #1 + Map: false + →Consolidating Union + →Negate Diffs + →Stream l13 + →Unarranged Raw Stream + →Arranged l12 + cte l15 = + →Union + →Fused with Child Map/Filter/Project + Project: #0, #4, #2, #1 + Filter: #3{usable} + Map: true + →Read l14 + →Fused with Child Map/Filter/Project + Project: #1, #4, #3, #1 + Filter: (#2{object_type} = "materialized-view") AND (#3) IS NOT NULL + Map: false + →Arranged l9 + Key: (#0..=#3) + cte l16 = + →Differential Join %0:l15[#2{cluster_id}] » %1:mz_cluster_replicas[#1{cluster_id}] + →Arrange (#2{cluster_id}) + →Stream l15 + →Arrange (#1{cluster_id}) + →Fused with Child Map/Filter/Project + Project: #0, #2 + →Arranged mz_catalog.mz_cluster_replicas + Key: (#0{id}) + cte l17 = + →Arrange (#3{dataflow_id}, #4{id}) + →Stream l16 + cte l18 = + →Differential Join %1:mz_hydration_statuses[#0{object_id}, #1{replica_id}] » %0:l17[#3{dataflow_id}, #4{id}] + →Arranged l17 + →Arranged mz_internal.mz_hydration_statuses + cte l19 = + →Map/Filter/Project + Project: #0..=#3, #5 + Map: (#4 = 0) + →Accumulable GroupAggregate + Simple aggregates: sum(boolean_to_integer(NOT(coalesce(#4{hydrated}, false)))) + Key: + Project: #0..=#3 + →Union + →Map/Filter/Project + Project: #0..=#4 + Map: null + →Consolidating Union + →Negate Diffs + →Differential Join %1[#0, #1] » %0:l17[#3{dataflow_id}, #4{id}] + →Arranged l17 + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #3, #4 + →Read l18 + →Fused with Child Map/Filter/Project + Project: #0..=#2, #4 + →Read l16 + →Fused with Child Map/Filter/Project + Project: #0..=#2, #4, #5 + →Read l18 + cte l20 = + →Differential Join %0:l9[#0{object_id}] » %1:mz_columns[#0{id}] + →Arrange (#0{object_id}) + →Fused with Child Map/Filter/Project + Project: #1, #2 + →Arranged l9 + Key: (#0..=#3) + →Arrange (#0{id}) + →Fused with Child Map/Filter/Project + Project: #1, #0, #2, #4 + →Arranged mz_catalog.mz_columns + Key: (#1{name}) + cte l21 = + →Arrange (#0{object_id}, #1{object_type}, uint8_to_numeric(#3{position})) + →Stream l20 + cte l22 = + →Differential Join %0:l21[#0{object_id}, #1{object_type}, uint8_to_numeric(#3{position})] » %1:mz_comments[#0{id}, #1{object_type}, integer_to_numeric(#2{object_sub_id})] + →Arranged l21 + →Arrange (#0{id}, #1{object_type}, integer_to_numeric(#2{object_sub_id})) + →Fused with Child Map/Filter/Project + Filter: (#2{object_sub_id}) IS NOT NULL + →Arranged mz_internal.mz_comments + Key: (#0{id}) + cte l23 = + →Differential Join %1[#0{object_id}] » %0:l9[#1{object_id}] + →Arrange (#1{object_id}) + →Fused with Child Map/Filter/Project + Project: #0..=#2 + →Arranged l9 + Key: (#0..=#3) + →Arrange (#0{object_id}) + →Map/Filter/Project + Project: #0..=#4, #6 + →Consolidating Monotonic Top1 + Group By #0 + Order By #7 asc nulls_last, #5 asc nulls_last + →Differential Join %1:l10[#0{id}] » %0[#2{cluster_id}] + after %0: + Project: #11, #12, #0, #13, #14, #1, #15, #16 + Map: case when #12{uses_index} then #1{name} else null end, case when (#12{uses_index} AND (#13{replica_count} = #14{hydrated_replica_count}) AND (#13{replica_count} > 0)) then 1 else case when (#12{uses_index} AND (#13{replica_count} > 0)) then 2 else case when NOT(#12{uses_index}) then 3 else 4 end end end + →Arrange (#2{cluster_id}) + →Map/Filter/Project + Project: #0..=#2, #5, #6 + Map: bigint_to_integer(#3), bigint_to_integer(#4) + →Accumulable GroupAggregate + Simple aggregates: count(#3{replica_id}), count(case when #4{hydrated} then #3{replica_id} else null end) + Key: + Project: #0..=#2 + →Union + →Map/Filter/Project + Project: #0..=#4 + Map: null, null + →Consolidating Union + →Negate Diffs + →Unarranged Raw Stream + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #0..=#2 + →Read l19 + →Unarranged Raw Stream + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #0..=#2 + →Read l15 + →Stream l19 + →Arranged l10 + cte l24 = + →Arrange (#1{object_id}, #2{object_type}) + →Stream l23 + cte l25 = + →Differential Join %1:mz_comments[#0{id}, #1{object_type}] » %0:l24[#1{object_id}, #2{object_type}] + →Arranged l24 + →Arrange (#0{id}, #1{object_type}) + →Fused with Child Map/Filter/Project + Project: #0, #1, #3 + Filter: (#2{object_sub_id}) IS NULL + →Arranged mz_internal.mz_comments + Key: (#0{id}) + cte l26 = + →Union + →Map/Filter/Project + Project: #0..=#9 + Map: null + →Consolidating Union + →Negate Diffs + →Differential Join %1[#0, #1] » %0:l24[#1{object_id}, #2{object_type}] + →Arranged l24 + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #1, #2 + →Read l25 + →Fused with Child Map/Filter/Project + Project: #0..=#2, #1, #3, #4, #7, #5, #6 + →Read l23 + →Fused with Child Map/Filter/Project + Project: #0..=#2, #1, #3, #4, #7, #5, #6, #8 + →Read l25 + cte l27 = + →Differential Join %1[#0] » %0:l26[#1{object_id}] + after %0: + Project: #2, #0, #3..=#11 + Map: jsonb_strip_nulls(#1) + →Arrange (#1{object_id}) + →Stream l26 + →Non-incremental GroupAggregate + Aggregation: jsonb_object_agg[order_by=[]](row(row(#1{name}, coalesce(jsonbable_to_jsonb(case when ((#2{type} = "int") OR (#2{type} = "real") OR (#2{type} = "float") OR (#2{type} = "uint2") OR (#2{type} = "uint4") OR (#2{type} = "uint8") OR (#2{type} = "bigint") OR (#2{type} = "double") OR (#2{type} = "integer") OR (#2{type} = "numeric") OR (#2{type} = "smallint") OR (#2{type} = "double precision")) then jsonb_build_object("type", "number", "description", jsonbable_to_jsonb(#3{comment})) else case when (#2{type} = "boolean") then jsonb_build_object("type", "boolean", "description", jsonbable_to_jsonb(#3{comment})) else case when (#2{type} = "bytea") then jsonb_build_object("type", "string", "description", jsonbable_to_jsonb(#3{comment}), "contentEncoding", "base64", "contentMediaType", "application/octet-stream") else case when (#2{type} = "date") then jsonb_build_object("type", "string", "format", "date", "description", jsonbable_to_jsonb(#3{comment})) else case when (#2{type} = "time") then jsonb_build_object("type", "string", "format", "time", "description", jsonbable_to_jsonb(#3{comment})) else case when ilike["timestamp%"](#2{type}) then jsonb_build_object("type", "string", "format", "date-time", "description", jsonbable_to_jsonb(#3{comment})) else case when (#2{type} = "jsonb") then jsonb_build_object("type", "object", "description", jsonbable_to_jsonb(#3{comment})) else case when (#2{type} = "uuid") then jsonb_build_object("type", "string", "format", "uuid", "description", jsonbable_to_jsonb(#3{comment})) else jsonb_build_object("type", "string", "description", jsonbable_to_jsonb(#3{comment})) end end end end end end end end), json_null)))) + Key: + Project: #0 + →Union + →Map/Filter/Project + Project: #0..=#3 + Map: null + →Consolidating Union + →Negate Diffs + →Differential Join %1[#0..=#2] » %0:l21[#0{object_id}, #1{object_type}, uint8_to_numeric(#3{position})] + →Arranged l21 + →Distinct GroupAggregate + Key: + Project: #0, #1, #3 + Map: integer_to_numeric(#2{object_sub_id}) + →Fused with Child Map/Filter/Project + Project: #0, #1, #4 + →Read l22 + →Fused with Child Map/Filter/Project + Project: #0, #2, #4 + →Read l20 + →Fused with Child Map/Filter/Project + Project: #0, #2, #3, #5 + →Read l22 + cte l28 = + →Union + →Map/Filter/Project + Project: #0..=#10 + Map: null + →Consolidating Union + →Negate Diffs + →Fused with Child Map/Filter/Project + Project: #0..=#9 + →Read l27 + →Stream l26 + →Stream l27 + cte l29 = + →Arrange (#1{index_id}) + →Fused with Child Map/Filter/Project + Project: #0..=#2 + Filter: #3{usable} + →Read l14 + cte l30 = + →Differential Join %1[#0, #1] » %0:l28[#3{object_id}, #5{cluster_id}] + →Arrange (#3{object_id}, #5{cluster_id}) + →Fused with Child Map/Filter/Project + Filter: #4{uses_index} + →Read l28 + →Non-incremental GroupAggregate + Aggregation: jsonb_agg[order_by=[#0 asc nulls_last, #1 asc nulls_last]](row(jsonbable_to_jsonb(#2{name}), #3{position}, #2{name})) + Key: + Project: #0, #1 + →Consolidating Monotonic GroupAggregate + Aggregations: min + Key: + Project: #0, #1, #3 + →Differential Join %0:l29[#1{index_id}] » %1:mz_index_columns[#0{index_id}] » %2:mz_columns[#0{id}, #2{position}] + →Arranged l29 + →Arrange (#0{index_id}) + →Read mz_catalog.mz_index_columns + →Arrange (#0{id}, #2{position}) + →Fused with Child Map/Filter/Project + Project: #1, #0, #2 + →Arranged mz_catalog.mz_columns + Key: (#1{name}) + cte l31 = + →Union + →Map/Filter/Project + Project: #0..=#11 + Map: null + →Consolidating Union + →Negate Diffs + →Fused with Child Map/Filter/Project + Project: #0..=#10 + →Read l30 + →Stream l28 + →Stream l30 + cte l32 = + →Differential Join %1[#0, #1] » %0:l31[#3{object_id}, #5{cluster_id}] + →Arrange (#3{object_id}, #5{cluster_id}) + →Fused with Child Map/Filter/Project + Filter: #4{uses_index} + →Read l31 + →Consolidating Monotonic GroupAggregate + Aggregations: min + Key: + Project: #0, #1 + →Differential Join %1:mz_comments[#0{id}] » %0:l29[#1{index_id}] + after %0: + Project: #4, #5, #3 + Filter: (#1{object_type} = "index") AND (#2{object_sub_id}) IS NULL + →Arranged l29 + →Arranged mz_internal.mz_comments + cte l33 = + →Union + →Map/Filter/Project + Project: #0..=#10 + Map: null + →Consolidating Union + →Negate Diffs + →Fused with Child Map/Filter/Project + Project: #0..=#9 + →Read l32 + →Fused with Child Map/Filter/Project + Project: #0..=#2, #4, #6..=#11 + →Read l31 + →Stream l32 + cte l34 = + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #1 + →Read l33 + cte l35 = + →Differential Join %0:l34[#0] » %1[#0] + →Arranged l34 + →Distinct GroupAggregate + →Fused with Child Map/Filter/Project + Project: #0 + Filter: NOT(#3{usable}) + →Read l14 + →Return + →Differential Join %0:l33[#1] » %1[#0] + after %1: + Project: #1, #2, #12, #13, #4, #3, #14..=#16 + Map: coalesce(#7{comment}, #10{comment}), coalesce(#8{columns}, {}), coalesce(#9{indexed_columns}, []), jsonb_build_object("hydrated", jsonbable_to_jsonb(((#5{replica_count} = #6{hydrated_replica_count}) AND (#5{replica_count} > 0))), "replica_count", jsonbable_to_jsonb(integer_to_numeric(#5{replica_count})), "hydrated_replica_count", jsonbable_to_jsonb(integer_to_numeric(#6{hydrated_replica_count}))), (#11 AND NOT(#3{uses_index})) + →Arrange (#1) + →Stream l33 + →Arrange (#0) + →Union + →Fused with Child Map/Filter/Project + Project: #0, #1 + Map: true + →Read l35 + →Map/Filter/Project + Project: #0, #1 + Map: false + →Consolidating Union + →Negate Diffs + →Stream l35 + →Unarranged Raw Stream + →Arranged l34 + +Source mz_catalog.mz_index_columns + project=(#0..=#2) + filter=((#2{on_position}) IS NOT NULL) + +Used Indexes: + - mz_internal.mz_hydration_statuses_ind (differential join) + - mz_catalog.mz_clusters_ind (*** full scan ***, differential join, delta join lookup) + - mz_catalog.mz_indexes_ind (*** full scan ***) + - mz_catalog.mz_roles_ind (*** full scan ***) + - mz_catalog.mz_cluster_replicas_ind (*** full scan ***) + - mz_internal.mz_comments_ind (*** full scan ***, differential join) + - mz_catalog.mz_databases_ind (*** full scan ***) + - mz_catalog.mz_schemas_ind (*** full scan ***) + - mz_catalog.mz_objects_ind (*** full scan ***) + - mz_catalog.mz_columns_ind (*** full scan ***) + +Target cluster: mz_catalog_server + +EOF + query T multiline EXPLAIN SELECT * FROM "mz_internal"."mz_mcp_data_products"; ---- @@ -10978,7 +11519,7 @@ Explained Query: Project: #3, #4 Filter: case when (#2{ref_kind} = "func") then (#5{object_type} = "8") else case when (#2{ref_kind} = "type") then (#5{object_type} = "7") else ((#5{object_type} != "7") AND (#5{object_type} != "8")) end end →Unarranged Raw Stream - →Constant (1118 rows) + →Constant (1139 rows) →Arrange (#1{schema_name}..=#3{object_type}) →Fused with Child Map/Filter/Project Project: #5, #4, #2, #3 diff --git a/test/sqllogictest/information_schema_tables.slt b/test/sqllogictest/information_schema_tables.slt index 12a990ca960c4..2966f6a8f535d 100644 --- a/test/sqllogictest/information_schema_tables.slt +++ b/test/sqllogictest/information_schema_tables.slt @@ -489,6 +489,10 @@ mz_mcp_data_product_details VIEW materialize mz_internal +mz_mcp_data_product_reads +VIEW +materialize +mz_internal mz_mcp_data_products VIEW materialize diff --git a/test/sqllogictest/mz_catalog_server_index_accounting.slt b/test/sqllogictest/mz_catalog_server_index_accounting.slt index a40c89714335e..7412abcf55dc9 100644 --- a/test/sqllogictest/mz_catalog_server_index_accounting.slt +++ b/test/sqllogictest/mz_catalog_server_index_accounting.slt @@ -87,7 +87,7 @@ mz_message_batch_counts_sent_raw_s2_primary_idx CREATE␠INDEX␠"mz_message_ba mz_message_counts_received_raw_s2_primary_idx CREATE␠INDEX␠"mz_message_counts_received_raw_s2_primary_idx"␠IN␠CLUSTER␠[s2]␠ON␠"mz_introspection"."mz_message_counts_received_raw"␠("channel_id",␠"from_worker_id",␠"to_worker_id") mz_message_counts_sent_raw_s2_primary_idx CREATE␠INDEX␠"mz_message_counts_sent_raw_s2_primary_idx"␠IN␠CLUSTER␠[s2]␠ON␠"mz_introspection"."mz_message_counts_sent_raw"␠("channel_id",␠"from_worker_id",␠"to_worker_id") mz_metric_sinks_ind CREATE␠INDEX␠"mz_metric_sinks_ind"␠IN␠CLUSTER␠[s2]␠ON␠[s559␠AS␠"mz_internal"."mz_metric_sinks"]␠("id") -mz_notices_ind CREATE␠INDEX␠"mz_notices_ind"␠IN␠CLUSTER␠[s2]␠ON␠[s861␠AS␠"mz_internal"."mz_notices"]␠("id") +mz_notices_ind CREATE␠INDEX␠"mz_notices_ind"␠IN␠CLUSTER␠[s2]␠ON␠[s862␠AS␠"mz_internal"."mz_notices"]␠("id") mz_object_arrangement_size_history_object_ind CREATE␠INDEX␠"mz_object_arrangement_size_history_object_ind"␠IN␠CLUSTER␠[s2]␠ON␠[s774␠AS␠"mz_internal"."mz_object_arrangement_size_history"]␠("object_id") mz_object_arrangement_size_history_ts_ind CREATE␠INDEX␠"mz_object_arrangement_size_history_ts_ind"␠IN␠CLUSTER␠[s2]␠ON␠[s774␠AS␠"mz_internal"."mz_object_arrangement_size_history"]␠("collection_timestamp") mz_object_arrangement_sizes_ind CREATE␠INDEX␠"mz_object_arrangement_sizes_ind"␠IN␠CLUSTER␠[s2]␠ON␠[s772␠AS␠"mz_internal"."mz_object_arrangement_sizes"]␠("replica_id") diff --git a/test/sqllogictest/oid.slt b/test/sqllogictest/oid.slt index cc00776df3af1..306924e3c0b5e 100644 --- a/test/sqllogictest/oid.slt +++ b/test/sqllogictest/oid.slt @@ -1277,3 +1277,4 @@ SELECT oid, name FROM mz_objects WHERE id LIKE 's%' AND oid < 20000 ORDER BY oid 17148 mz_catalog_item_metadata_ind 17150 mz_builtin_object_mappings 17151 mz_builtin_object_mappings_ind +17152 mz_mcp_data_product_reads diff --git a/test/sqllogictest/rbac_mcp_agent.slt b/test/sqllogictest/rbac_mcp_agent.slt index f247482e9eae5..43603a8b7cc11 100644 --- a/test/sqllogictest/rbac_mcp_agent.slt +++ b/test/sqllogictest/rbac_mcp_agent.slt @@ -650,6 +650,154 @@ DROP MATERIALIZED VIEW agent_objects.fallback_mv; ---- COMPLETE 0 +# DEX-208: mz_mcp_data_product_reads lists each product once, with the one +# cluster the /api/mcp route picks to read it on. +simple conn=agent_restricted,user=agent +SELECT object_name, object_type, cluster, uses_index, indexed_columns, faster_read_available +FROM mz_internal.mz_mcp_data_product_reads ORDER BY object_name; +---- +"materialize"."agent_objects"."compute_only",materialized-view,NULL,f,[],f +"materialize"."agent_objects"."multi_indexed",materialized-view,quickstart,t,["id"],f +"materialize"."agent_objects"."next_arrivals",materialized-view,NULL,f,[],f +"materialize"."agent_objects"."transfer_windows",materialized-view,NULL,f,[],f +COMPLETE 4 + +# A materialized view whose only index sits on a cluster the agent cannot use +# reads its stored results. Hydration follows the view's own cluster, and +# faster_read_available says an index exists. +simple conn=mz_system,user=mz_system +CREATE MATERIALIZED VIEW agent_objects.hidden_index_mv IN CLUSTER agent_serving AS SELECT 1 AS id; +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +CREATE INDEX hidden_index_mv_idx IN CLUSTER agent_compute ON agent_objects.hidden_index_mv (id); +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +GRANT SELECT ON agent_objects.hidden_index_mv TO agent; +---- +COMPLETE 0 + +simple conn=agent_restricted,user=agent +SELECT cluster, uses_index, (hydration->>'replica_count')::int, faster_read_available +FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = '"materialize"."agent_objects"."hidden_index_mv"'; +---- +NULL,f,1,t +COMPLETE 1 + +# An index on a cluster with no replicas never makes progress. A view indexed +# there and on a cluster with a replica reads on the latter, though the dead +# cluster sorts first by name. +simple conn=mz_system,user=mz_system +CREATE CLUSTER agent_aaa_dead REPLICAS (); +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +GRANT USAGE ON CLUSTER agent_aaa_dead TO agent; +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +CREATE VIEW agent_objects.two_index_view AS SELECT 1 AS id, 'a' AS label; +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +CREATE INDEX two_index_view_dead IN CLUSTER agent_aaa_dead ON agent_objects.two_index_view (label); +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +CREATE INDEX two_index_view_live IN CLUSTER agent_serving ON agent_objects.two_index_view (id); +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +GRANT SELECT ON agent_objects.two_index_view TO agent; +---- +COMPLETE 0 + +simple conn=agent_restricted,user=agent +SELECT cluster, uses_index, indexed_columns, (hydration->>'replica_count')::int +FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = '"materialize"."agent_objects"."two_index_view"'; +---- +agent_serving,t,["id"],1 +COMPLETE 1 + +# With only the dead index left, the view still reads there: it has nowhere +# else to go, and replica_count 0 tells the agent the read cannot progress. +simple conn=mz_system,user=mz_system +DROP INDEX agent_objects.two_index_view_live; +---- +COMPLETE 0 + +simple conn=agent_restricted,user=agent +SELECT cluster, uses_index, indexed_columns, (hydration->>'replica_count')::int +FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = '"materialize"."agent_objects"."two_index_view"'; +---- +agent_aaa_dead,t,["label"],0 +COMPLETE 1 + +# A materialized view prefers its stored results over a dead index. +simple conn=mz_system,user=mz_system +CREATE INDEX hidden_index_mv_dead IN CLUSTER agent_aaa_dead ON agent_objects.hidden_index_mv (id); +---- +COMPLETE 0 + +simple conn=agent_restricted,user=agent +SELECT cluster, uses_index, (hydration->>'replica_count')::int +FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = '"materialize"."agent_objects"."hidden_index_mv"'; +---- +NULL,f,1 +COMPLETE 1 + +# The object comment wins over the comment of the picked index, which fills in +# when the object has none. Column comments land in `columns`. +simple conn=mz_system,user=mz_system +COMMENT ON INDEX agent_objects.multi_idx_quickstart IS 'index comment'; +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +COMMENT ON COLUMN agent_objects.multi_indexed.id IS 'the id'; +---- +COMPLETE 0 + +simple conn=agent_restricted,user=agent +SELECT description, columns FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = '"materialize"."agent_objects"."multi_indexed"'; +---- +index comment,{"id":{"description":"the id","type":"number"}} +COMPLETE 1 + +simple conn=mz_system,user=mz_system +COMMENT ON MATERIALIZED VIEW agent_objects.multi_indexed IS 'object comment'; +---- +COMPLETE 0 + +simple conn=agent_restricted,user=agent +SELECT description FROM mz_internal.mz_mcp_data_product_reads WHERE object_name = '"materialize"."agent_objects"."multi_indexed"'; +---- +object comment +COMPLETE 1 + +simple conn=mz_system,user=mz_system +DROP CLUSTER agent_aaa_dead CASCADE; +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +DROP VIEW agent_objects.two_index_view; +---- +COMPLETE 0 + +simple conn=mz_system,user=mz_system +DROP MATERIALIZED VIEW agent_objects.hidden_index_mv; +---- +COMPLETE 0 + # mz_session_role_memberships() is callable in a restricted session (it only # exposes the current session's role chain, not the full system graph). # Verify it walks the full transitive chain: grant agent membership in a parent @@ -1355,6 +1503,15 @@ WHERE object_name = '"materialize"."dp_semantics"."mv_types"'; ---- false +# mz_mcp_data_product_reads maps columns the same way. +query T +SELECT r.columns = d.schema->'properties' +FROM mz_internal.mz_mcp_data_product_reads r +JOIN mz_internal.mz_mcp_data_product_details d USING (object_name) +WHERE object_name = '"materialize"."dp_semantics"."mv_types"'; +---- +true + # ----------------------------------------------------------------------------- # Cleanup for this section. # ----------------------------------------------------------------------------- diff --git a/test/testdrive/catalog.td b/test/testdrive/catalog.td index 944f55e7e1ea8..80658463197a3 100644 --- a/test/testdrive/catalog.td +++ b/test/testdrive/catalog.td @@ -646,6 +646,7 @@ mz_index_advice "" mz_materialization_dependencies "" mz_materialization_lag "" mz_mcp_data_product_details "" +mz_mcp_data_product_reads "" mz_mcp_data_products "" mz_notices "" mz_notices_redacted ""