UTL_MATCH package implementation
1. Purpose
IvorySQL implements the four functions of Oracle’s UTL_MATCH package: EDIT_DISTANCE, EDIT_DISTANCE_SIMILARITY, JARO_WINKLER, and JARO_WINKLER_SIMILARITY. Behaviors including NULL semantics, byte-based comparison, transposition truncation, and prefix-boost conditions match Oracle. For user examples, see UTL_MATCH package.
2. Implementation
2.1. Code organization and call path
| Source path | Responsibility |
|---|---|
|
Registers the C entry points |
|
C implementation of the four functions (edit distance, Jaro, Jaro-Winkler) |
|
Regression cases |
|
Expected results and comments |
|
Build integration and SQL merge for the new directory |
UTL_MATCH.EDIT_DISTANCE(s1, s2)
→ sys.ora_utl_match_edit_distance(s1 text, s2 text) RETURNS integer
→ edit_distance()
UTL_MATCH.EDIT_DISTANCE_SIMILARITY(s1, s2)
→ sys.ora_utl_match_edit_distance_similarity(s1 text, s2 text) RETURNS integer
→ edit_distance_similarity()
UTL_MATCH.JARO_WINKLER(s1, s2)
→ sys.ora_utl_match_jaro_winkler(s1 text, s2 text) RETURNS float8
→ jaro_winkler() → jaro()
UTL_MATCH.JARO_WINKLER_SIMILARITY(s1, s2)
→ sys.ora_utl_match_jaro_winkler_similarity(s1 text, s2 text) RETURNS integer
→ jaro_winkler() → jaro()
The UTL_MATCH.* functions in the package body only forward arguments. The four C entry points are declared IMMUTABLE and are not declared STRICT, because Oracle does not propagate NULL but returns a fixed sentinel value per function (see below).
2.2. Edit distance (Levenshtein)
edit_distance() computes the minimum number of single-character edits (each insertion, deletion, or substitution counts as 1) needed to transform s1 into s2:
-
It fills a two-row dynamic-programming matrix row by row. The transition is: take the diagonal value when the characters match, otherwise
min(above + 1, left + 1, diagonal + 1). -
Exploiting the symmetry of the Levenshtein distance, the shorter string is swapped into the column dimension first, keeping memory at
O(min(n1, n2)). -
CHECK_FOR_INTERRUPTS()is called every 1024 inner-loop iterations, so even large quadratic comparisons remain cancellable and honorstatement_timeout. -
Comparison is byte-based, consistent with Oracle; the distance between an empty string and any string is the other string’s length in bytes.
edit_distance_similarity() is computed as:
similarity = ROUND((1 - ED / MAX(LEN1, LEN2)) * 100) -- floored at 0
where ED is the edit distance and LEN1, LEN2 are the lengths in bytes. Two empty strings are considered 100% similar. Rounding is half-up (floor(v + 0.5)), consistent with Oracle: (1 - 3/8) * 100 = 62.5 rounds to 63.
2.3. Jaro-Winkler
The Jaro similarity is based on the number of matching characters m and the number of transpositions t:
| 0 if m = 0
sim_jaro = |
| (m/|s1| + m/|s2| + (m - t)/m) / 3 otherwise
-
The match window is
floor(max(len1, len2) / 2) - 1: byte i of s1 can only pair with an identical, not-yet-used byte of s2 within the window. -
tis the number of transpositions (half the number of out-of-order matched pairs). Unlike the textbook fractional value, Oracle truncates half-transpositions (integer divisiont/2), soJARO_WINKLER_SIMILARITY('ABCDEF', 'BCADEF')returns94rather than the textbook92. -
Example with MARTHA / MARHTA: the window is
6/2 - 1 = 2; all six characters pair up, som = 6; the displaced characters are H and T, and one swap restores the order, sot = 1;sim_jaro = (6/6 + 6/6 + 5/6) / 3 = 0.9444.
The Winkler prefix boost amplifies the Jaro similarity for strings that share a common prefix:
sim_jw = sim_jaro + l * 0.1 * (1 - sim_jaro)
-
lis the common-prefix length in bytes, capped at 4. -
Unlike the classic algorithm, Oracle applies the boost unconditionally (there is no 0.7 boost threshold):
JARO_WINKLER('ABCCCCCCCCCC', 'ABDDDDDDDDDD')is boosted from 0.4444 to 0.5556.
JARO_WINKLER_SIMILARITY returns ROUND(jaro_winkler * 100), also rounded half-up.
2.4. NULL, empty strings, and length boundaries
The sentinel-value semantics of Oracle are implemented at the C level and do not rely on STRICT:
| Function | Return value for NULL input |
|---|---|
|
|
|
|
|
|
|
|
There is no equivalent of fuzzystrmatch’s `MAX_LEVENSHTEIN_STRLEN (255 bytes) limit from PostgreSQL. The input length limit is the VARCHAR2 maximum of 32767 bytes, consistent with Oracle; the regression cases include an input of length 3000.
3. Implementation Examples
3.1. Edit distance and similarity
SELECT UTL_MATCH.EDIT_DISTANCE('shackleford', 'shackelford') FROM dual; -- 2
SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('shackleford', 'shackelford') FROM dual; -- 82
The package forwards the arguments to the corresponding sys.ora_utl_match_* entry point. The distance is 2, and the longer string is 11 bytes; the similarity is calculated from these values and rounded.
4. Regression verification
The utl_match regression group covers common example values (shackleford/shackelford, kitten/sitting, MARTHA/MARHTA, DWAYNE/DUANE, DIXON/DICKSONX, JELLYFISH/SMELLYFISH), case sensitivity, spaces, byte-level behavior of multi-byte characters, empty strings, NULL sentinel values, half-transposition truncation, the unconditional prefix boost and its 4-byte cap, match-window boundaries, half-up rounding boundaries, plus direct C entry-point calls and PL/iSQL package interface cases.
From a configured and built IvorySQL source tree, run make -C contrib/ivorysql_ora oracle-installcheck ORA_REGRESS=utl_match to verify this test group. The target instance must have the matching version of the ivorysql_ora extension installed.