Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/AWS_Redshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ template:
FETCH FIRST 1000 ROWS ONLY
</output-good>
</example>

<example>
<input>统计订单表 TEST.ORDERS 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT "t"."customer_id" AS "customer_id", SUM("t"."amount") AS "total_amount"
FROM (SELECT "customer_id", "amount" FROM "TEST"."ORDERS" WHERE "create_time" >= '2024-01-01' LIMIT 100) "t"
GROUP BY "t"."customer_id"
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
"o"."customer_id" AS "customer_id",
SUM("o"."amount") AS "total_amount"
FROM "TEST"."ORDERS" "o"
WHERE "o"."create_time" >= '2024-01-01'
GROUP BY "o"."customer_id"
ORDER BY "total_amount" DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: AWS Redshift 1.0
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/ClickHouse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ template:
LIMIT 5
</output-good>
</example>

<example>
<input>统计订单表 default.orders 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT "t"."customer_id" AS "customer_id", SUM("t"."amount") AS "total_amount"
FROM (SELECT "customer_id", "amount" FROM "default"."orders" WHERE "create_time" >= '2024-01-01' LIMIT 100) "t"
GROUP BY "t"."customer_id"
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
"o"."customer_id" AS "customer_id",
SUM("o"."amount") AS "total_amount"
FROM "default"."orders" "o"
WHERE "o"."create_time" >= '2024-01-01'
GROUP BY "o"."customer_id"
ORDER BY "total_amount" DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: ClickHouse 23.3
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/DM.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ template:
LIMIT 0, 1000 -- 达梦兼容写法
</output-good>
</example>

<example>
<input>统计订单表 TEST.ORDERS 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT "t"."customer_id" AS "customer_id", SUM("t"."amount") AS "total_amount"
FROM (SELECT "customer_id", "amount" FROM "TEST"."ORDERS" WHERE "create_time" >= '2024-01-01' LIMIT 100) "t"
GROUP BY "t"."customer_id"
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
"o"."customer_id" AS "customer_id",
SUM("o"."amount") AS "total_amount"
FROM "TEST"."ORDERS" "o"
WHERE "o"."create_time" >= '2024-01-01'
GROUP BY "o"."customer_id"
ORDER BY "total_amount" DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: DM Database 8
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/Doris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ template:
LIMIT 1000
</output-good>
</example>

<example>
<input>统计订单表 test.orders 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT `t`.`customer_id` AS `customer_id`, SUM(`t`.`amount`) AS `total_amount`
FROM (SELECT `customer_id`, `amount` FROM `test`.`orders` WHERE `create_time` >= '2024-01-01' LIMIT 100) `t`
GROUP BY `t`.`customer_id`
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
`o`.`customer_id` AS `customer_id`,
SUM(`o`.`amount`) AS `total_amount`
FROM `test`.`orders` `o`
WHERE `o`.`create_time` >= '2024-01-01'
GROUP BY `o`.`customer_id`
ORDER BY `total_amount` DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: Apache Doris 2.0
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/Elasticsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ template:
LIMIT 5
</output-good>
</example>

<example>
<input>统计 logs-* 索引中最近一天各个客户端IP的日志数量,返回数量最多的前100个IP</input>
<output-bad>
SELECT "t"."host.ip" AS "client_ip", COUNT("t"."message") AS "log_count"
FROM (SELECT "host.ip", "message" FROM "logs-*" LIMIT 100) "t"
GROUP BY "t"."host.ip"
-- 严重错误:内层LIMIT导致聚合只基于被截断的100行数据计算,结果错误!Elasticsearch SQL也不支持FROM子查询
</output-bad>
<output-good>
SELECT
"l"."host.ip" AS "client_ip",
COUNT("l"."message") AS "log_count"
FROM "logs-*" "l"
WHERE CAST("l"."@timestamp" AS TIMESTAMP) > DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY "l"."host.ip"
ORDER BY "log_count" DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在整个查询的最外层
</output-good>
</example>
</basic-examples>

example_engine: Elasticsearch SQL 8.9
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/Hive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ template:
WHERE `u`.`status` = 1
</output-good>
</example>

