Skip to content

fix: incorrect triggering of rule : Chinese full-width quotes are not recommended in DDL #3231

Merged
BugsGuru merged 2 commits intomainfrom
fix-rule/quotation
Apr 3, 2026
Merged

fix: incorrect triggering of rule : Chinese full-width quotes are not recommended in DDL #3231
BugsGuru merged 2 commits intomainfrom
fix-rule/quotation

Conversation

@winfredLIN
Copy link
Copy Markdown
Collaborator

关联的 issue

#3230

描述你的变更

修复:DDL语句中不建议使用中文全角引号 的问题

  • 根据规则目的:建议开启此规则,可避免MySQL会将中文全角引号识别为命名的一部分,执行结果与业务预期不符
  • 增加改动:
  • 从检查建表语句字符串中是否包含中文全角引号,改为:检查建表语句中表、字段、等对象名称是否包含含中文全角引号
  • 增加单元测试

测试

#3230 (comment)

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc

…dded test cases to verify correct identification of violations and false positives related to full-width quotation marks in identifiers and comments.
…te over object names. This improves detection of violations related to full-width double quotation marks in identifiers.
@actiontech-bot actiontech-bot requested a review from BugsGuru April 3, 2026 06:22
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 3, 2026

PR Reviewer Guide 🔍

🎫 Ticket compliance analysis 🔶

3230 - Partially compliant

Compliant requirements:

  • 修复全角引号识别问题
  • 增加单元测试验证规则正确性

Non-compliant requirements:

Requires further human verification:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

单元测试覆盖

新增的测试代码覆盖了DDL全角引号场景,建议验证测试用例是否足够全面,确保仅对标识符中的全角引号产生报警,而正确忽略COMMENT中的全角引号。

// 全角双引号 “ ”(Unicode U+201C / U+201D)。下面 SQL 用反引号引用标识符,无法用反引号包裹整段 Go raw string,故用变量 + fmt.Sprintf 拼接。
lq, rq := "“", "”"
rule := rulepkg.RuleHandlerMap[rulepkg.DDLCheckFullWidthQuotationMarks].Rule
expectViolation := newTestResult().addResult(rulepkg.DDLCheckFullWidthQuotationMarks)
expectClean := newTestResult()

// Should trigger: full-width quotation marks appear in identifier names
for _, tc := range []struct {
	name string
	sql  string
}{
	{"trigger_create_table_table_name", fmt.Sprintf("CREATE TABLE `%st1%s` (id int)", lq, rq)},
	{"trigger_create_table_column_name", fmt.Sprintf("CREATE TABLE t (`%scol1%s` int)", lq, rq)},
	{"trigger_create_table_column_name_mid", fmt.Sprintf("CREATE TABLE t (`col%smid%sx` int)", lq, rq)},
	{"trigger_create_table_table_name_mid", fmt.Sprintf("CREATE TABLE `t%smid%ssuf` (id int)", lq, rq)},
	{"trigger_create_table_inline_index_name", fmt.Sprintf("CREATE TABLE t (id int, INDEX `%sidx1%s` (id))", lq, rq)},
	{"trigger_alter_add_column_name", fmt.Sprintf("ALTER TABLE exist_tb_1 ADD COLUMN `%scol1%s` int", lq, rq)},
	{"trigger_alter_add_column_name_mid", fmt.Sprintf("ALTER TABLE exist_tb_1 ADD COLUMN `a%sb%s` int", lq, rq)},
	{"trigger_alter_change_column_rename", fmt.Sprintf("ALTER TABLE exist_tb_1 CHANGE COLUMN v1 `%sv1_new%s` varchar(255)", lq, rq)},
	{"trigger_alter_rename_table", fmt.Sprintf("ALTER TABLE exist_tb_1 RENAME TO `%st2%s`", lq, rq)},
	{"trigger_alter_add_index_name", fmt.Sprintf("ALTER TABLE exist_tb_1 ADD INDEX `%sidx_new%s` (v1)", lq, rq)},
	{"trigger_create_index_name", fmt.Sprintf("CREATE INDEX `%sidx1%s` ON exist_tb_1 (v1)", lq, rq)},
} {
	tc := tc
	t.Run(tc.name, func(t *testing.T) {
		runSingleRuleInspectCase(rule, t, tc.name, DefaultMysqlInspect(), tc.sql, expectViolation)
	})
}

// Should NOT trigger: identifiers are normal; full-width marks appear only in COMMENT values (if at all).
// This group directly verifies the reported false-positive bug: COMMENT content must be ignored.
// MySQL accepts both single-quoted ('') and double-quoted ("") string literals for COMMENT values,
// so we test both forms.
for _, tc := range []struct {
	name string
	sql  string
}{
	{"no_trigger_alter_column_comment_single_quote", fmt.Sprintf("alter table exist_tb_1 add column a int comment '%sa%s'", lq, rq)},
	{"no_trigger_create_column_comment_single_quote", fmt.Sprintf("create table t (id int comment '%saaa%s')", lq, rq)},
	{"no_trigger_create_table_comment_single_quote", fmt.Sprintf("create table t (id int) comment '%stable comment%s'", lq, rq)},
	{"no_trigger_create_index_comment_single_quote", fmt.Sprintf("create index idx1 on exist_tb_1 (v1) comment '%sidx comment%s'", lq, rq)},
	{"no_trigger_alter_column_comment_double_quote", fmt.Sprintf(`alter table exist_tb_1 add column a int comment "%sa%s"`, lq, rq)},
	{"no_trigger_create_column_comment_double_quote", fmt.Sprintf(`create table t (id int comment "%saaa%s")`, lq, rq)},
	{"no_trigger_create_table_comment_double_quote", fmt.Sprintf(`create table t (id int) comment "%stable comment%s"`, lq, rq)},
	{"no_trigger_create_index_comment_double_quote", fmt.Sprintf(`create index idx1 on exist_tb_1 (v1) comment "%sidx comment%s"`, lq, rq)},
	{"no_trigger_create_table_ascii_comment", `create table t (id int comment 'normal comment')`},
	{"no_trigger_alter_ascii_comment", `alter table exist_tb_1 add column a int comment 'some normal comment'`},
	{"no_trigger_create_table_no_comment", `create table t (id int)`},
} {
	tc := tc
	t.Run(tc.name, func(t *testing.T) {
		runSingleRuleInspectCase(rule, t, tc.name, DefaultMysqlInspect(), tc.sql, expectClean)
	})
}

t.Run("no_trigger_select_non_ddl", func(t *testing.T) {
	runSingleRuleInspectCase(rule, t, "no_trigger_select_non_ddl", DefaultMysqlInspect(),
		`select "1"`,
		expectClean)
})
对象名称检测

更新后的代码通过循环getObjectNames函数返回的标识符进行检测,建议确认getObjectNames能够完整且准确地提取所有相关对象名称,避免遗漏导致潜在误报或漏报。

for _, name := range getObjectNames(input.Node) {
	if strings.ContainsAny(name, `“”`) { // “” are the full-width double quotation marks (U+201C and U+201D)
		addResult(input.Res, input.Rule, input.Rule.Name)
		return nil
	}

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 3, 2026

PR Code Suggestions ✨

No code suggestions found for the PR.

@BugsGuru BugsGuru merged commit 8e8cbcc into main Apr 3, 2026
4 checks passed
@BugsGuru BugsGuru deleted the fix-rule/quotation branch April 3, 2026 06:25
LordofAvernus pushed a commit that referenced this pull request Apr 9, 2026
fix: incorrect triggering of rule : Chinese full-width quotes are not recommended in DDL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants