googleapis/mcp-toolbox · error

EXECUTE IMMEDIATE is not allowed when dataset restrictions a

Error message

EXECUTE IMMEDIATE is not allowed when dataset restrictions are in place, as its contents cannot be safely analyzed

What it means

When dataset restrictions (allowedDatasets) are in effect, every table referenced by a query must be statically analyzable so access can be validated. EXECUTE IMMEDIATE runs arbitrary dynamic SQL whose table references cannot be parsed ahead of time, so parseSQL rejects it outright to prevent bypassing dataset restrictions.

Source

Thrown at internal/tools/bigquery/bigquerycommon/table_name_parser.go:267

						return 0, fmt.Errorf("querying non-dataset-level INFORMATION_SCHEMA view %q is not allowed when dataset restrictions are in place", viewName)
					}
					if infoSchemaIdx == 0 {
						return 0, fmt.Errorf("querying INFORMATION_SCHEMA views without a dataset prefix is not allowed when dataset restrictions are in place")
					}
					if infoSchemaIdx > 2 {
						return 0, fmt.Errorf("invalid INFORMATION_SCHEMA query path %q", strings.Join(parts, "."))
					}
					parts = parts[:infoSchemaIdx+1]
				}

				if len(parts) == 1 {
					keyword := strings.ToLower(parts[0])
					switch keyword {
					case "call":
						return 0, fmt.Errorf("CALL is not allowed when dataset restrictions are in place, as the called procedure's contents cannot be safely analyzed")
					case "immediate":
						if lastToken == "execute" {
							return 0, fmt.Errorf("EXECUTE IMMEDIATE is not allowed when dataset restrictions are in place, as its contents cannot be safely analyzed")
						}
					case "procedure", "function":
						if lastToken == "create" || lastToken == "create or replace" {
							return 0, fmt.Errorf("unanalyzable statements like '%s %s' are not allowed", strings.ToUpper(lastToken), strings.ToUpper(keyword))
						}
					case verbCreate, verbAlter, verbDrop, verbSelect, verbInsert, verbUpdate, verbDelete, verbMerge:
						if statementVerb == "" {
							statementVerb = keyword
						}
					}

					if statementVerb == verbCreate || statementVerb == verbAlter || statementVerb == verbDrop {
						if keyword == "schema" || keyword == "dataset" {
							return 0, fmt.Errorf("dataset-level operations like '%s %s' are not allowed when dataset restrictions are in place", strings.ToUpper(statementVerb), strings.ToUpper(keyword))
						}
					}

					if _, ok := tableFollowsKeywords[keyword]; ok {

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Remove the EXECUTE IMMEDIATE statement from the query
  2. Rewrite the logic as a plain static SQL statement listing its tables explicitly
  3. Execute the query against a tool/source without dataset restrictions, only if the user is authorized for all datasets
  4. Perform dynamic SQL server-side in a stored procedure and call that procedure where its contents are trusted

Example fix

// before
EXECUTE IMMEDIATE 'SELECT * FROM proj.ds.table WHERE id = 1';
// after
SELECT * FROM proj.ds.table WHERE id = 1;
Defensive patterns

Strategy: validation

Validate before calling

if strings.Contains(strings.ToUpper(sql), "EXECUTE IMMEDIATE") {
    return fmt.Errorf("query rejected: EXECUTE IMMEDIATE is not permitted under dataset restrictions")
}

Try / catch

tables, err := parser.Parse(sql)
if err != nil {
    if strings.Contains(err.Error(), "EXECUTE IMMEDIATE") {
        // surface a user-facing message: dynamic SQL is not allowed here
        return err
    }
    return err
}

Prevention

When it happens

Trigger: Calling TableParser/parseSQL on a statement containing the EXECUTE IMMEDIATE keyword while dataset restrictions are configured.

Common situations: Porting stored-procedure-style or dynamic-SQL workflows from other engines to BigQuery; generating SQL templates that build queries dynamically; users pasting scripting statements into a tool that validates table access.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/b6ac735e74439c93. Report an issue: GitHub.