Python language shapers and sizers
This example uses the following file name: shaperSizer.py
Code
The code in this example demonstrates both a shaper and sizer, each of which use the same code
and perform essentially the same task. The code returns as output the given input. It does not
demonstrate retrieving the literal fields, just the shaper/sizer
functionality.
import nzae
class ShaperSizerAe(nzae.Ae):
def _runSizer(self):
self._runShaper()
def _runShaper(self):
# GET THE INPUT TYPE.
inputType = self.getInputType(0)
columnName = self.getDataTypeName(inputType)
# HANDLE STRINGS.
if inputType in self.STRING_DATA_TYPES:
size = self.getInputSize(0)
self.addOutputColumnString(columnName, inputType, size)
# HANDLE NUMERICS.
elif inputType in self.NUMERIC_DATA_TYPES:
precision = self.getInputPrecision(0)
scale = self.getInputScale(0)
self.addOutputColumnNumeric(columnName, inputType, precision, scale)
# HANDLE OTHER.
else:
self.addOutputColumn(columnName, inputType)
def _getFunctionResult(self, row):
return row
ShaperSizerAe.run()
Deployment
Deploy the script:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language python64 \
--template deploy ./shaperSizer.py --version 3Registration
Register the example as a UDF and a
UDTF:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language python64 --version 3 \
--template udf --exe shaperSizer.py --sig "sizer(varchar(ANY))" \
--return "varchar(ANY)"$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language python64 --version 3 \
--template udtf --exe shaperSizer.py --sig "shaper(VARARGS)" \
--return "table(ANY)"Running
Run the query in nzsql. It returns as output the given input:
SELECT sizer('test');
SIZER
-------
test
(1 row)
SELECT * FROM TABLE WITH FINAL(shaper('test'));
NATIONAL_VARIABLE
-------------------
test
(1 row)
SELECT * FROM TABLE WITH FINAL(shaper(32.1));
NUMERIC32
-----------
32.1
(1 row)
SELECT * FROM TABLE WITH FINAL(shaper(42));
INT32
-------
42
(1 row)