Line 5:
Line 5:
We assume that you've downloaded the <code>food.parquet</code> file locally, and that you've launched DuckDB CLI. (see [[Reusing Open Food Facts Data#Parquet file hosted on Hugging Face .28beta.29|Reusing Open Food Facts Data]]).
We assume that you've downloaded the <code>food.parquet</code> file locally, and that you've launched DuckDB CLI. (see [[Reusing Open Food Facts Data#Parquet file hosted on Hugging Face .28beta.29|Reusing Open Food Facts Data]]).
−
==== Modify the type of output ====
+
=== Modify the type of output ===
By default, DuckDB adopts its own display mode. But this mode truncates the display when the output is too long. There are many others outputs, see [https://duckdb.org/docs/api/cli/output_formats.html DuckDB documentation about them].
By default, DuckDB adopts its own display mode. But this mode truncates the display when the output is too long. There are many others outputs, see [https://duckdb.org/docs/api/cli/output_formats.html DuckDB documentation about them].
−
==== Describe the data ====
+
=== Describe the data ===
It's interesting to have the whole list of fields and their characteristics.
It's interesting to have the whole list of fields and their characteristics.
.mode box -- comment: change display mode to nice table
.mode box -- comment: change display mode to nice table
Line 18:
Line 18:
At the end of the page, we provide the full list of fields based on this query.
At the end of the page, we provide the full list of fields based on this query.
−
==== Creating a new parquet file for my country ====
+
=== Creating a new parquet file for my country ===
COPY (
COPY (
SELECT * FROM 'food.parquet'
SELECT * FROM 'food.parquet'
Line 25:
Line 25:
TO 'off-canada.parquet' (FORMAT 'parquet', COMPRESSION 'zstd');
TO 'off-canada.parquet' (FORMAT 'parquet', COMPRESSION 'zstd');
−
==== Display product name (for main language) of all products of with category <code>en:butters</code> ====
+
=== Display product name (for main language) of all products of with category <code>en:butters</code>===
SELECT
SELECT
code,
code,
Line 31:
Line 31:
list_filter (product_name, x -> x.lang == 'main')
list_filter (product_name, x -> x.lang == 'main')
) ['text']
) ['text']
−
FROM
+
FROM 'food.parquet'
−
'food.parquet'
+
WHERE list_contains (categories_tags, 'en:butters')
−
WHERE
+
LIMIT 10;
−
list_contains (categories_tags, 'en:butters')
−
LIMIT
−
10;
┌───────────────┬─────────────────────────────────────────────────────────────────────┐
┌───────────────┬─────────────────────────────────────────────────────────────────────┐
│ code │ unnest(list_filter(product_name, (x -> (x.lang = 'main'))))['text'] │
│ code │ unnest(list_filter(product_name, (x -> (x.lang = 'main'))))['text'] │
Line 55:
Line 52:
└─────────────────────────────────────────────────────────────────────────────────────┘
└─────────────────────────────────────────────────────────────────────────────────────┘
−
==== Who are the biggest contributors? ====
+
=== Who are the biggest contributors? ===
''Ex: Top 10 best contributors in OFF''
''Ex: Top 10 best contributors in OFF''
Line 81:
Line 78:
└──────────────────────────────────────┘
└──────────────────────────────────────┘
−
==== Number of added products per year ====
+
=== Number of added products per year ===
−
--entry_dates_tags is a list of texts. We take the value at position 3: the year
+
-- entry_dates_tags is a list of texts. We take the value at position 3: the year
SELECT entry_dates_tags[3] AS year, count(*) AS count
SELECT entry_dates_tags[3] AS year, count(*) AS count
FROM read_parquet('food.parquet')
FROM read_parquet('food.parquet')
Line 110:
Line 107:
└──────────────────┘
└──────────────────┘
−
==== Search for a string in a list ====
+
=== Search for a string in a list ===
Eg. search in the list <code>allergen_tags</code> values containing <code>fr:</code>. You have to convert the array into a string using the <code>array_to_string</code>.
Eg. search in the list <code>allergen_tags</code> values containing <code>fr:</code>. You have to convert the array into a string using the <code>array_to_string</code>.
SELECT count(code) as nb, ANY_VALUE(code), allergens_tags
SELECT count(code) as nb, ANY_VALUE(code), allergens_tags
−
FROM 'food.parquet'
+
FROM 'food.parquet'
−
where regexp_matches(array_to_string(allergens_tags, ','), 'fr:')
+
WHERE regexp_matches(array_to_string(allergens_tags, ','), 'fr:')
−
group by allergens_tags
+
GROUP BY allergens_tags
−
order by nb desc
+
ORDER BY nb DESC
+
LIMIT 7;
+
┌─────┬─────────────────┬──────────────────────────────────────────────┐
+
│ nb │ any_value(code) │ allergens_tags │
+
├─────┼─────────────────┼──────────────────────────────────────────────┤
+
│ 337 │ 0205004001439 │ [en:gluten, fr:avoine] │
+
│ 131 │ 3445731105896 │ [fr:non] │
+
│ 116 │ 3175681118911 │ [en:gluten, en:milk, en:soybeans, fr:avoine] │
+
│ 104 │ 3175681140776 │ [en:gluten, en:nuts, fr:avoine] │
+
│ 104 │ 20936365 │ [en:gluten, en:milk, fr:avoine] │
+
│ 56 │ 3222476727046 │ [en:gluten, fr:avoine, fr:avoine] │
+
│ 55 │ 3700389705158 │ [fr:non-renseigne] │
+
└─────┴─────────────────┴──────────────────────────────────────────────┘
+
+
=== Playing with dates ===
+
+
==== Last products created in 2023? ====
+
<pre>
+
SELECT code
+
, strftime(epoch_ms(created_t * 1000), '%Y-%m-%dT%H:%M:%SZ') AS iso_date
+
, creator
+
, 'https://world.openfoodfacts.org/product/' || code as url
+
FROM read_parquet('food.parquet')
+
WHERE true
+
AND created_t < epoch (TIMESTAMP '2024-01-01 00:00:00')
+
AND created_t > epoch (TIMESTAMP '2023-12-31 00:00:00')
+
ORDER BY iso_date desc
+
LIMIT 7;
+
┌───────────────┬──────────────────────┬──────────────────┬───────────────────────────────────────────────────────┐
+
│ code │ iso_date │ creator │ url │
+
├───────────────┼──────────────────────┼──────────────────┼───────────────────────────────────────────────────────┤
+
│ 0699058466038 │ 2023-12-31T23:59:09Z │ smoothie-app │ https://world.openfoodfacts.org/product/0699058466038 │
+
│ 2030503844885 │ 2023-12-31T23:58:38Z │ kiliweb │ https://world.openfoodfacts.org/product/2030503844885 │
+
│ 3551720206854 │ 2023-12-31T23:56:51Z │ kiliweb │ https://world.openfoodfacts.org/product/3551720206854 │
+
│ 17705288 │ 2023-12-31T23:54:45Z │ smoothie-app │ https://world.openfoodfacts.org/product/17705288 │
+
│ 72734452 │ 2023-12-31T23:52:06Z │ insectproductadd │ https://world.openfoodfacts.org/product/72734452 │
+
│ 3495040348439 │ 2023-12-31T23:51:38Z │ kiliweb │ https://world.openfoodfacts.org/product/3495040348439 │
+
│ 8801114140741 │ 2023-12-31T23:50:58Z │ foodless │ https://world.openfoodfacts.org/product/8801114140741 │
+
└───────────────┴──────────────────────┴──────────────────┴───────────────────────────────────────────────────────┘
+
</pre>
==== How many images where uploaded in 2024? ====
==== How many images where uploaded in 2024? ====
Line 141:
Line 177:
└─────────────┘
└─────────────┘
−
==== Annex: parquet schema ====
+
=== Count things ===
+
+
CASE allows to filter things when counting, eg. a given value in a list. Example:
+
+
-- count the number of products (nb),
+
-- the number of products with en:ingredients-photo-selected (nb_ing_photo_selected),
+
-- the number of products with en:ingredients-photo-to-be-selected (nb_ing_photo_to_be_selected),
+
-- the number of products with ingredients-photo-selected or ingredients-photo-to-be-selected (nb_ing_photo_tags),
+
-- and the number of products lacking either en:ingredients-photo-selected
+
-- or en:ingredients-photo-to-be-selected (ing_photo_tags_lacking)
+
SELECT
+
count(*) as nb,
+
count(CASE WHEN 'en:ingredients-photo-selected' IN (SELECT unnest(states_tags)) THEN 1 END) AS nb_ing_photo_selected,
+
count(CASE WHEN 'en:ingredients-photo-to-be-selected' IN (SELECT unnest(states_tags)) THEN 1 END) AS nb_ing_photo_to_be_selected,
+
nb_ing_photo_selected + nb_ing_photo_to_be_selected as nb_ing_photo_tags,
+
nb - nb_ing_photo_tags as ing_photo_tags_lacking
+
FROM read_parquet('food.parquet');
+
┌─────────┬───────────────────────┬─────────────────────────────┬───────────────────┬────────────────────────┐
+
│ nb │ nb_ing_photo_selected │ nb_ing_photo_to_be_selected │ nb_ing_photo_tags │ ing_photo_tags_lacking │
+
├─────────┼───────────────────────┼─────────────────────────────┼───────────────────┼────────────────────────┤
+
│ 3621956 │ 951676 │ 1949239 │ 2900915 │ 721041 │
+
└─────────┴───────────────────────┴─────────────────────────────┴───────────────────┴────────────────────────┘
+
+
UNNEST allows to play with nested fields such as `images`, `nutriments`, `packagings`, etc.
+
+
-- Top 10 languages for the ingredients' images
+
SELECT n.unnest.key, count(code) as nb
+
FROM (SELECT code, images FROM read_parquet('food.parquet')) as f,
+
UNNEST(f.images) as n
+
WHERE n.unnest.key like 'ingredients_%'
+
GROUP BY n.unnest.key
+
ORDER BY nb DESC
+
LIMIT 10;
+
┌────────────────┬────────┐
+
│ key │ nb │
+
├────────────────┼────────┤
+
│ ingredients_fr │ 574588 │
+
│ ingredients_en │ 209511 │
+
│ ingredients_de │ 106001 │
+
│ ingredients_es │ 77712 │
+
│ ingredients_it │ 28467 │
+
│ ingredients_nl │ 18756 │
+
│ ingredients_pt │ 10820 │
+
│ ingredients_pl │ 9465 │
+
│ ingredients_ro │ 7115 │
+
│ ingredients_sv │ 6744 │
+
└────────────────┴────────┘
+
+
+
=== Annex: parquet schema ===
{| class="wikitable"
{| class="wikitable"
! column_name !! column_type
! column_name !! column_type