Table grain quick validation with SQL

I was doing some exploratory data analysis on a number of tables I didn’t have much information about and, unfortunately, didn’t know their grain.
The grain is what one row of a table represents. I wrote about why it matters so much, and how I approach an undocumented table, in For quality work, understand your data grain first.
I needed a quick way to validate my assumptions about the table grain, identify contradicting observations (rows), and check for duplicates at the same time.
Therefore I decided to use a combination of TO_JSON_STRING and FARM_FINGERPRINT. The first creates a JSON representation of the entire row (given a table alias), while the second converts the resulting string into a INT64 hash.
By comparing the total number of rows in a group against the distinct count of these fingerprints, we can determine whether the proposed grain is correct and whether there are duplicates in the data.
This was a quick exercise but use this with care. Depending on your SQL implementation, data volumes and context, results may vary.

SELECT
order_id,
product_name,
COUNT(FARM_FINGERPRINT(TO_JSON_STRING(i))) AS count_duplicates,
COUNT(DISTINCT FARM_FINGERPRINT(TO_JSON_STRING(i))) AS count_grain
FROM input_data i
GROUP BY ALL
HAVING count_duplicates > 1 OR count_grain > 1
SELECT
order_id,
product_name,
order_status,
COUNT(FARM_FINGERPRINT(TO_JSON_STRING(i))) AS count_duplicates,
COUNT(DISTINCT FARM_FINGERPRINT(TO_JSON_STRING(i))) AS count_grain
FROM input_data i
GROUP BY ALL
HAVING count_duplicates > 1 OR count_grain > 1

Enjoyed this? Here are some related articles you might find useful: