UDA examples
Example #1
Calculate an average for a double precision value. This UDA performs the same function as the SQL
AVG aggregate, but only for the DOUBLE data
type.
SUM=1
COUNT=2
function initState(state)
state[SUM] = 0
state[COUNT] = 0
return state
end
function accumulate(state, value)
state[SUM] = state[SUM] + value
state[COUNT] = state[COUNT] + 1
return state
end
function merge(state1, state2)
state1[SUM] = state1[SUM] + state2[SUM] state1[COUNT] = state1[COUNT] + state2[COUNT] return state1
end
function finalResult(state)
return state[SUM] / state[COUNT]
end
function getState()
state = {}
state[1] = { "sum", double }
state[2] = { "count", double }
return state
end
function getType()
return "uda"
end
function getName()
return "lua_avg"
end
function getArgs()
args = {}
args[1] = { "value", double }
return args
end
function getResult()
return double
end
Example #2
The second UDA example is an analytic UDA (a UDA that is invoked with an OVER clause). This UDA
can be used to create unique session identifiers for weblog data based on a session timeout
value.
--[[---------------------------------------------------------
Example usage in SQL:
select
customer,
click_time,
sessionize(click_time, 900) over (partition by customer order by click_time)
from
weblog_data
--]]---------------------------------------------------------
function initState(state)
state[1] = 0
state[2] = 0
return state
end
function accumulate(state, ts, seconds)
if ts - state[1] > seconds then
state[2] = getNextId()
end
state[1] = ts
return state
end
function finalResult(state)
return state[2]
end
function getName()
return "sessionize"
end
function getType()
return "uda"
end
function getState()
state={}
state[1] = { "", timestamp }
state[2] = { "", integer }
return state
end
function getArgs()
args={}
args[1] = { "click_time", timestamp }
args[2] = { "timeout", integer }
return args
end
function getResult()
return integer
end
function getOptions()
options={}
options[OPT_AGGTYPE] = "analytic"
return options
end