Función de tabla simple en lenguaje Perl
Este ejemplo utiliza el siguiente nombre de archivo: ' TimeSplitMany.pm
Código
El código de este ejemplo crea una función de tabla que toma un número variable de representaciones de cadenas de tiempo separadas por comas (por ejemplo "12:30:55,4:20:18"). La función produce una fila para cada representación temporal; cada fila muestra la hora, dividida en sus partes componentes. Tenga en cuenta que debe tomar el control de la E/S para el AE en el '
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;
virtual
Despliegue el script:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language perl --version 3 \
--template deploy TimeSplitMany.pm
Registro
Registra la función de mesa 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))"
En ejecución
Ejecute la consulta en 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)