UTL_MATCH package
1. Purpose
The Oracle-compatible UTL_MATCH package provides string matching and similarity computation. It contains four functions: EDIT_DISTANCE, EDIT_DISTANCE_SIMILARITY, JARO_WINKLER, and JARO_WINKLER_SIMILARITY, with behavior matching Oracle. It can be used for spell checking, fuzzy matching and deduplication of short text such as names or addresses, and external data cleansing.
2. Feature Description
2.1. Functions
| Function | Return type | Description |
|---|---|---|
|
|
Minimum number of edits (each insertion, deletion, or substitution of one character counts as 1) needed to transform s1 into s2 |
|
|
Edit-distance-based similarity score, 0–100. 0 means completely different, 100 means identical |
|
|
Jaro-Winkler similarity, 0–1 |
|
|
|
2.2. Syntax
SELECT UTL_MATCH.EDIT_DISTANCE(s1, s2) FROM dual;
SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY(s1, s2) FROM dual;
SELECT UTL_MATCH.JARO_WINKLER(s1, s2) FROM dual;
SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY(s1, s2) FROM dual;
s1 and s2 are the strings to compare (VARCHAR2). In Oracle compatibility mode (ivorysql.compatible_mode = oracle), call them directly.
3. Syntax Examples
EDIT_DISTANCE_SIMILARITY produces an integer score from 0 to 100 based on edit distance:
-- completely different strings, returns 0
SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('123456', '456789') AS similarity FROM dual;
-- one character differs, returns 83
SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('123456', '123956') AS similarity FROM dual;
-- identical strings, returns 100
SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('123456', '123456') AS similarity FROM dual;
JARO_WINKLER_SIMILARITY boosts common prefixes and scores the same inputs higher:
-- completely different strings, returns 0
SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('123456', '456789') AS similarity FROM dual;
-- one character differs, returns 92
SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('123456', '123956') AS similarity FROM dual;
-- identical strings, returns 100
SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('123456', '123456') AS similarity FROM dual;
The following examples compare several pairs of strings:
| Call | Result |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SELECT UTL_MATCH.EDIT_DISTANCE('shackleford', 'shackelford') AS edit_distance,
UTL_MATCH.EDIT_DISTANCE_SIMILARITY('shackleford', 'shackelford') AS similarity,
ROUND(UTL_MATCH.JARO_WINKLER('shackleford', 'shackelford'), 4) AS jaro_winkler,
UTL_MATCH.JARO_WINKLER_SIMILARITY('shackleford', 'shackelford') AS jw_similarity
FROM dual;
4. Matching rules
-
Case-sensitive:
EDIT_DISTANCE('abc', 'ABC')returns3, consistent with Oracle. -
Spaces are treated as ordinary characters:
EDIT_DISTANCE('a b', 'ab')returns1. -
Byte-based comparison: strings are compared byte by byte rather than character by character (consistent with Oracle). For example, under UTF-8,
EDIT_DISTANCE('中文测试', '中文测A')returns3(the final multi-byte character differs fromAin all 3 of its UTF-8 bytes: 1 substitution plus 2 deletions), andEDIT_DISTANCE_SIMILARITY('中文测试', '中文测A')returns75(the maximum length in the formula is 12 bytes). -
Length limit: the input length limit is 32767 bytes, the same as Oracle’s
VARCHAR2limit. IvorySQL’s implementation does not go through the PostgreSQLfuzzystrmatchextension, so it is not subject to the 255-byte limit of itslevenshteinfunction.
5. NULL and empty strings
UTL_MATCH handles NULL the way Oracle does: it does not propagate NULL but returns a fixed value per function:
| Function | Return value when an input is NULL |
|---|---|
|
|
|
|
|
|
|
|
In Oracle, an empty string is NULL. IvorySQL enables empty-string-to-NULL conversion by default (ivorysql.enable_emptystring_to_NULL = on), so '' behaves like NULL. With that setting disabled, the algorithmic behavior of empty strings becomes observable: EDIT_DISTANCE('', '') returns 0, and EDIT_DISTANCE_SIMILARITY('', '') returns 100.
6. Implementation notes
For algorithm details and the internal design, see UTL_MATCH implementation design.