calculateShape()

The calculateShape() method allows a UDTF to dynamically determine its output shape (the column names and data types returned by the UDTF). This makes it possible to build versatile functions that can alter their behavior dynaically based on the input values.

A single argument containing a Lua table is passed to the calculateShape() method. The contents of the Lua table are shown here.

args.count         The number of arguments
args[i].name       Name of the argument (not currently implemented)
args[i].type       nzLua datatype for the argument


args[i].isconst    Boolean indicating if the argument is a constant
args[i].value      Value of the argument if it is a constant

The value of every constant argument is passed into the calculateShape() method. For non-constant arguments, only the data type and size will be present. The calculateShape() method can then use the values of the constant arguments to determine the output columns and data types that the UDTF will return.

Example

function calculateShape(args)
    if args[1].value < 1 or args[1].value > 1024 then
        error("Invalid number of output columns!", 0)
    end
    local shape={}
    for i=1,args[1].value do
        shape[i] = { "c" || i, varchar(100) }
    end
    return shape
end