Kategorien:

Aggregatfunktionen (Ähnlichkeitsschätzung), Fensterfunktionen (Ähnlichkeitsschätzung)

MINHASH

Gibt einen MinHash-Status zurück, der ein Array der Größe k enthält, das durch Anwenden von k verschiedenen Hash-Funktionen auf die Eingabezeilen konstruiert wurde und das Minimum jeder Hash-Funktion behält. Dieser MinHash-Status kann dann in die Funktion APPROXIMATE_SIMILARITY eingegeben werden, um die Ähnlichkeit mit einem oder mehreren anderen MinHash-Status zu schätzen.

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

Siehe auch:

MINHASH_COMBINE

Syntax

Aggregatfunktion

MINHASH( <k> , [ DISTINCT ] expr+ )

MINHASH( <k> , * )

Fensterfunktionen

MINHASH( <k> , [ DISTINCT ] expr+ ) OVER ( [ PARTITION BY <expr1> ] )

MINHASH( <k> , * ) OVER ( [ PARTITION BY <expr1> ] )

Informationen zur OVER-Klausel finden Sie unter Syntax und Verwendung von Fensterfunktionen.

Argumente

k

Die Anzahl der zu erstellenden Hashfunktionen. Je größer der Wert, desto besser die Näherung. Dieser Wert hat jedoch einen linearen Einfluss auf die Berechnungszeit für das Schätzen der Ähnlichkeit mit APPROXIMATE_SIMILARITY. Der empfohlene Wert beträgt 100. Der Maximalwert beträgt 1.024.

expr

Ein oder mehrere Ausdrücke (typischerweise Spaltennamen), die Grundlage für die zu berechnenden Hash-Werte bilden.

*

Erstellt Hash-Werte für alle Spalten in den Eingabezeilen.

Nutzungshinweise

  • Diese Funktion kann als Aggregatfunktion oder Fensterfunktion verwendet werden.

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

Beispiele

USE SCHEMA snowflake_sample_data.tpch_sf1;

SELECT MINHASH(5, *) FROM orders;

+----------------------+
| MINHASH(5, *)        |
|----------------------|
| {                    |
|   "state": [         |
|     78678383574307,  |
|     586952033158539, |
|     525995912623966, |
|     508991839383217, |
|     492677003405678  |
|   ],                 |
|   "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.

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

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;

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

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