Skip to main content

Validate Query Context

Type: Procedure

Analyze a SQL query against governance policies before execution. Parses the SQL to identify referenced tables and columns, checks each against the governance catalog, and returns a compliance report with parser diagnostics and agent-ready next steps. Optionally provide intent parameters to also run authorization checks.

Use Cases

  • An AI agent validates a generated SQL query before running it
  • A code review bot checks data access patterns in SQL scripts
  • A developer tests whether their query touches PII or restricted columns
  • An automated pipeline validates queries at build time, before deployment

Input Parameters

ParameterTypeRequiredDescription
sqlstringYesThe SQL query to validate. Supports SELECT, INSERT, UPDATE, DELETE, MERGE, and CTAS statements.
operationstringNoThe intended operation type (e.g., read, export, transform). When provided, triggers authorization evaluation in addition to compliance checking.
intended_usestringNoCanonical intended-use label. Used for authorization when an operation is provided or inferred.
actor_rolestringNoThe role executing the query. Defaults to the session role. Used for authorization evaluation.
destinationobjectNoDestination metadata for embedded transfer authorization.
consumer_jurisdictionstringNoConsumer jurisdiction for embedded transfer authorization.

When operation and intended_use are provided, the response includes a decision object equivalent to calling authorize-use for each table referenced in the query.

Output Schema

FieldTypeDescription
statusstringOK if no violations found, WARNING if advisory issues detected
compliantbooleantrue if no enforced policy violations were found
validation_idstringUnique identifier for the validation trace. Use with explain-why.
tables_accessedarrayList of fully qualified table names referenced in the query
sql_findingsarraySQL-level findings (e.g., SELECT * usage, missing WHERE clause)
sql_findings[].typestringFinding type: info, warning, violation
sql_findings[].messagestringDescription of the finding
columns_accessedarrayColumn-level governance details
columns_accessed[].tablestringFully qualified table name
columns_accessed[].columnstringColumn name
columns_accessed[].governedbooleanWhether this column has governance rules
columns_accessed[].is_piibooleanWhether this column is classified as PII
columns_accessed[].sensitivitystringColumn sensitivity level
columns_accessed[].maskingobjectMasking configuration (same schema as inspect-data-meaning)
policies_evaluatedarrayPolicies that were checked during validation
summarystringHuman-readable compliance summary
decisionobjectAuthorization decision (only present when operation and intended_use are provided). Same schema as authorize-use response.
extraction_methodstringCurrent parser strategy used for table and column extraction.
parser_confidencenumberConfidence in SQL extraction. Low confidence means the query should not be executed without review.
unsupported_patternsarraySQL patterns that require conservative handling.
ambiguous_columnsarrayColumn names that could not be mapped to one table confidently.
unknown_tablesarrayReferenced tables that are not governed by Metatate.
table_column_mapobjectPer-table confident column mapping used for embedded authorization.
agent_actionobjectCompact machine-actionable guidance for agents and pipelines.

Example Response

{
"status": "success",
"data": {
"status": "WARNING",
"compliant": true,
"tables_accessed": [
"ANALYTICS_DB.CORE.CUSTOMERS"
],
"sql_findings": [
{
"type": "warning",
"message": "Query accesses 3 PII columns: EMAIL, PHONE, SSN. Ensure masking is applied."
},
{
"type": "info",
"message": "Table ANALYTICS_DB.CORE.CUSTOMERS has sensitivity level 'confidential'."
}
],
"columns_accessed": [
{
"table": "ANALYTICS_DB.CORE.CUSTOMERS",
"column": "CUSTOMER_ID",
"governed": true,
"is_pii": false,
"sensitivity": "internal",
"masking": {
"enabled": false,
"type": "none"
}
},
{
"table": "ANALYTICS_DB.CORE.CUSTOMERS",
"column": "EMAIL",
"governed": true,
"is_pii": true,
"sensitivity": "confidential",
"masking": {
"enabled": true,
"type": "partial"
}
},
{
"table": "ANALYTICS_DB.CORE.CUSTOMERS",
"column": "SSN",
"governed": true,
"is_pii": true,
"sensitivity": "restricted",
"masking": {
"enabled": true,
"type": "hash"
}
}
],
"policies_evaluated": [
"pol_customer_pii_001",
"pol_audit_001"
],
"summary": "Query is compliant but accesses PII columns with active masking. Non-exempt roles will see masked values."
},
"errors": []
}

SQL Examples

Validate a query

CALL METATATE_APP.CORE.VALIDATE_QUERY_CONTEXT(
OBJECT_CONSTRUCT(
'sql', 'SELECT customer_id, email, ssn FROM analytics_db.core.customers WHERE region = ''US'''
)
);

Validate with authorization check

CALL METATATE_APP.CORE.VALIDATE_QUERY_CONTEXT(
OBJECT_CONSTRUCT(
'sql', 'SELECT * FROM analytics_db.core.customers LIMIT 1000',
'operation', 'export',
'intended_use', 'Export customer list for marketing campaign',
'actor_role', 'MARKETING_ANALYST'
)
);

Validate a JOIN query

CALL METATATE_APP.CORE.VALIDATE_QUERY_CONTEXT(
OBJECT_CONSTRUCT(
'sql', 'SELECT c.email, o.total_amount FROM analytics_db.core.customers c JOIN analytics_db.core.orders o ON c.customer_id = o.customer_id WHERE o.total_amount > 10000'
)
);

Validate an INSERT statement

CALL METATATE_APP.CORE.VALIDATE_QUERY_CONTEXT(
OBJECT_CONSTRUCT(
'sql', 'INSERT INTO analytics_db.staging.customer_export SELECT customer_id, name, email FROM analytics_db.core.customers',
'operation', 'transform',
'intended_use', 'Staging data for downstream ETL pipeline'
)
);

JSON Request / Response (API)

Request:

{
"method": "tools/call",
"params": {
"name": "validate-query-context",
"arguments": {
"sql": "SELECT customer_id, email FROM analytics_db.core.customers WHERE region = 'US'",
"operation": "read",
"intended_use": "Customer engagement analysis"
}
}
}

Response:

{
"content": [
{
"type": "text",
"text": "{\"status\":\"success\",\"data\":{\"status\":\"WARNING\",\"compliant\":true,\"tables_accessed\":[\"ANALYTICS_DB.CORE.CUSTOMERS\"],\"sql_findings\":[{\"type\":\"warning\",\"message\":\"Query accesses PII column: EMAIL. Masking is active.\"}],\"columns_accessed\":[{\"table\":\"ANALYTICS_DB.CORE.CUSTOMERS\",\"column\":\"CUSTOMER_ID\",\"governed\":true,\"is_pii\":false,\"sensitivity\":\"internal\",\"masking\":{\"enabled\":false,\"type\":\"none\"}},{\"table\":\"ANALYTICS_DB.CORE.CUSTOMERS\",\"column\":\"EMAIL\",\"governed\":true,\"is_pii\":true,\"sensitivity\":\"confidential\",\"masking\":{\"enabled\":true,\"type\":\"partial\"}}],\"policies_evaluated\":[\"pol_customer_pii_001\"],\"summary\":\"Query is compliant. EMAIL column is masked for non-exempt roles.\",\"decision\":{\"decision\":\"ALLOW\",\"confidence\":0.95,\"summary\":\"Read access for customer engagement analysis is permitted.\"}},\"errors\":[]}"
}
]
}
Try it in the app

Open Metatate and navigate to the Test Tools tab to run validate-query-context interactively. Paste any SQL query and see a formatted compliance report with highlighted findings.