<example>
<input>统计订单表 ods.orders 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT `t`.`customer_id` AS `customer_id`, SUM(`t`.`amount`) AS `total_amount`
FROM (SELECT `customer_id`, `amount` FROM `ods`.`orders` WHERE YEAR(`create_time`) = 2024 LIMIT 100) `t`
GROUP BY `t`.`customer_id`
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
`o`.`customer_id` AS `customer_id`,
SUM(`o`.`amount`) AS `total_amount`
FROM `ods`.`orders` `o`
WHERE YEAR(`o`.`create_time`) = 2024
GROUP BY `o`.`customer_id`
ORDER BY `total_amount` DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: Apache Hive 2.X
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/Kingbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ template:
WHERE "u"."status" = 1
</output-good>
</example>

<example>
<input>统计订单表 TEST.ORDERS 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT "t"."customer_id" AS "customer_id", SUM("t"."amount") AS "total_amount"
FROM (SELECT "customer_id", "amount" FROM "TEST"."ORDERS" WHERE "create_time" >= '2024-01-01' LIMIT 100) "t"
GROUP BY "t"."customer_id"
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
"o"."customer_id" AS "customer_id",
SUM("o"."amount") AS "total_amount"
FROM "TEST"."ORDERS" "o"
WHERE "o"."create_time" >= '2024-01-01'
GROUP BY "o"."customer_id"
ORDER BY "total_amount" DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: Kingbase V8
Expand Down
20 changes: 20 additions & 0 deletions backend/templates/sql_examples/Microsoft_SQL_Server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ template:
OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY
</output-good>
</example>

<example>
<input>统计订单表 Sales.Orders 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT [t].[customer_id] AS [customer_id], SUM([t].[amount]) AS [total_amount]
FROM (SELECT TOP 100 [customer_id], [amount] FROM [Sales].[Orders] WHERE [create_time] >= '2024-01-01') [t]
GROUP BY [t].[customer_id]
-- 严重错误:TOP加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT TOP 100
[o].[customer_id] AS [customer_id],
SUM([o].[amount]) AS [total_amount]
FROM [Sales].[Orders] [o]
WHERE [o].[create_time] >= '2024-01-01'
GROUP BY [o].[customer_id]
ORDER BY [total_amount] DESC
-- 正确:TOP只加在最外层查询上,聚合基于完整数据计算,只限制最终返回结果的行数
</output-good>
</example>
</basic-examples>

example_engine: Microsoft SQL Server 2022
Expand Down
23 changes: 22 additions & 1 deletion backend/templates/sql_examples/MySQL.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,35 @@ template:
SELECT `desc`, ROUND(active_ratio*100) FROM `public`.`users` -- 错误:百分比格式错误
</output-bad>
<output-good>
SELECT
SELECT
`u`.`desc` AS `description`,
CONCAT(ROUND(`u`.`active_ratio` * 100, 2), '%') AS `active_percent`
FROM `public`.`users` `u`
WHERE `u`.`status` = 1
LIMIT 1000
</output-good>
</example>

<example>
<input>统计订单表 test.orders 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT `t`.`customer_id` AS `customer_id`, SUM(`t`.`amount`) AS `total_amount`
FROM (SELECT `customer_id`, `amount` FROM `test`.`orders` WHERE `create_time` >= '2024-01-01' LIMIT 100) `t`
GROUP BY `t`.`customer_id`
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
`o`.`customer_id` AS `customer_id`,
SUM(`o`.`amount`) AS `total_amount`
FROM `test`.`orders` `o`
WHERE `o`.`create_time` >= '2024-01-01'
GROUP BY `o`.`customer_id`
ORDER BY `total_amount` DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: MySQL 8.0
Expand Down
22 changes: 17 additions & 5 deletions backend/templates/sql_examples/Oracle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ template:
<step>1. 分析用户问题,确定查询需求</step>
<step>2. 根据表结构生成基础SQL</step>
<step>3. <strong>强制检查:逐字验证SQL中使用的表名和字段名是否在<m-schema>中定义,且字符必须完全匹配(包括繁简体、大小写、特殊字符),不得有任何转换。如发现任何不一致,必须重新生成SQL。</strong></step>
<step>4. <strong>强制检查:应用数据量限制规则(默认限制或用户指定数量)</strong></step>
<step>4. <strong>强制检查:应用数据量限制规则(默认限制或用户指定数量),限制必须位于最外层查询,不得出现在子查询、派生表、CTE内部</strong></step>
<step>5. 应用其他规则(引号、别名、格式化等)</step>
<step>6. <strong>最终验证:GROUP BY查询的ROWNUM位置是否正确?</strong></step>
<step>7. <strong>强制检查:验证SQL语法是否符合<db-engine>规范</strong></step>
Expand Down Expand Up @@ -110,14 +110,26 @@ template:
SELECT COUNT("订单ID") FROM "TEST"."ORDERS" "t1" -- 错误:函数未加别名
</output-bad>
<output-good version="12c-below">
SELECT
SELECT "order_id", "amount", "total_orders", "discount_percent" FROM (
SELECT
"t1"."订单ID" AS "order_id",
"t1"."金额" AS "amount",
COUNT("t1"."订单ID") AS "total_orders",
TO_CHAR("t1"."折扣率" * 100, '990.99') || '%' AS "discount_percent"
FROM "TEST"."ORDERS" "t1"
GROUP BY "t1"."订单ID", "t1"."金额", "t1"."折扣率"
)
WHERE ROWNUM <= 100
</output-good>
<output-good version="12c+">
SELECT
"t1"."订单ID" AS "order_id",
"t1"."金额" AS "amount",
COUNT("t1"."订单ID") AS "total_orders",
TO_CHAR("t1"."折扣率" * 100, '990.99') || '%' AS "discount_percent"
FROM "TEST"."ORDERS" "t1"
GROUP BY "t1"."订单ID", "t1"."金额", "t1"."折扣率"
WHERE ROWNUM <= 100
FETCH FIRST 100 ROWS ONLY
</output-good>
</example>

