title: Duplicate results in an AWS Athena (Presto) DISTINCT SQL Query?
body: <p>I have a bunch of files on S3 that contain just MD5s, one per line. I created an AWS Athena table to run a de-duplication query against the MD5s. In total there are hundreds of millions of MD5s in those files and in the table.</p>
  
  <p>Athena Table Creation Query:</p>
  
  <pre><code>CREATE EXTERNAL TABLE IF NOT EXISTS database.md5s (
    `md5` string 
  )
  ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
  WITH SERDEPROPERTIES (
    'serialization.format' = ',',
    'field.delim' = ','
  ) LOCATION 's3://bucket/folder/';
  </code></pre>
  
  <p>Here are all the "dedup" queries I've tried (These should all be the same):</p>
  
  <pre><code>SELECT DISTINCT md5
  FROM md5s;`
  
  SELECT md5
  FROM md5s
  GROUP BY md5;
  
  SELECT md5
  FROM md5s
  GROUP BY DISTINCT md5;
  
  SELECT DISTINCT md5
  FROM md5s
  GROUP BY DISTINCT md5;
  </code></pre>
  
  <p>All results output .csvs from Athena still have repeated MD5s. What gives?</p>
  
  <hr>
  
  <p><strong>Is Athena Doing Partial Deduplication?</strong> - Even more peculiar, if I perform a <code>COUNT(DISTINCT md5)</code> in Athena, the count I get is different than the number of rows returned on export.</p>
  
  <ul>
  <li><code>COUNT(DISTINCT md5)</code> in Athena: 97,533,226 </li>
  <li>records in export of distinct MD5s: 97,581,616</li>
  <li>there 14,790 duplicates in the results export, so <strong>both the COUNT(DISTINCT) counts are bad, and the results export are bad.</strong></li>
  </ul>
  
  <hr>
  
  <p><strong>Is Athena CREATING Duplicates on Export?</strong> - The plot thickens. If I query my Athena Table for one of the MD5s that is duplicated in the Athena result export, I only get one result/row from the table. I tested this with a <code>LIKE</code> query to make sure whitespace wasn't causing the issue. This means Athena is ADDING duplicates to the export. There are never more than two of the same MD5 in the results.</p>
  
  <pre><code>select
    md5,
    to_utf8(md5)
  from md5s
  where md5 like '%0061c3d72c2957f454eef9d4b05775d7%';
  </code></pre>
  
  <hr>
  
  <p><strong>Are Athena's Counts &amp; Results File Both Wrong?</strong> - I deduped these same records using MySQL, and ended up with 97,531,010 unique MD5s. Athenas counts and results details are below.</p>
  
  <ul>
  <li><code>COUNT(DISTINCT md5)</code> in Athena: 97,533,226 </li>
  <li>records in export of distinct MD5s: 97,581,616</li>
  <li>there 14,790 duplicates in the results export, so it seems that <strong>both the COUNT(DISTINCT) counts are bad, and the results export are bad.</strong></li>
  </ul>
  
  <hr>
  
  <p><strong>I think this is an Athena bug</strong> - I've filed a ticket with AWS's dev team to get this fixed, and will update this post when it is.</p>
  
  <p>Here is the related AWS Forum Post where other users are seeing the same issues.
  <a href="https://forums.aws.amazon.com/thread.jspa?messageID=764702" rel="noreferrer">https://forums.aws.amazon.com/thread.jspa?messageID=764702</a></p>