雜訊聚合函數

概觀

雜訊聚合函數是提供常見聚合(例如 sum()count()approx_distinct())以及草圖(例如 approx_set())的隨機、雜訊近似值的函數。透過將隨機雜訊注入結果,雜訊聚合函數會讓確定或確認所聚合的確切資料變得更加困難。

雖然這些函數中的許多函數都類似差分隱私機制,但這些函數傳回的值以及包含這些函數的查詢結果一般而言並非差分隱私。如需更多詳細資訊,請參閱下方的限制。希望支援強大隱私保證的使用者應先與適當的技術專家討論。

計數、總和與平均值

noisy_count_gaussian(col, noise_scale[, random_seed]) -> bigint()

計算 col 中非 NULL 的值,然後將平均值為 0 且標準差為 noise_scale 的常態分佈隨機雙精度值新增至真實計數。雜訊計數會經過後處理,使其為非負數,並四捨五入為 bigint。

如果提供 random_seed,則會用於植入隨機數字產生器。否則,雜訊會從安全隨機值產生。

SELECT noisy_count_gaussian(orderkey, 20.0) FROM tpch.tiny.lineitem; -- 60179 (1 row)
SELECT noisy_count_gaussian(orderkey, 20.0) FROM tpch.tiny.lineitem WHERE false; -- NULL (1 row)

注意

count() 不同的是,當 col 的 (真實) 計數為 0 時,此函數會傳回 NULL

可以使用 noisy_count_gaussian(DISTINCT col, ...) 或使用 noisy_approx_distinct_sfm() 來執行相異計數。一般來說,noisy_count_gaussian() 會傳回更準確的結果,但計算成本更高。

noisy_count_if_gaussian(col, noise_scale[, random_seed]) -> bigint()

計算 colTRUE 的值,然後將平均值為 0 且標準差為 noise_scale 的常態分佈隨機雙精度值新增至真實計數。雜訊計數會經過後處理,使其為非負數,並四捨五入為 bigint。

如果提供 random_seed,則會用於植入隨機數字產生器。否則,雜訊會從安全隨機值產生。

SELECT noisy_count_if_gaussian(orderkey > 10000, 20.0) FROM tpch.tiny.lineitem; -- 50180 (1 row)
SELECT noisy_count_if_gaussian(orderkey > 10000, 20.0) FROM tpch.tiny.lineitem WHERE false; -- NULL (1 row)

注意

count_if() 不同的是,當 (真實) 計數為 0 時,此函數會傳回 NULL

noisy_sum_gaussian(col, noise_scale, lower, upper[, random_seed]) -> double()

計算 col 中輸入值的總和,然後新增一個平均值為 0 且標準差為 noise_scale 的常態分佈隨機雙精度值。每個值在新增至總和之前都會被限制在 [lower, upper] 的範圍內。

如果提供 random_seed,則會用於植入隨機數字產生器。否則,雜訊會從安全隨機值產生。

noisy_sum_gaussian(col, noise_scale[, random_seed]) -> double()

計算 col 中輸入值的總和,然後新增一個平均值為 0 且標準差為 noise_scale 的常態分佈隨機雙精度值。

如果提供 random_seed,則會用於植入隨機數字產生器。否則,雜訊會從安全隨機值產生。

noisy_avg_gaussian(col, noise_scale, lower, upper[, random_seed]) -> double()

計算 col 中所有輸入值的平均值(算術平均數),然後加上一個平均值為 0、標準差為 noise_scale 的常態分佈隨機雙精度浮點數值。每個值在取平均值之前都會被截斷到 [lower, upper] 的範圍內。

如果提供 random_seed,則會用於植入隨機數字產生器。否則,雜訊會從安全隨機值產生。

noisy_avg_gaussian(col, noise_scale[, random_seed]) -> double()

計算 col 中所有輸入值的平均值(算術平均數),然後加上一個平均值為 0、標準差為 noise_scale 的常態分佈隨機雙精度浮點數值。

如果提供 random_seed,則會用於植入隨機數字產生器。否則,雜訊會從安全隨機值產生。

近似不重複計數/草圖繪製

透過 Sketch-Flip-Merge (SFM) 資料草圖 [Hehir2023],支援雜訊近似不重複計數和草圖繪製(類似於確定性的 HyperLogLog 函數)。

noisy_approx_set_sfm(col, epsilon[, buckets[, precision]]) -> SfmSketch()

傳回 col 中輸入值的 SFM 草圖。這類似於 approx_set() 函數,該函數會傳回(確定性的)HyperLogLog 草圖。

  • col 支援多種類型,類似於 HyperLogLog

  • epsilon (double) 是一個正數,用於控制草圖中的雜訊程度,如 [Hehir2023] 中所述。較小的 epsilon 值對應於較多雜訊的草圖。

  • buckets (int) 預設為 4096。

  • precision (int) 預設為 24。

注意

approx_set() 不同,當 col 為空時,此函數會傳回 NULL。如果這種行為不符合需求,請搭配 noisy_empty_approx_set_sfm() 使用 coalesce()

