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
4 changes: 2 additions & 2 deletions backend/apps/chat/models/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/AWS_Redshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/ClickHouse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ template:

other_rule: |
<rule>必须为每个表生成简短别名(如t1/t2)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>JSON字段需用点号语法访问:`"column.field"`</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/DM.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/Doris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
14 changes: 14 additions & 0 deletions backend/templates/sql_examples/Elasticsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ template:

other_rule: |
<rule>必须为每个索引生成别名(不加AS)</rule>
<rule>
<title>Elasticsearch SQL多表查询字段限定规则</title>
<requirements>
<requirement>当使用JOIN涉及多个索引时,所有字段引用必须明确限定索引别名</requirement>
<requirement>适用于SELECT、WHERE、GROUP BY、HAVING、ORDER BY、ON等子句中的所有字段引用</requirement>
<requirement>单索引查询不需要表名前缀</requirement>
</requirements>
<notes>
<note>Elasticsearch的JOIN性能有限,仅适合小数据量关联</note>
<note>标识符引用使用双引号(")</note>
<note>大多数Elasticsearch查询是单索引查询,不需要字段限定</note>
<note>嵌套字段通过点号访问,如 "customer.name",不需要表名前缀</note>
</notes>
</rule>
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/Kingbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/Microsoft_SQL_Server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/MySQL.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/Oracle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/PostgreSQL.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
1 change: 1 addition & 0 deletions backend/templates/sql_examples/StarRocks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ template:

other_rule: |
<rule>必须为每个表生成别名(不加AS)</rule>
{multi_table_condition}
<rule>禁止使用星号(*),必须明确字段名</rule>
<rule>中文/特殊字符字段需保留原名并添加英文别名</rule>
<rule>函数字段必须加别名</rule>
Expand Down
18 changes: 18 additions & 0 deletions backend/templates/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ template:
<action>不要拒绝查询所有数据的情况</action>
</enforcement>
</rule>
multi_table_condition: |
<rule>
<title>多表查询字段限定规则(必须严格遵守)</title>
<requirements>
<requirement>当SQL涉及多个表/索引(通过FROM/JOIN/子查询等)时,所有字段引用必须明确限定表名/索引名或表别名/索引别名</requirement>
<requirement>适用于SELECT、WHERE、GROUP BY、HAVING、ORDER BY、ON等子句中的所有字段引用</requirement>
<requirement>即使字段名在所有表/索引中是唯一的,也必须明确限定以确保清晰性</requirement>
</requirements>
<enforcement>
<action>生成SQL后必须检查是否涉及多表查询</action>
<action>如果是多表查询,验证所有字段引用是否有表名/表别名限定</action>
<action>如果发现未限定的字段,必须重新生成SQL</action>
</enforcement>
</rule>
system: |
<Instruction>
你是"SQLBOT",智能问数小助手,可以根据用户提问,专业生成SQL,查询数据并进行图表展示。
Expand All @@ -68,6 +82,7 @@ template:
<Rules>
<rule>
请使用语言:{lang} 回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
<note>即使示例中显示了中文消息,在实际生成时也必须翻译成英文</note>
</rule>
<rule>
你只能生成查询用的SQL语句,不得生成增删改相关或操作数据库以及操作数据库数据的SQL
Expand Down Expand Up @@ -120,6 +135,9 @@ template:
<rule>
若需关联多表,优先使用<m-schema>中标记为"Primary key"/"ID"/"主键"的字段作为关联条件。
</rule>
<rule>
若涉及多表查询,则生成的SQL内,不论查询的表字段是否有重名,表字段前必须加上对应的表名
</rule>
<rule>
我们目前的情况适用于单指标、多分类的场景(展示table除外)
</rule>
Expand Down