UTL_MATCH package
1. Purpose
IvorySQL provides the Oracle-compatible UTL_MATCH package for string matching. It exposes four functions for edit distance and Jaro-Winkler similarity, with the same NULL and empty-string behaviour as a real Oracle Database (verified against Oracle Database 19c).
Set the Oracle compatibility mode before using the package:
SET ivorysql.compatible_mode = oracle;
2. Function descriptions
Function |
Return type |
Description |
|
INTEGER |
Minimum number of insert/delete/substitute edits to transform |
|
INTEGER |
Similarity in the 0..100 range, derived from the edit distance (100 for identical strings). |
|
DOUBLE PRECISION |
Jaro-Winkler similarity in the 0..1 range (case-sensitive). |
|
INTEGER |
|
3. Examples
3.1. Edit distance
SELECT UTL_MATCH.EDIT_DISTANCE('shackleford','shackelford');
edit_distance
---------------
2
(1 row)
SELECT UTL_MATCH.EDIT_DISTANCE('kitten','sitting');
edit_distance
---------------
3
(1 row)
3.2. Jaro-Winkler similarity
SELECT ROUND(UTL_MATCH.JARO_WINKLER('shackleford','shackelford')::numeric, 4);
round
-------
0.9818
(1 row)
SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('shackleford','shackelford');
jaro_winkler_similarity
-------------------------
98
(1 row)
Classic Jaro-Winkler reference values (rounded to 4 decimals):
SELECT ROUND(UTL_MATCH.JARO_WINKLER('MARTHA','MARHTA')::numeric, 4);
round
-------
0.9611
(1 row)
SELECT ROUND(UTL_MATCH.JARO_WINKLER('DWAYNE','DUANE')::numeric, 4);
round
-------
0.8400
(1 row)
4. NULL, empty strings, and multibyte input
The functions deliberately do not use strict NULL propagation; each function reproduces Oracle’s own result:
Function |
Result when any argument is NULL |
|
|
|
|
|
|
|
|
SELECT UTL_MATCH.EDIT_DISTANCE(NULL,'abc');
edit_distance
---------------
-1
(1 row)
Empty strings are handled by the algorithm when ivorysql.enable_emptystring_to_NULL is off. In real Oracle, '' is NULL, so these rows are only observable when that mapping is disabled:
SET ivorysql.enable_emptystring_to_NULL = off;
SELECT UTL_MATCH.EDIT_DISTANCE('','abc');
edit_distance
---------------
3
(1 row)
Comparisons are byte-based, not character-based. Replacing the multibyte character 试 (3 bytes in UTF-8) with the single-byte a counts as one substitution plus two deletions:
SELECT UTL_MATCH.EDIT_DISTANCE('中文测试','中文测a');
edit_distance
---------------
3
(1 row)
SELECT UTL_MATCH.EDIT_DISTANCE('中文测试','中文测试');
edit_distance
---------------
0
(1 row)
Other implementation details pinned by the regression suite: half-transpositions are truncated (t/2), the Winkler prefix boost is unconditional (no 0.7 threshold) and capped at 4 characters, the Jaro match window is floor(max(len)/2) - 1, and similarities are rounded half-up.
5. Implementation and regression tests
The C implementations live in contrib/ivorysql_ora/src/builtin_packages/utl_match/utl_match.c, with the SQL declarations in utl_match—1.0.sql of the same directory. The golden values above come from the Oracle PL/SQL Packages and Types Reference plus values verified on a real Oracle 19c instance (AL32UTF8), and are enforced by the regression pair:
-
contrib/ivorysql_ora/sql/utl_match.sql -
contrib/ivorysql_ora/expected/utl_match.out