@@ -270,6 +270,13 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
270
270
- Filter early in CTEs
271
271
- Use indexes (typically primary keys) when available
272
272
273
+ 9. Numeric Calculations:
274
+ - Always CAST numeric inputs before ROUND: ROUND(CAST(value AS NUMERIC), 2)
275
+ - For percentages: ROUND(CAST(value * 100.0 AS NUMERIC), 2)
276
+ - For monetary values: ROUND(CAST(value AS NUMERIC), 2)
277
+ - For ratios/divisions: ROUND(CAST(CAST(numerator AS NUMERIC) / NULLIF(denominator, 0) AS NUMERIC), 2)
278
+ - Handle NULLs: COALESCE(value, 0)
279
+
273
280
IMPLEMENTATION REQUIREMENTS:
274
281
- Generate only SELECT queries (no modifications)
275
282
- Include LIMIT ${ maxRows } in final results
@@ -432,6 +439,25 @@ function formatQueryResponse(sqlQuery: string): string {
432
439
* - Include simple aggregation pattern
433
440
* - Avoid unnecessary JOINs for simple calculations
434
441
*
442
+ * 9. "function round(double precision, integer) does not exist"
443
+ * Problem: PostgreSQL type mismatch with ROUND function
444
+ * Solution:
445
+ * - Always CAST to NUMERIC before ROUND
446
+ * - Use proper numeric calculation patterns:
447
+ * * Percentages: ROUND(CAST(value * 100.0 AS NUMERIC), 2)
448
+ * * Money: ROUND(CAST(value AS NUMERIC), 2)
449
+ * * Ratios: ROUND(CAST(CAST(num AS NUMERIC) / NULLIF(denom, 0) AS NUMERIC), 2)
450
+ * Example fix:
451
+ * Instead of:
452
+ * ROUND(price * quantity, 2)
453
+ * Use:
454
+ * ROUND(CAST(price * quantity AS NUMERIC), 2)
455
+ * Testing:
456
+ * - Test with decimal values
457
+ * - Test with integer values
458
+ * - Test with NULL values
459
+ * - Test with zero denominators in divisions
460
+ *
435
461
* IMPLEMENTATION REQUIREMENTS:
436
462
* 1. Schema Awareness
437
463
* - All queries must be built using actual schema information
0 commit comments