Skip to content

execute_sql async

execute_sql(
    agent_context_id: AgentContextIdentifier,
    connection_id: ConnectionIdentifier,
    query: SqlDataQuery,
    *,
    config_overrides: Optional[AsyncContextClientConfig] = None,
) -> ExecuteSqlResponse

Runs one read-only SQL query over the tables of a connection in an agent context, and returns the result as typed columns and rows. Use this to read structured data directly when you can write the query yourself. If the result's rows are larger than 5 MiB, the call fails with ValidationException. Add a LIMIT clause or select fewer columns.

Parameters:

  • agent_context_id (AgentContextIdentifier) –

    The agent context that owns the connection, by ID or full ARN.

  • connection_id (ConnectionIdentifier) –

    The connection to run against. Must be a GLUE_DATA_CATALOG connection linked to the agent context. Its access configuration (accessConfig) determines how AWS Context reads the tables.

  • query (SqlDataQuery) –

    The query to run: a SQL statement and the AWS Glue tables that it reads. For the SQL rules and limits, see SqlQuery.

Raises:

  • AccessDeniedException –

    You don't have sufficient access to perform this operation. Verify that you have permission to perform the operation on the resource.

  • InternalServerException –

    The request failed because of an internal error. Retry the request.

  • ThrottlingException –

    The request was denied because it exceeded a request rate quota. Wait, then retry the request.

  • ValidationException –

    The input fails to satisfy the constraints of the operation. Correct the input before you retry the request.

  • ConflictException –

    The request conflicts with the current state of the resource. Check the resource's current state before you retry the request.

  • ResourceNotFoundException –

    The specified resource was not found. Check that the ID is correct and that the resource exists in your account and Region.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

Run a SQL query The following example totals order value by sales region from an AWS Glue table that a connection in the agent context links, and returns the top three sales regions.

>>> await client.execute_sql(agent_context_id='ac-0123456789abcdef0123456789abcdef', connection_id='conn-0123456789abcdef0123456789abcdef', query={'sql': {'statement': 'SELECT "sales_region", SUM("order_total") AS "total_sales" FROM "arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders" GROUP BY "sales_region" ORDER BY "total_sales" DESC LIMIT 3', 'tables': [{'glueTableArn': 'arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders'}]}})
Run a SQL query on a connection that isn't linked, which fails with ResourceNotFoundException
The following example fails with ResourceNotFoundException because the connection isn't linked to the agent context.
>>> await client.execute_sql(agent_context_id='ac-0123456789abcdef0123456789abcdef', connection_id='conn-fedcba9876543210fedcba9876543210', query={'sql': {'statement': 'SELECT "order_id" FROM "arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders" LIMIT 10', 'tables': [{'glueTableArn': 'arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders'}]}})
Source code in docs_src/capo_context/_services/async_context.py
async def execute_sql(
    self,
    agent_context_id: "capo_context.types.agent_context_identifier.AgentContextIdentifier",
    connection_id: "capo_context.types.connection_identifier.ConnectionIdentifier",
    query: "capo_context.types.sql_data_query.SqlDataQuery",
    *,
    config_overrides: Optional[AsyncContextClientConfig] = None,
) -> "capo_context.types.execute_sql_response.ExecuteSqlResponse":
    """<p>Runs one read-only SQL query over the tables of a connection in an agent context, and returns the result as typed columns and rows. Use this to read structured data directly when you can write the query yourself. If the result's rows are larger than 5 MiB, the call fails with <code>ValidationException</code>. Add a <code>LIMIT</code> clause or select fewer columns.</p>

    Args:
        agent_context_id: <p>The agent context that owns the connection, by ID or full ARN.</p>
        connection_id: <p>The connection to run against. Must be a GLUE_DATA_CATALOG connection linked to the agent context. Its access configuration (<code>accessConfig</code>) determines how AWS Context reads the tables.</p>
        query: <p>The query to run: a SQL statement and the AWS Glue tables that it reads. For the SQL rules and limits, see <code>SqlQuery</code>.</p>

    Raises:
        capo_context.errors.access_denied_exception.AccessDeniedException: <p>You don't have sufficient access to perform this operation. Verify that you have permission to perform the operation on the resource.</p>
        capo_context.errors.internal_server_exception.InternalServerException: <p>The request failed because of an internal error. Retry the request.</p>
        capo_context.errors.throttling_exception.ThrottlingException: <p>The request was denied because it exceeded a request rate quota. Wait, then retry the request.</p>
        capo_context.errors.validation_exception.ValidationException: <p>The input fails to satisfy the constraints of the operation. Correct the input before you retry the request.</p>
        capo_context.errors.conflict_exception.ConflictException: <p>The request conflicts with the current state of the resource. Check the resource's current state before you retry the request.</p>
        capo_context.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource was not found. Check that the ID is correct and that the resource exists in your account and Region.</p>
        capo_context.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        Run a SQL query
        The following example totals order value by sales region from an AWS Glue table that a connection in the agent context links, and returns the top three sales regions.

        >>> await client.execute_sql(agent_context_id='ac-0123456789abcdef0123456789abcdef', connection_id='conn-0123456789abcdef0123456789abcdef', query={'sql': {'statement': 'SELECT "sales_region", SUM("order_total") AS "total_sales" FROM "arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders" GROUP BY "sales_region" ORDER BY "total_sales" DESC LIMIT 3', 'tables': [{'glueTableArn': 'arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders'}]}})
        Run a SQL query on a connection that isn't linked, which fails with ResourceNotFoundException
        The following example fails with ResourceNotFoundException because the connection isn't linked to the agent context.

        >>> await client.execute_sql(agent_context_id='ac-0123456789abcdef0123456789abcdef', connection_id='conn-fedcba9876543210fedcba9876543210', query={'sql': {'statement': 'SELECT "order_id" FROM "arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders" LIMIT 10', 'tables': [{'glueTableArn': 'arn:aws:glue:us-east-1:111122223333:table/sales_analytics/orders'}]}})
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_context.types.execute_sql_request.ExecuteSqlRequest]",
    ) -> AsyncOperationResponse[
        "capo_context.types.execute_sql_response.ExecuteSqlResponse"
    ]:
        import capo_context._operations.context.execute_sql

        (
            output,
            http_response,
        ) = await capo_context._operations.context.execute_sql.async_execute_sql(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_context.types.execute_sql_request.ExecuteSqlRequest",
    ) -> dict[str, Any]:
        import capo_context.types.execute_sql_request

        return capo_context.types.execute_sql_request.redact_sensitive(input_)

    def _redact_output(
        output: "capo_context.types.execute_sql_response.ExecuteSqlResponse",
    ) -> dict[str, Any]:
        import capo_context.types.execute_sql_response

        return capo_context.types.execute_sql_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "ExecuteSql", redact_input=_redact_input, redact_output=_redact_output
        ),
    )
    input_: capo_context.types.execute_sql_request.ExecuteSqlRequest = {
        "agent_context_id": agent_context_id,
        "connection_id": connection_id,
        "query": query,
    }

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output