🔍 Code Extractor

class SqlGenerationResult

Maturity: 41

A dataclass that encapsulates the results of an SQL query generation operation, including the generated query, explanation, confidence score, and metadata about database objects used.

File:
/tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
Lines:
28 - 34
Complexity:
simple

Purpose

This dataclass serves as a structured container for SQL generation results, providing a standardized way to return and pass around information about generated SQL queries. It captures not only the SQL query itself but also contextual information like confidence levels, explanatory text, and tracking of which database tables and columns were referenced. This is particularly useful for SQL generation systems that need to provide transparency and traceability of generated queries.

Source Code

class SqlGenerationResult:
    """Result of SQL generation pass"""
    sql_query: str
    explanation: str
    confidence: float
    tables_used: List[str]
    columns_used: Dict[str, List[str]]

Parameters

Name Type Default Kind
bases - -

Parameter Details

sql_query: The generated SQL query string that can be executed against a database. This is the primary output of the SQL generation process.

explanation: A human-readable explanation of what the SQL query does, useful for documentation, debugging, or user understanding of the generated query.

confidence: A float value (typically between 0.0 and 1.0) indicating the confidence level or quality score of the generated SQL query. Higher values indicate greater confidence in the correctness of the query.

tables_used: A list of table names (strings) that are referenced in the generated SQL query. This helps track which database tables are involved in the query.

columns_used: A dictionary mapping table names (keys) to lists of column names (values) that are used from each table. This provides detailed tracking of which specific columns from which tables are referenced in the query.

Return Value

Instantiation returns a SqlGenerationResult object with all five attributes populated. Since this is a dataclass, the __init__ method is automatically generated and returns an instance of SqlGenerationResult. The class itself doesn't have methods that return values, but instances can be used to access the stored SQL generation metadata.

Class Interface

Methods

__init__(sql_query: str, explanation: str, confidence: float, tables_used: List[str], columns_used: Dict[str, List[str]]) -> None

Purpose: Automatically generated constructor that initializes all instance attributes. This is created by the @dataclass decorator.

Parameters:

  • sql_query: The generated SQL query string
  • explanation: Human-readable explanation of the query
  • confidence: Confidence score (float) for the generated query
  • tables_used: List of table names referenced in the query
  • columns_used: Dictionary mapping table names to lists of column names used

Returns: None (initializes the instance)

Attributes

Name Type Description Scope
sql_query str The generated SQL query string that can be executed against a database instance
explanation str A human-readable explanation describing what the SQL query does instance
confidence float A confidence score indicating the quality or reliability of the generated SQL query instance
tables_used List[str] List of database table names that are referenced in the generated SQL query instance
columns_used Dict[str, List[str]] Dictionary mapping table names to lists of column names, tracking which columns from which tables are used in the query instance

Dependencies

  • dataclasses
  • typing

Required Imports

from dataclasses import dataclass
from typing import Dict, List

Usage Example

from dataclasses import dataclass
from typing import Dict, List

@dataclass
class SqlGenerationResult:
    sql_query: str
    explanation: str
    confidence: float
    tables_used: List[str]
    columns_used: Dict[str, List[str]]

# Create an instance with SQL generation results
result = SqlGenerationResult(
    sql_query="SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100",
    explanation="This query retrieves user names and order totals for orders exceeding $100 by joining users and orders tables.",
    confidence=0.95,
    tables_used=["users", "orders"],
    columns_used={
        "users": ["name", "id"],
        "orders": ["total", "user_id"]
    }
)

# Access the attributes
print(f"Generated SQL: {result.sql_query}")
print(f"Confidence: {result.confidence}")
print(f"Tables used: {', '.join(result.tables_used)}")
for table, columns in result.columns_used.items():
    print(f"Columns from {table}: {', '.join(columns)}")

Best Practices

  • This is an immutable data container by design (dataclass). Treat instances as read-only after creation.
  • The confidence value should typically be normalized between 0.0 and 1.0 for consistency across different SQL generation implementations.
  • Ensure tables_used list contains unique table names to avoid duplication.
  • The columns_used dictionary should only include tables that are actually present in tables_used for consistency.
  • When creating instances, validate that the sql_query is a non-empty string and syntactically valid SQL if possible.
  • The explanation field should be clear and concise, suitable for end-user consumption or logging purposes.
  • Consider using this class as a return type for SQL generation functions to provide comprehensive results in a structured format.
  • Since this is a dataclass, it automatically provides __repr__, __eq__, and other useful methods for debugging and comparison.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class TableSelectionResult 74.9% similar

    A dataclass that encapsulates the results of a table selection operation, including selected tables, reasoning, confidence score, and suggested joins.

    From: /tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
  • class IterationResult 70.1% similar

    A dataclass that encapsulates the complete results of a single iteration in a two-pass process, including table selection, SQL generation, and execution outcomes.

    From: /tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
  • class QueryIteration 65.5% similar

    A dataclass representing a single iteration in an iterative SQL query generation and evaluation workflow, capturing the query, its results, assessment, and improvement suggestions.

    From: /tf/active/vicechatdev/full_smartstat/enhanced_sql_workflow.py
  • class AnalysisResult_v1 64.8% similar

    A dataclass that encapsulates the results from statistical analysis operations, including metadata, file paths, and timestamps.

    From: /tf/active/vicechatdev/vice_ai/models.py
  • class AnalysisResult 64.7% similar

    A dataclass that encapsulates the results from statistical analysis operations, including metadata, file paths, and timestamps.

    From: /tf/active/vicechatdev/vice_ai/smartstat_models.py
← Back to Browse