Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SET NAMES usage which is no-op in SingleStore #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 11 additions & 17 deletions singlestoredb/mysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,18 @@ def set_charset(self, charset):

def set_character_set(self, charset, collation=None):
"""
Set charaset (and collation) on the server.
Set session charaset (and collation) on the server.

Send "SET NAMES charset [COLLATE collation]" query.
Send "SET [COLLATION|CHARACTER_SET]_SERVER = [collation|charset]" query.
Update Connection.encoding based on charset.

If charset/collation are being set to utf8mb4, the corresponding global
variables (COLLATION_SERVER and CHARACTER_SET_SERVER) must be also set
to utf8mb4. This is true by default for SingleStore 8.7+. For previuous
versions or non-default setting user must manully run the query
`SET global collation_connection = utf8mb4_general_ci`
replacing utf8mb4_general_ci with {collation}.

Parameters
----------
charset : str
Expand All @@ -997,9 +1004,9 @@ def set_character_set(self, charset, collation=None):
encoding = charset_by_name(charset).encoding

if collation:
query = f'SET NAMES {charset} COLLATE {collation}'
query = f'SET COLLATION_SERVER={collation}'
else:
query = f'SET NAMES {charset}'
query = f'SET CHARACTER_SET_SERVER={charset}'
self._execute_command(COMMAND.COM_QUERY, query)
self._read_packet()
self.charset = charset
Expand Down Expand Up @@ -1103,19 +1110,6 @@ def connect(self, sock=None):
self._get_server_information()
self._request_authentication()

# Send "SET NAMES" query on init for:
# - Ensure charaset (and collation) is set to the server.
# - collation_id in handshake packet may be ignored.
# - If collation is not specified, we don't know what is server's
# default collation for the charset. For example, default collation
# of utf8mb4 is:
# - MySQL 5.7, MariaDB 10.x: utf8mb4_general_ci
# - MySQL 8.0: utf8mb4_0900_ai_ci
#
# Reference:
# - https://github.com/PyMySQL/PyMySQL/issues/1092
# - https://github.com/wagtail/wagtail/issues/9477
# - https://zenn.dev/methane/articles/2023-mysql-collation (Japanese)
self.set_character_set(self.charset, self.collation)

if self.sql_mode is not None:
Expand Down
Loading