Opened 6 years ago

Closed 6 years ago

#29251 closed Bug (fixed)

MySQL backend returns bytes instead of strings in some queries involving database functions

Reported by: Mariusz Felisiak Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: mysql bytes
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

PR 9808 revealed a bug in the MySQL backend. mysqlclient returns bytes instead of strings in some edge cases, e.g.:

LPad('name', Length('alias'))
Replace('name', Length('alias'))

Change History (7)

comment:1 by Mariusz Felisiak, 6 years ago

Owner: changed from nobody to Mariusz Felisiak

comment:2 by Tim Graham, 6 years ago

Description: modified (diff)
Has patch: set
Summary: The MySQL back-end returns bytes instead of strings in some cases.MySQL backend returns bytes instead of strings in some queries involving database functions
Triage Stage: UnreviewedReady for checkin
Type: UncategorizedBug

comment:3 by Tim Graham, 6 years ago

Did you search for a MySQL ticket about this so we know if the behavior might change in future versions?

comment:4 by Mariusz Felisiak, 6 years ago

Yes, but I didn't find any specific tickets. I checked that mysql-connector-python behaves in the same way, then I noticed that MySQL returns different data types in both cases, i.e.

  • SELECT LPAD(`test_xyz`.`column1`, CHAR_LENGTH(`test_xyz`.`column2`), ' ') AS `filled` FROM `test_xyz`; returns filled as a longtext, probably because it cannot predict a no. of chars,
  • SELECT LPAD(`test_xyz`.`column1`, 2, ' ') AS `filled` FROM `test_xyz`; returns filled as a varchar(2).

I also found related discussion https://github.com/django/django/pull/3754#discussion_r22146187 and following comment "...Looking briefly at the database driver, it appears that longtext/BLOB is defined as returning bytes...".

comment:5 by Mariusz Felisiak, 6 years ago

IMO this behavior will not (cannot) be changed in the MySQL engine.

comment:6 by Tim Graham, 6 years ago

A new PR uses a different approach.

comment:7 by GitHub <noreply@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 6141c752:

Fixed #29251 -- Added bytes to str conversion in LPad/RPad database functions on MySQL.

Thanks Tim Graham for the review.

Note: See TracTickets for help on using tickets.
Back to Top