class QueryIteration
A dataclass representing a single iteration in an iterative SQL query generation and evaluation workflow, capturing the query, its results, assessment, and improvement suggestions.
/tf/active/vicechatdev/full_smartstat/enhanced_sql_workflow.py
47 - 56
simple
Purpose
QueryIteration serves as a data container for tracking each step in an iterative SQL query refinement process. It stores the generated SQL query, explanation, sample data results, quality assessment, improvement suggestions, and satisfaction status. This class is typically used in workflows that iteratively improve SQL queries based on evaluation feedback, allowing the system to track progress and determine when a satisfactory query has been generated.
Source Code
class QueryIteration:
"""Single iteration of SQL query generation and evaluation"""
iteration_number: int
sql_query: str
query_explanation: str
data_sample: pd.DataFrame
data_assessment: Dict[str, Any]
suggested_improvements: List[str]
is_satisfactory: bool
improvement_reason: str = None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
iteration_number: Integer representing the sequence number of this iteration in the query refinement process (e.g., 1 for first attempt, 2 for second, etc.)
sql_query: String containing the SQL query generated in this iteration
query_explanation: String providing a human-readable explanation of what the SQL query does and its purpose
data_sample: Pandas DataFrame containing a sample of the data returned by executing the SQL query
data_assessment: Dictionary containing the evaluation results of the query and data quality, with keys describing various assessment metrics and their values
suggested_improvements: List of strings, each describing a specific improvement that could be made to the query in the next iteration
is_satisfactory: Boolean flag indicating whether this iteration's query meets the requirements and no further refinement is needed
improvement_reason: Optional string explaining why improvements are needed if is_satisfactory is False, or None if the query is satisfactory
Return Value
Instantiation returns a QueryIteration object that encapsulates all information about a single query generation and evaluation cycle. This object is immutable by convention (as a dataclass) and serves as a record in the iteration history.
Class Interface
Methods
__init__(iteration_number: int, sql_query: str, query_explanation: str, data_sample: pd.DataFrame, data_assessment: Dict[str, Any], suggested_improvements: List[str], is_satisfactory: bool, improvement_reason: str = None)
Purpose: Initialize a QueryIteration instance with all required iteration data
Parameters:
iteration_number: Integer sequence number of this iterationsql_query: The SQL query string generated in this iterationquery_explanation: Human-readable explanation of the querydata_sample: Pandas DataFrame with sample results from query executiondata_assessment: Dictionary containing evaluation metrics and assessment resultssuggested_improvements: List of improvement suggestions for next iterationis_satisfactory: Boolean indicating if query meets requirementsimprovement_reason: Optional explanation of why improvements are needed
Returns: QueryIteration instance
__repr__() -> str
Purpose: Auto-generated string representation of the QueryIteration instance
Returns: String representation showing all field values
__eq__(other) -> bool
Purpose: Auto-generated equality comparison between QueryIteration instances
Parameters:
other: Another QueryIteration instance to compare with
Returns: True if all fields are equal, False otherwise
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
iteration_number |
int | Sequence number of this iteration in the refinement process | instance |
sql_query |
str | The SQL query string generated in this iteration | instance |
query_explanation |
str | Human-readable explanation of what the query does | instance |
data_sample |
pd.DataFrame | Sample of data returned by executing the SQL query | instance |
data_assessment |
Dict[str, Any] | Dictionary containing evaluation metrics and quality assessment results | instance |
suggested_improvements |
List[str] | List of specific improvements suggested for the next iteration | instance |
is_satisfactory |
bool | Flag indicating whether this query meets requirements and needs no further refinement | instance |
improvement_reason |
str | Optional explanation of why improvements are needed (None if satisfactory) | instance |
Dependencies
pandastyping
Required Imports
from dataclasses import dataclass
import pandas as pd
from typing import Dict, List, Any
Usage Example
import pandas as pd
from dataclasses import dataclass
from typing import Dict, List, Any
# Create a QueryIteration instance
iteration = QueryIteration(
iteration_number=1,
sql_query="SELECT * FROM users WHERE age > 18",
query_explanation="Retrieves all users older than 18 years",
data_sample=pd.DataFrame({'id': [1, 2], 'name': ['Alice', 'Bob'], 'age': [25, 30]}),
data_assessment={'row_count': 2, 'completeness': 1.0, 'quality_score': 0.95},
suggested_improvements=['Add ORDER BY clause', 'Consider adding LIMIT'],
is_satisfactory=False,
improvement_reason="Query lacks ordering and pagination"
)
# Access attributes
print(f"Iteration {iteration.iteration_number}")
print(f"Query: {iteration.sql_query}")
print(f"Satisfactory: {iteration.is_satisfactory}")
print(f"Sample data shape: {iteration.data_sample.shape}")
# Check if improvements are needed
if not iteration.is_satisfactory:
print(f"Improvements needed: {', '.join(iteration.suggested_improvements)}")
print(f"Reason: {iteration.improvement_reason}")
Best Practices
- This is a dataclass, so it automatically generates __init__, __repr__, and __eq__ methods
- Treat instances as immutable records - do not modify attributes after creation
- Use this class to build a history of iterations by creating a new instance for each refinement cycle
- The improvement_reason field should be populated when is_satisfactory is False to provide context
- Ensure data_sample is a valid pandas DataFrame before instantiation
- The data_assessment dictionary should follow a consistent schema across iterations for easier comparison
- Consider serializing instances to JSON for logging or persistence (may require custom serialization for DataFrame)
- This class is typically instantiated by workflow orchestration code, not directly by end users
- Use iteration_number to track progress and detect infinite loops in refinement workflows
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class IterationResult 78.3% similar
-
function query_iteration_to_dict 70.5% similar
-
class SqlGenerationResult 65.5% similar
-
class TableSelectionResult 59.0% similar
-
class EnhancedSQLWorkflow 56.1% similar