R language shapers and sizers

Using a Shaper

You can access the shaper functionality in the following ways:
  • When a new UDTF is registered in one of the following ways:
    • Setting the nz.shaper variable to a function by using the API described above
    • Setting the nz.shaper variable to the std character value, and setting the nz.shaper.list variable to a list of which the elements follow the pattern column-name=NZ.data-type
  • When the CODE_PLAIN channel is used, by setting the additional SHAPER_LIST Dynamic Environment variable so that it follows the same pattern of column-name=NZ.data-type. Subsequent occurrences of this pattern are separated by commas.
  • By setting the output.signature parameter in some of the nzLibrary for R package functions

Code

This example demonstrates a shaper and a sizer that use the same code and do essentially the same task, returning the given input as output. This example does not demonstrate the retrieval of literal fields.

Put the following code in the /tmp/shapersizer.R file:
nz.shaper <- function() {
# getInputColumnInfo returns a vector (type,isConstant,size,scale)
# the last two elements only if applicable
inputType <- getInputColumnInfo(0)[1]
columnName <- names(which(getNpsDataTypes() == inputType))
# HANDLE STRINGS.
if (inputType %in% c(NZ.FIXED, NZ.VARIABLE, NZ.NATIONAL_FIXED,
NZ.NATIONAL_VARIABLE)) {
size <- getInputColumnInfo(0)[3]
addOutputColumnString(inputType, columnName, size)
}
# HANDLE NUMERICS.
else if (inputType %in% c(NZ.NUMERIC32, NZ.NUMERIC64, NZ.NUMERIC128)) {
stop('numeric data types are not supported in R')
# precision <- getInputColumnInfo(0)[3]
# scale <- getInputColumnInfo(0)[4]
# addOutputColumnNumeric(inputType, columnName, precision, scale)
}
# HANDLE OTHER.
else
addOutputColumn(inputType, columnName)
updateInfo()
}
nz.fun <- function() {
while(getNext()) {
setOutput(0, getInputColumn(0))
outputResult()
}
}

Compilation

Compile the code as follows:
/nz/export/ae/utilities/bin/compile_ae --language r --version 3 \
--template compile --user nz --db dev /tmp/shapersizer.R

Registration

Register the example as a UDF and a UDTF:
/nz/export/ae/utilities/bin/register_ae --language r --version 3 \
--template udf --exe shapersizer.R --sig "sizer(VARCHAR(ANY))" \
--return "VARCHAR(ANY)" --user nz --db dev
/nz/export/ae/utilities/bin/register_ae --language r --version 3 \
--template udtf --exe shapersizer.R --sig "shaper(VARARGS)" \
--return "TABLE(ANY)" --user nz --db dev

Running

When you run the AE , you get the following results:
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::double));
DOUBLE
--------
32.1
(1 row)
SELECT * FROM TABLE WITH FINAL(shaper(42));
INT32
-------
42
(1 row)

Another example

This example must be compiled and registered as outputting TABLE(ANY).

Code

Put the following code in the /tmp/shapersizer2.R file:
nz.fun <- function () {
while(getNext()) {
setOutput(0, sqrt(as.double(getInputColumn(0))))
outputResult()
}
}
nz.shaper <- function() {
addOutputColumn(NZ.DOUBLE,'value')
updateInfo()
}

Compilation

Compile the code as follows:
/nz/export/ae/utilities/bin/compile_ae --language r --version 3 \
--template compile /tmp/shapersizer2.R

Registration

Register the example as a UDTF:
/nz/export/ae/utilities/bin/register_ae --language r --version 3 \
--template udtf --exe shapersizer2.R --sig "shaper2(VARARGS)" \
--return "TABLE(ANY)"
Running the following query returns the same results as the previous example:
SELECT * FROM nza..iris, TABLE WITH FINAL(nzr..r_udtf_any(sepallength,
'CODE_PLAIN="while(getNext()){
setOutput(0, sqrt(as.double(getInputColumn(0)))); outputResult()}",
SHAPER_LIST="value=NZ.DOUBLE",NZAE_ROW_BUFFER=no'))

To keep the connection between the input and output rows, buffering must be turned off. For more information, see Row buffering.

Running

Run the query in nzsql. It returns as output the given input:
SELECT sizerPl('test');
SIZER
-------
test
(1 row)
SELECT * FROM TABLE WITH FINAL(shaperPl('test'));
NATIONAL_VARIABLE
-------------------
test
(1 row)
SELECT * FROM TABLE WITH FINAL(shaperPl(32.1));
NUMERIC32
-----------
32.1
(1 row)
SELECT * FROM TABLE WITH FINAL(shaperPl(42));
INT32
-------
42
(1 row)