Funzione di tabella semplice in linguaggio Perl
Questo esempio utilizza il seguente nome di file: 'TimeSplitMany.pm
Codice
Il codice di questo esempio crea una funzione tabella che accetta un numero variabile di rappresentazioni di stringhe di tempo separate da virgole (ad esempio "12:30:55,4:20:18"). La funzione produce una riga per ogni rappresentazione temporale; ogni riga mostra il tempo, suddiviso nelle sue parti. Si noti che è necessario prendere il controllo dell'I/O per l'AE nel '
sub
_runUdtf()function
.package TimeSplitMany;
use nzae::Ae;
use strict;
use autodie;
our @ISA = qw(nzae::Ae);
my $ae = TimeSplitMany->new();
$ae->run();
sub runUdtf(@)
{
my $self = shift;
while ($self->getNext())
{
my @row = $self->getInputRow();
if ( scalar( @row ) > 0 )
{
my @timeArray = split(/,/, $row[0]);
if ( scalar( @timeArray ) > 0 )
{
for (my $i = 0; $i <= $#timeArray; $i++)
{
my @time = split(/:/, $timeArray[$i]);
if ( scalar( @time ) > 0 )
{
$self->output( @time );
}
else
{
croak(nzae::Exceptions::AeInternalError->new("Error in
splitting input "));
}
}
}
else
{
croak(nzae::Exceptions::AeInternalError->new("Error in input
"));
}
}
else
{
croak(nzae::Exceptions::AeInternalError->new("Error fetching
row"));
}
}
}
1;
Distribuzione
Distribuire lo script:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language perl --version 3 \
--template deploy TimeSplitMany.pm
Registrazione
Registra la funzione tabella AE:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language perl --version 3 \
--template udtf --exe TimeSplitMany.pm \
--sig "time_split_many(varchar(1000))" \
--return "table(hour varchar(2), minute varchar(2), second varchar(2))"
In esecuzione
Eseguire la query in nzsql:
SELECT * FROM TABLE WITH FINAL (time_split_many('13:22:47,22:12:45,03:22:09'));
HOUR | MINUTE | SECOND
-----------+--------------+--------
13 | 22 | 47
22 | 12 | 45
03 | 22 | 09
(3 rows)