title: Check if a polygon intersects other polygons within each cluster
body: <p>I have a table with these two columns - cluster_num and Polygons. Cluster_num is an integer type nullable type field and Polygons is a Geography type repeated field like so :</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">cluster_num</th>
  <th style="text-align: center;">Polygons</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">1</td>
  <td style="text-align: center;">polygon1.1</td>
  </tr>
  <tr>
  <td style="text-align: center;"></td>
  <td style="text-align: center;">polygon1.2</td>
  </tr>
  <tr>
  <td style="text-align: center;">2</td>
  <td style="text-align: center;">polygon2.1</td>
  </tr>
  <tr>
  <td style="text-align: center;"></td>
  <td style="text-align: center;">polygon2.2</td>
  </tr>
  <tr>
  <td style="text-align: center;"></td>
  <td style="text-align: center;">polygon2.3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>NOTE: For the sake of brevity, I have abbreviated each polygon here.</p>
  <p>I need to go inside each &quot;cluster_num&quot; individually and check if there are any intersecting pairs of polygons. Cluster_num had been generated by running a DBSCAN clustering over the entire dataset of Polygons.</p>
  <p>The issue is that I need to check for the intersection between each distinct pair of polygons for each distinct cluster_num. As an example, in a hypothetical cluster_num = 4 row having 5 polygons, there can be 10 distinct pairs of polygons and I need to check for intersection in each pair.</p>
  <p>The first step to solve this would be generating these distinct pairs for EACH individual cluster_num and I am stumped at this. The second step would be to just use st_intersects for each pair of the generated pairs and check for the intersection. However, I am not able to figure out an SQL query (or a succession of queries) to solve the first step.</p>
  <p><strong>Edit</strong>: Desired output after the first step:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">cluster_num</th>
  <th style="text-align: center;">Polygons</th>
  <th style="text-align: center;">Polygons_</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">1</td>
  <td style="text-align: center;">polygon1.1</td>
  <td style="text-align: center;">polygon1.2</td>
  </tr>
  <tr>
  <td style="text-align: center;">2</td>
  <td style="text-align: center;">polygon2.1</td>
  <td style="text-align: center;">polygon2.2</td>
  </tr>
  <tr>
  <td style="text-align: center;"></td>
  <td style="text-align: center;">polygon2.1</td>
  <td style="text-align: center;">polygon2.3</td>
  </tr>
  <tr>
  <td style="text-align: center;"></td>
  <td style="text-align: center;">polygon2.2</td>
  <td style="text-align: center;">polygon2.3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Note that we have all <strong>DISTINCT</strong> combinations of polygons for <strong>EACH</strong> individual cluster_num.</p>
  <p>I have looked into <code>cross join</code> to generate a cartesian product, however, this will give me repeated pairs and also I am not sure how to do a <code>cross join</code> for each cluster_num individually.</p>