commit dd975cef7f2
Author: Sergei Golubchik <serg@mariadb.org>
Date:   Sat May 6 22:11:03 2023 +0200

    cleanup: remove sql_type_uuid.cc
    
    this makes template changes easier

diff --git a/plugin/type_uuid/CMakeLists.txt b/plugin/type_uuid/CMakeLists.txt
index 9a379abef04..6b0d6a458bf 100644
--- a/plugin/type_uuid/CMakeLists.txt
+++ b/plugin/type_uuid/CMakeLists.txt
@@ -14,5 +14,5 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
 
 MYSQL_ADD_PLUGIN(type_uuid
-                 plugin.cc sql_type_uuid.cc item_uuidfunc.cc
+                 plugin.cc item_uuidfunc.cc
                  MANDATORY RECOMPILE_FOR_EMBEDDED)
diff --git a/plugin/type_uuid/sql_type_uuid.cc b/plugin/type_uuid/sql_type_uuid.cc
deleted file mode 100644
index 9b4a731dbb2..00000000000
--- a/plugin/type_uuid/sql_type_uuid.cc
+++ /dev/null
@@ -1,117 +0,0 @@
-/* Copyright (c) 2019,2021 MariaDB Corporation
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; version 2 of the License.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
-
-#define MYSQL_SERVER
-#include "mariadb.h"
-#include "my_net.h"
-#include "sql_class.h" // THD, SORT_FIELD_ATTR
-#include "opt_range.h" // SEL_ARG
-#include "sql_type_uuid.h"
-
-
-static bool get_digit(char ch, uint *val)
-{
-  if (ch >= '0' && ch <= '9')
-  {
-    *val= (uint) ch - '0';
-    return false;
-  }
-  if (ch >= 'a' && ch <= 'f')
-  {
-    *val= (uint) ch - 'a' + 0x0a;
-    return false;
-  }
-  if (ch >= 'A' && ch <= 'F')
-  {
-    *val= (uint) ch - 'A' + 0x0a;
-    return false;
-  }
-  return true;
-}
-
-
-static bool get_digit(uint *val, const char *str, const char *end)
-{
-  if (str >= end)
-    return true;
-  return get_digit(*str, val);
-}
-
-
-static size_t skip_hyphens(const char *str, const char *end)
-{
-  const char *str0= str;
-  for ( ; str < end; str++)
-  {
-    if (str[0] != '-')
-      break;
-  }
-  return str - str0;
-}
-
-
-static const char *get_two_digits(char *val, const char *str, const char *end)
-{
-  uint hi, lo;
-  if (get_digit(&hi, str++, end))
-    return NULL;
-  str+= skip_hyphens(str, end);
-  if (get_digit(&lo, str++, end))
-    return NULL;
-  *val= (char) ((hi << 4) + lo);
-  return str;
-}
-
-
-bool UUID::ascii_to_fbt(const char *str, size_t str_length)
-{
-  const char *end= str + str_length;
-  /*
-    The format understood:
-    - Hyphen is not allowed on the first and the last position.
-    - Otherwise, hyphens are allowed on any (odd and even) position,
-      with any amount.
-  */
-  if (str_length < 32)
-    goto err;
-
-  for (uint oidx= 0; oidx < binary_length(); oidx++)
-  {
-    if (!(str= get_two_digits(&m_buffer[oidx], str, end)))
-      goto err;
-    // Allow hypheps after two digits, but not after the last digit
-    if (oidx + 1 < binary_length())
-      str+= skip_hyphens(str, end);
-  }
-  if (str < end)
-    goto err; // Some input left
-  return false;
-err:
-  bzero(m_buffer, sizeof(m_buffer));
-  return true;
-}
-
-size_t UUID::to_string(char *dst, size_t dstsize) const
-{
-  my_uuid2str((const uchar *) m_buffer, dst, 1);
-  return MY_UUID_STRING_LENGTH;
-}
-
-
-const Name &UUID::default_value()
-{
-  static Name def(STRING_WITH_LEN("00000000-0000-0000-0000-000000000000"));
-  return def;
-}
diff --git a/plugin/type_uuid/sql_type_uuid.h b/plugin/type_uuid/sql_type_uuid.h
index ee39f542f05..193b6a9aaed 100644
--- a/plugin/type_uuid/sql_type_uuid.h
+++ b/plugin/type_uuid/sql_type_uuid.h
@@ -19,11 +19,97 @@
 #include "sql_type_fixedbin_storage.h"
 class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
 {
+  bool get_digit(char ch, uint *val)
+  {
+    if (ch >= '0' && ch <= '9')
+    {
+      *val= (uint) ch - '0';
+      return false;
+    }
+    if (ch >= 'a' && ch <= 'f')
+    {
+      *val= (uint) ch - 'a' + 0x0a;
+      return false;
+    }
+    if (ch >= 'A' && ch <= 'F')
+    {
+      *val= (uint) ch - 'A' + 0x0a;
+      return false;
+    }
+    return true;
+  }
+
+  bool get_digit(uint *val, const char *str, const char *end)
+  {
+    if (str >= end)
+      return true;
+    return get_digit(*str, val);
+  }
+
+  size_t skip_hyphens(const char *str, const char *end)
+  {
+    const char *str0= str;
+    for ( ; str < end; str++)
+    {
+      if (str[0] != '-')
+        break;
+    }
+    return str - str0;
+  }
+
+  const char *get_two_digits(char *val, const char *str, const char *end)
+  {
+    uint hi, lo;
+    if (get_digit(&hi, str++, end))
+      return NULL;
+    str+= skip_hyphens(str, end);
+    if (get_digit(&lo, str++, end))
+      return NULL;
+    *val= (char) ((hi << 4) + lo);
+    return str;
+  }
+
 public:
   using FixedBinTypeStorage::FixedBinTypeStorage;
-  bool ascii_to_fbt(const char *str, size_t str_length);
-  size_t to_string(char *dst, size_t dstsize) const;
-  static const Name &default_value();
+  bool ascii_to_fbt(const char *str, size_t str_length)
+  {
+    const char *end= str + str_length;
+    /*
+      The format understood:
+      - Hyphen is not allowed on the first and the last position.
+      - Otherwise, hyphens are allowed on any (odd and even) position,
+        with any amount.
+    */
+    if (str_length < 32)
+      goto err;
+
+    for (uint oidx= 0; oidx < binary_length(); oidx++)
+    {
+      if (!(str= get_two_digits(&m_buffer[oidx], str, end)))
+        goto err;
+      // Allow hypheps after two digits, but not after the last digit
+      if (oidx + 1 < binary_length())
+        str+= skip_hyphens(str, end);
+    }
+    if (str < end)
+      goto err; // Some input left
+    return false;
+  err:
+    bzero(m_buffer, sizeof(m_buffer));
+    return true;
+  }
+
+  size_t to_string(char *dst, size_t dstsize) const
+  {
+    my_uuid2str((const uchar *) m_buffer, dst, 1);
+    return MY_UUID_STRING_LENGTH;
+  }
+
+  static const Name &default_value()
+  {
+    static Name def(STRING_WITH_LEN("00000000-0000-0000-0000-000000000000"));
+    return def;
+  }
 
   /*
     Binary (in-memory) UUIDv1 representation:

commit 563acca9dca
Author: Sergei Golubchik <serg@mariadb.org>
Date:   Wed May 17 16:14:24 2023 +0200

    MDEV-29959 UUID Sorting
    
    * UUIDs version >= 8 are now stored without byte-swapping
    * UUIDs with version >=8 and variant=0 are now considered invalid
    * old tables are supported
    * comparison (and other type aggregation, e.g. for UNION) of old
      (always swapped) and new UUIDs is not implemented

diff --git a/plugin/type_uuid/mysql-test/type_uuid/order.result b/plugin/type_uuid/mysql-test/type_uuid/order.result
new file mode 100644
index 00000000000..c463a6aa627
--- /dev/null
+++ b/plugin/type_uuid/mysql-test/type_uuid/order.result
@@ -0,0 +1,291 @@
+create table t1 (a uuid);
+insert t1 select sformat('11223344-5566-{:x}777-{}888-99aabbccddee', ver.seq, elt(var.seq,0,8,'c','e')) from seq_0_to_15 as ver, seq_1_to_4 as var;
+Warnings:
+Warning	1292	Incorrect uuid value: '11223344-5566-8777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-9777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-a777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-b777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-c777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-d777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-e777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+Warning	1292	Incorrect uuid value: '11223344-5566-f777-0888-99aabbccddee' for column `test`.`t1`.`a` at row 5
+select * from t1;
+a
+11223344-5566-0777-0888-99aabbccddee
+11223344-5566-0777-8888-99aabbccddee
+11223344-5566-0777-c888-99aabbccddee
+11223344-5566-0777-e888-99aabbccddee
+11223344-5566-1777-0888-99aabbccddee
+11223344-5566-1777-8888-99aabbccddee
+11223344-5566-1777-c888-99aabbccddee
+11223344-5566-1777-e888-99aabbccddee
+11223344-5566-2777-0888-99aabbccddee
+11223344-5566-2777-8888-99aabbccddee
+11223344-5566-2777-c888-99aabbccddee
+11223344-5566-2777-e888-99aabbccddee
+11223344-5566-3777-0888-99aabbccddee
+11223344-5566-3777-8888-99aabbccddee
+11223344-5566-3777-c888-99aabbccddee
+11223344-5566-3777-e888-99aabbccddee
+11223344-5566-4777-0888-99aabbccddee
+11223344-5566-4777-8888-99aabbccddee
+11223344-5566-4777-c888-99aabbccddee
+11223344-5566-4777-e888-99aabbccddee
+11223344-5566-5777-0888-99aabbccddee
+11223344-5566-5777-8888-99aabbccddee
+11223344-5566-5777-c888-99aabbccddee
+11223344-5566-5777-e888-99aabbccddee
+11223344-5566-6777-0888-99aabbccddee
+11223344-5566-6777-8888-99aabbccddee
+11223344-5566-6777-c888-99aabbccddee
+11223344-5566-6777-e888-99aabbccddee
+11223344-5566-7777-0888-99aabbccddee
+11223344-5566-7777-8888-99aabbccddee
+11223344-5566-7777-c888-99aabbccddee
+11223344-5566-7777-e888-99aabbccddee
+NULL
+11223344-5566-8777-8888-99aabbccddee
+11223344-5566-8777-c888-99aabbccddee
+11223344-5566-8777-e888-99aabbccddee
+NULL
+11223344-5566-9777-8888-99aabbccddee
+11223344-5566-9777-c888-99aabbccddee
+11223344-5566-9777-e888-99aabbccddee
+NULL
+11223344-5566-a777-8888-99aabbccddee
+11223344-5566-a777-c888-99aabbccddee
+11223344-5566-a777-e888-99aabbccddee
+NULL
+11223344-5566-b777-8888-99aabbccddee
+11223344-5566-b777-c888-99aabbccddee
+11223344-5566-b777-e888-99aabbccddee
+NULL
+11223344-5566-c777-8888-99aabbccddee
+11223344-5566-c777-c888-99aabbccddee
+11223344-5566-c777-e888-99aabbccddee
+NULL
+11223344-5566-d777-8888-99aabbccddee
+11223344-5566-d777-c888-99aabbccddee
+11223344-5566-d777-e888-99aabbccddee
+NULL
+11223344-5566-e777-8888-99aabbccddee
+11223344-5566-e777-c888-99aabbccddee
+11223344-5566-e777-e888-99aabbccddee
+NULL
+11223344-5566-f777-8888-99aabbccddee
+11223344-5566-f777-c888-99aabbccddee
+11223344-5566-f777-e888-99aabbccddee
+select * from t1 order by a;
+a
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+NULL
+11223344-5566-0777-0888-99aabbccddee
+11223344-5566-1777-0888-99aabbccddee
+11223344-5566-2777-0888-99aabbccddee
+11223344-5566-3777-0888-99aabbccddee
+11223344-5566-4777-0888-99aabbccddee
+11223344-5566-5777-0888-99aabbccddee
+11223344-5566-6777-0888-99aabbccddee
+11223344-5566-6777-8888-99aabbccddee
+11223344-5566-6777-c888-99aabbccddee
+11223344-5566-6777-e888-99aabbccddee
+11223344-5566-7777-0888-99aabbccddee
+11223344-5566-7777-8888-99aabbccddee
+11223344-5566-7777-c888-99aabbccddee
+11223344-5566-7777-e888-99aabbccddee
+11223344-5566-8777-8888-99aabbccddee
+11223344-5566-8777-c888-99aabbccddee
+11223344-5566-8777-e888-99aabbccddee
+11223344-5566-9777-8888-99aabbccddee
+11223344-5566-9777-c888-99aabbccddee
+11223344-5566-9777-e888-99aabbccddee
+11223344-5566-a777-8888-99aabbccddee
+11223344-5566-a777-c888-99aabbccddee
+11223344-5566-a777-e888-99aabbccddee
+11223344-5566-b777-8888-99aabbccddee
+11223344-5566-b777-c888-99aabbccddee
+11223344-5566-b777-e888-99aabbccddee
+11223344-5566-c777-8888-99aabbccddee
+11223344-5566-c777-c888-99aabbccddee
+11223344-5566-c777-e888-99aabbccddee
+11223344-5566-d777-8888-99aabbccddee
+11223344-5566-d777-c888-99aabbccddee
+11223344-5566-d777-e888-99aabbccddee
+11223344-5566-e777-8888-99aabbccddee
+11223344-5566-e777-c888-99aabbccddee
+11223344-5566-e777-e888-99aabbccddee
+11223344-5566-f777-8888-99aabbccddee
+11223344-5566-f777-c888-99aabbccddee
+11223344-5566-f777-e888-99aabbccddee
+11223344-5566-0777-8888-99aabbccddee
+11223344-5566-1777-8888-99aabbccddee
+11223344-5566-2777-8888-99aabbccddee
+11223344-5566-3777-8888-99aabbccddee
+11223344-5566-4777-8888-99aabbccddee
+11223344-5566-5777-8888-99aabbccddee
+11223344-5566-0777-c888-99aabbccddee
+11223344-5566-1777-c888-99aabbccddee
+11223344-5566-2777-c888-99aabbccddee
+11223344-5566-3777-c888-99aabbccddee
+11223344-5566-4777-c888-99aabbccddee
+11223344-5566-5777-c888-99aabbccddee
+11223344-5566-0777-e888-99aabbccddee
+11223344-5566-1777-e888-99aabbccddee
+11223344-5566-2777-e888-99aabbccddee
+11223344-5566-3777-e888-99aabbccddee
+11223344-5566-4777-e888-99aabbccddee
+11223344-5566-5777-e888-99aabbccddee
+show create table t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `a` uuid DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
+# now let's use the table as above, but created in 10.8
+select * from t2;
+a
+11223344-5566-0777-0888-99aabbccddee
+11223344-5566-0777-8888-99aabbccddee
+11223344-5566-0777-c888-99aabbccddee
+11223344-5566-0777-e888-99aabbccddee
+11223344-5566-1777-0888-99aabbccddee
+11223344-5566-1777-8888-99aabbccddee
+11223344-5566-1777-c888-99aabbccddee
+11223344-5566-1777-e888-99aabbccddee
+11223344-5566-2777-0888-99aabbccddee
+11223344-5566-2777-8888-99aabbccddee
+11223344-5566-2777-c888-99aabbccddee
+11223344-5566-2777-e888-99aabbccddee
+11223344-5566-3777-0888-99aabbccddee
+11223344-5566-3777-8888-99aabbccddee
+11223344-5566-3777-c888-99aabbccddee
+11223344-5566-3777-e888-99aabbccddee
+11223344-5566-4777-0888-99aabbccddee
+11223344-5566-4777-8888-99aabbccddee
+11223344-5566-4777-c888-99aabbccddee
+11223344-5566-4777-e888-99aabbccddee
+11223344-5566-5777-0888-99aabbccddee
+11223344-5566-5777-8888-99aabbccddee
+11223344-5566-5777-c888-99aabbccddee
+11223344-5566-5777-e888-99aabbccddee
+11223344-5566-6777-0888-99aabbccddee
+11223344-5566-6777-8888-99aabbccddee
+11223344-5566-6777-c888-99aabbccddee
+11223344-5566-6777-e888-99aabbccddee
+11223344-5566-7777-0888-99aabbccddee
+11223344-5566-7777-8888-99aabbccddee
+11223344-5566-7777-c888-99aabbccddee
+11223344-5566-7777-e888-99aabbccddee
+11223344-5566-8777-0888-99aabbccddee
+11223344-5566-8777-8888-99aabbccddee
+11223344-5566-8777-c888-99aabbccddee
+11223344-5566-8777-e888-99aabbccddee
+11223344-5566-9777-0888-99aabbccddee
+11223344-5566-9777-8888-99aabbccddee
+11223344-5566-9777-c888-99aabbccddee
+11223344-5566-9777-e888-99aabbccddee
+11223344-5566-a777-0888-99aabbccddee
+11223344-5566-a777-8888-99aabbccddee
+11223344-5566-a777-c888-99aabbccddee
+11223344-5566-a777-e888-99aabbccddee
+11223344-5566-b777-0888-99aabbccddee
+11223344-5566-b777-8888-99aabbccddee
+11223344-5566-b777-c888-99aabbccddee
+11223344-5566-b777-e888-99aabbccddee
+11223344-5566-c777-0888-99aabbccddee
+11223344-5566-c777-8888-99aabbccddee
+11223344-5566-c777-c888-99aabbccddee
+11223344-5566-c777-e888-99aabbccddee
+11223344-5566-d777-0888-99aabbccddee
+11223344-5566-d777-8888-99aabbccddee
+11223344-5566-d777-c888-99aabbccddee
+11223344-5566-d777-e888-99aabbccddee
+11223344-5566-e777-0888-99aabbccddee
+11223344-5566-e777-8888-99aabbccddee
+11223344-5566-e777-c888-99aabbccddee
+11223344-5566-e777-e888-99aabbccddee
+11223344-5566-f777-0888-99aabbccddee
+11223344-5566-f777-8888-99aabbccddee
+11223344-5566-f777-c888-99aabbccddee
+11223344-5566-f777-e888-99aabbccddee
+select * from t2 order by a;
+a
+11223344-5566-0777-0888-99aabbccddee
+11223344-5566-1777-0888-99aabbccddee
+11223344-5566-2777-0888-99aabbccddee
+11223344-5566-3777-0888-99aabbccddee
+11223344-5566-4777-0888-99aabbccddee
+11223344-5566-5777-0888-99aabbccddee
+11223344-5566-6777-0888-99aabbccddee
+11223344-5566-7777-0888-99aabbccddee
+11223344-5566-8777-0888-99aabbccddee
+11223344-5566-9777-0888-99aabbccddee
+11223344-5566-a777-0888-99aabbccddee
+11223344-5566-b777-0888-99aabbccddee
+11223344-5566-c777-0888-99aabbccddee
+11223344-5566-d777-0888-99aabbccddee
+11223344-5566-e777-0888-99aabbccddee
+11223344-5566-f777-0888-99aabbccddee
+11223344-5566-0777-8888-99aabbccddee
+11223344-5566-1777-8888-99aabbccddee
+11223344-5566-2777-8888-99aabbccddee
+11223344-5566-3777-8888-99aabbccddee
+11223344-5566-4777-8888-99aabbccddee
+11223344-5566-5777-8888-99aabbccddee
+11223344-5566-6777-8888-99aabbccddee
+11223344-5566-7777-8888-99aabbccddee
+11223344-5566-8777-8888-99aabbccddee
+11223344-5566-9777-8888-99aabbccddee
+11223344-5566-a777-8888-99aabbccddee
+11223344-5566-b777-8888-99aabbccddee
+11223344-5566-c777-8888-99aabbccddee
+11223344-5566-d777-8888-99aabbccddee
+11223344-5566-e777-8888-99aabbccddee
+11223344-5566-f777-8888-99aabbccddee
+11223344-5566-0777-c888-99aabbccddee
+11223344-5566-1777-c888-99aabbccddee
+11223344-5566-2777-c888-99aabbccddee
+11223344-5566-3777-c888-99aabbccddee
+11223344-5566-4777-c888-99aabbccddee
+11223344-5566-5777-c888-99aabbccddee
+11223344-5566-6777-c888-99aabbccddee
+11223344-5566-7777-c888-99aabbccddee
+11223344-5566-8777-c888-99aabbccddee
+11223344-5566-9777-c888-99aabbccddee
+11223344-5566-a777-c888-99aabbccddee
+11223344-5566-b777-c888-99aabbccddee
+11223344-5566-c777-c888-99aabbccddee
+11223344-5566-d777-c888-99aabbccddee
+11223344-5566-e777-c888-99aabbccddee
+11223344-5566-f777-c888-99aabbccddee
+11223344-5566-0777-e888-99aabbccddee
+11223344-5566-1777-e888-99aabbccddee
+11223344-5566-2777-e888-99aabbccddee
+11223344-5566-3777-e888-99aabbccddee
+11223344-5566-4777-e888-99aabbccddee
+11223344-5566-5777-e888-99aabbccddee
+11223344-5566-6777-e888-99aabbccddee
+11223344-5566-7777-e888-99aabbccddee
+11223344-5566-8777-e888-99aabbccddee
+11223344-5566-9777-e888-99aabbccddee
+11223344-5566-a777-e888-99aabbccddee
+11223344-5566-b777-e888-99aabbccddee
+11223344-5566-c777-e888-99aabbccddee
+11223344-5566-d777-e888-99aabbccddee
+11223344-5566-e777-e888-99aabbccddee
+11223344-5566-f777-e888-99aabbccddee
+show create table t2;
+Table	Create Table
+t2	CREATE TABLE `t2` (
+  `a` uuid /* old */ DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
+select * from t1, t2 where t1.a=t2.a;
+ERROR HY000: Illegal parameter data types uuid and uuid /* old */ for operation '='
+select * from t1 union all select * from t2;
+ERROR HY000: Illegal parameter data types uuid and uuid /* old */ for operation 'UNION'
+drop table t1, t2;
diff --git a/plugin/type_uuid/mysql-test/type_uuid/order.test b/plugin/type_uuid/mysql-test/type_uuid/order.test
new file mode 100644
index 00000000000..0e1e231264b
--- /dev/null
+++ b/plugin/type_uuid/mysql-test/type_uuid/order.test
@@ -0,0 +1,25 @@
+# try all combinations of version and variant
+source include/have_sequence.inc;
+
+create table t1 (a uuid);
+insert t1 select sformat('11223344-5566-{:x}777-{}888-99aabbccddee', ver.seq, elt(var.seq,0,8,'c','e')) from seq_0_to_15 as ver, seq_1_to_4 as var;
+select * from t1;
+select * from t1 order by a;
+show create table t1;
+
+--echo # now let's use the table as above, but created in 10.8
+let $datadir= `select @@datadir`;
+--copy_file $MTR_SUITE_DIR/std_data/mdev-29959.frm $datadir/test/t2.frm
+--copy_file $MTR_SUITE_DIR/std_data/mdev-29959.MYI $datadir/test/t2.MYI
+--copy_file $MTR_SUITE_DIR/std_data/mdev-29959.MYD $datadir/test/t2.MYD
+select * from t2;
+select * from t2 order by a;
+show create table t2;
+
+# not implemented
+--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
+select * from t1, t2 where t1.a=t2.a;
+--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
+select * from t1 union all select * from t2;
+
+drop table t1, t2;
diff --git a/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.MYD b/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.MYD
new file mode 100644
index 00000000000..b928561c2da
Binary files /dev/null and b/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.MYD differ
diff --git a/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.MYI b/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.MYI
new file mode 100644
index 00000000000..1e060f4fa34
Binary files /dev/null and b/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.MYI differ
diff --git a/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.frm b/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.frm
new file mode 100644
index 00000000000..a253a20a418
Binary files /dev/null and b/plugin/type_uuid/mysql-test/type_uuid/std_data/mdev-29959.frm differ
diff --git a/plugin/type_uuid/mysql-test/type_uuid/type_uuid.result b/plugin/type_uuid/mysql-test/type_uuid/type_uuid.result
index b3955d09735..0cb5402a40b 100644
--- a/plugin/type_uuid/mysql-test/type_uuid/type_uuid.result
+++ b/plugin/type_uuid/mysql-test/type_uuid/type_uuid.result
@@ -413,156 +413,6 @@ a
 00000000-0000-0000-0000-000000000000
 00000000-0000-0000-0000-000000000000
 00000000-0000-0000-0000-000000000000
-00000001-0000-0000-0000-000000000000
-00000002-0000-0000-0000-000000000000
-00000003-0000-0000-0000-000000000000
-00000004-0000-0000-0000-000000000000
-00000005-0000-0000-0000-000000000000
-00000006-0000-0000-0000-000000000000
-00000007-0000-0000-0000-000000000000
-00000008-0000-0000-0000-000000000000
-00000009-0000-0000-0000-000000000000
-0000000a-0000-0000-0000-000000000000
-0000000b-0000-0000-0000-000000000000
-0000000c-0000-0000-0000-000000000000
-0000000d-0000-0000-0000-000000000000
-0000000e-0000-0000-0000-000000000000
-0000000f-0000-0000-0000-000000000000
-00000100-0000-0000-0000-000000000000
-00000200-0000-0000-0000-000000000000
-00000300-0000-0000-0000-000000000000
-00000400-0000-0000-0000-000000000000
-00000500-0000-0000-0000-000000000000
-00000600-0000-0000-0000-000000000000
-00000700-0000-0000-0000-000000000000
-00000800-0000-0000-0000-000000000000
-00000900-0000-0000-0000-000000000000
-00000a00-0000-0000-0000-000000000000
-00000b00-0000-0000-0000-000000000000
-00000c00-0000-0000-0000-000000000000
-00000d00-0000-0000-0000-000000000000
-00000e00-0000-0000-0000-000000000000
-00000f00-0000-0000-0000-000000000000
-00010000-0000-0000-0000-000000000000
-00020000-0000-0000-0000-000000000000
-00030000-0000-0000-0000-000000000000
-00040000-0000-0000-0000-000000000000
-00050000-0000-0000-0000-000000000000
-00060000-0000-0000-0000-000000000000
-00070000-0000-0000-0000-000000000000
-00080000-0000-0000-0000-000000000000
-00090000-0000-0000-0000-000000000000
-000a0000-0000-0000-0000-000000000000
-000b0000-0000-0000-0000-000000000000
-000c0000-0000-0000-0000-000000000000
-000d0000-0000-0000-0000-000000000000
-000e0000-0000-0000-0000-000000000000
-000f0000-0000-0000-0000-000000000000
-01000000-0000-0000-0000-000000000000
-02000000-0000-0000-0000-000000000000
-03000000-0000-0000-0000-000000000000
-04000000-0000-0000-0000-000000000000
-05000000-0000-0000-0000-000000000000
-06000000-0000-0000-0000-000000000000
-07000000-0000-0000-0000-000000000000
-08000000-0000-0000-0000-000000000000
-09000000-0000-0000-0000-000000000000
-0a000000-0000-0000-0000-000000000000
-0b000000-0000-0000-0000-000000000000
-0c000000-0000-0000-0000-000000000000
-0d000000-0000-0000-0000-000000000000
-0e000000-0000-0000-0000-000000000000
-0f000000-0000-0000-0000-000000000000
-00000000-0001-0000-0000-000000000000
-00000000-0002-0000-0000-000000000000
-00000000-0003-0000-0000-000000000000
-00000000-0004-0000-0000-000000000000
-00000000-0005-0000-0000-000000000000
-00000000-0006-0000-0000-000000000000
-00000000-0007-0000-0000-000000000000
-00000000-0008-0000-0000-000000000000
-00000000-0009-0000-0000-000000000000
-00000000-000a-0000-0000-000000000000
-00000000-000b-0000-0000-000000000000
-00000000-000c-0000-0000-000000000000
-00000000-000d-0000-0000-000000000000
-00000000-000e-0000-0000-000000000000
-00000000-000f-0000-0000-000000000000
-00000000-0100-0000-0000-000000000000
-00000000-0200-0000-0000-000000000000
-00000000-0300-0000-0000-000000000000
-00000000-0400-0000-0000-000000000000
-00000000-0500-0000-0000-000000000000
-00000000-0600-0000-0000-000000000000
-00000000-0700-0000-0000-000000000000
-00000000-0800-0000-0000-000000000000
-00000000-0900-0000-0000-000000000000
-00000000-0a00-0000-0000-000000000000
-00000000-0b00-0000-0000-000000000000
-00000000-0c00-0000-0000-000000000000
-00000000-0d00-0000-0000-000000000000
-00000000-0e00-0000-0000-000000000000
-00000000-0f00-0000-0000-000000000000
-00000000-0000-0001-0000-000000000000
-00000000-0000-0002-0000-000000000000
-00000000-0000-0003-0000-000000000000
-00000000-0000-0004-0000-000000000000
-00000000-0000-0005-0000-000000000000
-00000000-0000-0006-0000-000000000000
-00000000-0000-0007-0000-000000000000
-00000000-0000-0008-0000-000000000000
-00000000-0000-0009-0000-000000000000
-00000000-0000-000a-0000-000000000000
-00000000-0000-000b-0000-000000000000
-00000000-0000-000c-0000-000000000000
-00000000-0000-000d-0000-000000000000
-00000000-0000-000e-0000-000000000000
-00000000-0000-000f-0000-000000000000
-00000000-0000-0100-0000-000000000000
-00000000-0000-0200-0000-000000000000
-00000000-0000-0300-0000-000000000000
-00000000-0000-0400-0000-000000000000
-00000000-0000-0500-0000-000000000000
-00000000-0000-0600-0000-000000000000
-00000000-0000-0700-0000-000000000000
-00000000-0000-0800-0000-000000000000
-00000000-0000-0900-0000-000000000000
-00000000-0000-0a00-0000-000000000000
-00000000-0000-0b00-0000-000000000000
-00000000-0000-0c00-0000-000000000000
-00000000-0000-0d00-0000-000000000000
-00000000-0000-0e00-0000-000000000000
-00000000-0000-0f00-0000-000000000000
-00000000-0000-0000-0001-000000000000
-00000000-0000-0000-0002-000000000000
-00000000-0000-0000-0003-000000000000
-00000000-0000-0000-0004-000000000000
-00000000-0000-0000-0005-000000000000
-00000000-0000-0000-0006-000000000000
-00000000-0000-0000-0007-000000000000
-00000000-0000-0000-0008-000000000000
-00000000-0000-0000-0009-000000000000
-00000000-0000-0000-000a-000000000000
-00000000-0000-0000-000b-000000000000
-00000000-0000-0000-000c-000000000000
-00000000-0000-0000-000d-000000000000
-00000000-0000-0000-000e-000000000000
-00000000-0000-0000-000f-000000000000
-00000000-0000-0000-0100-000000000000
-00000000-0000-0000-0200-000000000000
-00000000-0000-0000-0300-000000000000
-00000000-0000-0000-0400-000000000000
-00000000-0000-0000-0500-000000000000
-00000000-0000-0000-0600-000000000000
-00000000-0000-0000-0700-000000000000
-00000000-0000-0000-0800-000000000000
-00000000-0000-0000-0900-000000000000
-00000000-0000-0000-0a00-000000000000
-00000000-0000-0000-0b00-000000000000
-00000000-0000-0000-0c00-000000000000
-00000000-0000-0000-0d00-000000000000
-00000000-0000-0000-0e00-000000000000
-00000000-0000-0000-0f00-000000000000
 00000000-0000-0000-0000-000000000001
 00000000-0000-0000-0000-000000000002
 00000000-0000-0000-0000-000000000003
@@ -653,144 +503,6 @@ a
 00000000-0000-0000-0000-0d0000000000
 00000000-0000-0000-0000-0e0000000000
 00000000-0000-0000-0000-0f0000000000
-SELECT COALESCE(NULL, a) FROM t1 ORDER BY a;
-COALESCE(NULL, a)
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000000-0000-0000-0000-000000000000
-00000001-0000-0000-0000-000000000000
-00000002-0000-0000-0000-000000000000
-00000003-0000-0000-0000-000000000000
-00000004-0000-0000-0000-000000000000
-00000005-0000-0000-0000-000000000000
-00000006-0000-0000-0000-000000000000
-00000007-0000-0000-0000-000000000000
-00000008-0000-0000-0000-000000000000
-00000009-0000-0000-0000-000000000000
-0000000a-0000-0000-0000-000000000000
-0000000b-0000-0000-0000-000000000000
-0000000c-0000-0000-0000-000000000000
-0000000d-0000-0000-0000-000000000000
-0000000e-0000-0000-0000-000000000000
-0000000f-0000-0000-0000-000000000000
-00000100-0000-0000-0000-000000000000
-00000200-0000-0000-0000-000000000000
-00000300-0000-0000-0000-000000000000
-00000400-0000-0000-0000-000000000000
-00000500-0000-0000-0000-000000000000
-00000600-0000-0000-0000-000000000000
-00000700-0000-0000-0000-000000000000
-00000800-0000-0000-0000-000000000000
-00000900-0000-0000-0000-000000000000
-00000a00-0000-0000-0000-000000000000
-00000b00-0000-0000-0000-000000000000
-00000c00-0000-0000-0000-000000000000
-00000d00-0000-0000-0000-000000000000
-00000e00-0000-0000-0000-000000000000
-00000f00-0000-0000-0000-000000000000
-00010000-0000-0000-0000-000000000000
-00020000-0000-0000-0000-000000000000
-00030000-0000-0000-0000-000000000000
-00040000-0000-0000-0000-000000000000
-00050000-0000-0000-0000-000000000000
-00060000-0000-0000-0000-000000000000
-00070000-0000-0000-0000-000000000000
-00080000-0000-0000-0000-000000000000
-00090000-0000-0000-0000-000000000000
-000a0000-0000-0000-0000-000000000000
-000b0000-0000-0000-0000-000000000000
-000c0000-0000-0000-0000-000000000000
-000d0000-0000-0000-0000-000000000000
-000e0000-0000-0000-0000-000000000000
-000f0000-0000-0000-0000-000000000000
-01000000-0000-0000-0000-000000000000
-02000000-0000-0000-0000-000000000000
-03000000-0000-0000-0000-000000000000
-04000000-0000-0000-0000-000000000000
-05000000-0000-0000-0000-000000000000
-06000000-0000-0000-0000-000000000000
-07000000-0000-0000-0000-000000000000
-08000000-0000-0000-0000-000000000000
-09000000-0000-0000-0000-000000000000
-0a000000-0000-0000-0000-000000000000
-0b000000-0000-0000-0000-000000000000
-0c000000-0000-0000-0000-000000000000
-0d000000-0000-0000-0000-000000000000
-0e000000-0000-0000-0000-000000000000
-0f000000-0000-0000-0000-000000000000
-00000000-0001-0000-0000-000000000000
-00000000-0002-0000-0000-000000000000
-00000000-0003-0000-0000-000000000000
-00000000-0004-0000-0000-000000000000
-00000000-0005-0000-0000-000000000000
-00000000-0006-0000-0000-000000000000
-00000000-0007-0000-0000-000000000000
-00000000-0008-0000-0000-000000000000
-00000000-0009-0000-0000-000000000000
-00000000-000a-0000-0000-000000000000
-00000000-000b-0000-0000-000000000000
-00000000-000c-0000-0000-000000000000
-00000000-000d-0000-0000-000000000000
-00000000-000e-0000-0000-000000000000
-00000000-000f-0000-0000-000000000000
-00000000-0100-0000-0000-000000000000
-00000000-0200-0000-0000-000000000000
-00000000-0300-0000-0000-000000000000
-00000000-0400-0000-0000-000000000000
-00000000-0500-0000-0000-000000000000
-00000000-0600-0000-0000-000000000000
-00000000-0700-0000-0000-000000000000
-00000000-0800-0000-0000-000000000000
-00000000-0900-0000-0000-000000000000
-00000000-0a00-0000-0000-000000000000
-00000000-0b00-0000-0000-000000000000
-00000000-0c00-0000-0000-000000000000
-00000000-0d00-0000-0000-000000000000
-00000000-0e00-0000-0000-000000000000
-00000000-0f00-0000-0000-000000000000
-00000000-0000-0001-0000-000000000000
-00000000-0000-0002-0000-000000000000
-00000000-0000-0003-0000-000000000000
-00000000-0000-0004-0000-000000000000
-00000000-0000-0005-0000-000000000000
-00000000-0000-0006-0000-000000000000
-00000000-0000-0007-0000-000000000000
-00000000-0000-0008-0000-000000000000
-00000000-0000-0009-0000-000000000000
-00000000-0000-000a-0000-000000000000
-00000000-0000-000b-0000-000000000000
-00000000-0000-000c-0000-000000000000
-00000000-0000-000d-0000-000000000000
-00000000-0000-000e-0000-000000000000
-00000000-0000-000f-0000-000000000000
-00000000-0000-0100-0000-000000000000
-00000000-0000-0200-0000-000000000000
-00000000-0000-0300-0000-000000000000
-00000000-0000-0400-0000-000000000000
-00000000-0000-0500-0000-000000000000
-00000000-0000-0600-0000-000000000000
-00000000-0000-0700-0000-000000000000
-00000000-0000-0800-0000-000000000000
-00000000-0000-0900-0000-000000000000
-00000000-0000-0a00-0000-000000000000
-00000000-0000-0b00-0000-000000000000
-00000000-0000-0c00-0000-000000000000
-00000000-0000-0d00-0000-000000000000
-00000000-0000-0e00-0000-000000000000
-00000000-0000-0f00-0000-000000000000
 00000000-0000-0000-0001-000000000000
 00000000-0000-0000-0002-000000000000
 00000000-0000-0000-0003-000000000000
@@ -821,6 +533,144 @@ COALESCE(NULL, a)
 00000000-0000-0000-0d00-000000000000
 00000000-0000-0000-0e00-000000000000
 00000000-0000-0000-0f00-000000000000
+00000000-0000-0001-0000-000000000000
+00000000-0000-0002-0000-000000000000
+00000000-0000-0003-0000-000000000000
+00000000-0000-0004-0000-000000000000
+00000000-0000-0005-0000-000000000000
+00000000-0000-0006-0000-000000000000
+00000000-0000-0007-0000-000000000000
+00000000-0000-0008-0000-000000000000
+00000000-0000-0009-0000-000000000000
+00000000-0000-000a-0000-000000000000
+00000000-0000-000b-0000-000000000000
+00000000-0000-000c-0000-000000000000
+00000000-0000-000d-0000-000000000000
+00000000-0000-000e-0000-000000000000
+00000000-0000-000f-0000-000000000000
+00000000-0000-0100-0000-000000000000
+00000000-0000-0200-0000-000000000000
+00000000-0000-0300-0000-000000000000
+00000000-0000-0400-0000-000000000000
+00000000-0000-0500-0000-000000000000
+00000000-0000-0600-0000-000000000000
+00000000-0000-0700-0000-000000000000
+00000000-0000-0800-0000-000000000000
+00000000-0000-0900-0000-000000000000
+00000000-0000-0a00-0000-000000000000
+00000000-0000-0b00-0000-000000000000
+00000000-0000-0c00-0000-000000000000
+00000000-0000-0d00-0000-000000000000
+00000000-0000-0e00-0000-000000000000
+00000000-0000-0f00-0000-000000000000
+00000000-0001-0000-0000-000000000000
+00000000-0002-0000-0000-000000000000
+00000000-0003-0000-0000-000000000000
+00000000-0004-0000-0000-000000000000
+00000000-0005-0000-0000-000000000000
+00000000-0006-0000-0000-000000000000
+00000000-0007-0000-0000-000000000000
+00000000-0008-0000-0000-000000000000
+00000000-0009-0000-0000-000000000000
+00000000-000a-0000-0000-000000000000
+00000000-000b-0000-0000-000000000000
+00000000-000c-0000-0000-000000000000
+00000000-000d-0000-0000-000000000000
+00000000-000e-0000-0000-000000000000
+00000000-000f-0000-0000-000000000000
+00000000-0100-0000-0000-000000000000
+00000000-0200-0000-0000-000000000000
+00000000-0300-0000-0000-000000000000
+00000000-0400-0000-0000-000000000000
+00000000-0500-0000-0000-000000000000
+00000000-0600-0000-0000-000000000000
+00000000-0700-0000-0000-000000000000
+00000000-0800-0000-0000-000000000000
+00000000-0900-0000-0000-000000000000
+00000000-0a00-0000-0000-000000000000
+00000000-0b00-0000-0000-000000000000
+00000000-0c00-0000-0000-000000000000
+00000000-0d00-0000-0000-000000000000
+00000000-0e00-0000-0000-000000000000
+00000000-0f00-0000-0000-000000000000
+00000001-0000-0000-0000-000000000000
+00000002-0000-0000-0000-000000000000
+00000003-0000-0000-0000-000000000000
+00000004-0000-0000-0000-000000000000
+00000005-0000-0000-0000-000000000000
+00000006-0000-0000-0000-000000000000
+00000007-0000-0000-0000-000000000000
+00000008-0000-0000-0000-000000000000
+00000009-0000-0000-0000-000000000000
+0000000a-0000-0000-0000-000000000000
+0000000b-0000-0000-0000-000000000000
+0000000c-0000-0000-0000-000000000000
+0000000d-0000-0000-0000-000000000000
+0000000e-0000-0000-0000-000000000000
+0000000f-0000-0000-0000-000000000000
+00000100-0000-0000-0000-000000000000
+00000200-0000-0000-0000-000000000000
+00000300-0000-0000-0000-000000000000
+00000400-0000-0000-0000-000000000000
+00000500-0000-0000-0000-000000000000
+00000600-0000-0000-0000-000000000000
+00000700-0000-0000-0000-000000000000
+00000800-0000-0000-0000-000000000000
+00000900-0000-0000-0000-000000000000
+00000a00-0000-0000-0000-000000000000
+00000b00-0000-0000-0000-000000000000
+00000c00-0000-0000-0000-000000000000
+00000d00-0000-0000-0000-000000000000
+00000e00-0000-0000-0000-000000000000
+00000f00-0000-0000-0000-000000000000
+00010000-0000-0000-0000-000000000000
+00020000-0000-0000-0000-000000000000
+00030000-0000-0000-0000-000000000000
+00040000-0000-0000-0000-000000000000
+00050000-0000-0000-0000-000000000000
+00060000-0000-0000-0000-000000000000
+00070000-0000-0000-0000-000000000000
+00080000-0000-0000-0000-000000000000
+00090000-0000-0000-0000-000000000000
+000a0000-0000-0000-0000-000000000000
+000b0000-0000-0000-0000-000000000000
+000c0000-0000-0000-0000-000000000000
+000d0000-0000-0000-0000-000000000000
+000e0000-0000-0000-0000-000000000000
+000f0000-0000-0000-0000-000000000000
+01000000-0000-0000-0000-000000000000
+02000000-0000-0000-0000-000000000000
+03000000-0000-0000-0000-000000000000
+04000000-0000-0000-0000-000000000000
+05000000-0000-0000-0000-000000000000
+06000000-0000-0000-0000-000000000000
+07000000-0000-0000-0000-000000000000
+08000000-0000-0000-0000-000000000000
+09000000-0000-0000-0000-000000000000
+0a000000-0000-0000-0000-000000000000
+0b000000-0000-0000-0000-000000000000
+0c000000-0000-0000-0000-000000000000
+0d000000-0000-0000-0000-000000000000
+0e000000-0000-0000-0000-000000000000
+0f000000-0000-0000-0000-000000000000
+SELECT COALESCE(NULL, a) FROM t1 ORDER BY a;
+COALESCE(NULL, a)
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
+00000000-0000-0000-0000-000000000000
 00000000-0000-0000-0000-000000000001
 00000000-0000-0000-0000-000000000002
 00000000-0000-0000-0000-000000000003
@@ -911,6 +761,156 @@ COALESCE(NULL, a)
 00000000-0000-0000-0000-0d0000000000
 00000000-0000-0000-0000-0e0000000000
 00000000-0000-0000-0000-0f0000000000
+00000000-0000-0000-0001-000000000000
+00000000-0000-0000-0002-000000000000
+00000000-0000-0000-0003-000000000000
+00000000-0000-0000-0004-000000000000
+00000000-0000-0000-0005-000000000000
+00000000-0000-0000-0006-000000000000
+00000000-0000-0000-0007-000000000000
+00000000-0000-0000-0008-000000000000
+00000000-0000-0000-0009-000000000000
+00000000-0000-0000-000a-000000000000
+00000000-0000-0000-000b-000000000000
+00000000-0000-0000-000c-000000000000
+00000000-0000-0000-000d-000000000000
+00000000-0000-0000-000e-000000000000
+00000000-0000-0000-000f-000000000000
+00000000-0000-0000-0100-000000000000
+00000000-0000-0000-0200-000000000000
+00000000-0000-0000-0300-000000000000
+00000000-0000-0000-0400-000000000000
+00000000-0000-0000-0500-000000000000
+00000000-0000-0000-0600-000000000000
+00000000-0000-0000-0700-000000000000
+00000000-0000-0000-0800-000000000000
+00000000-0000-0000-0900-000000000000
+00000000-0000-0000-0a00-000000000000
+00000000-0000-0000-0b00-000000000000
+00000000-0000-0000-0c00-000000000000
+00000000-0000-0000-0d00-000000000000
+00000000-0000-0000-0e00-000000000000
+00000000-0000-0000-0f00-000000000000
+00000000-0000-0001-0000-000000000000
+00000000-0000-0002-0000-000000000000
+00000000-0000-0003-0000-000000000000
+00000000-0000-0004-0000-000000000000
+00000000-0000-0005-0000-000000000000
+00000000-0000-0006-0000-000000000000
+00000000-0000-0007-0000-000000000000
+00000000-0000-0008-0000-000000000000
+00000000-0000-0009-0000-000000000000
+00000000-0000-000a-0000-000000000000
+00000000-0000-000b-0000-000000000000
+00000000-0000-000c-0000-000000000000
+00000000-0000-000d-0000-000000000000
+00000000-0000-000e-0000-000000000000
+00000000-0000-000f-0000-000000000000
+00000000-0000-0100-0000-000000000000
+00000000-0000-0200-0000-000000000000
+00000000-0000-0300-0000-000000000000
+00000000-0000-0400-0000-000000000000
+00000000-0000-0500-0000-000000000000
+00000000-0000-0600-0000-000000000000
+00000000-0000-0700-0000-000000000000
+00000000-0000-0800-0000-000000000000
+00000000-0000-0900-0000-000000000000
+00000000-0000-0a00-0000-000000000000
+00000000-0000-0b00-0000-000000000000
+00000000-0000-0c00-0000-000000000000
+00000000-0000-0d00-0000-000000000000
+00000000-0000-0e00-0000-000000000000
+00000000-0000-0f00-0000-000000000000
+00000000-0001-0000-0000-000000000000
+00000000-0002-0000-0000-000000000000
+00000000-0003-0000-0000-000000000000
+00000000-0004-0000-0000-000000000000
+00000000-0005-0000-0000-000000000000
+00000000-0006-0000-0000-000000000000
+00000000-0007-0000-0000-000000000000
+00000000-0008-0000-0000-000000000000
+00000000-0009-0000-0000-000000000000
+00000000-000a-0000-0000-000000000000
+00000000-000b-0000-0000-000000000000
+00000000-000c-0000-0000-000000000000
+00000000-000d-0000-0000-000000000000
+00000000-000e-0000-0000-000000000000
+00000000-000f-0000-0000-000000000000
+00000000-0100-0000-0000-000000000000
+00000000-0200-0000-0000-000000000000
+00000000-0300-0000-0000-000000000000
+00000000-0400-0000-0000-000000000000
+00000000-0500-0000-0000-000000000000
+00000000-0600-0000-0000-000000000000
+00000000-0700-0000-0000-000000000000
+00000000-0800-0000-0000-000000000000
+00000000-0900-0000-0000-000000000000
+00000000-0a00-0000-0000-000000000000
+00000000-0b00-0000-0000-000000000000
+00000000-0c00-0000-0000-000000000000
+00000000-0d00-0000-0000-000000000000
+00000000-0e00-0000-0000-000000000000
+00000000-0f00-0000-0000-000000000000
+00000001-0000-0000-0000-000000000000
+00000002-0000-0000-0000-000000000000
+00000003-0000-0000-0000-000000000000
+00000004-0000-0000-0000-000000000000
+00000005-0000-0000-0000-000000000000
+00000006-0000-0000-0000-000000000000
+00000007-0000-0000-0000-000000000000
+00000008-0000-0000-0000-000000000000
+00000009-0000-0000-0000-000000000000
+0000000a-0000-0000-0000-000000000000
+0000000b-0000-0000-0000-000000000000
+0000000c-0000-0000-0000-000000000000
+0000000d-0000-0000-0000-000000000000
+0000000e-0000-0000-0000-000000000000
+0000000f-0000-0000-0000-000000000000
+00000100-0000-0000-0000-000000000000
+00000200-0000-0000-0000-000000000000
+00000300-0000-0000-0000-000000000000
+00000400-0000-0000-0000-000000000000
+00000500-0000-0000-0000-000000000000
+00000600-0000-0000-0000-000000000000
+00000700-0000-0000-0000-000000000000
+00000800-0000-0000-0000-000000000000
+00000900-0000-0000-0000-000000000000
+00000a00-0000-0000-0000-000000000000
+00000b00-0000-0000-0000-000000000000
+00000c00-0000-0000-0000-000000000000
+00000d00-0000-0000-0000-000000000000
+00000e00-0000-0000-0000-000000000000
+00000f00-0000-0000-0000-000000000000
+00010000-0000-0000-0000-000000000000
+00020000-0000-0000-0000-000000000000
+00030000-0000-0000-0000-000000000000
+00040000-0000-0000-0000-000000000000
+00050000-0000-0000-0000-000000000000
+00060000-0000-0000-0000-000000000000
+00070000-0000-0000-0000-000000000000
+00080000-0000-0000-0000-000000000000
+00090000-0000-0000-0000-000000000000
+000a0000-0000-0000-0000-000000000000
+000b0000-0000-0000-0000-000000000000
+000c0000-0000-0000-0000-000000000000
+000d0000-0000-0000-0000-000000000000
+000e0000-0000-0000-0000-000000000000
+000f0000-0000-0000-0000-000000000000
+01000000-0000-0000-0000-000000000000
+02000000-0000-0000-0000-000000000000
+03000000-0000-0000-0000-000000000000
+04000000-0000-0000-0000-000000000000
+05000000-0000-0000-0000-000000000000
+06000000-0000-0000-0000-000000000000
+07000000-0000-0000-0000-000000000000
+08000000-0000-0000-0000-000000000000
+09000000-0000-0000-0000-000000000000
+0a000000-0000-0000-0000-000000000000
+0b000000-0000-0000-0000-000000000000
+0c000000-0000-0000-0000-000000000000
+0d000000-0000-0000-0000-000000000000
+0e000000-0000-0000-0000-000000000000
+0f000000-0000-0000-0000-000000000000
 #
 # Lexicographical ORDER BY
 #
@@ -1659,10 +1659,10 @@ INSERT INTO t1 VALUES (CAST(CONCAT('2','0000000-0000-0000-0000-000000000003') AS
 SELECT * FROM t1 ORDER BY a;
 a
 10000000-0000-0000-0000-000000000001
-20000000-0000-0000-0000-000000000001
 10000000-0000-0000-0000-000000000002
-20000000-0000-0000-0000-000000000002
 10000000-0000-0000-0000-000000000003
+20000000-0000-0000-0000-000000000001
+20000000-0000-0000-0000-000000000002
 20000000-0000-0000-0000-000000000003
 DROP TABLE t1;
 #
@@ -3178,8 +3178,8 @@ CREATE TABLE t1 (d UUID);
 INSERT INTO t1 VALUES ('00000000-0000-0000-0000-111111111111'), ('11111111-0000-0000-0000-000000000000');
 SELECT * FROM t1 ORDER BY d;
 d
-11111111-0000-0000-0000-000000000000
 00000000-0000-0000-0000-111111111111
+11111111-0000-0000-0000-000000000000
 SELECT * FROM t1 WHERE d <= ALL (SELECT * FROM t1);
 d
 11111111-0000-0000-0000-000000000000
diff --git a/plugin/type_uuid/mysql-test/type_uuid/type_uuid_memory.result b/plugin/type_uuid/mysql-test/type_uuid/type_uuid_memory.result
index f2a1e27d0d0..21086fef2cb 100644
--- a/plugin/type_uuid/mysql-test/type_uuid/type_uuid_memory.result
+++ b/plugin/type_uuid/mysql-test/type_uuid/type_uuid_memory.result
@@ -25,7 +25,7 @@ a
 00000000-0000-0000-0000-0000000000ff
 EXPLAIN SELECT * FROM t1 WHERE a='00000000-0000-0000-0000-0000000000ff';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ref	a	a	17	const	4	Using where
+1	SIMPLE	t1	ref	a	a	17	const	2	Using where
 SELECT * FROM t1 WHERE a='garbage';
 a
 Warnings:
@@ -66,7 +66,7 @@ EXPLAIN SELECT * FROM t1 WHERE a IN
 '00000000-0000-0000-0000-0000000000f0'
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	17	NULL	12	Using where
+1	SIMPLE	t1	range	a	a	17	NULL	6	Using where
 SELECT * FROM t1 WHERE a IN
 (
 '00000000-0000-0000-0000-000000000080',
@@ -85,7 +85,7 @@ EXPLAIN SELECT * FROM t1 WHERE a IN
 'garbage'
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	17	NULL	8	Using where
+1	SIMPLE	t1	range	a	a	17	NULL	4	Using where
 Warnings:
 Warning	1292	Incorrect uuid value: 'garbage'
 SELECT * FROM t1 WHERE a BETWEEN
@@ -178,7 +178,7 @@ a
 00000000-0000-0000-0000-0000000000ff
 EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=CAST('00000000-0000-0000-0000-0000000000ff' AS UUID);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t1	ref	a	a	17	const	4	100.00	Using where
+1	SIMPLE	t1	ref	a	a	17	const	2	100.00	Using where
 Warnings:
 Note	1003	select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = UUID'00000000-0000-0000-0000-0000000000ff'
 DROP TABLE t1;
diff --git a/plugin/type_uuid/mysql-test/type_uuid/type_uuid_myisam.result b/plugin/type_uuid/mysql-test/type_uuid/type_uuid_myisam.result
index 8df2c85ae48..736aea4555f 100644
--- a/plugin/type_uuid/mysql-test/type_uuid/type_uuid_myisam.result
+++ b/plugin/type_uuid/mysql-test/type_uuid/type_uuid_myisam.result
@@ -25,7 +25,7 @@ a
 00000000-0000-0000-0000-0000000000ff
 EXPLAIN SELECT * FROM t1 WHERE a='00000000-0000-0000-0000-0000000000ff';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ref	a	a	17	const	1	Using where; Using index
+1	SIMPLE	t1	ref	a	a	17	const	7	Using where; Using index
 SELECT * FROM t1 WHERE a='garbage';
 a
 Warnings:
@@ -41,7 +41,7 @@ a
 00000000-0000-0000-0000-0000000000ff
 EXPLAIN SELECT * FROM t1 WHERE a>='00000000-0000-0000-0000-0000000000fe';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	17	NULL	3	Using where; Using index
+1	SIMPLE	t1	range	a	a	17	NULL	28	Using where; Using index
 SELECT * FROM t1 WHERE a>='garbage';
 a
 EXPLAIN SELECT * FROM t1 WHERE a>='garbage';
@@ -64,7 +64,7 @@ EXPLAIN SELECT * FROM t1 WHERE a IN
 '00000000-0000-0000-0000-0000000000f0'
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	17	NULL	4	Using where; Using index
+1	SIMPLE	t1	range	a	a	17	NULL	3	Using where; Using index
 SELECT * FROM t1 WHERE a IN
 (
 '00000000-0000-0000-0000-000000000080',
@@ -96,7 +96,7 @@ EXPLAIN SELECT * FROM t1 WHERE a BETWEEN
 '00000000-0000-0000-0000-000000000080' AND
 '00000000-0000-0000-0000-000000000081';
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	range	a	a	17	NULL	2	Using where; Using index
+1	SIMPLE	t1	range	a	a	17	NULL	1	Using where; Using index
 SELECT * FROM t1 WHERE a BETWEEN
 '00000000-0000-0000-0000-000000000080' AND
 'garbage';
@@ -111,7 +111,7 @@ a
 00000000-0000-0000-0000-0000000000ff
 EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=CAST('00000000-0000-0000-0000-0000000000ff' AS UUID);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	SIMPLE	t1	ref	a	a	17	const	1	100.00	Using where; Using index
+1	SIMPLE	t1	ref	a	a	17	const	7	100.00	Using where; Using index
 Warnings:
 Note	1003	select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = UUID'00000000-0000-0000-0000-0000000000ff'
 DROP TABLE t1;
diff --git a/plugin/type_uuid/plugin.cc b/plugin/type_uuid/plugin.cc
index bd320a9a897..22c0c883fad 100644
--- a/plugin/type_uuid/plugin.cc
+++ b/plugin/type_uuid/plugin.cc
@@ -21,10 +21,31 @@
 #include <mysql/plugin_data_type.h>
 #include <mysql/plugin_function.h>
 
+class Type_handler_uuid: public UUIDBundle::Type_handler_fbt
+{
+public:
+  Field *make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *root,
+                            const LEX_CSTRING *name, const Record_addr &addr,
+                            const Bit_addr &bit,
+                            const Column_definition_attributes *attr,
+                            uint32 flags) const override
+  {
+    if (share->mysql_version >= 110100)
+      return UUIDBundle::Type_handler_fbt::make_table_field_from_def(share,
+               root, name, addr, bit, attr, flags);
+    return
+      reinterpret_cast<const UUIDoldBundle::Type_handler_fbt*>(this)->
+        UUIDoldBundle::Type_handler_fbt::make_table_field_from_def(share,
+               root, name, addr, bit, attr, flags);
+  }
+};
+
+static Type_handler_uuid type_handler_uuid;
+
 static struct st_mariadb_data_type plugin_descriptor_type_uuid=
 {
   MariaDB_DATA_TYPE_INTERFACE_VERSION,
-  UUIDBundle::type_handler_fbt()
+  &type_handler_uuid
 };
 
 /*************************************************************************/
@@ -71,6 +92,13 @@ static Plugin_function
   plugin_descriptor_function_uuid(&Create_func_uuid::s_singleton),
   plugin_descriptor_function_sys_guid(&Create_func_sys_guid::s_singleton);
 
+int uuid_init(void*)
+{
+  UUIDBundle::type_handler_fbt()->set_name(type_handler_uuid.name());
+  UUIDoldBundle::type_handler_fbt()->set_name(Name(STRING_WITH_LEN("uuid /* old */")));
+  return 0;
+}
+
 /*************************************************************************/
 
 maria_declare_plugin(type_uuid)
@@ -81,7 +109,7 @@ maria_declare_plugin(type_uuid)
   "MariaDB Corporation",        // plugin author
   "Data type UUID",             // the plugin description
   PLUGIN_LICENSE_GPL,           // the plugin license (see include/mysql/plugin.h)
-  0,                            // Pointer to plugin initialization function
+  uuid_init,                    // Pointer to plugin initialization function
   0,                            // Pointer to plugin deinitialization function
   0x0100,                       // Numeric version 0xAABB means AA.BB version
   NULL,                         // Status variables
diff --git a/plugin/type_uuid/sql_type_uuid.h b/plugin/type_uuid/sql_type_uuid.h
index 193b6a9aaed..aa9c768d52b 100644
--- a/plugin/type_uuid/sql_type_uuid.h
+++ b/plugin/type_uuid/sql_type_uuid.h
@@ -17,6 +17,8 @@
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
 
 #include "sql_type_fixedbin_storage.h"
+
+template <bool force_swap>
 class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
 {
   bool get_digit(char ch, uint *val)
@@ -93,6 +95,8 @@ class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
     }
     if (str < end)
       goto err; // Some input left
+    if (m_buffer[6] < 0 && m_buffer[8] > 0)
+      goto err; // impossible combination: version >= 8, variant = 0
     return false;
   err:
     bzero(m_buffer, sizeof(m_buffer));
@@ -172,21 +176,31 @@ class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
   // Convert the in-memory representation to the in-record representation
   static void memory_to_record(char *to, const char *from)
   {
-    segment(0).memory_to_record(to, from);
-    segment(1).memory_to_record(to, from);
-    segment(2).memory_to_record(to, from);
-    segment(3).memory_to_record(to, from);
-    segment(4).memory_to_record(to, from);
+    if (force_swap || (from[6] > 0 && from[6] < 0x60 && from[8] < 0))
+    {
+      segment(0).memory_to_record(to, from);
+      segment(1).memory_to_record(to, from);
+      segment(2).memory_to_record(to, from);
+      segment(3).memory_to_record(to, from);
+      segment(4).memory_to_record(to, from);
+    }
+    else
+      memcpy(to, from, binary_length());
   }
 
   // Convert the in-record representation to the in-memory representation
   static void record_to_memory(char *to, const char *from)
   {
-    segment(0).record_to_memory(to, from);
-    segment(1).record_to_memory(to, from);
-    segment(2).record_to_memory(to, from);
-    segment(3).record_to_memory(to, from);
-    segment(4).record_to_memory(to, from);
+    if (force_swap || (from[6] < 0 && from[8] > 0))
+    {
+      segment(0).record_to_memory(to, from);
+      segment(1).record_to_memory(to, from);
+      segment(2).record_to_memory(to, from);
+      segment(3).record_to_memory(to, from);
+      segment(4).record_to_memory(to, from);
+    }
+    else
+      memcpy(to, from, binary_length());
   }
 
   /*
@@ -265,8 +279,8 @@ class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
 
 };
 
-
 #include "sql_type_fixedbin.h"
-typedef FixedBinTypeBundle<UUID> UUIDBundle;
+typedef FixedBinTypeBundle<UUID<true> > UUIDoldBundle;
+typedef FixedBinTypeBundle<UUID<false> > UUIDBundle;
 
 #endif // SQL_TYPE_UUID_INCLUDED
