11from langchain .chat_models import init_chat_model
22
3+ from piglets .database import DatabaseConnector
34from piglets .policies import SemanticRules
45from piglets .types import (
6+ Database ,
57 ProfilingQueries ,
8+ QueryResult ,
9+ QueryResults ,
610 SemanticLinkingResult ,
711 Table ,
12+ TableProfileResult ,
813)
914
10-
1115class Profiler ():
1216 def __init__ (
1317 self ,
1418 model_name : str ,
19+ database : Database ,
1520 model_provider : str = None ,
1621 rules : SemanticRules | None = None ,
1722 ):
1823 self .model_name = model_name
24+ self .database = database
1925 self .model_provider = model_provider
2026 self .rules = rules or SemanticRules ()
2127
22- def profile_table (
28+ def _generate_table_profiler_queries (
2329 self ,
2430 natural_language_query : str ,
25- database_type : str | None ,
2631 table : Table ,
27- semantic_linking_result : SemanticLinkingResult ,
32+ semantic_linking_result : SemanticLinkingResult | None = None ,
2833 ) -> ProfilingQueries :
2934
3035 llm = init_chat_model (model = self .model_name , model_provider = self .model_provider )
3136 llm = llm .with_structured_output (ProfilingQueries )
3237 critical_rules = self .rules .critical_rules_to_string ()
33- sql_type = f"{ database_type .upper ()} SQL" if database_type else "SQL"
34- semantic_table_functions_lower = {k .lower (): v for k , v in semantic_linking_result .table_functions .items ()}
35- semantic_table_function = semantic_table_functions_lower .get (table .name .lower (), "Unknown Role" )
38+ sql_type = f"{ self .database .database_type .upper ()} SQL"
39+ semantic_table_functions_lower = (
40+ {k .lower (): v for k , v in semantic_linking_result .table_functions .items ()}
41+ if semantic_linking_result
42+ else {}
43+ )
44+ semantic_table_function = semantic_table_functions_lower .get (
45+ table .name .lower (),
46+ "Unknown Role" ,
47+ )
3648
3749 # TODO: Include column descriptions here once Column supports them.
3850 DATA_PROFILE_QUERY_GENERATION_PROMPT = f"""
@@ -76,7 +88,7 @@ def profile_table(
7688
7789 *** OUTPUT FORMAT ***
7890 Return structured data matching the ProfilingQueries schema:
79- - exploratory_queries : a list of 3-8 objects.
91+ - query : a list of 3-8 objects.
8092 - Each object must contain:
8193 - motivation: a concise reason for running the profiling query.
8294 - query: the { sql_type } query string.
@@ -89,3 +101,108 @@ def profile_table(
89101 profiling_queries = llm .invoke (DATA_PROFILE_QUERY_GENERATION_PROMPT )
90102
91103 return profiling_queries
104+
105+ # TODO: Parallel query execution
106+ def _execute_table_profiling_queries (
107+ self ,
108+ database_connector : DatabaseConnector ,
109+ profiling_queries : ProfilingQueries ,
110+ ) -> QueryResults :
111+ query_results = QueryResults ()
112+ for profiling_query in profiling_queries .query :
113+ query_result : QueryResult = database_connector .execute_query (profiling_query )
114+ query_results .query_results .append (query_result )
115+ return query_results
116+
117+ def _profile_table_from_query_results (
118+ self ,
119+ natural_language_query : str ,
120+ query_results : QueryResults ,
121+ table : Table ,
122+ ) -> TableProfileResult :
123+ llm = init_chat_model (model = self .model_name , model_provider = self .model_provider )
124+ llm = llm .with_structured_output (TableProfileResult )
125+
126+ TABLE_PROFILE_PROMPT = f"""
127+ *** TASK ***
128+ Based on the exploration history and results above,
129+ determine if table { table .name } is RELEVANT to the User
130+ Question.
131+ *** EXPLORATION EVIDENCE ***
132+ { query_results .to_string ()}
133+ *** USER QUESTION ***
134+ { natural_language_query }
135+ *** DECISION GUIDELINES ***
136+ - **Direct Match**: Contains the specific answer data.
137+ - **Bridge Table**: Contains IDs needed to join other relevant tables (CRITICAL: Keep even if no other useful data).
138+ - **Filter Source**: Contains columns needed to restrict the
139+ result.
140+ - **Calculation Support**: Contains numerical columns
141+ needed for aggregation (e.g., score for AVG, price for
142+ SUM).
143+ *** OUTPUT GUIDELINES ***
144+ - relevant: Whether this table is relevant to the user question.
145+ - relevant_columns: Columns relevant to the user question.
146+ - column_name: The relevant column name.
147+ - relevance_reason: Explain the column's logical role.
148+ - observations: Summarize factual findings from exploration.
149+ - table_summary: A concise summary of what this table
150+ represents in the context of the query.
151+ *** OUTPUT FORMAT ***
152+ Return structured data matching the TableProfileResult schema:
153+ - relevant: boolean.
154+ - relevant_columns: list of TableProfileColumnResult objects.
155+ - Each relevant_columns item must contain:
156+ - column_name: string.
157+ - relevance_reason: string.
158+ - observations: string.
159+ - table_summary: string.
160+
161+ Example shape:
162+ ```json
163+ {{
164+ "relevant": true,
165+ "relevant_columns": [
166+ {{
167+ "column_name": "name",
168+ "relevance_reason": "...",
169+ "observations": "..."
170+ }}
171+ ],
172+ "table_summary": "..."
173+ }}
174+ ```
175+ Provide your analysis:
176+ """
177+
178+ table_profile_result = llm .invoke (TABLE_PROFILE_PROMPT )
179+
180+ return table_profile_result
181+
182+ def profile_table (
183+ self ,
184+ natural_language_query : str ,
185+ table : Table ,
186+ database_connector : DatabaseConnector ,
187+ semantic_linking_result : SemanticLinkingResult | None = None ,
188+ ) -> TableProfileResult :
189+ profiling_queries = self ._generate_table_profiler_queries (
190+ natural_language_query = natural_language_query ,
191+ table = table ,
192+ semantic_linking_result = semantic_linking_result ,
193+ )
194+ query_results = self ._execute_table_profiling_queries (
195+ database_connector = database_connector ,
196+ profiling_queries = profiling_queries ,
197+ )
198+ table_profile_result = self ._profile_table_from_query_results (
199+ natural_language_query = natural_language_query ,
200+ query_results = query_results ,
201+ table = table ,
202+ )
203+ return table_profile_result
204+
205+ #TODO profile self.database by profiling all of it's tables in parallel
206+ # returning a list of TableProfileResults
207+ #def profile_database(self) -> DatabaseProfileResult:
208+
0 commit comments