2017-09-12 12 views
0

我有一個適用於MySQL的SQL腳本(最高版本)。我在Linux機器上從源代碼構建了MariaDB-10.2.8的副本,並啓動並運行了數據庫。但是,我的SQL腳本失敗,因爲MariaDB從UNHEX()調用中返回NULL,不應該這樣做。爲什麼MariaDB-10.2.8 UNHEX()返回NULL?

該調用產生一個特定格式的20字節隨機二進制字符串(它是一個BitTorrent節點ID)。我將一些必需的字節與一些隨機字節連接起來,某些字節被限制在一個特定的值範圍內。這些被構造成40字符的十六進制字符串,然後我通過UNHEX()運行。

的SQL是:

unhex(concat('414C2', 
       hex(8 + round(rand() * 7)), 
       substr(sha(uuid()), 6, 33), 
       hex(2 + round(rand()) * 8))) 

如果你脫下UNHEX()函數,你會得到一個40個字符的十六進制字符串:

MariaDB [bt]> select concat('414c2', hex(8+round(rand()*7)),substr(sha(uuid()),6,33),hex(2+round(rand())*8)); 
+-----------------------------------------------------------------------------------------+ 
| concat('414c2', hex(8+round(rand()*7)),substr(sha(uuid()),6,33),hex(2+round(rand())*8)) | 
+-----------------------------------------------------------------------------------------+ 
| 414c29115056f1bd332d4e2e3eb5edd3fc90c0a2            | 
+-----------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

,但如果你UNHEX()它:

MariaDB [bt]> select unhex(concat('414c2', hex(8+round(rand()*7)),substr(sha(uuid()),6,33),hex(2+round(rand())*8))); 
+------------------------------------------------------------------------------------------------+ 
| unhex(concat('414c2', hex(8+round(rand()*7)),substr(sha(uuid()),6,33),hex(2+round(rand())*8))) | 
+------------------------------------------------------------------------------------------------+ 
| NULL                       | 
+------------------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

相比之下,在MySQL實例相同的命令:

mysql> select unhex(upper(concat('414c2', hex(8+round(rand()*7)),substr(sha(uuid()),6,33),hex(2+round(rand())*8)))); 
+-------------------------------------------------------------------------------------------------------+ 
| unhex(upper(concat('414c2', hex(8+round(rand()*7)),substr(sha(uuid()),6,33),hex(2+round(rand())*8)))) | 
+-------------------------------------------------------------------------------------------------------+ 
| AL*w?? 
???r?%??                       | 
+-------------------------------------------------------------------------------------------------------+ 
1 row in set (0.02 sec) 

UNHEX()40個字符的十六進制字符串,其中所有字節可打印的工作原理確定的MariaDB的的:

MariaDB [bt]> select unhex('4142434445464748494a4b4c4d4e4f5051525354'); 
+---------------------------------------------------+ 
| unhex('4142434445464748494a4b4c4d4e4f5051525354') | 
+---------------------------------------------------+ 
| ABCDEFGHIJKLMNOPQRST        | 
+---------------------------------------------------+ 
1 row in set (0.00 sec) 

任何想法,爲什麼一個隨機的十六進制字符串是行不通的?

+0

似乎在10.2.2上爲我工作。是'upper()'必要的嗎? –

+0

不,這只是我一路試過的東西。 (我最好的猜測是,我在某種程度上獲取了字符串中的非十六進制字符,或十六進制數字不均勻的數字,或者是導致hex-> binary轉換問題的原因。) –

回答