Skip to content

Commit

Permalink
Merge branch 'olivierdalang-ordered_copy_statements' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mla committed Nov 28, 2021
2 parents f0bc418 + dfb1f50 commit acbf533
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pg_sample
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ if ($opt{help}) {
exit 0;
}

if ($opt{random} && $opt{ordered}) {
print("Error: --random and --ordered are mutually exclusive");
exit 1;
}

@ARGV or die "\nUsage: $0 [ option... ] [ dbname ]\n\n\t" .
"$0 --help for detailed options\n";

Expand Down Expand Up @@ -617,6 +622,16 @@ while (my $row = lower_keys($sth->fetchrow_hashref)) {
if ($opt{random} && $pg_version < version->declare('9.5')) {
$order = $opt{random} ? 'ORDER BY random()' : '';
}
elsif ($opt{ordered})
{
my @cols = find_candidate_key($table);
if (@cols) {
my $cols = join ', ', map { $dbh->quote_identifier($_) } @cols;
$order = "ORDER BY $cols";
} else {
notice "No candidate key found for '$table'; ignoring --ordered";
}
}

$dbh->do(qq{
CREATE $unlogged TABLE $sample_table AS
Expand Down
28 changes: 27 additions & 1 deletion t/pg_sample.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use warnings;
use Carp;
use DBI;
use Getopt::Long qw/ GetOptions :config no_ignore_case /;
use Test::More tests => 12;
use Test::More tests => 14;

$| = 1;

Expand Down Expand Up @@ -215,6 +215,11 @@ if ($@) {

### End Parititions

# Create unordered table to test --ordered
$dbh->do(qq{CREATE TABLE "test_ordered" (id SERIAL PRIMARY KEY, name TEXT);});
$dbh->do(qq{CREATE INDEX "my_index" ON "test_ordered" (name);});
$dbh->do(qq{INSERT INTO "test_ordered" VALUES (1, 'b'), (2, 'a');});
$dbh->do(qq{CLUSTER "test_ordered" USING "my_index"; -- with this, default SELECT will return 2,1;});

# Perform code coverage analysis? Requires Devel::Cover module.
if ($opt{cover}) {
Expand Down Expand Up @@ -248,6 +253,27 @@ my $row = $dbh->selectrow_hashref(qq{
});
is($row->{name}, "\\.", "escaping");

# without --ordered, test_ordered returns as per clustered order
my($ord) = $dbh->selectrow_array(qq{ SELECT STRING_AGG(id::text, ',') FROM "test_ordered" GROUP BY TRUE });
is($ord, '2,1', "ordered test case broken, this should return by clustered order");

@opts = ('--ordered');
push @opts, '--verbose' if $opt{verbose};
$cmd = "pg_sample @opts $opt{db_name} > sample_ordered.sql";
system($cmd) == 0 or die "pg_sample failed: $?";

$dbh->disconnect;
$template1_dbh->do("DROP DATABASE $opt{db_name}");
$template1_dbh->do("CREATE DATABASE $opt{db_name}");
$dbh = connect_db();

# Load the sample DB
$cmd = "psql -q -X -v ON_ERROR_STOP=1 $opt{db_name} < sample_ordered.sql";
system($cmd) == 0 or die "pg_sample failed: $?";

$ord = $dbh->selectrow_array(qq{ SELECT STRING_AGG(id::text, ',') FROM "test_ordered" GROUP BY TRUE });
is($ord, '1,2', "results should be ordered");

$dbh->disconnect;
$template1_dbh->do("DROP DATABASE $opt{db_name}");

Expand Down

0 comments on commit acbf533

Please sign in to comment.