You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(chat): constrain SQL row-limit placement to outermost query in prompt templates (#1202)
The row-limit policy in query_limit/no_query_limit did not specify WHERE
the limit should be applied, so LLMs could put LIMIT/ROWNUM/TOP inside
subqueries, truncating the data feeding aggregation (limiting accessed
rows instead of the final result set).
- query_limit/no_query_limit: limit must be applied only to the outermost
query; never inside subqueries, derived tables or CTEs; add conditional
per-group top-N note (window functions where supported)
- process_check (template.yaml + Oracle override): step 4 now verifies
limit placement
- sql_examples: add nested-aggregation bad/good examples for all 11
engines showing the limit on the outer query only
- Oracle: fix invalid GROUP BY...WHERE ROWNUM example and wrong
ROWNUM-before-ORDER BY few-shot answers (wrap with outer query)
SELECT COUNT("订单ID") FROM "TEST"."ORDERS" "t1" -- 错误:函数未加别名
111
111
</output-bad>
112
112
<output-good version="12c-below">
113
-
SELECT
113
+
SELECT "order_id", "amount", "total_orders", "discount_percent" FROM (
114
+
SELECT
115
+
"t1"."订单ID" AS "order_id",
116
+
"t1"."金额" AS "amount",
117
+
COUNT("t1"."订单ID") AS "total_orders",
118
+
TO_CHAR("t1"."折扣率" * 100, '990.99') || '%' AS "discount_percent"
119
+
FROM "TEST"."ORDERS" "t1"
120
+
GROUP BY "t1"."订单ID", "t1"."金额", "t1"."折扣率"
121
+
)
122
+
WHERE ROWNUM <= 100
123
+
</output-good>
124
+
<output-good version="12c+">
125
+
SELECT
114
126
"t1"."订单ID" AS "order_id",
115
127
"t1"."金额" AS "amount",
116
128
COUNT("t1"."订单ID") AS "total_orders",
117
129
TO_CHAR("t1"."折扣率" * 100, '990.99') || '%' AS "discount_percent"
118
130
FROM "TEST"."ORDERS" "t1"
119
131
GROUP BY "t1"."订单ID", "t1"."金额", "t1"."折扣率"
120
-
WHERE ROWNUM <= 100
132
+
FETCH FIRST 100 ROWS ONLY
121
133
</output-good>
122
134
</example>
123
135
@@ -197,11 +209,11 @@ template:
197
209
example_answer_1: |
198
210
{"success":true,"sql":"SELECT \"country\" AS \"country_name\", \"continent\" AS \"continent_name\", \"year\" AS \"year\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" ORDER BY \"country\", \"year\"","tables":["sample_country_gdp"],"chart-type":"line"}
199
211
example_answer_1_with_limit: |
200
-
{"success":true,"sql":"SELECT \"country\" AS \"country_name\", \"continent\" AS \"continent_name\", \"year\" AS \"year\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" WHERE ROWNUM <= 1000 ORDER BY \"country\", \"year\"","tables":["sample_country_gdp"],"chart-type":"line"}
212
+
{"success":true,"sql":"SELECT \"country_name\", \"continent_name\", \"year\", \"gdp\" FROM (SELECT \"country\" AS \"country_name\", \"continent\" AS \"continent_name\", \"year\" AS \"year\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" ORDER BY \"country\", \"year\") WHERE ROWNUM <= 1000","tables":["sample_country_gdp"],"chart-type":"line"}
201
213
example_answer_2: |
202
214
{"success":true,"sql":"SELECT \"country\" AS \"country_name\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" WHERE \"year\" = '2024' ORDER BY \"gdp\" DESC","tables":["sample_country_gdp"],"chart-type":"pie"}
203
215
example_answer_2_with_limit: |
204
-
{"success":true,"sql":"SELECT \"country\" AS \"country_name\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" WHERE \"year\" = '2024' AND ROWNUM <= 1000 ORDER BY \"gdp\" DESC","tables":["sample_country_gdp"],"chart-type":"pie"}
216
+
{"success":true,"sql":"SELECT \"country_name\", \"gdp\" FROM (SELECT \"country\" AS \"country_name\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" WHERE \"year\" = '2024' ORDER BY \"gdp\" DESC) WHERE ROWNUM <= 1000","tables":["sample_country_gdp"],"chart-type":"pie"}
205
217
example_answer_3: |
206
218
{"success":true,"sql":"SELECT \"country\" AS \"country_name\", \"gdp\" AS \"gdp\" FROM \"Sample_Database\".\"sample_country_gdp\" WHERE \"year\" = '2025' AND \"country\" = '中国'","tables":["sample_country_gdp"],"chart-type":"table"}
0 commit comments