UDAF: What is state type used for?

Hello community,

I am reading the documentation on the UDAF topic and struggling to understand the state type used. It is being referenced throughout the documentation and is always referenced by the following string The STATE type of a UDAF may be a scalar type, ARRAY type, or a RECORD type.

Can someone provide more information?

Thank you all!

Have a great day!

The STATE is just the summary of the information needed to compute the aggregate, for some set of rows. For example, for an average aggregate, you might have this as your state:

record(count_of_rows bigint, sum_of_values bigint)

At the end, when you are asked to get the aggregate result, you will return it from the STATE. Also, you can merge two STATE values together by summing the count_of_rows and sum_of_values together from the two different states.

The example here has a state record(s bigint, c bigint) similar to what I described.