Hub 文件
執行 SQL 操作
加入 Hugging Face 社群
並獲得增強的文件體驗
開始使用
執行 SQL 操作
使用 DuckDB 執行 SQL 操作為高效查詢資料集提供了無限可能。讓我們透過一些示例來展示 DuckDB 函式的強大功能。
為了演示,我們將探索一個引人入勝的資料集。MMLU 資料集是一個多工測試,包含涵蓋各種知識領域的選擇題。
為了預覽資料集,讓我們選擇 3 行樣本
FROM 'hf://datasets/cais/mmlu/all/test-*.parquet' USING SAMPLE 3;
┌──────────────────────┬──────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────┐
│ question │ subject │ choices │ answer │
│ varchar │ varchar │ varchar[] │ int64 │
├──────────────────────┼──────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│ The model of light… │ conceptual_physics │ [wave model, particle model, Both of these, Neither of these] │ 1 │
│ A person who is lo… │ professional_psych… │ [his/her life scripts., his/her own feelings, attitudes, and beliefs., the emotional reactions and behaviors of the people he/she is interacting with.… │ 1 │
│ The thermic effect… │ nutrition │ [is substantially higher for carbohydrate than for protein, is accompanied by a slight decrease in body core temperature., is partly related to sympat… │ 2 │
└──────────────────────┴──────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────┘
此命令從資料集中隨機檢索 3 行樣本供我們檢視。
首先,讓我們檢查資料集的 schema。下表概述了我們資料集的結構
DESCRIBE FROM 'hf://datasets/cais/mmlu/all/test-*.parquet' USING SAMPLE 3;
┌─────────────┬─────────────┬─────────┬─────────┬─────────┬─────────┐
│ column_name │ column_type │ null │ key │ default │ extra │
│ varchar │ varchar │ varchar │ varchar │ varchar │ varchar │
├─────────────┼─────────────┼─────────┼─────────┼─────────┼─────────┤
│ question │ VARCHAR │ YES │ │ │ │
│ subject │ VARCHAR │ YES │ │ │ │
│ choices │ VARCHAR[] │ YES │ │ │ │
│ answer │ BIGINT │ YES │ │ │ │
└─────────────┴─────────────┴─────────┴─────────┴─────────┴─────────┘
接下來,讓我們分析資料集中是否存在重複記錄
SELECT *,
COUNT(*) AS counts
FROM 'hf://datasets/cais/mmlu/all/test-*.parquet'
GROUP BY ALL
HAVING counts > 2;
┌──────────┬─────────┬───────────┬────────┬────────┐
│ question │ subject │ choices │ answer │ counts │
│ varchar │ varchar │ varchar[] │ int64 │ int64 │
├──────────┴─────────┴───────────┴────────┴────────┤
│ 0 rows │
└──────────────────────────────────────────────────┘
幸運的是,我們的資料集不包含任何重複記錄。
讓我們用條形圖表示基於主題的問題比例
SELECT
subject,
COUNT(*) AS counts,
BAR(COUNT(*), 0, (SELECT COUNT(*) FROM 'hf://datasets/cais/mmlu/all/test-*.parquet')) AS percentage
FROM
'hf://datasets/cais/mmlu/all/test-*.parquet'
GROUP BY
subject
ORDER BY
counts DESC;
┌──────────────────────────────┬────────┬────────────────────────────────────────────────────────────────────────────────┐
│ subject │ counts │ percentage │
│ varchar │ int64 │ varchar │
├──────────────────────────────┼────────┼────────────────────────────────────────────────────────────────────────────────┤
│ professional_law │ 1534 │ ████████▋ │
│ moral_scenarios │ 895 │ █████ │
│ miscellaneous │ 783 │ ████▍ │
│ professional_psychology │ 612 │ ███▍ │
│ high_school_psychology │ 545 │ ███ │
│ high_school_macroeconomics │ 390 │ ██▏ │
│ elementary_mathematics │ 378 │ ██▏ │
│ moral_disputes │ 346 │ █▉ │
├──────────────────────────────┴────────┴────────────────────────────────────────────────────────────────────────────────┤
│ 57 rows (8 shown) 3 columns │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
現在,讓我們準備一個包含與**營養**相關問題的子資料集,並建立問題到正確答案的對映。請注意,我們有一個**choices**列,可以使用**answer**列作為索引獲取正確答案。
SELECT *
FROM 'hf://datasets/cais/mmlu/all/test-*.parquet'
WHERE subject = 'nutrition' LIMIT 3;
┌──────────────────────┬───────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────┐
│ question │ subject │ choices │ answer │
│ varchar │ varchar │ varchar[] │ int64 │
├──────────────────────┼───────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────┤
│ Which foods tend t… │ nutrition │ [Meat, Confectionary, Fruits and vegetables, Potatoes] │ 2 │
│ In which one of th… │ nutrition │ [If the incidence rate of the disease falls., If survival time with the disease increases., If recovery of the disease is faster., If the population in which the… │ 1 │
│ Which of the follo… │ nutrition │ [The flavonoid class comprises flavonoids and isoflavonoids., The digestibility and bioavailability of isoflavones in soya food products are not changed by proce… │ 0 │
└──────────────────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────┘
SELECT question,
choices[answer] AS correct_answer
FROM 'hf://datasets/cais/mmlu/all/test-*.parquet'
WHERE subject = 'nutrition' LIMIT 3;
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────┐
│ question │ correct_answer │
│ varchar │ varchar │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┤
│ Which foods tend to be consumed in lower quantities in Wales and Scotland (as of 2020)?\n │ Confectionary │
│ In which one of the following circumstances will the prevalence of a disease in the population increase, all else being constant?\n │ If the incidence rate of the disease falls. │
│ Which of the following statements is correct?\n │ │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────┘
為確保資料整潔,讓我們刪除問題末尾的任何換行符,並過濾掉任何空答案
SELECT regexp_replace(question, '\n', '') AS question,
choices[answer] AS correct_answer
FROM 'hf://datasets/cais/mmlu/all/test-*.parquet'
WHERE subject = 'nutrition' AND LENGTH(correct_answer) > 0 LIMIT 3;
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────┐
│ question │ correct_answer │
│ varchar │ varchar │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┤
│ Which foods tend to be consumed in lower quantities in Wales and Scotland (as of 2020)? │ Confectionary │
│ In which one of the following circumstances will the prevalence of a disease in the population increase, all else being constant? │ If the incidence rate of the disease falls. │
│ Which vitamin is a major lipid-soluble antioxidant in cell membranes? │ Vitamin D │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────┘
最後,讓我們重點介紹本節中使用的一些 DuckDB 函式
DESCRIBE
,返回表 schema。USING SAMPLE
,樣本用於隨機選擇資料集的子集。BAR
,繪製一個帶,其寬度與 (x - min) 成比例,當 x = max 時寬度等於字元寬度。寬度預設為 80。string[begin:end]
,使用切片約定提取字串。缺失的 begin 或 end 引數分別被解釋為列表的開頭或結尾。負值也被接受。regexp_replace
,如果字串包含 regexp 模式,則用 replacement 替換匹配部分。LENGTH
,獲取字串中的字元數。
DuckDB 的SQL 函式概述中提供了大量有用的函式。最棒的是,您可以直接在 Hugging Face 資料集上使用它們。