- Categories:
Aggregate functions (Frequency Estimation) , Window function syntax and usage
APPROX_TOP_K_COMBINE¶
Combines (merges) input states into a single output state.
This allows scenarios where APPROX_TOP_K_ACCUMULATE is run over horizontal partitions of the same table, producing an algorithm state for each table partition. These states can later be combined using APPROX_TOP_K_COMBINE, producing the same output state as a single run of APPROX_TOP_K_ACCUMULATE over the entire table.
- See also:
Syntax¶
Arguments¶
stateAn expression that contains state information generated by a call to APPROX_TOP_K_ACCUMULATE.
countersThis is the maximum number of distinct values that can be tracked at a time during the estimation process. For example, if
countersis set to 100000, then the algorithm tracks 100,000 distinct values, attempting to keep the 100,000 most frequent values.The maximum number of
countersis100000(100,000).
Returns¶
This returns information about the “state” of the top K calculation.
This state information is not usually useful by itself, but can be passed to
the function APPROX_TOP_K_ESTIMATE.
Usage notes¶
If
countersis defined, the output state uses the specified number of counters.If
countersis not defined, all input states must have the same number of counters.
Decimal-float (DECFLOAT) values aren’t supported.
Examples¶
This example shows how to use the three related functions
APPROX_TOP_K_ACCUMULATE, APPROX_TOP_K_ESTIMATE, and
APPROX_TOP_K_COMBINE.
Note
This example uses more counters than distinct data values in order to get consistent results. In real-world applications, the number of distinct values is usually larger than the number of counters, so the approximations can vary.
This example generates one table with 8 rows that have values 1 - 8, and a second table with 8 rows that have values 5 - 12. Thus the most frequent values in the union of the two tables are the values 5-8, each of which has a count of 2.
Create a simple table and data:
Create a table that contains the “state” that represents the current approximate Top K information for the table named sequence_demo:
Now create a second table and add data. (In a more realistic situation, the user could have loaded more data into the first table and divided the data into non-overlapping sets based on the time that the data was loaded.)
Get the “state” information for just the new data.
Combine the “state” information for the two batches of rows:
Get the approximate Top K value of the combined set of rows:
Output: