Set Digest functions
Trino offers several functions that deal with the MinHash technique.
MinHash is used to quickly estimate the Jaccard similarity coefficient between two sets.
It is commonly used in data mining to detect near-duplicate web pages at scale. By using this information, the search engines efficiently avoid showing within the search results two pages that are nearly identical.
The following example showcases how the Set Digest functions can be used to naively estimate the similarity between texts. The input texts are split by using the function ngrams() to 4-shingles which are used as input for creating a set digest of each initial text. The set digests are compared to each other to get an approximation of the similarity of their corresponding initial texts:
The above result listing points out, as expected, that the texts
with the id 1
and 3
are quite similar.
One may argue that the text with the id 2
is somewhat similar to
the texts with the id 1
and 3
. Due to the fact in the example above
4-shingles are taken into account for measuring the similarity of the texts,
there are no intersections found for the text pairs 1
and 2
, respectively
3
and 2
and therefore there the similarity index for these text pairs
is 0
.
Data structures
Trino implements Set Digest data sketches by encapsulating the following components:
The HyperLogLog structure is used for the approximation of the distinct elements in the original set.
The MinHash structure is used to store a low memory footprint signature of the original set. The similarity of any two sets is estimated by comparing their signatures.
The Trino type for this data structure is called setdigest
.
Trino offers the ability to merge multiple Set Digest data sketches.
Serialization
Data sketches can be serialized to and deserialized from varbinary
. This
allows them to be stored for later use.
Functions
make_set_digest
Create a setdigest
corresponding to a bigint
array:
Create a setdigest
corresponding to a varchar
array:
merge_set_digest
Returns the setdigest
of the aggregate union of the individual setdigest
Set Digest structures.
cardinality
Returns the cardinality of the set digest from its internal
HyperLogLog
component.
Examples:
intersection_cardinality
Examples:
jaccard_index
Returns the estimation of Jaccard index for the two set digests.
x
and y
must be of type setdigest
.
Examples:
hash_counts
Returns a map containing the Murmur3Hash128
hashed values and the count of their occurences within
the internal MinHash
structure belonging to x
.
x
must be of type setdigest
.
Examples: