UDF examples
UDF example #1
This is a simple example UDF that adds two numbers together and returns the
result.
function evaluate(a,b)
return a + b
end
function getName()
return "adder"
end
function getType()
return "udf"
end
function getArgs()
args={}
args[1] = { "a", double }
args[2] = { "b", double }
return args
end
function getResult()
return double
end
UDF example #2
This UDF takes a string and counts the number of unique characters the string contains, returning
the
result.
function evaluate( str )
count = 0
chars = {}
for ch in string.gmatch(str, "." ) do
if chars[ch] == null then
chars[ch] = 1
count = count + 1
end
end
return count
end
function getType()
return "UDF"
end
function getName()
return "unique_chars"
end
function getArgs()
args={}
args[1] = { "str", varchar(any) }
return args
end
function getResult()
return integer
end
UDF Example #3 (calculateSize)
In some situations it can be very useful to support a dynamic output size for a VARCHAR or
NUMERIC result. This example shows using the calculateSize method to dynamically set the size of a
VARCHAR result column as the sum of the length of the two VARCHAR
arguments.
function evaluate(s1, s2 )
return s1 || s2
end
function calculateSize( args )
return args[1].length + args[2].length
end
function getType()
return "UDF"
end
function getName()
return "concat"
end
function getArgs()
return {{ "", varchar("any") },
{ "", varchar("any") }}
end
function getResult()
return varchar(any)
end