Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit dc2febf

Browse files
author
Michael Liebmann
committed
feat: add simple aggregation pattern and overload error handling
1 parent 6ef14ff commit dc2febf

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/actions/chatWithYourDb.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,22 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
251251
- Verify totals match expected counts
252252
- Use appropriate DISTINCT counts
253253
- Document aggregation level in comments
254+
14. ALWAYS use simple queries when possible
255+
15. AVOID JOINs unless necessary
256+
16. USE indexes (primary keys) when available
254257
255258
PATTERN TEMPLATES (adapt to actual schema):
256-
1. Basic Counts with Existence Check:
259+
1. Simple Aggregation Pattern:
260+
SELECT
261+
COUNT(*) as record_count,
262+
ROUND(
263+
SUM(CAST(numeric_field AS NUMERIC)),
264+
2
265+
) as total_value
266+
FROM main_table
267+
WHERE condition; -- Optional
268+
269+
2. Basic Counts with Existence Check:
257270
WITH base_counts AS (
258271
SELECT
259272
t1.id, -- Replace with actual primary key
@@ -268,7 +281,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
268281
SUM(has_related) as related_count
269282
FROM base_counts;
270283
271-
2. Segmentation with Totals:
284+
3. Segmentation with Totals:
272285
WITH base_data AS (
273286
SELECT
274287
t1.id,
@@ -310,7 +323,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
310323
SELECT * FROM totals
311324
ORDER BY result_type DESC, segment;
312325
313-
3. Correlation Analysis:
326+
4. Correlation Analysis:
314327
WITH base_data AS (
315328
SELECT
316329
t1.id,
@@ -352,7 +365,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
352365
FROM segment_data
353366
GROUP BY segment;
354367
355-
4. Percentile-based Segmentation:
368+
5. Percentile-based Segmentation:
356369
WITH base_data AS (
357370
SELECT
358371
t1.*,
@@ -373,7 +386,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
373386
SELECT * FROM segment_metrics
374387
ORDER BY segment;
375388
376-
5. Multi-Level Aggregation Pattern:
389+
6. Multi-Level Aggregation Pattern:
377390
WITH base_aggregates AS (
378391
SELECT
379392
t1.primary_key, -- Replace with actual PK
@@ -565,6 +578,14 @@ function formatQueryResponse(sqlQuery: string): string {
565578
* )
566579
* SELECT COUNT(*) FROM order_metrics
567580
*
581+
* 8. "Overloaded Error"
582+
* Problem: Query too complex or taking too long
583+
* Solution:
584+
* - Use simpler queries for basic aggregations
585+
* - Add timeout handling
586+
* - Include simple aggregation pattern
587+
* - Avoid unnecessary JOINs for simple calculations
588+
*
568589
* IMPLEMENTATION REQUIREMENTS:
569590
* 1. Schema Awareness
570591
* - All queries must be built using actual schema information

0 commit comments

Comments
 (0)