View a markdown version of this page

String similarity functions - Amazon Athena

String similarity functions

Use these functions to measure how alike two strings are, for example to match names that are spelled in slightly different ways.

Distance functions count edits, so a lower value means more alike, and similarity functions return a score from 0 to 1, where higher means more alike.

damerau_levenshtein function

Returns the smallest number of single-character edits that change s1 into s2. An edit is an insertion, a deletion, a substitution, or a swap of two adjacent characters. levenshtein counts the same swap as two edits.

The comparison is case-sensitive, so a and A are different characters. If either argument is NULL, the result is NULL.

Syntax

damerau_levenshtein(s1, s2)

Arguments

The damerau_levenshtein function takes the following arguments:

s1

The first string to compare, as a VARCHAR value.

s2

The second string to compare, as a VARCHAR value.

Return type

BIGINT

Examples

The following SQL query returns the distance between two strings that differ by two swaps.

SELECT damerau_levenshtein('duckdb', 'udckbd') AS result

The query returns the following result.

result

2

The following SQL query compares one swap with levenshtein, changes the case of one letter, and passes NULL.

SELECT damerau_levenshtein('ab', 'ba') AS one_swap, levenshtein('ab', 'ba') AS levenshtein_swap, damerau_levenshtein('Duck', 'duck') AS case_change, damerau_levenshtein(NULL, 'abc') AS null_input

The query returns the following result.

one_swap levenshtein_swap case_change null_input

1

2

1

NULL

hamming function

Returns the Hamming distance between string1 and string2: the number of character positions at which the two strings differ. The comparison is case-sensitive. If either argument is NULL, the result is NULL.

mismatches is an alias of hamming.

Syntax

hamming(string1, string2)

Arguments

The hamming function takes the following arguments:

string1

The first string to compare, as a VARCHAR value.

string2

The second string to compare, as a VARCHAR value. It must have the same length as string1.

Return type

BIGINT

Examples

The following SQL query compares two four-letter strings that differ in one position.

SELECT hamming('duck', 'luck') AS result

The query returns the following result.

result

1

The following SQL query shows that the comparison is case-sensitive.

SELECT hamming('Duck', 'duck') AS result

The query returns the following result.

result

1

The following SQL query uses the mismatches alias. The strings karolin and kathrin differ in three positions.

SELECT mismatches('karolin', 'kathrin') AS result

The query returns the following result.

result

3

Considerations

If the two strings have different lengths, or if either string is empty, the query fails. To compare strings of different lengths, use the levenshtein function.

Differences from Athena engine version 3

In Athena engine version 3, this function is named hamming_distance. DuckDB does not have a hamming_distance function. Use hamming or its alias mismatches instead.

In Athena engine version 3, hamming_distance('', '') returns 0. In DuckDB, hamming fails when either string is empty.

jaccard function

Returns the Jaccard similarity of string1 and string2: the number of distinct characters that appear in both strings, divided by the number of distinct characters that appear in either string. The result is a DOUBLE from 0 (no characters in common) to 1 (the same set of characters).

Because the function compares sets of characters, the order and repetition of characters do not affect the result. The comparison is case-sensitive. If either argument is NULL, the result is NULL.

Syntax

jaccard(string1, string2)

Arguments

The jaccard function takes the following arguments:

string1

The first string to compare, as a VARCHAR value.

string2

The second string to compare, as a VARCHAR value.

Return type

DOUBLE

Examples

The following SQL query compares duck and luck, which share three of their five distinct characters.

SELECT jaccard('duck', 'luck') AS result

The query returns the following result.

result

0.6

The following SQL query compares two strings that have the same characters in a different order.

SELECT jaccard('abc', 'cba') AS result

The query returns the following result.

result

1.0

Considerations

If either string is empty, the query fails.

jaro_similarity function

Returns the Jaro similarity of string1 and string2, a DOUBLE from 0 (no similarity) to 1 (identical strings). The score is based on how many characters the two strings have in common within a short distance of the same position. It is also based on how many of those characters are out of order. The comparison is case-sensitive.

If you pass score_cutoff, the function returns 0 for any similarity lower than the cutoff. If any argument is NULL, the result is NULL.

Syntax

jaro_similarity(string1, string2 [, score_cutoff ])

