Fortran language shapers and sizers

This example uses the following file name: shaperSizer.f

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. This example uses handlers for both the function logic and the shaper logic.
program shaperSizerProgram
call nzaeRun()
stop
end
subroutine nzaeHandleRequest(handle)
integer isShaper
isShaper = -1
c DETERMINE IF WE ARE RUNNING IN SHAPER MODE.
c CALL THE APPROPRIATE SUBROUTINE.
call nzaeIsShaper(handle, isShaper)
if (isShaper .eq. 1) then
call runShaper(handle)
else if (isShaper .eq. 0) then
call run(handle)
else
call nzaeUserError(handle, "nzaeIsShaper() failed.")
endif
return
end
subroutine runShaper(handle)
integer isUpper, isUdf, test, inputType, inputSize, inputScale
character(1) name
isUpper = 1
isUdf = -1
test = -1
inputType = -1
inputSize = -1
inputScale = -1
c DETERMINE OUTPUT COLUMN NAME.
call nzaeIsShaper(handle, isUdf)
if (isUdf .eq. 1) then
call nzaeIsSystemCatalogUpperCase(handle, isUpper)
endif
if (isUpper .eq. 1) then
name = 'I'
else
name = 'i'
endif
c COPY STRING TYPES.
call nzaeGetInputType(handle, 0, inputType)
call nzaeIsAStringType(inputType, test)
if (test .eq. 1) then
call nzaeGetInputSize(handle, 0, inputSize)
call nzaeAddOutputColumnString(handle,
+ inputType,
+ name,
+ inputSize)
return
endif
c COPY NUMERIC TYPES.
call nzaeIsANumericType(inputType, test)
if (test .eq. 1) then
call nzaeGetInputSize(handle, 0, inputSize)
call nzaeGetInputScale(handle, 0, inputPrecision)
call nzaeAddOutputColumnNumeric(handle,
+ inputType,
+ name,
+ inputSize
+ inputScale)
return
endif
c COPY ALL OTHER TYPES.
call nzaeAddOutputColumn(handle, inputType, name)
return
end
subroutine run(handle)
integer hasNext, leftInput, rightInput, result
character operator
hasNext = -1
leftInput = 0
rightInput = 0
operator = 'f'
10 call nzaeGetNext(handle, hasNext)
if (hasNext .eq. 0) then
goto 20
endif
c COPY OVER THE INPUT COLUMN.
call nzaeOutputInputColumn(handle, 0, 0)
call nzaeOutputResult(handle)
c LOOP GETTING THE NEXT ROW.
goto 10
20 return
end

Compilation

Use the standard compile:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language fortran --version 3 \
--template compile shaperSizer.f

Registration

Register the example as a UDF and a UDTF:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language fortran --version 3 \
--template udtf --exe shaperSizer --sig "shaper_sizer_table(VARARGS)" \
--return "table(ANY)"
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language fortran --version 3 \
--template udf --exe shaperSizer --sig "shaper_sizer(VARCHAR(ANY))" \
--return "VARCHAR(ANY)"

Running

Run the query in nzsql. It returns as output the given input:
SELECT shaper_sizer('test');
SHAPER_SIZER
--------------
test
(1 row)
SELECT * FROM TABLE WITH FINAL(shaper_sizer_table('test'));
I
------
test
(1 row)
SELECT * FROM TABLE WITH FINAL(shaper_sizer_table(1.1));
I
-----
1.1
(1 row)