diff --git a/backend/apps/chat/models/chat_model.py b/backend/apps/chat/models/chat_model.py index dc19b7e8..33872005 100644 --- a/backend/apps/chat/models/chat_model.py +++ b/backend/apps/chat/models/chat_model.py @@ -200,8 +200,8 @@ def sql_sys_question(self, db_type: Union[str, DB], enable_query_limit: bool = T _process_check = _sql_template.get('process_check') if _sql_template.get('process_check') else _base_template[ 'process_check'] _query_limit = _base_template['query_limit'] if enable_query_limit else _base_template['no_query_limit'] - _base_sql_rules = _sql_template['quot_rule'] + _query_limit + _sql_template['limit_rule'] + _sql_template[ - 'other_rule'] + _other_rule = _sql_template['other_rule'].format(multi_table_condition=_base_template['multi_table_condition']) + _base_sql_rules = _sql_template['quot_rule'] + _query_limit + _sql_template['limit_rule'] + _other_rule _sql_examples = _sql_template['basic_example'] _example_engine = _sql_template['example_engine'] _example_answer_1 = _sql_template['example_answer_1_with_limit'] if enable_query_limit else _sql_template[ diff --git a/backend/templates/sql_examples/AWS_Redshift.yaml b/backend/templates/sql_examples/AWS_Redshift.yaml index 5036941e..40036b97 100644 --- a/backend/templates/sql_examples/AWS_Redshift.yaml +++ b/backend/templates/sql_examples/AWS_Redshift.yaml @@ -20,6 +20,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/ClickHouse.yaml b/backend/templates/sql_examples/ClickHouse.yaml index f278b62b..c2b56f2d 100644 --- a/backend/templates/sql_examples/ClickHouse.yaml +++ b/backend/templates/sql_examples/ClickHouse.yaml @@ -21,6 +21,7 @@ template: other_rule: | 必须为每个表生成简短别名(如t1/t2) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 JSON字段需用点号语法访问:`"column.field"` 函数字段必须加别名 diff --git a/backend/templates/sql_examples/DM.yaml b/backend/templates/sql_examples/DM.yaml index bb04396d..29e65d82 100644 --- a/backend/templates/sql_examples/DM.yaml +++ b/backend/templates/sql_examples/DM.yaml @@ -21,6 +21,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/Doris.yaml b/backend/templates/sql_examples/Doris.yaml index 5b2ea360..b973c529 100644 --- a/backend/templates/sql_examples/Doris.yaml +++ b/backend/templates/sql_examples/Doris.yaml @@ -21,6 +21,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/Elasticsearch.yaml b/backend/templates/sql_examples/Elasticsearch.yaml index cddc4577..14188c2f 100644 --- a/backend/templates/sql_examples/Elasticsearch.yaml +++ b/backend/templates/sql_examples/Elasticsearch.yaml @@ -21,6 +21,20 @@ template: other_rule: | 必须为每个索引生成别名(不加AS) + + Elasticsearch SQL多表查询字段限定规则 + + 当使用JOIN涉及多个索引时,所有字段引用必须明确限定索引别名 + 适用于SELECT、WHERE、GROUP BY、HAVING、ORDER BY、ON等子句中的所有字段引用 + 单索引查询不需要表名前缀 + + + Elasticsearch的JOIN性能有限,仅适合小数据量关联 + 标识符引用使用双引号(") + 大多数Elasticsearch查询是单索引查询,不需要字段限定 + 嵌套字段通过点号访问,如 "customer.name",不需要表名前缀 + + 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/Kingbase.yaml b/backend/templates/sql_examples/Kingbase.yaml index 568b4201..e3fa4caa 100644 --- a/backend/templates/sql_examples/Kingbase.yaml +++ b/backend/templates/sql_examples/Kingbase.yaml @@ -21,6 +21,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/Microsoft_SQL_Server.yaml b/backend/templates/sql_examples/Microsoft_SQL_Server.yaml index 83f560e2..856ac2a6 100644 --- a/backend/templates/sql_examples/Microsoft_SQL_Server.yaml +++ b/backend/templates/sql_examples/Microsoft_SQL_Server.yaml @@ -21,6 +21,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/MySQL.yaml b/backend/templates/sql_examples/MySQL.yaml index 1cbfca44..1335ec9e 100644 --- a/backend/templates/sql_examples/MySQL.yaml +++ b/backend/templates/sql_examples/MySQL.yaml @@ -20,6 +20,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/Oracle.yaml b/backend/templates/sql_examples/Oracle.yaml index ba3015b2..44e7ebed 100644 --- a/backend/templates/sql_examples/Oracle.yaml +++ b/backend/templates/sql_examples/Oracle.yaml @@ -83,6 +83,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/PostgreSQL.yaml b/backend/templates/sql_examples/PostgreSQL.yaml index ca3bcd89..42a9cfe4 100644 --- a/backend/templates/sql_examples/PostgreSQL.yaml +++ b/backend/templates/sql_examples/PostgreSQL.yaml @@ -15,6 +15,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/sql_examples/StarRocks.yaml b/backend/templates/sql_examples/StarRocks.yaml index d0dc10fe..212677fc 100644 --- a/backend/templates/sql_examples/StarRocks.yaml +++ b/backend/templates/sql_examples/StarRocks.yaml @@ -21,6 +21,7 @@ template: other_rule: | 必须为每个表生成别名(不加AS) + {multi_table_condition} 禁止使用星号(*),必须明确字段名 中文/特殊字符字段需保留原名并添加英文别名 函数字段必须加别名 diff --git a/backend/templates/template.yaml b/backend/templates/template.yaml index 82c990ec..a6f50da1 100644 --- a/backend/templates/template.yaml +++ b/backend/templates/template.yaml @@ -49,6 +49,20 @@ template: 不要拒绝查询所有数据的情况 + multi_table_condition: | + + 多表查询字段限定规则(必须严格遵守) + + 当SQL涉及多个表/索引(通过FROM/JOIN/子查询等)时,所有字段引用必须明确限定表名/索引名或表别名/索引别名 + 适用于SELECT、WHERE、GROUP BY、HAVING、ORDER BY、ON等子句中的所有字段引用 + 即使字段名在所有表/索引中是唯一的,也必须明确限定以确保清晰性 + + + 生成SQL后必须检查是否涉及多表查询 + 如果是多表查询,验证所有字段引用是否有表名/表别名限定 + 如果发现未限定的字段,必须重新生成SQL + + system: | 你是"SQLBOT",智能问数小助手,可以根据用户提问,专业生成SQL,查询数据并进行图表展示。 @@ -68,6 +82,7 @@ template: 请使用语言:{lang} 回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出 + 即使示例中显示了中文消息,在实际生成时也必须翻译成英文 你只能生成查询用的SQL语句,不得生成增删改相关或操作数据库以及操作数据库数据的SQL @@ -120,6 +135,9 @@ template: 若需关联多表,优先使用中标记为"Primary key"/"ID"/"主键"的字段作为关联条件。 + + 若涉及多表查询,则生成的SQL内,不论查询的表字段是否有重名,表字段前必须加上对应的表名 + 我们目前的情况适用于单指标、多分类的场景(展示table除外)