Geometric bewilderment

Lists: pgsql-hackers
From: Paul Matthews <plm(at)netspace(dot)net(dot)au>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Geometric bewilderment
Date: 2009-08-19 09:29:43
Message-ID: 4A8BC607.6070306@netspace.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

#!/usr/bin/perl
use DBI;
use warnings;
use strict;

my $dbname = shift or die;
my ($dbh, $stm);

$dbh = DBI->connect("dbi:Pg:dbname=$dbname", "", "") or die $!;
$dbh->do(<<EOSQL) or die $!;
DROP TABLE IF EXISTS points;
CREATE TABLE points(
pointpoint point,
pointbox box
);
EOSQL

for( my $i=0; $i<1000; $i++ ) {
my $x = rand(2.0)-1.0;
my $y = rand(2.0)-1.0;
$dbh->do(<<EOSQL) or die $!;
INSERT INTO points VALUES (
Point('$x,$y'),
Box(Point('$x,$y'),Point('$x,$y'))
);
EOSQL
}

check_box_point('<<');
check_box_point('&<');
check_box_point('&&');
check_box_point('&>');
check_box_point('>>');
check_box_point('@>');
check_box_point('&<|');
check_box_point('<<|');
check_box_point('|>>');
check_box_point('|&>');

check_point_box('<<');
check_point_box('&<');
check_point_box('&&');
check_point_box('&>');
check_point_box('>>');
check_point_box('<@');
check_point_box('&<|');
check_point_box('<<|');
check_point_box('|>>');
check_point_box('|&>');

#--------------------------------------------------------------------------
# Check 'box OP point' against 'box OP box(point,point)'
#--------------------------------------------------------------------------
sub check_box_point {
my $op = shift;
print "box $op point\n";

$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('-0.5,-0.5'),point('0.5,0.5')) AS b
WHERE
b $op pointbox
ORDER BY
x, y;
EOSQL

$stm->execute() or die $!;
open FH, ">a.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;

$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('-0.5,-0.5'),point('0.5,0.5')) AS b
WHERE
b $op pointpoint
ORDER BY
x, y;
EOSQL

$stm->execute() or die $!;
open FH, ">b.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;

system( "tkdiff a.out b.out >/dev/null 2>&1" );
}

#--------------------------------------------------------------------------
# Check 'point OP box' against 'box(point,point) OP box'
#--------------------------------------------------------------------------
sub check_point_box {
my $op = shift;
print "point $op box\n";

$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('0.25,0.25'),point('0.75,0.75')) AS b
WHERE
pointbox $op b
ORDER BY
x, y;
EOSQL

$stm->execute() or die $!;
open FH, ">a.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;

$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('0.25,0.25'),point('0.75,0.75')) AS b
WHERE
pointpoint $op b
ORDER BY
x, y;
EOSQL

$stm->execute() or die $!;
open FH, ">b.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;

system( "tkdiff a.out b.out >/dev/null 2>&1" );
}

Attachment Content-Type Size
unknown_filename text/html 4.3 KB
contains.c text/x-csrc 9.7 KB
contains.sql text/x-sql 9.9 KB
test1 text/plain 3.3 KB
test2 text/plain 2.2 KB

From: David Fetter <david(at)fetter(dot)org>
To: Paul Matthews <plm(at)netspace(dot)net(dot)au>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Geometric bewilderment
Date: 2009-08-19 21:42:10
Message-ID: 20090819214210.GA14330@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Aug 19, 2009 at 07:29:43PM +1000, Paul Matthews wrote:
> Suspect that attaching large amounts of code is a breach of
> etiquette.

Code attachments aren't, but HTML messages are, so in future, please
send only text :)

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate