Duplicate results in an AWS Athena (Presto) DISTINCT SQL Query?

T. Brian Jones picture T. Brian Jones · Feb 1, 2017 · Viewed 7.2k times · Source

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.

Athena Table Creation Query:

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/';

Here are all the "dedup" queries I've tried (These should all be the same):

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;

All results output .csvs from Athena still have repeated MD5s. What gives?


Is Athena Doing Partial Deduplication? - Even more peculiar, if I perform a COUNT(DISTINCT md5) in Athena, the count I get is different than the number of rows returned on export.

  • COUNT(DISTINCT md5) in Athena: 97,533,226
  • records in export of distinct MD5s: 97,581,616
  • there 14,790 duplicates in the results export, so both the COUNT(DISTINCT) counts are bad, and the results export are bad.

Is Athena CREATING Duplicates on Export? - 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 LIKE 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.

select
  md5,
  to_utf8(md5)
from md5s
where md5 like '%0061c3d72c2957f454eef9d4b05775d7%';

Are Athena's Counts & Results File Both Wrong? - I deduped these same records using MySQL, and ended up with 97,531,010 unique MD5s. Athenas counts and results details are below.

  • COUNT(DISTINCT md5) in Athena: 97,533,226
  • records in export of distinct MD5s: 97,581,616
  • there 14,790 duplicates in the results export, so it seems that both the COUNT(DISTINCT) counts are bad, and the results export are bad.

I think this is an Athena bug - I've filed a ticket with AWS's dev team to get this fixed, and will update this post when it is.

Here is the related AWS Forum Post where other users are seeing the same issues. https://forums.aws.amazon.com/thread.jspa?messageID=764702

Answer

T. Brian Jones picture T. Brian Jones · Aug 9, 2017

I have confirmed with the AWS team, that this was a known bug with AWS Athena at the time the question was asked. I'm not sure if this has been resolved.