googleapis/mcp-toolbox · error

unable to execute query: %w

Error message

unable to execute query: %w

What it means

Returned by Source.RunSQL on the readOnly path when QueryContext fails to run a SELECT statement. The query could not be executed at all — bad SQL syntax, missing privileges, invalid identifiers, or a connection failure — so no result rows are produced. All of these causes are wrapped under this single message with the ORA-xxxxx error preserved via %w.

Source

Thrown at internal/sources/oracle/oracle.go:164

	if !readOnly {
		result, err := s.OracleDB().ExecContext(ctx, statement, params...)
		if err != nil {
			return nil, fmt.Errorf("unable to execute DML statement: %w", err)
		}

		rowsAffected, err := result.RowsAffected()
		if err != nil {
			return nil, fmt.Errorf("unable to get rows affected: %w", err)
		}

		return map[string]any{
			"status":        "success",
			"rows_affected": rowsAffected,
		}, nil
	}
	rows, err := s.OracleDB().QueryContext(ctx, statement, params...)
	if err != nil {
		return nil, fmt.Errorf("unable to execute query: %w", err)
	}
	defer rows.Close()

	// If Columns() errors, it might be a DDL/DML without an OUTPUT clause.
	// We proceed, and results.Err() will catch actual query execution errors.
	// 'out' will remain an empty slice if cols is empty or err is not nil here.
	cols, _ := rows.Columns()

	// Get Column types
	colTypes, err := rows.ColumnTypes()
	if err != nil {
		if err := rows.Err(); err != nil {
			return nil, fmt.Errorf("query execution error: %w", err)
		}
		return []any{}, nil
	}

	out := []any{}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Read the wrapped ORA-xxxxx code to identify the exact cause (invalid identifier, missing table, privilege, etc.).
  2. Run the same SELECT in sqlplus/SQL Developer as the same DB user to validate syntax and grants.
  3. Fix Oracle dialect issues: use FETCH FIRST n ROWS ONLY instead of LIMIT; qualify objects with the owning schema.
  4. Grant SELECT on the referenced tables/views to the toolbox user if ORA-00942/01031 appears.
  5. For timeouts, tune the query or increase the context deadline / add appropriate indexes.

Example fix

// before (MySQL syntax → ORA-00933)
statement: "SELECT * FROM employees ORDER BY hire_date LIMIT 10"
// after (Oracle dialect)
statement: "SELECT * FROM employees ORDER BY hire_date FETCH FIRST 10 ROWS ONLY"
Defensive patterns

Strategy: try-catch

Validate before calling

// catch common Oracle-dialect mistakes before executing
s := strings.ToLower(statement)
if regexp.MustCompile(`\blimit\s+\d+`).MatchString(s) {
	return fmt.Errorf("Oracle uses FETCH FIRST n ROWS ONLY, not LIMIT")
}

Type guard

func isQueryExecError(err error) bool {
	return err != nil && strings.Contains(err.Error(), "unable to execute query")
}

Try / catch

rows, err := src.RunSQL(ctx, stmt, params, true)
if err != nil {
	if isQueryExecError(err) {
		switch {
		case strings.Contains(err.Error(), "ORA-00942"):
			return fmt.Errorf("table or view not found; check schema qualification and grants: %w", err)
		case strings.Contains(err.Error(), "ORA-00904"):
			return fmt.Errorf("invalid column name in query: %w", err)
		case errors.Is(err, context.DeadlineExceeded):
			return fmt.Errorf("query timed out; optimize or raise deadline: %w", err)
		}
		return err
	}
	return err
}

Prevention

When it happens

Trigger: RunSQL(ctx, statement, params, readOnly=true) calling s.OracleDB().QueryContext where the driver errors: ORA-00942 table does not exist, ORA-00904 invalid identifier, ORA-00933/00923 syntax errors, ORA-01031 insufficient privileges, or context cancellation/timeouts.

Common situations: LLM-generated SELECTs with typos in column or table names; querying views the user has no grants on; dialect mistakes (e.g. MySQL LIMIT syntax instead of FETCH FIRST); statement timeout exceeded on large scans; connection dropped before query dispatch.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


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