Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
** xref:7.20.adoc[17、sys_guid 函数]
** xref:7.21.adoc[18、空字符串转null]
** xref:7.22.adoc[19、CALL INTO]
** xref:7.23.adoc[20、UTL_MATCH 包]
* xref:realtime_query.adoc[实时查询分析]
* 容器化与云服务
** 容器化指南
Expand Down
134 changes: 134 additions & 0 deletions CN/modules/ROOT/pages/7.23.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
:sectnums:
:sectnumlevels: 5

:imagesdir: ./_images

= UTL_MATCH 包

== 用途

IvorySQL 提供与 Oracle 兼容的 `UTL_MATCH` 包,用于字符串匹配。它提供四个函数,分别计算编辑距离和 Jaro-Winkler 相似度,其 NULL 与空字符串行为与真实 Oracle 数据库一致(已在 Oracle Database 19c 上验证)。

使用该包前请先设置 Oracle 兼容模式:

```
SET ivorysql.compatible_mode = oracle;
```

== 函数说明

[cols="^1,^2,^3"]
|====
| 函数 | 返回类型 | 说明
| `EDIT_DISTANCE(s1, s2)` | INTEGER | 将 `s1` 转换为 `s2` 所需的最少插入/删除/替换次数。
| `EDIT_DISTANCE_SIMILARITY(s1, s2)` | INTEGER | 0..100 范围的相似度,由编辑距离推导(完全相同为 100)。
| `JARO_WINKLER(s1, s2)` | DOUBLE PRECISION | 0..1 范围的 Jaro-Winkler 相似度(区分大小写)。
| `JARO_WINKLER_SIMILARITY(s1, s2)` | INTEGER | `ROUND(JARO_WINKLER * 100)`,0..100 范围。
|====

== 示例

=== 编辑距离

```
SELECT UTL_MATCH.EDIT_DISTANCE('shackleford','shackelford');
edit_distance
---------------
2
(1 row)

SELECT UTL_MATCH.EDIT_DISTANCE('kitten','sitting');
edit_distance
---------------
3
(1 row)
```

=== Jaro-Winkler 相似度

```
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)
```

经典 Jaro-Winkler 参考值(保留 4 位小数):

```
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)
```

== NULL、空字符串与多字节输入

这些函数有意**不采用**严格的 NULL 传播;每个函数都复现 Oracle 自身的返回结果:

[cols="^1,^2"]
|====
| 函数 | 任一参数为 NULL 时的结果
| `EDIT_DISTANCE` | `-1`
| `EDIT_DISTANCE_SIMILARITY` | `0`(两个参数均为 NULL 时为 `100`)
| `JARO_WINKLER` | `0`
| `JARO_WINKLER_SIMILARITY` | `0`
|====

```
SELECT UTL_MATCH.EDIT_DISTANCE(NULL,'abc');
edit_distance
---------------
-1
(1 row)
```

当 `ivorysql.enable_emptystring_to_NULL` 为 `off` 时,空字符串按算法处理。在真实 Oracle 中 `''` 即 NULL,因此以下结果只有关闭该映射时才可观察到:

```
SET ivorysql.enable_emptystring_to_NULL = off;
SELECT UTL_MATCH.EDIT_DISTANCE('','abc');
edit_distance
---------------
3
(1 row)
```

比较按**字节**而非字符进行。因此将多字节字符 `试`(UTF-8 中为 3 字节)替换为单字节的 `a`,会按 1 次替换加 2 次删除计算:

```
SELECT UTL_MATCH.EDIT_DISTANCE('中文测试','中文测a');
edit_distance
---------------
3
(1 row)

SELECT UTL_MATCH.EDIT_DISTANCE('中文测试','中文测试');
edit_distance
---------------
0
(1 row)
```

回归测试还固定了以下实现细节:半换位按整数截断(`t/2`);Winkler 前缀加权无条件生效(无 0.7 阈值)且最多 4 个字符;Jaro 匹配窗口为 `floor(max(len)/2) - 1`;相似度按四舍五入(half-up)取整。

== 实现与回归测试

C 语言实现位于 `contrib/ivorysql_ora/src/builtin_packages/utl_match/utl_match.c`,同目录下的 `utl_match--1.0.sql` 为 SQL 声明。上述黄金值来自 Oracle PL/SQL Packages and Types Reference,以及在真实 Oracle 19c 实例(AL32UTF8)上验证的结果,并由以下回归用例固定:

* `contrib/ivorysql_ora/sql/utl_match.sql`
* `contrib/ivorysql_ora/expected/utl_match.out`
2 changes: 1 addition & 1 deletion CN/modules/ROOT/pages/9.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ createdb也接受下列命令行参数用于连接参数:

- `--maintenance-db=*dbname*`

指定要连接到来发现哪些其他数据库应该被聚簇的数据库名。如果没有指定,将使用 `postgres` 数据库。而如果它也不存在(或者如果它就是要创建新数据库的名称),将使用 `template1`。这可以是 http://www.postgresql.org/docs/17/libpq-connect.html#LIBPQ-CONNSTRING[connection string]。如果是这样,连接时字符串参数将覆盖所有冲突的命令行选项。
指定在创建新数据库时要连接到的数据库名。如果没有指定,将使用 `postgres` 数据库。而如果它也不存在(或者如果它就是要创建新数据库的名称),将使用 `template1`。这可以是 http://www.postgresql.org/docs/17/libpq-connect.html#LIBPQ-CONNSTRING[connection string]。如果是这样,连接时字符串参数将覆盖所有冲突的命令行选项。

==== 环境

Expand Down
1 change: 1 addition & 0 deletions EN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
** xref:7.20.adoc[17、sys_guid Function]
** xref:7.21.adoc[18、Empty String to NULL]
** xref:7.22.adoc[19、CALL INTO]
** xref:7.23.adoc[20、UTL_MATCH package]
* xref:realtime_query.adoc[Real-time Query Analytics]
* Containerization and Cloud Service
** Containerization
Expand Down
134 changes: 134 additions & 0 deletions EN/modules/ROOT/pages/7.23.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
:sectnums:
:sectnumlevels: 5

:imagesdir: ./_images

= UTL_MATCH package

== 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;
```

== Function descriptions

[cols="^1,^2,^3"]
|====
| Function | Return type | Description
| `EDIT_DISTANCE(s1, s2)` | INTEGER | Minimum number of insert/delete/substitute edits to transform `s1` into `s2`.
| `EDIT_DISTANCE_SIMILARITY(s1, s2)` | INTEGER | Similarity in the 0..100 range, derived from the edit distance (100 for identical strings).
| `JARO_WINKLER(s1, s2)` | DOUBLE PRECISION | Jaro-Winkler similarity in the 0..1 range (case-sensitive).
| `JARO_WINKLER_SIMILARITY(s1, s2)` | INTEGER | `ROUND(JARO_WINKLER * 100)`, in the 0..100 range.
|====

== Examples

=== 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)
```

=== 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)
```

== NULL, empty strings, and multibyte input

The functions deliberately do **not** use strict NULL propagation; each function reproduces Oracle's own result:

[cols="^1,^2"]
|====
| Function | Result when any argument is NULL
| `EDIT_DISTANCE` | `-1`
| `EDIT_DISTANCE_SIMILARITY` | `0` (`100` when both arguments are NULL)
| `JARO_WINKLER` | `0`
| `JARO_WINKLER_SIMILARITY` | `0`
|====

```
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.

== 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`
Loading