UDAF API methods
The methods described in this section can be used only by UDAFs.
When an SQL statement uses a UDAF, the database engine invokes the appropriate UDAF API methods
as needed. The steps in this process are:
- For each row of data being processed, the database engine checks to see if a row for the group
to which that row belongs has already been found:
- If so, pass the row to the accumulate() method along with the current value of the state variables for that group.
- If not, first call the initState() method, then call the accumulate() method with the newly initialized state values plus the row of data.
- After all rows have been processed, the database engine redistributes the final values of the state variables for each group of data so that all of the state variables for each group of data are moved to the same processing unit. For example, if a GROUP BY clause specifies the customer_id column, move all of the states that correspond to a particular customer ID to the same processing unit.
- The database engine merges all of the state variables together by calling the merge() method. On each call to merge, two sets of state variables are passed in. The merge() method merges the two states together and returns the result. The merge() method is called until there is only one state for each group specified by the GROUP BY clause.
- For each of the remaining states, the database engine calls the finalResult() method, passing in the final values for each state. The finalResult() method calculates a result based on the state variables and returns the result to the database for each group.