Kategorien:

Aggregatfunktionen (Ähnlichkeitsschätzung), Fensterfunktionen

MINHASH_COMBINE

Kombiniert mehrere MinHash-Eingabestatus zu einem einzigen MinHash-Ausgabestatus. Dieser Minhash-Status kann dann als Eingabe für die Funktion APPROXIMATE_SIMILARITY genutzt werden, um die Ähnlichkeit mit anderen MinHash-Status zu schätzen.

Dies erlaubt Anwendungsfälle, in denen MINHASH für horizontale Rowsets derselben Tabelle ausgeführt wird, wobei für jeden Rowset ein MinHash-Status generiert wird. Anschließend können diese Status mithilfe von MINHASH_COMBINE zusammengefasst werden, wodurch derselbe Ausgabestatus erzeugt wird wie bei einer einzelnen Ausführung von MINHASH für die gesamte Tabelle.

Weitere Informationen zu MinHash-Status finden Sie unter Schätzung der Ähnlichkeit von zwei oder mehr Sets.

Siehe auch:

MINHASH

Syntax

MINHASH_COMBINE( [ DISTINCT ] <state> )
Copy

Nutzungshinweise

  • DISTINCT kann als Argument eingefügt werden, hat jedoch keine Auswirkungen.

  • Der eingegebene MinHash state muss MinHash-Arrays gleicher Länge aufweisen.

Beispiele

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

Hier ist ein ausführlicheres Beispiel mit den drei verwandten Funktionen MINHASH, MINHASH_COMBINE und APPROXIMATE_SIMILARITY. In diesem Beispiel werden 3 Tabellen (ta, tb und tc) erstellt, von denen zwei (ta und tb) ähnlich und zwei (ta und tc) völlig verschieden sind.

Erstellen Sie Tabellen, und füllen Sie diese mit Werten:

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

-- Insert values into the 3 tables.
INSERT INTO ta (i) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
-- Almost the same as the preceding values.
INSERT INTO tb (i) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (11);
-- Different values and different number of values.
INSERT INTO tc (i) VALUES (-1), (-20), (-300), (-4000);
Copy

Berechnen Sie Minhash-Informationen für das anfängliche Dataset:

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;
Copy

Fügen Sie weitere Daten zu einer der Tabellen hinzu:

INSERT INTO ta (i) VALUES (12);
Copy

Verwendungsbeispiel für die Funktion MINHASH_COMBINE:

-- Record minhash information about only the new rows:
CREATE TABLE minhash_a_2 (mh) AS SELECT MINHASH(100, i) FROM ta WHERE i > 10;

-- Now combine all the minhash info for the old and new rows in table ta.
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)
    );
Copy

Diese Abfrage zeigt die ungefähre Ähnlichkeit der beiden ähnlichen Tabellen (ta und tb):

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

Diese Abfrage zeigt die ungefähre Ähnlichkeit der beiden sehr unterschiedlichen Tabellen (ta und tc):

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