Skip to main content
Glama
Teradata

Teradata MCP Server

Official
by Teradata

base_tableAffinity

Identify tables frequently accessed together in Teradata databases to reveal relationships and optimize SQL queries with rendered SQL metadata.

Instructions

Get tables commonly used together by database users, this is helpful to infer relationships between tables via SQLAlchemy, bind parameters if provided (prepared SQL), and return the fully rendered SQL (with literals) in metadata.

Arguments: database_name - Database name object_name - table or view name

Returns: ResponseType: formatted response with query results + metadata

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
database_nameYes
obj_nameYes

Implementation Reference

  • The handler function executing the logic for the 'base_tableAffinity' tool. It executes a predefined SQL query against DBQLObjTbl and DBQLogTbl to find tables commonly queried with the given database object, formats the results, and returns them with metadata.
    def handle_base_tableAffinity(conn: TeradataConnection, database_name: str, obj_name: str, *args, **kwargs):
        """
        Get tables commonly used together by database users, this is helpful to infer relationships between tables via SQLAlchemy, bind parameters if provided (prepared SQL), and return the fully rendered SQL (with literals) in metadata.
    
        Arguments:
          database_name - Database name
          object_name - table or view name
    
        Returns:
          ResponseType: formatted response with query results + metadata
        """
        logger.debug(f"Tool: handle_base_tableAffinity: Args: database_name: {database_name}, obj_name: {obj_name}")
        table_affiity_sql="""
        LOCKING ROW for ACCESS
        SELECT   TRIM(QTU2.DatabaseName)  AS "DatabaseName"
                , TRIM(QTU2.TableName)  AS "TableName"
                , COUNT(DISTINCT QTU1.QueryID) AS "QueryCount"
                , (current_timestamp - min(QTU2.CollectTimeStamp)) day(4) as "FirstQueryDaysAgo"
                , (current_timestamp - max(QTU2.CollectTimeStamp)) day(4) as "LastQueryDaysAgo"
        FROM    (
                            SELECT   objectdatabasename AS DatabaseName
                                , ObjectTableName AS TableName
                                , QueryId
                            FROM DBC.DBQLObjTbl /* for DBC */
                            WHERE Objecttype in ('Tab', 'Viw')
                            AND ObjectTableName = '{table_name}'
                            AND objectdatabasename = '{database_name}'
                            AND ObjectTableName IS NOT NULL
                            AND ObjectColumnName IS NULL
                            -- AND LogDate BETWEEN '2017-01-01' AND '2017-08-01' /* uncomment for PDCR */
                            --	AND LogDate BETWEEN current_date - 90 AND current_date - 1 /* uncomment for PDCR */
                            GROUP BY 1,2,3
                        ) AS QTU1
                        INNER JOIN
                        (
                            SELECT   objectdatabasename AS DatabaseName
                                , ObjectTableName AS TableName
                                , QueryId
                                , CollectTimeStamp
                            FROM DBC.DBQLObjTbl /* for DBC */
                            WHERE Objecttype in ('Tab', 'Viw')
                            AND ObjectTableName IS NOT NULL
                            AND ObjectColumnName IS NULL
                            GROUP BY 1,2,3, 4
                        ) AS QTU2
                        ON QTU1.QueryID=QTU2.QueryID
                        INNER JOIN DBC.DBQLogTbl QU /* uncomment for DBC */
                        -- INNER JOIN DBC.DBQLogTbl QU /* uncomment for PDCR */
                        ON QTU1.QueryID=QU.QueryID
        WHERE (TRIM(QTU2.TableName) <> TRIM(QTU1.TableName) OR  TRIM(QTU2.DatabaseName) <> TRIM(QTU1.DatabaseName))
        AND (QU.AMPCPUTime + QU.ParserCPUTime) > 0
        GROUP BY 1,2
        ORDER BY 3 DESC, 5 DESC
    --    having "QueryCount">10
        ;
    
        """
        with conn.cursor() as cur:
            rows = cur.execute(table_affiity_sql.format(table_name=obj_name, database_name=database_name))
            data = rows_to_json(cur.description, rows.fetchall())
        if len(data):
            affinity_info=f'This data contains the list of tables most commonly queried alongside object {database_name}.{obj_name}'
        else:
            affinity_info=f'Object {database_name}.{obj_name} is not often queried with any other table or queried at all, try other ways to infer its relationships.'
        metadata = {
            "tool_name": "handle_base_tableAffinity",
            "database": database_name,
            "object": obj_name,
            "table_count": len(data),
            "comment": affinity_info
        }
        logger.debug(f"Tool: handle_base_tableAffinity: metadata: {metadata}")
        return create_response(data, metadata)
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions SQLAlchemy integration, bind parameters, and metadata returns, but lacks critical details: whether this is a read-only operation, performance characteristics, authentication requirements, or what 'commonly used together' actually means algorithmically. The description provides some context but leaves significant behavioral questions unanswered.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is reasonably concise with three sentences: purpose statement, argument list, and return information. It's front-loaded with the core functionality. The argument section could be more integrated, but overall there's little wasted text.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 2 parameters, 0% schema coverage, no annotations, and no output schema, the description is incomplete. It identifies parameters and mentions SQL/metadata aspects but doesn't explain the algorithm for determining 'commonly used together', doesn't describe the response format despite no output schema, and doesn't address performance or limitations. The context signals indicate this tool needs more comprehensive documentation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the schema provides no parameter documentation. The description lists 'database_name' and 'object_name' in its Arguments section, correctly mapping to the schema's 'database_name' and 'obj_name'. However, it doesn't explain what these parameters represent beyond their names or provide format examples. The description adds basic parameter identification but minimal semantic value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Get tables commonly used together by database users' and adds helpful context about inferring relationships via SQLAlchemy and returning rendered SQL in metadata. It specifies the verb ('Get') and resource ('tables commonly used together'), but doesn't explicitly differentiate from siblings like base_tableUsage or base_tableList.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided about when to use this tool versus alternatives. With many sibling tools focused on tables (base_tableList, base_tableUsage, base_tablePreview, etc.), the description doesn't indicate when this affinity analysis is preferable to other table-related operations.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Teradata/teradata-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server