From 0ab49bec1456ca27b9e879fbeb74a068fb416c15 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Tue, 8 Oct 2024 18:46:56 +0800 Subject: [PATCH] executor: fix `set names` with `default_collation_for_utf8mb4` vars (#56469) close pingcap/tidb#56439 --- pkg/executor/set.go | 4 +++- .../r/new_character_set.result | 20 +++++++++++++++++++ .../integrationtest/t/new_character_set.test | 13 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/executor/set.go b/pkg/executor/set.go index 7649027df2abb..8d88a2d782b45 100644 --- a/pkg/executor/set.go +++ b/pkg/executor/set.go @@ -254,7 +254,9 @@ func (e *SetExecutor) setCharset(cs, co string, isSetName bool) error { var err error sessionVars := e.Ctx().GetSessionVars() if co == "" { - if co, err = charset.GetDefaultCollation(cs); err != nil { + if cs == mysql.UTF8MB4Charset { + co = sessionVars.DefaultCollationForUTF8MB4 + } else if co, err = charset.GetDefaultCollation(cs); err != nil { return err } } else { diff --git a/tests/integrationtest/r/new_character_set.result b/tests/integrationtest/r/new_character_set.result index 0711afae14891..44ad681270ae6 100644 --- a/tests/integrationtest/r/new_character_set.result +++ b/tests/integrationtest/r/new_character_set.result @@ -92,3 +92,23 @@ select hex('一a'), '一a'; hex('涓?') 涓? E4B83F 涓? set @@sql_mode=default; +set @@character_set_client=default; +set @@character_set_connection=default; +show variables like 'collation_connection'; +Variable_name Value +collation_connection utf8mb4_bin +set default_collation_for_utf8mb4 = 'utf8mb4_general_ci'; +set names utf8mb4; +show variables like 'collation_connection'; +Variable_name Value +collation_connection utf8mb4_general_ci +set default_collation_for_utf8mb4 = 'utf8mb4_0900_ai_ci'; +set names utf8mb4; +show variables like 'collation_connection'; +Variable_name Value +collation_connection utf8mb4_0900_ai_ci +set default_collation_for_utf8mb4 = default; +set names utf8mb4; +show variables like 'collation_connection'; +Variable_name Value +collation_connection utf8mb4_bin diff --git a/tests/integrationtest/t/new_character_set.test b/tests/integrationtest/t/new_character_set.test index 8b0ee0eb21818..ceb4f5c978d02 100644 --- a/tests/integrationtest/t/new_character_set.test +++ b/tests/integrationtest/t/new_character_set.test @@ -70,3 +70,16 @@ set @@character_set_connection=gbk; select hex('一a'), '一a'; set @@sql_mode=default; +set @@character_set_client=default; +set @@character_set_connection=default; + +show variables like 'collation_connection'; +set default_collation_for_utf8mb4 = 'utf8mb4_general_ci'; +set names utf8mb4; +show variables like 'collation_connection'; +set default_collation_for_utf8mb4 = 'utf8mb4_0900_ai_ci'; +set names utf8mb4; +show variables like 'collation_connection'; +set default_collation_for_utf8mb4 = default; +set names utf8mb4; +show variables like 'collation_connection';