3

I'm using MariaDB v5.x and want to return a long database's name with the query. I can successfully do this with a simple query, but with a more involved query the database name comes back truncated [34 characters]. Why does this happen and is there a way to retrieve the full, un-truncated name?

This example query works:

SELECT DATABASE() as 'database', hour as 'hour'
FROM time
WHERE hour = 1;

However, this type of query returns the truncated database name:

SELECT DATABASE() as 'database',
       t.hour as 'hour',
       d.day as 'day'
FROM   time t,
       day d
WHERE  t.day_pk = day.pk AND
       t.hour   = 1      AND
       d.day    = 22
UNION ALL
SELECT DATABASE() as 'database',
       t.hour as 'hour',
       d.day as 'day'
FROM   time t,
       day d
WHERE  t.day_pk = day.pk AND
       t.hour   = 1      AND
       d.day    = 24;

Thanks for any /all help.

D.

5
  • "MariaDB 5.x"? The unsupported one? And what is "x" in this case? They tend to introduce some major changes even in minor releases.
    – mustaccio
    Commented Jan 17, 2022 at 17:59
  • Please show us the actual input and output. The characters involved may be a clue as to what went wrong.
    – Rick James
    Commented Jan 17, 2022 at 19:55
  • It may help to use a CAST(...) on at least the first Select.
    – Rick James
    Commented Jan 17, 2022 at 20:11
  • I assume you are using UNION to demonstrate the problem, if not you can use d.day in (22,24). FWIW, I would avoid identifiers like time and day since they are reserved words (and no I would not use quotes to get around that problem). You may also want to have a look at ANSI joins instead of ",". In the long run I find it easier to maintain my code with these. Commented Jan 18, 2022 at 6:04
  • For @Lennart, the SQL is representative of the actual query, but good points regarding using time and day. As for using ASNI joins ... this format is, for me, the easiest to read and use when trying to tie a dozen tables together. Finally, since I'm not an SQL guru, any example of the d.day in (22,24) would be most helpful.
    – dave
    Commented Jan 19, 2022 at 1:24

2 Answers 2

2

I can confirm that the same behaviour is present even in MariaDB 10.7.1.

The truncation appears to happen when UNION is used.

Here is a workaround - use the substring function to indicate you want the full length:

SELECT substring(database() FROM 1 FOR length(database())) AS "database"
UNION ALL
SELECT substring(database() FROM 1 FOR length(database()));
5
  • This worked really well, @dbdemon, thank you. I understand what you did, taking the entire database name as a sub-string. This is not something I would have thought to do.
    – dave
    Commented Jan 19, 2022 at 1:14
  • For @dbdemon, is the FROM 1 related to the answer from @danblack where the results show Field 1: 'database'?
    – dave
    Commented Jan 19, 2022 at 1:35
  • Its part of the substring syntax that coerces the length of the field. UNION ALL leaves the type determination to the first query, but for currently unknown reasons this is different to the single query on its own.
    – danblack
    Commented Jan 19, 2022 at 2:37
  • For @danblack, it was the ... FOR length(database() that threw me. I was looking at FOR loops instead of the substring page. Thanks for pointing to the correct syntax.
    – dave
    Commented Jan 20, 2022 at 0:36
  • 1
2

Confirming what @dbdemon posted this is a bug that is also present in all versions. Using --column-type-info shows a little more insight as to what's happening.

$ db=$(printf 'a%.0s' {1..40})
$ echo $db
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
$ podman run -d --rm -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=$db -e MYSQL_USER=u -e MYSQL_PASSWORD=p --name m55 mariadb:5.5
1b2c0740bf664b8a3da8071bb96234a86fe8dd5f3eb6348598f5d0c2b59ede56

$ podman exec -ti m55 mysql -u u -pp  --column-type-info -e 'select database() as "database" union all select database()' $db
Field   1:  `database`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  latin1_swedish_ci (8)
Length:     34
Max_length: 34
Decimals:   0
Flags:      


+------------------------------------+
| database                           |
+------------------------------------+
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+------------------------------------+

$ podman exec -ti m55 mysql -u u -pp  --column-type-info -e 'select database()' $db
Field   1:  `database()`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  latin1_swedish_ci (8)
Length:     34
Max_length: 40
Decimals:   31
Flags:      


+------------------------------------------+
| database()                               |
+------------------------------------------+
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+------------------------------------------+

From the code we can see the 34 comes from the MAX_FIELD_NAME rather than the 64 NAME_CHAR_LEN from various definitions of Db in system tables.

These sorts of bugs are easy to fix. Please report them (whether easy or not).

Update: Bug MDEV-27544 created

Update: Fixed in 10.2.42+, 10.3.33+, 10.4.23+, 10.5.14+, 10.6.6+, 10.7.2+ and 10.8.0+

4
  • 1
    While a bit above my database knowledge, this information is quite interesting, thank you.
    – dave
    Commented Jan 19, 2022 at 1:37
  • It was a fun investigation, and just showing that MariaDB is just code and things happen for a reason. I've lodged MDEV-27544 as a bug report but the level of detail in your original question was enough to do a bug report. The currently minimum developed version is 10.2, so while you are on 5.5 you'll have to survive with @dbdemon's workarounds. By the time you upgrade it should be fixed.
    – danblack
    Commented Jan 19, 2022 at 2:45
  • For @danblack, the bug report is most awesome, thank you. Unfortunately, my legacy system will be stuck on v5.5 for quite some time, but when we eventually get to v10 there will lots of new tricks to take advantage of, this being one.
    – dave
    Commented Jan 20, 2022 at 0:33
  • Well its fixed now. Not really a trick, just something that should of been fixed a while ago (if anyone noticed). Happy to have any feedback of what would make an upgrade easier (probably off the channel, should be able to find my email easy enough).
    – danblack
    Commented Jan 20, 2022 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.