Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 860
T. Brian Jones Avatar asked Feb 01 '17 01:02

T. Brian Jones


People also ask

How can I delete duplicate records in SQL using distinct?

SELECT ALL columns FROM table; The syntax diagram is: SELECT [ALL | DISTINCT] columns FROM table; If a table has a properly defined primary key, SELECT DISTINCT * FROM table; and SELECT * FROM table; return identical results because all rows are unique.

How do I find duplicate rows in SQL?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

What is the difference between Presto and Athena?

Athena is a great choice for getting started with analytics if you have nothing set up yet. Redshift Spectrum is great for Redshift customers. Presto is for everything else, including large data sets, more regular analytics, and higher user concurrency.


2 Answers

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.

like image 69
T. Brian Jones Avatar answered Oct 23 '22 23:10

T. Brian Jones


When in doubt please use CTAS to remove any duplicates :

CREATE TABLE new_table
WITH (
      format = 'Parquet',
      parquet_compression = 'SNAPPY')
AS SELECT DISTINCT *
FROM old_table;

Reference: https://docs.aws.amazon.com/athena/latest/ug/ctas-examples.html

like image 2
Avish Saha Avatar answered Oct 23 '22 22:10

Avish Saha