Perl language simple table function
This example uses the following file name: TimeSplitMany.pm
Code
The code in this example creates a table function that takes a variable number of string
representations of time that is separated by commas (for example "12:30:55,4:20:18”). The function
outputs one row for each time-representation; each row displays the time, split into its component
parts. Note that you must take control of the I/O for the AE in the
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;
Deployment
Deploy the
script:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language perl --version 3 \
--template deploy TimeSplitMany.pmRegistration
Register the table function
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))"Running
Run the 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)