Expand Down Expand Up @@ -197,11 +209,11 @@ template:
example_answer_1: |
{"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"}
example_answer_1_with_limit: |
{"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"}
{"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"}
example_answer_2: |
{"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"}
example_answer_2_with_limit: |
{"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"}
{"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"}
example_answer_3: |
{"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"}
example_answer_3_with_limit: |
Expand Down
23 changes: 22 additions & 1 deletion backend/templates/sql_examples/PostgreSQL.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,34 @@ template:
SELECT "user", ROUND(active_ratio) FROM "PUBLIC"."USERS" -- 错误:百分比格式错误
</output-bad>
<output-good>
SELECT
SELECT
"u"."user" AS "username",
ROUND("u"."active_ratio" * 100, 2) || '%' AS "active_percent"
FROM "PUBLIC"."USERS" "u"
WHERE "u"."status" = 1
</output-good>
</example>

<example>
<input>统计订单表 TEST.ORDERS 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT "t"."customer_id" AS "customer_id", SUM("t"."amount") AS "total_amount"
FROM (SELECT "customer_id", "amount" FROM "TEST"."ORDERS" WHERE "create_time" >= '2024-01-01' LIMIT 100) "t"
GROUP BY "t"."customer_id"
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
"o"."customer_id" AS "customer_id",
SUM("o"."amount") AS "total_amount"
FROM "TEST"."ORDERS" "o"
WHERE "o"."create_time" >= '2024-01-01'
GROUP BY "o"."customer_id"
ORDER BY "total_amount" DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: PostgreSQL17.6 (Debian 17.6-1.pgdg12+1)
Expand Down
21 changes: 21 additions & 0 deletions backend/templates/sql_examples/StarRocks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ template:
LIMIT 1000
</output-good>
</example>

<example>
<input>统计订单表 test.orders 中2024年每个客户的消费总金额,返回金额最高的前100名客户</input>
<output-bad>
SELECT `t`.`customer_id` AS `customer_id`, SUM(`t`.`amount`) AS `total_amount`
FROM (SELECT `customer_id`, `amount` FROM `test`.`orders` WHERE `create_time` >= '2024-01-01' LIMIT 100) `t`
GROUP BY `t`.`customer_id`
-- 严重错误:LIMIT加在了内层子查询中,聚合只基于被截断的100行数据计算,结果错误!
</output-bad>
<output-good>
SELECT
`o`.`customer_id` AS `customer_id`,
SUM(`o`.`amount`) AS `total_amount`
FROM `test`.`orders` `o`
WHERE `o`.`create_time` >= '2024-01-01'
GROUP BY `o`.`customer_id`
ORDER BY `total_amount` DESC
LIMIT 100
-- 正确:聚合基于完整数据计算,LIMIT只限制最终返回结果的行数,且只出现在最外层查询
</output-good>
</example>
</basic-examples>

example_engine: StarRocks 3.0
Expand Down
Loading
Loading