Catégories :

Fonctions d’agrégation (Similarity Estimation) , Syntaxe et utilisation des fonctions de fenêtre

APPROXIMATE_SIMILARITY

Renvoie une estimation de la similarité (indice de Jaccard) des entrées sur la base de leurs états MinHash. Pour plus d’informations sur les états MinHash, voir Estimation de la similarité de deux ensembles ou plus.

Alias :

APPROXIMATE_JACCARD_INDEX

Voir aussi :

MINHASH , MINHASH_COMBINE

Syntaxe

APPROXIMATE_SIMILARITY( [ DISTINCT ] <expr> [ , ... ] )

APPROXIMATE_SIMILARITY(*)

Arguments

expr

L’expression doit être un ou plusieurs états MinHash renvoyés par des appels à la fonction MINHASH. En d’autres termes, les expressions doivent être des informations d’état MinHash et non la colonne ou l’expression pour lesquelles vous souhaitez une similarité approximative. (L’exemple ci-dessous permet de clarifier ce point.)

Pour plus d’informations sur les états MinHash, voir Estimation de la similarité de deux ensembles ou plus.

Renvoie

Nombre en virgule flottante compris entre 0,0 et 1,0 (inclus), où 1,0 indique que les ensembles sont identiques et 0,0 indique que les ensembles ne se chevauchent pas.

Notes sur l’utilisation

  • DISTINCT peut être inclus comme argument, mais n’a aucun effet.

  • Les statuts MinHash d’entrée doivent avoir des tableaux MinHash de même longueur.

  • La longueur du tableau des statuts MinHash d’entrée est un indicateur de la qualité de l’approximation.

    Plus la valeur de k utilisée dans la fonction MINHASH est grande, meilleure est l’approximation. Cependant, cette valeur a un impact linéaire sur le temps de calcul nécessaire pour estimer la similarité.

Exemples

USE SCHEMA snowflake_sample_data.tpch_sf1;

SELECT APPROXIMATE_SIMILARITY(mh) FROM
    (
      (SELECT MINHASH(100, C5) mh FROM orders WHERE c2 <= 50000)
         UNION
      (SELECT MINHASH(100, C5) mh FROM orders WHERE C2 > 50000)
    );

+----------------------------+
| APPROXIMATE_SIMILARITY(MH) |
|----------------------------|
|                       0.97 |
+----------------------------+

Here is a more extensive example, showing the three related functions MINHASH, MINHASH_COMBINE and APPROXIMATE_SIMILARITY. This example creates 3 tables (ta, tb, and tc), two of which (ta and tb) are similar, and two of which (ta and tc) are completely dissimilar.

Créer et remplir des tables avec des valeurs :

CREATE TABLE ta (i INTEGER);
CREATE TABLE tb (i INTEGER);
CREATE TABLE tc (i INTEGER);

INSERT INTO ta (i) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO tb (i) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (11);
INSERT INTO tc (i) VALUES (-1), (-20), (-300), (-4000);

Calculer les informations minhash pour l’ensemble initial de données :

CREATE TABLE minhash_a_1 (mh) AS SELECT MINHASH(100, i) FROM ta;
CREATE TABLE minhash_b (mh) AS SELECT MINHASH(100, i) FROM tb;
CREATE TABLE minhash_c (mh) AS SELECT MINHASH(100, i) FROM tc;

Ajouter plus de données à l’une des tables :

INSERT INTO ta (i) VALUES (12);

Demonstrate the MINHASH_COMBINE function:

CREATE TABLE minhash_a_2 (mh) AS SELECT MINHASH(100, i) FROM ta WHERE i > 10;

CREATE TABLE minhash_a (mh) AS
  SELECT MINHASH_COMBINE(mh)
    FROM (
      (SELECT mh FROM minhash_a_1)
      UNION ALL
      (SELECT mh FROM minhash_a_2)
    );

This query shows the approximate similarity of the two similar tables (ta and tb):

SELECT APPROXIMATE_SIMILARITY(mh)
  FROM (
    (SELECT mh FROM minhash_a)
    UNION ALL
    (SELECT mh FROM minhash_b)
  );
+-----------------------------+
| APPROXIMATE_SIMILARITY (MH) |
|-----------------------------|
|                        0.75 |
+-----------------------------+

This query shows the approximate similarity of the two very different tables (ta and tc):

SELECT APPROXIMATE_SIMILARITY(mh)
  FROM (
    (SELECT mh FROM minhash_a)
    UNION ALL
    (SELECT mh FROM minhash_c)
  );
+-----------------------------+
| APPROXIMATE_SIMILARITY (MH) |
|-----------------------------|
|                           0 |
+-----------------------------+