title: Hive How to limit the number of entries in collect_set
body: <p>Let's say I have a table that contains two columns:</p>
  <pre><code>Category Productname
  Cat1     prod1
  Cat1     prod2
  Cat1     prod3
  Cat1     prod4
  Cat1     prod5
  Cat2     prod6
  Cat2     prod7
  Cat2     prod8
  </code></pre>
  <p>Now if I do something like:</p>
  <pre><code>SELECT Category, collect_set(Productname)
  FROM myTable;
  </code></pre>
  <p>I would get something like:</p>
  <pre><code>Cat1    [prod1...prod5]
  Cat2    [prod6...prod8]
  </code></pre>
  <p>Now, there are 5 products in Cat1 and 3 in Cat2. However, I want to limit the number of products in each category. Let's say the upper limit is 3. The 3 products can be any random ones as long as they belong to the same category. Also, the upper limit can be a large number. It's worth noting that I want the collect_set to stop once it has reached the upper threshold. Because generating the output and then perform the filtering can be expensive. Methods other than collect_set are also welcome. Thanks for any suggestions!</p>