From a6a68ee1696b88b4d8930489b8fbf7fa75a991be Mon Sep 17 00:00:00 2001 From: himmel Date: Mon, 30 Nov 2026 16:00:34 +0800 Subject: [PATCH] docs: document UTL_MATCH --- .../utl_match_impl.adoc | 130 ++++++++++++++++++ .../builtin_types_functions.adoc | 33 ++++- .../compat_utl_match.adoc | 115 ++++++++++++++++ .../utl_match_impl.adoc | 128 +++++++++++++++++ .../builtin_types_functions.adoc | 31 +++++ .../compat_utl_match.adoc | 115 ++++++++++++++++ 6 files changed, 551 insertions(+), 1 deletion(-) create mode 100644 CN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc create mode 100644 CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc create mode 100644 EN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc create mode 100644 EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc diff --git a/CN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc b/CN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc new file mode 100644 index 00000000..791d4568 --- /dev/null +++ b/CN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc @@ -0,0 +1,130 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_MATCH 包实现说明 + +== 目的 + +IvorySQL 实现 Oracle `UTL_MATCH` 包的四个函数:`EDIT_DISTANCE`、`EDIT_DISTANCE_SIMILARITY`、`JARO_WINKLER` 和 `JARO_WINKLER_SIMILARITY`,包括 NULL 语义、字节级比较、换位取整及前缀加权条件等行为均与 Oracle 保持一致。用户示例见 xref:master/oracle_compatibility/compat_utl_match.adoc[UTL_MATCH 包]。 + +== 实现说明 + +=== 代码组织与调用关系 + +[cols="3,3",options="header"] +|=== +|源码路径 |职责 +|`contrib/ivorysql_ora/src/builtin_packages/utl_match/utl_match--1.0.sql` |注册 C 入口 `sys.ora_utl_match_*`,并定义 `UTL_MATCH` 包规格与包体 +|`contrib/ivorysql_ora/src/builtin_packages/utl_match/utl_match.c` |四个函数的 C 实现(编辑距离、Jaro、Jaro-Winkler) +|`contrib/ivorysql_ora/sql/utl_match.sql` |回归用例 +|`contrib/ivorysql_ora/expected/utl_match.out` |期望的结果与注释 +|`Makefile`、`meson.build`、`ivorysql_ora_merge_sqls` |将新目录纳入构建与 SQL 合并流程 +|=== + +[source,text] +---- +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() +---- + +包体中的 `UTL_MATCH.*` 仅作参数转发,四个 C 入口声明为 `IMMUTABLE`,且不声明为 `STRICT`,因为 Oracle 对 NULL 输入不传播 NULL,而是按函数返回固定哨兵值(见下文)。 + +=== 编辑距离(Levenshtein) + +`edit_distance()` 计算将 s1 变换为 s2 所需的最少单字符编辑次数(插入、删除、替换各计 1): + +* 使用两行动态规划矩阵逐行填表,转移方程为:字符相同取对角线值,字符不同取 `min(上+1, 左+1, 左上+1)`。 +* 利用编辑距离的对称性,先将较短的字符串交换为列维度,内存占用为 `O(min(n1, n2))`。 +* 内层循环每累计 1024 次迭代调用一次 `CHECK_FOR_INTERRUPTS()`,保证大输入(O(n1×n2) 的比较)仍可被取消或遵守 `statement_timeout`。 +* 比较以字节为单位,与 Oracle 一致;空串与任意串的距离为另一串的字节长度。 + +`edit_distance_similarity()` 的计算公式为: + +[source,text] +---- +similarity = ROUND((1 - ED / MAX(LEN1, LEN2)) * 100) -- 结果下限为 0 +---- + +其中 `ED` 为编辑距离,`LEN1`、`LEN2` 为字节长度,两个空串视为 100% 相似。四舍五入采用 half-up(`floor(v + 0.5)`),与 Oracle 一致,例如 `(1 - 3/8) * 100 = 62.5` 进位为 `63`。 + +=== Jaro-Winkler + +Jaro 相似度基于匹配字符数 `m` 和换位数 `t`: + +[source,text] +---- + | 0 若 m = 0 +sim_jaro = | + | (m/|s1| + m/|s2| + (m - t)/m) / 3 其他情况 +---- + +* 匹配窗口为 `floor(max(len1, len2) / 2) - 1`:s1 的第 i 个字节只与 s2 中距离不超过窗口的同名字节配对。 +* `t` 为换位数(失配的匹配字符对数的一半)。与教科书的小数换位不同,Oracle 将半换位**截断取整**(整数除法 `t/2`),例如 `JARO_WINKLER_SIMILARITY('ABCDEF', 'BCADEF')` 返回 `94` 而非教科书的 `92`。 +* 以 MARTHA / MARHTA 为例:窗口为 `6/2 - 1 = 2`,六个字符全部配上对,`m = 6`;失配字符为 H、T,一次交换即可复原,`t = 1`;`sim_jaro = (6/6 + 6/6 + 5/6) / 3 = 0.9444`。 + +Winkler 前缀加权在 Jaro 相似度基础上提升共同前缀的权重: + +[source,text] +---- +sim_jw = sim_jaro + l * 0.1 * (1 - sim_jaro) +---- + +* `l` 为共同前缀的字节数,**上限为 4**。 +* 与经典算法不同,Oracle **无条件**应用前缀提升(没有 0.7 的提升阈值),例如 `JARO_WINKLER('ABCCCCCCCCCC', 'ABDDDDDDDDDD')` 从 0.4444 提升到 0.5556。 + +`JARO_WINKLER_SIMILARITY` 返回 `ROUND(jaro_winkler * 100)`,同样采用 half-up 取整。 + +=== NULL、空串与长度边界 + +C 层即实现 Oracle 的哨兵值语义,不依赖 `STRICT`: + +[cols="4,3",options="header"] +|=== +|函数 |NULL 输入的返回值 +|`ora_utl_match_edit_distance` |`-1` +|`ora_utl_match_edit_distance_similarity` |单侧 NULL 返回 `0`,双侧 NULL 返回 `100` +|`ora_utl_match_jaro_winkler` |`0` +|`ora_utl_match_jaro_winkler_similarity` |`0` +|=== + +实现不设 PostgreSQL `fuzzystrmatch` 中 `levenshtein` 的 `MAX_LEVENSHTEIN_STRLEN`(255 字节)上限,输入长度上限即 `VARCHAR2` 的 32767 字节,与 Oracle 一致;回归用例包含长度 3000 的输入。 + +== 实现示例 + +=== 编辑距离与相似度 + +[source,sql] +---- +SELECT UTL_MATCH.EDIT_DISTANCE('shackleford', 'shackelford') FROM dual; +-- 2 +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('shackleford', 'shackelford') FROM dual; +-- 82 +---- + +包函数把参数传给对应的 `sys.ora_utl_match_*` 入口。编辑距离为 2,较长字符串为 11 字节,相似度按公式计算并取整。 + +=== NULL 输入 + +[source,sql] +---- +SELECT UTL_MATCH.EDIT_DISTANCE(NULL, 'abc') FROM dual; -- -1 +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY(NULL, NULL) FROM dual; -- 100 +---- + +C 入口没有声明 `STRICT`,因此能够分别处理 NULL 参数,而不是由 PostgreSQL 直接返回 NULL。 + +== 回归验证 + +`utl_match` 回归组覆盖常见示例值(shackleford/shackelford、kitten/sitting、MARTHA/MARHTA、DWAYNE/DUANE、DIXON/DICKSONX、JELLYFISH/SMELLYFISH)、大小写敏感、空格、多字节字符的字节级行为、空串、NULL 哨兵值、半换位截断、无条件前缀提升及其 4 字节上限、匹配窗口边界、half-up 取整边界,以及直接调用 C 入口和 PL/iSQL 包接口的用例。 + +在已配置并编译的 IvorySQL 源码目录中,可运行 `make -C contrib/ivorysql_ora oracle-installcheck ORA_REGRESS=utl_match` 验证该测试组;目标实例需已安装相应版本的 `ivorysql_ora` 扩展。 diff --git a/CN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc b/CN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc index 9c3d170d..70ee8675 100644 --- a/CN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc +++ b/CN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc @@ -1150,4 +1150,35 @@ ivorysql=# select sys_context('USERENV','DB_NAME'); ivorysql (1 row) -``` \ No newline at end of file +``` +=== `UTL_MATCH` 包 + +功能:提供 Oracle 兼容的字符串匹配与相似度计算,包含 `EDIT_DISTANCE`、`EDIT_DISTANCE_SIMILARITY`、`JARO_WINKLER`、`JARO_WINKLER_SIMILARITY` 四个函数,用于拼写检查、错别字纠正以及姓名、地址等短文本的模糊匹配与去重。测试用例如下: + +``` +SELECT UTL_MATCH.EDIT_DISTANCE('shackleford','shackelford'); + edit_distance +--------------- + 2 +(1 row) + +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('shackleford','shackelford'); + edit_distance_similarity +-------------------------- + 82 +(1 row) + +SELECT ROUND(UTL_MATCH.JARO_WINKLER('shackleford','shackelford'),4); + round +--------- + 0.9818 +(1 row) + +SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('shackleford','shackelford'); + jaro_winkler_similarity +------------------------- + 98 +(1 row) +``` + +详见 xref:master/oracle_compatibility/compat_utl_match.adoc[UTL_MATCH 包]。 \ No newline at end of file diff --git a/CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc b/CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc new file mode 100644 index 00000000..999ced5e --- /dev/null +++ b/CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc @@ -0,0 +1,115 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_MATCH 包 + +== 目的 + +`UTL_MATCH` 包用于计算两个字符串的编辑距离和相似度,包含 `EDIT_DISTANCE`、`EDIT_DISTANCE_SIMILARITY`、`JARO_WINKLER` 和 `JARO_WINKLER_SIMILARITY` 四个函数。 + +== 功能说明 + +=== 函数一览 + +[cols="4,2,4",options="header"] +|=== +|函数 |返回类型 |说明 +|`EDIT_DISTANCE(s1, s2)` |`INTEGER` |将 s1 转换为 s2 所需的最少编辑次数(插入、删除、替换一个字符各记 1 次) +|`EDIT_DISTANCE_SIMILARITY(s1, s2)` |`INTEGER` |基于编辑距离的相似度分数,范围 0–100,0 表示完全不同,100 表示完全相同 +|`JARO_WINKLER(s1, s2)` |`BINARY_DOUBLE` |Jaro-Winkler 相似度,范围 0–1 +|`JARO_WINKLER_SIMILARITY(s1, s2)` |`INTEGER` |`JARO_WINKLER` 结果乘以 100 并四舍五入,范围 0–100 +|=== + +=== 调用方式 + +[source,sql] +---- +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`、`s2` 为待比较的字符串(`VARCHAR2`)。在 Oracle 兼容模式(`ivorysql.compatible_mode = oracle`)下可直接使用。 + +== 语法示例 + +`EDIT_DISTANCE_SIMILARITY` 按编辑距离给出 0–100 的整数分数: + +[source,sql] +---- +-- 完全不同的字符串,返回 0 +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('123456', '456789') AS similarity FROM dual; + +-- 仅一个字符不同,返回 83 +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('123456', '123956') AS similarity FROM dual; + +-- 完全相同的字符串,返回 100 +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('123456', '123456') AS similarity FROM dual; +---- + +`JARO_WINKLER_SIMILARITY` 计算 Jaro-Winkler 相似度: + +[source,sql] +---- +-- 完全不同的字符串,返回 0 +SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('123456', '456789') AS similarity FROM dual; + +-- 仅一个字符不同,返回 92 +SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('123456', '123956') AS similarity FROM dual; + +-- 完全相同的字符串,返回 100 +SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('123456', '123456') AS similarity FROM dual; +---- + +以下示例比较几组字符串: + +[cols="5,2",options="header"] +|=== +|调用 |结果 +|`EDIT_DISTANCE('shackleford', 'shackelford')` |`2` +|`EDIT_DISTANCE_SIMILARITY('shackleford', 'shackelford')` |`82` +|`JARO_WINKLER('shackleford', 'shackelford')` |`0.9818` +|`JARO_WINKLER_SIMILARITY('shackleford', 'shackelford')` |`98` +|`EDIT_DISTANCE('kitten', 'sitting')` |`3` +|`EDIT_DISTANCE_SIMILARITY('kitten', 'sitting')` |`57` +|`JARO_WINKLER_SIMILARITY('MARTHA', 'MARHTA')` |`96` +|`JARO_WINKLER_SIMILARITY('DWAYNE', 'DUANE')` |`84` +|`JARO_WINKLER_SIMILARITY('DIXON', 'DICKSONX')` |`81` +|`JARO_WINKLER_SIMILARITY('JELLYFISH', 'SMELLYFISH')` |`90` +|=== + +[source,sql] +---- +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; +---- + +== 匹配规则 + +* 大小写敏感:`EDIT_DISTANCE('abc', 'ABC')` 返回 `3`,与 Oracle 一致。 +* 空格作为普通字符参与计算:`EDIT_DISTANCE('a b', 'ab')` 返回 `1`。 +* 按字节比较,而非按字符比较(与 Oracle 一致)。例如 UTF-8 编码下 `EDIT_DISTANCE('中文测试', '中文测A')` 返回 `3`(最后一个多字节字符与 `A` 的 3 个字节完全不同:1 次替换加 2 次删除),`EDIT_DISTANCE_SIMILARITY('中文测试', '中文测A')` 返回 `75`(分母取字节长度 12)。 +* 输入长度上限为 32767 字节(`VARCHAR2` 上限),与 Oracle 相同。IvorySQL 的实现不经过 PostgreSQL `fuzzystrmatch` 扩展,因此不受其 `levenshtein` 函数 255 字节长度限制的约束。 + +== NULL 与空字符串 + +`UTL_MATCH` 的 NULL 处理按函数分别处理 NULL: + +[cols="4,3",options="header"] +|=== +|函数 |任一参数为 NULL 时的返回值 +|`EDIT_DISTANCE(s1, s2)` |`-1` +|`EDIT_DISTANCE_SIMILARITY(s1, s2)` |一个为 NULL 返回 `0`;两个都为 NULL 返回 `100` +|`JARO_WINKLER(s1, s2)` |`0` +|`JARO_WINKLER_SIMILARITY(s1, s2)` |`0` +|=== + +Oracle 中空字符串即 `NULL`。IvorySQL 默认开启空字符串转 `NULL`(`ivorysql.enable_emptystring_to_NULL = on`),此时 `''` 的行为与 `NULL` 相同;关闭该参数后可以观察空字符串本身的算法行为,例如 `EDIT_DISTANCE('', '')` 返回 `0`,`EDIT_DISTANCE_SIMILARITY('', '')` 返回 `100`。 + +== 实现说明 + +算法细节与内部实现见 xref:master/compatibility_features_design/utl_match_impl.adoc[UTL_MATCH 实现设计]。 diff --git a/EN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc b/EN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc new file mode 100644 index 00000000..e65bca78 --- /dev/null +++ b/EN/modules/ROOT/pages/master/compatibility_features_design/utl_match_impl.adoc @@ -0,0 +1,128 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_MATCH package implementation + +== 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 xref:master/oracle_compatibility/compat_utl_match.adoc[UTL_MATCH package]. + +== Implementation + +=== Code organization and call path + +[cols="3,3",options="header"] +|=== +|Source path |Responsibility +|`contrib/ivorysql_ora/src/builtin_packages/utl_match/utl_match--1.0.sql` |Registers the C entry points `sys.ora_utl_match_*` and defines the `UTL_MATCH` package specification and body +|`contrib/ivorysql_ora/src/builtin_packages/utl_match/utl_match.c` |C implementation of the four functions (edit distance, Jaro, Jaro-Winkler) +|`contrib/ivorysql_ora/sql/utl_match.sql` |Regression cases +|`contrib/ivorysql_ora/expected/utl_match.out` |Expected results and comments +|`Makefile`, `meson.build`, `ivorysql_ora_merge_sqls` |Build integration and SQL merge for the new directory +|=== + +[source,text] +---- +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). + +=== 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 honor `statement_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: + +[source,text] +---- +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`. + +=== Jaro-Winkler + +The Jaro similarity is based on the number of matching characters `m` and the number of transpositions `t`: + +[source,text] +---- + | 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. +* `t` is the number of transpositions (half the number of out-of-order matched pairs). Unlike the textbook fractional value, Oracle **truncates** half-transpositions (integer division `t/2`), so `JARO_WINKLER_SIMILARITY('ABCDEF', 'BCADEF')` returns `94` rather than the textbook `92`. +* Example with MARTHA / MARHTA: the window is `6/2 - 1 = 2`; all six characters pair up, so `m = 6`; the displaced characters are H and T, and one swap restores the order, so `t = 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: + +[source,text] +---- +sim_jw = sim_jaro + l * 0.1 * (1 - sim_jaro) +---- + +* `l` is 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. + +=== NULL, empty strings, and length boundaries + +The sentinel-value semantics of Oracle are implemented at the C level and do not rely on `STRICT`: + +[cols="4,3",options="header"] +|=== +|Function |Return value for NULL input +|`ora_utl_match_edit_distance` |`-1` +|`ora_utl_match_edit_distance_similarity` |`0` if exactly one input is NULL, `100` if both are +|`ora_utl_match_jaro_winkler` |`0` +|`ora_utl_match_jaro_winkler_similarity` |`0` +|=== + +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. + +== Implementation Examples + +=== Edit distance and similarity + +[source,sql] +---- +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. + +=== NULL input + +[source,sql] +---- +SELECT UTL_MATCH.EDIT_DISTANCE(NULL, 'abc') FROM dual; -- -1 +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY(NULL, NULL) FROM dual; -- 100 +---- + +The C entry points are not `STRICT`, so they can handle NULL arguments instead of PostgreSQL returning NULL automatically. + +== 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. diff --git a/EN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc b/EN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc index 8150dedf..0f0441c2 100644 --- a/EN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc +++ b/EN/modules/ROOT/pages/master/oracle_compatibility/builtin_types_functions.adoc @@ -1149,3 +1149,34 @@ ivorysql=# select sys_context('USERENV','DB_NAME'); (1 row) ``` +=== `UTL_MATCH` package + +function: provides Oracle-compatible string matching and similarity computation. It contains four functions — `EDIT_DISTANCE`, `EDIT_DISTANCE_SIMILARITY`, `JARO_WINKLER`, and `JARO_WINKLER_SIMILARITY` — for spell checking, typo correction, and fuzzy matching or deduplication of short text such as names and addresses. Test cases are as follows: + +``` +SELECT UTL_MATCH.EDIT_DISTANCE('shackleford','shackelford'); + edit_distance +--------------- + 2 +(1 row) + +SELECT UTL_MATCH.EDIT_DISTANCE_SIMILARITY('shackleford','shackelford'); + edit_distance_similarity +-------------------------- + 82 +(1 row) + +SELECT ROUND(UTL_MATCH.JARO_WINKLER('shackleford','shackelford'),4); + round +--------- + 0.9818 +(1 row) + +SELECT UTL_MATCH.JARO_WINKLER_SIMILARITY('shackleford','shackelford'); + jaro_winkler_similarity +------------------------- + 98 +(1 row) +``` + +See xref:master/oracle_compatibility/compat_utl_match.adoc[UTL_MATCH package] for details. diff --git a/EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc b/EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc new file mode 100644 index 00000000..36ebcb20 --- /dev/null +++ b/EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_match.adoc @@ -0,0 +1,115 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_MATCH package + +== 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. + +== Feature Description + +=== Functions + +[cols="4,2,4",options="header"] +|=== +|Function |Return type |Description +|`EDIT_DISTANCE(s1, s2)` |`INTEGER` |Minimum number of edits (each insertion, deletion, or substitution of one character counts as 1) needed to transform s1 into s2 +|`EDIT_DISTANCE_SIMILARITY(s1, s2)` |`INTEGER` |Edit-distance-based similarity score, 0–100. 0 means completely different, 100 means identical +|`JARO_WINKLER(s1, s2)` |`BINARY_DOUBLE` |Jaro-Winkler similarity, 0–1 +|`JARO_WINKLER_SIMILARITY(s1, s2)` |`INTEGER` |`JARO_WINKLER` multiplied by 100 and rounded, 0–100 +|=== + +=== Syntax + +[source,sql] +---- +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. + +== Syntax Examples + +`EDIT_DISTANCE_SIMILARITY` produces an integer score from 0 to 100 based on edit distance: + +[source,sql] +---- +-- 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: + +[source,sql] +---- +-- 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: + +[cols="5,2",options="header"] +|=== +|Call |Result +|`EDIT_DISTANCE('shackleford', 'shackelford')` |`2` +|`EDIT_DISTANCE_SIMILARITY('shackleford', 'shackelford')` |`82` +|`JARO_WINKLER('shackleford', 'shackelford')` |`0.9818` +|`JARO_WINKLER_SIMILARITY('shackleford', 'shackelford')` |`98` +|`EDIT_DISTANCE('kitten', 'sitting')` |`3` +|`EDIT_DISTANCE_SIMILARITY('kitten', 'sitting')` |`57` +|`JARO_WINKLER_SIMILARITY('MARTHA', 'MARHTA')` |`96` +|`JARO_WINKLER_SIMILARITY('DWAYNE', 'DUANE')` |`84` +|`JARO_WINKLER_SIMILARITY('DIXON', 'DICKSONX')` |`81` +|`JARO_WINKLER_SIMILARITY('JELLYFISH', 'SMELLYFISH')` |`90` +|=== + +[source,sql] +---- +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; +---- + +== Matching rules + +* Case-sensitive: `EDIT_DISTANCE('abc', 'ABC')` returns `3`, consistent with Oracle. +* Spaces are treated as ordinary characters: `EDIT_DISTANCE('a b', 'ab')` returns `1`. +* 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')` returns `3` (the final multi-byte character differs from `A` in all 3 of its UTF-8 bytes: 1 substitution plus 2 deletions), and `EDIT_DISTANCE_SIMILARITY('中文测试', '中文测A')` returns `75` (the maximum length in the formula is 12 bytes). +* Length limit: the input length limit is 32767 bytes, the same as Oracle's `VARCHAR2` limit. IvorySQL's implementation does not go through the PostgreSQL `fuzzystrmatch` extension, so it is not subject to the 255-byte limit of its `levenshtein` function. + +== NULL and empty strings + +`UTL_MATCH` handles NULL the way Oracle does: it does not propagate NULL but returns a fixed value per function: + +[cols="4,3",options="header"] +|=== +|Function |Return value when an input is NULL +|`EDIT_DISTANCE(s1, s2)` |`-1` +|`EDIT_DISTANCE_SIMILARITY(s1, s2)` |`0` if exactly one input is NULL; `100` if both are NULL +|`JARO_WINKLER(s1, s2)` |`0` +|`JARO_WINKLER_SIMILARITY(s1, s2)` |`0` +|=== + +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`. + +== Implementation notes + +For algorithm details and the internal design, see xref:master/compatibility_features_design/utl_match_impl.adoc[UTL_MATCH implementation design].