From d5ec5d9ca87f22fba49df30799a821c4b681fe47 Mon Sep 17 00:00:00 2001 From: Roberto de Bem Date: Sat, 21 Dec 2024 15:00:37 -0300 Subject: [PATCH 1/3] PT-2410 - Removing the enclosed by on \N(Null) values and removing the space existent after the comma --- bin/pt-archiver | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/pt-archiver b/bin/pt-archiver index 7925fe62..41f83e8b 100755 --- a/bin/pt-archiver +++ b/bin/pt-archiver @@ -6966,7 +6966,7 @@ sub main { warn "Invalid output format:". $o->get('format'); warn "Using default 'dump' format"; } elsif ($o->get('output-format') || '' eq 'csv') { - $fields_separated_by = ", "; + $fields_separated_by = ","; $optionally_enclosed_by = '"'; } my $need_hdr = $o->get('header') && !-f $archive_file; @@ -7508,7 +7508,7 @@ sub escape { s/([\t\n\\])/\\$1/g if defined $_; # Escape tabs etc my $s = defined $_ ? $_ : '\N'; # NULL = \N # var & ~var will return 0 only for numbers - if ($s !~ /^[0-9,.E]+$/ && $optionally_enclosed_by eq '"') { + if ($s !~ /^[0-9,.E]+$/ && $optionally_enclosed_by eq '"' && $s ne '\N') { $s =~ s/([^\\])"/$1\\"/g; $s = $optionally_enclosed_by."$s".$optionally_enclosed_by; } From 3cb46e61f74824109101a0213d18b44f22ad3ff7 Mon Sep 17 00:00:00 2001 From: Sveta Smirnova Date: Thu, 13 Mar 2025 22:57:46 +0300 Subject: [PATCH 2/3] PT-2410 - pt-archiver brokes when using output-format=csv and has null values - Added test case --- t/pt-archiver/pt-2410.t | 75 +++++++++++++++++++++++++++++++ t/pt-archiver/samples/pt-2410.sql | 10 +++++ 2 files changed, 85 insertions(+) create mode 100644 t/pt-archiver/pt-2410.t create mode 100644 t/pt-archiver/samples/pt-2410.sql diff --git a/t/pt-archiver/pt-2410.t b/t/pt-archiver/pt-2410.t new file mode 100644 index 00000000..68c6ba67 --- /dev/null +++ b/t/pt-archiver/pt-2410.t @@ -0,0 +1,75 @@ +#!/usr/bin/env perl + +BEGIN { + die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" + unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; + unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; +}; + +use strict; +use warnings FATAL => 'all'; +use English qw(-no_match_vars); +use Test::More; + +use charnames ':full'; + +use PerconaTest; +use Sandbox; +require "$trunk/bin/pt-archiver"; + +my $dp = new DSNParser(opts=>$dsn_opts); +my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); +my $dbh = $sb->get_dbh_for('source'); + +if ( !$dbh ) { + plan skip_all => 'Cannot connect to sandbox source'; +} + +my $output; +my $exit_status; +my $cnf = "/tmp/12345/my.sandbox.cnf"; +my $cmd = "$trunk/bin/pt-archiver"; + +$sb->wipe_clean($dbh); +$sb->create_dbs($dbh, ['test']); + +$sb->load_file('source', 't/pt-archiver/samples/pt-2410.sql'); + +($output, $exit_status) = full_output( + sub { pt_archiver::main( + qw(--where 1=1 --output-format=csv), + '--source', "L=1,D=pt_2410,t=test,F=$cnf", + '--file', '/tmp/pt-2410.csv') }, +); + +is( + $exit_status, + 0, + 'pt-archiver comleted' +); + +$output = `cat /tmp/pt-2410.csv`; +like( + $output, + qr/1,\\N,"testing..."/, + 'NULL values stored correctly' +) or diag($output); + +$dbh->do("load data local infile '/tmp/pt-2410.csv' into table pt_2410.test COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'"); + +$output = `/tmp/12345/use pt_2410 -N -e 'SELECT * FROM test'`; + +like( + $output, + qr/1 NULL testing.../, + 'NULL values loaded correctly' +) or diag($output); + +# ############################################################################# +# Done. +# ############################################################################# +diag(`rm -f /tmp/pt-2410.csv`); +$sb->wipe_clean($dbh); +ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox"); +done_testing; +exit; diff --git a/t/pt-archiver/samples/pt-2410.sql b/t/pt-archiver/samples/pt-2410.sql new file mode 100644 index 00000000..c64f6b24 --- /dev/null +++ b/t/pt-archiver/samples/pt-2410.sql @@ -0,0 +1,10 @@ +CREATE DATABASE pt_2410; +USE pt_2410; + +CREATE TABLE test( + id int not null primary key auto_increment, + column1 int default null, + column2 varchar(50) not null); + +INSERT INTO test VALUES (null,null,'testing...'); +INSERT INTO test VALUES (null,null,'testing...'); From f4a324581afae991ae950342617cbec053688010 Mon Sep 17 00:00:00 2001 From: Sveta Smirnova Date: Thu, 13 Mar 2025 23:26:56 +0300 Subject: [PATCH 3/3] PT-2410 - pt-archiver brokes when using output-format=csv and has null values - Updated t/pt-archiver/file.t --- t/pt-archiver/file.t | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/t/pt-archiver/file.t b/t/pt-archiver/file.t index 7295e7ea..44aa7b4e 100644 --- a/t/pt-archiver/file.t +++ b/t/pt-archiver/file.t @@ -123,11 +123,11 @@ $output = output( ); $output = `cat archive.test.table_2`; is($output, <