From 9a788e16df9ae45f975b6a4238918d4882d05847 Mon Sep 17 00:00:00 2001 From: Brian Fraser Date: Thu, 16 Aug 2012 14:07:58 -0300 Subject: [PATCH] Fix for 930693: ChangeHandler and text columns with just whitespace --- bin/pt-table-sync | 2 +- bin/pt-upgrade | 2 +- lib/ChangeHandler.pm | 5 ++++- t/lib/ChangeHandler.t | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bin/pt-table-sync b/bin/pt-table-sync index 2207c55b..f1835661 100755 --- a/bin/pt-table-sync +++ b/bin/pt-table-sync @@ -3512,7 +3512,7 @@ sub make_fetch_back_query { my $col = $_; if ( $self->{hex_blob} && $tbl_struct->{type_for}->{$col} =~ m/blob|text|binary/ ) { - $col = "IF(`$col`='', '', CONCAT('0x', HEX(`$col`))) AS `$col`"; + $col = "IF(BINARY(`$col`)='', '', CONCAT('0x', HEX(`$col`))) AS `$col`"; } else { $col = "`$col`"; diff --git a/bin/pt-upgrade b/bin/pt-upgrade index daa72ddc..9c0aea3a 100755 --- a/bin/pt-upgrade +++ b/bin/pt-upgrade @@ -4746,7 +4746,7 @@ sub make_fetch_back_query { my $col = $_; if ( $self->{hex_blob} && $tbl_struct->{type_for}->{$col} =~ m/blob|text|binary/ ) { - $col = "IF(`$col`='', '', CONCAT('0x', HEX(`$col`))) AS `$col`"; + $col = "IF(BINARY(`$col`)='', '', CONCAT('0x', HEX(`$col`))) AS `$col`"; } else { $col = "`$col`"; diff --git a/lib/ChangeHandler.pm b/lib/ChangeHandler.pm index da0516ff..cbba6f66 100644 --- a/lib/ChangeHandler.pm +++ b/lib/ChangeHandler.pm @@ -488,7 +488,10 @@ sub make_fetch_back_query { my $col = $_; if ( $self->{hex_blob} && $tbl_struct->{type_for}->{$col} =~ m/blob|text|binary/ ) { - $col = "IF(`$col`='', '', CONCAT('0x', HEX(`$col`))) AS `$col`"; + # Here we cast to binary, as otherwise, since text columns are + # space padded, MySQL would compare ' ' and '' to be the same. + # See https://bugs.launchpad.net/percona-toolkit/+bug/930693 + $col = "IF(BINARY(`$col`)='', '', CONCAT('0x', HEX(`$col`))) AS `$col`"; } else { $col = "`$col`"; diff --git a/t/lib/ChangeHandler.t b/t/lib/ChangeHandler.t index 835fdd31..2f7aa676 100644 --- a/t/lib/ChangeHandler.t +++ b/t/lib/ChangeHandler.t @@ -357,7 +357,7 @@ $ch = new ChangeHandler( is( $ch->make_fetch_back_query('1=1'), - "SELECT `a`, IF(`x`='', '', CONCAT('0x', HEX(`x`))) AS `x`, `b` FROM `test`.`lt` WHERE 1=1 LIMIT 1", + "SELECT `a`, IF(BINARY(`x`)='', '', CONCAT('0x', HEX(`x`))) AS `x`, `b` FROM `test`.`lt` WHERE 1=1 LIMIT 1", "Wraps BLOB column in CONCAT('0x', HEX(col)) AS col" ); @@ -402,7 +402,7 @@ $ch = new ChangeHandler( is( $ch->make_fetch_back_query('1=1'), - "SELECT IF(`t`='', '', CONCAT('0x', HEX(`t`))) AS `t` FROM `test`.`t` WHERE 1=1 LIMIT 1", + "SELECT IF(BINARY(`t`)='', '', CONCAT('0x', HEX(`t`))) AS `t` FROM `test`.`t` WHERE 1=1 LIMIT 1", "Don't prepend 0x to blank blob/text column value (issue 1052)" );