Skip to content

Commit

Permalink
MDEV-27544 database() function should return 64 characters
Browse files Browse the repository at this point in the history
Database names are 64 utf8 characters per the system tables
that refer to them.

The current database() function is returning 34 characters.

The result of limiting this function results to max length of 34
became apparent when used in a UNION ALL where the results are
truncated to 34 characters.

For (uninvestigated) reasons, SELECT DATABASE() on its own
would always return the right number of characters.

Thanks Alexander Barkov for the review.

Thanks dave for noticing the bug in the stackexchange post
https://dba.stackexchange.com/questions/306183/why-is-my-database-name-truncated
  • Loading branch information
grooverdan committed Jan 20, 2022
1 parent 810ef91 commit 1d27b57
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
20 changes: 19 additions & 1 deletion mysql-test/r/func_system.result
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ create table t1 (version char(60)) select database(), user(), version() as 'vers
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`database()` varchar(34) CHARACTER SET utf8 DEFAULT NULL,
`database()` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
`user()` varchar(141) CHARACTER SET utf8 DEFAULT NULL,
`version` char(60) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
Expand Down Expand Up @@ -95,3 +95,21 @@ select left(concat(a,version()),1) from t1;
left(concat(a,version()),1)
a
drop table t1;
#
# Start of 10.2 tests
#

MDEV-27544 database() function under UNION ALL truncates results to 34 characters


SET NAMES utf8;
create database betäubungsmittelverschreibungsverordnung;
use betäubungsmittelverschreibungsverordnung;
select database() as "database" union all select database();
database
betäubungsmittelverschreibungsverordnung
betäubungsmittelverschreibungsverordnung
drop database betäubungsmittelverschreibungsverordnung;
#
# End of 10.2 tests
#
20 changes: 20 additions & 0 deletions mysql-test/t/func_system.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,23 @@ select left(concat(a,version()),1) from t1;
drop table t1;

# End of 4.1 tests

--echo #
--echo # Start of 10.2 tests
--echo #

--echo
--echo MDEV-27544 database() function under UNION ALL truncates results to 34 characters
--echo
--echo

SET NAMES utf8;
create database betäubungsmittelverschreibungsverordnung;
use betäubungsmittelverschreibungsverordnung;
select database() as "database" union all select database();
drop database betäubungsmittelverschreibungsverordnung;


--echo #
--echo # End of 10.2 tests
--echo #
2 changes: 1 addition & 1 deletion sql/item_strfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ class Item_func_database :public Item_func_sysconst
String *val_str(String *);
bool fix_length_and_dec()
{
max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
max_length= NAME_CHAR_LEN * system_charset_info->mbmaxlen;
maybe_null=1;
return FALSE;
}
Expand Down

0 comments on commit 1d27b57

Please sign in to comment.