Catégories :

Fonctions d’agrégation (estimation de la similarité), fonctions de fenêtre (estimation de la similarité)

MINHASH_COMBINE

Combine les états d’entrée MinHash en un seul état de sortie MinHash. Cet état Minhash peut ensuite être entré dans la fonction APPROXIMATE_SIMILARITY pour estimer la similarité avec les autres états MinHash.

Cela permet des cas d’utilisation dans lesquels MINHASH est exécuté sur des ensembles de lignes horizontaux de la même table, générant un état MinHash pour chaque ensemble de lignes. Ces états peuvent ensuite être combinés à l’aide de MINHASH_COMBINE, produisant le même état de sortie qu’une seule exécution de MINHASH sur la table entière.

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

Voir aussi :

MINHASH

Syntaxe

Fonction d’agrégation

MINHASH_COMBINE( [ DISTINCT ] <state> )

Fonction de fenêtre

MINHASH_COMBINE( [ DISTINCT ] <state> ) OVER ( [ PARTITION BY <expr> ] )

Pour plus d’informations sur la clause OVER, voir Syntaxe et utilisation des fonctions de fenêtre.

Arguments

state

Expression contenant des informations d’état MinHash générées par un appel à MINHASH. Les statuts MinHash d’entrée doivent avoir des tableaux de même longueur.

Notes sur l’utilisation

Exemples

USE SCHEMA snowflake_sample_data.tpch_sf1;

SELECT MINHASH_COMBINE(mh) FROM
    (
      (SELECT MINHASH(5, c2) mh FROM orders WHERE c2 <= 10000)
        UNION
      (SELECT MINHASH(5, c2) mh FROM orders WHERE c2 > 10000 AND c2 <= 20000)
        UNION
      (SELECT MINHASH(5, C2) mh FROM orders WHERE c2 > 20000)
    );

+-----------------------+
| MINHASH_COMBINE(MH)   |
|-----------------------|
| {                     |
|   "state": [          |
|     628914288006793,  |
|     1071764954434168, |
|     991489123966035,  |
|     2395105834644106, |
|     680224867834949   |
|   ],                  |
|   "type": "minhash",  |
|   "version": 1        |
| }                     |
+-----------------------+

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 |
+-----------------------------+