Spaces:
Running
Running
Commit Β·
09b342a
1
Parent(s): 2e7546f
made fan out changes
Browse files- ai/signatures.py +78 -0
ai/signatures.py
CHANGED
|
@@ -87,6 +87,59 @@ class AnalyzeAndPlan(dspy.Signature):
|
|
| 87 |
β For: "total amount of PO123", "PO value", "purchase order cost".
|
| 88 |
β NEVER sum gold_amount + diamond_amount from PO line tables β that misses labour.
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 91 |
RULE 2 β STATUS FILTERING (DEFAULT = 'closed', ALWAYS)
|
| 92 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -261,6 +314,31 @@ class SQLGeneration(dspy.Signature):
|
|
| 261 |
FROM sales_table_v2_sales_order_line_pricing lp
|
| 262 |
GROUP BY lp.variant_sku ORDER BY gold_cost DESC LIMIT 10
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
5. USE PRE-COMPUTED TOTALS β NEVER RECONSTRUCT THEM:
|
| 265 |
- For order-level metrics (revenue, AOV): use sales_table_v2_sales_order.total_amount
|
| 266 |
- For PO totals: use purchase_orders_v6_purchase_order.total_amount
|
|
|
|
| 87 |
β For: "total amount of PO123", "PO value", "purchase order cost".
|
| 88 |
β NEVER sum gold_amount + diamond_amount from PO line tables β that misses labour.
|
| 89 |
|
| 90 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 91 |
+
RULE 1A β FAN-OUT: DEDUPLICATE BEFORE AGGREGATING ON JOIN CHAINS
|
| 92 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
+
purchase_orders_v6_po_sales_order_link has MULTIPLE rows per po_id.
|
| 94 |
+
Joining purchase_order β po_sales_order_link and then doing SUM(total_amount)
|
| 95 |
+
counts the same PO amount once per linked sales order β WRONG.
|
| 96 |
+
|
| 97 |
+
WRONG:
|
| 98 |
+
SELECT po.vendor_id, SUM(po.total_amount)
|
| 99 |
+
FROM purchase_orders_v6_purchase_order po
|
| 100 |
+
JOIN purchase_orders_v6_po_sales_order_link lnk ON po.po_id = lnk.po_id
|
| 101 |
+
GROUP BY po.vendor_id
|
| 102 |
+
|
| 103 |
+
CORRECT β wrap purchase_order in a DISTINCT subquery first:
|
| 104 |
+
SELECT vendor_id, SUM(total_amount)
|
| 105 |
+
FROM (
|
| 106 |
+
SELECT DISTINCT po.po_id, po.vendor_id, po.total_amount
|
| 107 |
+
FROM purchase_orders_v6_purchase_order po
|
| 108 |
+
JOIN purchase_orders_v6_po_sales_order_link lnk ON po.po_id = lnk.po_id
|
| 109 |
+
) deduped
|
| 110 |
+
GROUP BY vendor_id
|
| 111 |
+
|
| 112 |
+
Apply the DISTINCT-subquery fix whenever po_sales_order_link is in the JOIN chain
|
| 113 |
+
and you are aggregating any column from purchase_orders_v6_purchase_order.
|
| 114 |
+
|
| 115 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 116 |
+
RULE 1B β ROW MULTIPLICATION FROM DETAIL TABLE JOINS
|
| 117 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 118 |
+
sales_table_v2_sales_order_line_diamond and purchase_orders_v6_po_line_diamond
|
| 119 |
+
have MULTIPLE rows per line item (one per diamond type/shape/quality).
|
| 120 |
+
Joining them directly to pricing or header tables inflates every SUM.
|
| 121 |
+
For cost calculations: use sales_order_line_pricing which already has
|
| 122 |
+
rolled-up amounts (diamond_amount_per_unit, gold_amount_per_unit).
|
| 123 |
+
Only use detail tables when the question asks about diamond/gold PROPERTIES
|
| 124 |
+
(shape, quality, karat, carat weight) β never for cost or revenue totals.
|
| 125 |
+
|
| 126 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 127 |
+
RULE 1C β IGI/NC CERTIFICATION IS IN variant_sku, NOT quality
|
| 128 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 129 |
+
The quality column in diamond tables contains diamond grades (e.g. 'GH VVS').
|
| 130 |
+
IGI and NC are NOT values in that column.
|
| 131 |
+
Certification is the LAST segment of variant_sku:
|
| 132 |
+
105186-10K-Q12-IGI β IGI certified β variant_sku LIKE '%-IGI'
|
| 133 |
+
105186-10K-Q12-NC β non-certified β variant_sku LIKE '%-NC'
|
| 134 |
+
Apply this filter on sales_order_line or sales_order_line_pricing.
|
| 135 |
+
|
| 136 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
+
RULE 1D β NO product_master TABLE EXISTS
|
| 138 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 139 |
+
There is no product_master, products, or product_catalog table in this schema.
|
| 140 |
+
Product names do not exist β use product_id as the only product identifier.
|
| 141 |
+
Never reference a table that is not in the provided schema.
|
| 142 |
+
|
| 143 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 144 |
RULE 2 β STATUS FILTERING (DEFAULT = 'closed', ALWAYS)
|
| 145 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 314 |
FROM sales_table_v2_sales_order_line_pricing lp
|
| 315 |
GROUP BY lp.variant_sku ORDER BY gold_cost DESC LIMIT 10
|
| 316 |
|
| 317 |
+
4. FAN-OUT β DISTINCT subquery when joining purchase_order to po_sales_order_link:
|
| 318 |
+
po_sales_order_link has multiple rows per po_id β SUM(total_amount) double-counts.
|
| 319 |
+
WRONG: SELECT po.vendor_id, SUM(po.total_amount) FROM purchase_order po
|
| 320 |
+
JOIN po_sales_order_link lnk ON po.po_id = lnk.po_id GROUP BY po.vendor_id
|
| 321 |
+
CORRECT: SELECT vendor_id, SUM(total_amount) FROM (
|
| 322 |
+
SELECT DISTINCT po.po_id, po.vendor_id, po.total_amount
|
| 323 |
+
FROM purchase_orders_v6_purchase_order po
|
| 324 |
+
JOIN purchase_orders_v6_po_sales_order_link lnk ON po.po_id = lnk.po_id
|
| 325 |
+
) deduped GROUP BY vendor_id
|
| 326 |
+
|
| 327 |
+
4b. ROW MULTIPLICATION β never join sales_order_line_diamond or po_line_diamond
|
| 328 |
+
directly to pricing/header tables for cost calculations; they have multiple rows
|
| 329 |
+
per line item. Use sales_order_line_pricing rolled-up amounts instead.
|
| 330 |
+
|
| 331 |
+
4c. CERTIFICATION (IGI/NC) β comes from variant_sku last segment, NOT quality column:
|
| 332 |
+
IGI β variant_sku LIKE '%-IGI' NC β variant_sku LIKE '%-NC'
|
| 333 |
+
Apply on sales_order_line or sales_order_line_pricing.
|
| 334 |
+
|
| 335 |
+
4d. NO product_master TABLE β never reference it; it does not exist in this schema.
|
| 336 |
+
Use product_id only. Never invent tables not present in schema_info.
|
| 337 |
+
|
| 338 |
+
4e. REVENUE SOURCE β never mix:
|
| 339 |
+
Order-level: sales_table_v2_sales_order.total_amount
|
| 340 |
+
Line-level: sales_table_v2_sales_order_line_pricing.line_total
|
| 341 |
+
|
| 342 |
5. USE PRE-COMPUTED TOTALS β NEVER RECONSTRUCT THEM:
|
| 343 |
- For order-level metrics (revenue, AOV): use sales_table_v2_sales_order.total_amount
|
| 344 |
- For PO totals: use purchase_orders_v6_purchase_order.total_amount
|