noisy_approx_set_sfm_from_index_and_zeros(col_index, col_zeros, epsilon, buckets[, precision]) -> SfmSketch()

傳回 col_indexcol_zeros 中輸入值的 SFM 草圖。

這類似於 noisy_approx_set_sfm(),但該函數會計算 colMurmur3Hash128.hash64(),並如 [FlajoletMartin1985] 中所述計算 SFM PCSA 儲存桶索引和尾隨零的數量。在此函數中,呼叫者必須明確計算雜湊儲存桶索引和零本身,並將它們作為引數 col_indexcol_zeros 傳遞。

  • col_index (bigint) 必須在 0..buckets-1 的範圍內。

  • col_zeros (bigint) 必須在 0..64 的範圍內。如果超過 precision,則會裁剪至 precision-1

  • epsilon (double) 是一個正數,用於控制草圖中的雜訊程度,如 [Hehir2023] 中所述。較小的 epsilon 值對應於較多雜訊的草圖。

  • buckets (int) 是 SFM PCSA 草圖中的儲存桶數量,如 [Hehir2023] 中所述。

  • precision (int) 預設為 24。

注意

noisy_approx_set_sfm() 類似,當 col_indexcol_zerosNULL 時,此函數會傳回 NULL。如果這種行為不符合需求,請搭配 noisy_empty_approx_set_sfm() 使用 coalesce()

noisy_approx_distinct_sfm(col, epsilon[, buckets[, precision]]) -> bigint()

等同於 cardinality(noisy_approx_set_sfm(col, epsilon, buckets, precision)),這會傳回 col 資料行的近似基數(不重複計數)。這類似於(確定性的)approx_distinct() 函數。

注意

approx_distinct() 不同,當 col 為空時,此函數會傳回 NULL

noisy_empty_approx_set_sfm(epsilon[, buckets[, precision]]) -> SfmSketch()

傳回一個不含任何項目的 SFM 草圖。這類似於 empty_approx_set() 函數,該函數會傳回空的(確定性的)HyperLogLog 草圖。

  • epsilon (double) 是一個正數,用於控制草圖中的雜訊程度,如 [Hehir2023] 中所述。較小的 epsilon 值對應於較多雜訊的草圖。

  • buckets (int) 預設為 4096。

  • precision (int) 預設為 24。

cardinality(SfmSketch) -> bigint()

傳回 SfmSketch 物件的估計基數(不重複計數)。

merge(SfmSketch) -> SfmSketch()

一個彙總函數,傳回個別 SfmSketch 物件的集合聯集的合併 SfmSketch,類似於 merge(HyperLogLog)

SELECT year, cardinality(merge(sketch)) AS annual_distinct_count
FROM monthly_sketches
GROUP BY 1
merge_sfm(ARRAY[SfmSketch, ...]) -> SfmSketch()

一個純量函數,傳回 SfmSketch 物件陣列的集合聯集的合併 SfmSketch,類似於 merge_hll()

SELECT cardinality(merge_sfm(ARRAY[
    noisy_approx_set_sfm(col_1, 5.0),
    noisy_approx_set_sfm(col_2, 5.0),
    noisy_approx_set_sfm(col_3, 5.0)
])) AS distinct_count_over_3_cols
FROM my_table

限制

雖然這些函數類似於差分隱私機制,但這些函數傳回的值通常不是差分隱私的。如果將這些函數用於保護隱私的目的,請務必記住幾個重要的限制,包括

  • 當彙總空集合時,所有雜訊彙總函數都會傳回 NULL。這表示 NULL 傳回值會無雜訊地指出沒有資料。

  • 與雜訊彙總函數結合使用的 GROUP BY 子句會揭露非雜訊資訊:群組的存在或不存在會無雜訊地指出資料的存在或不存在。請參閱,例如,[Wilkins2024]

  • 依賴浮點雜訊的函數可能會受到推論攻擊的影響,例如在 [Mironov2012][Casacuberta2022] 中識別出的那些攻擊。


[Casacuberta2022]

Casacuberta, S., Shoemate, M., Vadhan, S., & Wagaman, C. (2022). Widespread Underestimation of Sensitivity in Differentially Private Libraries and How to Fix It. In Proceedings of the 2022 ACM SIGSAC Conference on Computer and Communications Security (pp. 471-484).

[Hehir2023] (1,2,3,4,5)

Hehir, J., Ting, D., & Cormode, G. (2023). Sketch-Flip-Merge: Mergeable Sketches for Private Distinct Counting. In Proceedings of the 40th International Conference on Machine Learning (Vol. 202).

[Mironov2012]

Mironov, I. (2012). On significance of the least significant bits for differential privacy. In Proceedings of the 2012 ACM Conference on Computer and Communications Security (pp. 650-661).

[Wilkins2024]

Wilkins, A., Kifer, D., Zhang, D., & Karrer, B. (2024). 高斯稀疏直方圖機制之精確隱私分析。 隱私與機密性期刊, 14 (1)。

[FlajoletMartin1985]

Flajolet, P, Martin, G. N. (1985). 用於數據庫應用之概率計數演算法。 收錄於計算機與系統科學期刊, 31:182–209, 1985