Arguments

The jaro_similarity function takes the following arguments:

string1

The first string to compare, as a VARCHAR value.

string2

The second string to compare, as a VARCHAR value.

score_cutoff

The minimum score, as a DOUBLE value. When the similarity is lower than score_cutoff, the function returns 0 instead. If you omit score_cutoff, the function uses 0, so it returns every score unchanged. This argument is optional.

Return type

DOUBLE

Examples

The following SQL query compares duck and duckdb. All four characters of duck match, but the two extra characters in duckdb keep the similarity less than 1.

SELECT jaro_similarity('duck', 'duckdb') AS result

The query returns the following result.

result

0.8888888888888888

The following SQL query passes a score_cutoff of 0.9. The similarity, about 0.89, is lower than the cutoff, so the result is 0.0.

SELECT jaro_similarity('duck', 'duckdb', 0.9) AS result

The query returns the following result.

result

0.0

Considerations

Two empty strings have a similarity of 0, not 1.

jaro_winkler_similarity function

Returns the Jaro-Winkler similarity of string1 and string2, a DOUBLE from 0 to 1. The score starts from the Jaro similarity that jaro_similarity returns. When that similarity is greater than 0.7, the score is raised for each character of the prefix that the strings share, up to four characters. Otherwise, the result equals the Jaro similarity. The comparison is case-sensitive.

If you pass score_cutoff, the function returns 0 for any similarity lower than the cutoff. If any argument is NULL, the result is NULL.

Syntax

jaro_winkler_similarity(string1, string2 [, score_cutoff ])

Arguments

The jaro_winkler_similarity function takes the following arguments:

string1

The first string to compare, as a VARCHAR value.

string2

The second string to compare, as a VARCHAR value.

score_cutoff

The minimum score, as a DOUBLE value. When the similarity is lower than score_cutoff, the function returns 0 instead. If you omit score_cutoff, the function uses 0, so it returns every score unchanged. This argument is optional.

Return type

DOUBLE

Examples

The following SQL query compares duck and duckdb. Because the strings share a four-character prefix, the result is higher than the Jaro similarity of the same strings.

SELECT jaro_winkler_similarity('duck', 'duckdb') AS result

The query returns the following result.

result

0.9333333333333333

The following SQL query compares the Jaro and Jaro-Winkler similarity of two strings that share the prefix mar. The Jaro-Winkler score is higher because of that shared prefix.

SELECT jaro_similarity('martha', 'marhta') AS jaro, jaro_winkler_similarity( 'martha', 'marhta' ) AS jaro_winkler

The query returns the following result.

jaro jaro_winkler

0.9444444444444445

0.9611111111111111

The following SQL query passes a score_cutoff of 0.95. The similarity, about 0.93, is lower than the cutoff, so the result is 0.0.

SELECT jaro_winkler_similarity( 'duck', 'duckdb', 0.95 ) AS result

The query returns the following result.

result

0.0

Considerations

Two empty strings have a similarity of 0, not 1.

levenshtein function

Returns the Levenshtein distance between string1 and string2. To find it, the function counts the fewest edits that change string1 into string2, where each edit inserts, deletes, or replaces one character. The strings can have different lengths. The comparison is case-sensitive. If either argument is NULL, the result is NULL.

editdist3 is an alias of levenshtein.

Syntax

levenshtein(string1, string2)

Arguments

The levenshtein function takes the following arguments:

string1

The first string to compare, as a VARCHAR value.

string2

The second string to compare, as a VARCHAR value.

Return type

BIGINT

Examples

The following SQL query returns the number of edits that change duck into db.

SELECT levenshtein('duck', 'db') AS result

The query returns the following result.

result

3

The following SQL query shows that the comparison is case-sensitive.

SELECT levenshtein('Duck', 'duck') AS result

The query returns the following result.

result

1

The following SQL query uses the editdist3 alias. Changing kitten into sitting takes three edits: two replaced characters and one inserted character.

SELECT editdist3('kitten', 'sitting') AS result

The query returns the following result.

result

3

Differences from Athena engine version 3

In Athena engine version 3, this function is named levenshtein_distance. DuckDB does not have a levenshtein_distance function. Use levenshtein or its alias editdist3 instead.