# acdat SQL 参考手册

> acdat v0.1.1 完整 SQL 接口速查。所有类型、函数与托管目录表均位于固定模式 acdat 下，text 与 bytea 输入须与对应类型的自动机配对使用。

---

LLMS 索引： [llms.txt](/zh/llms.txt)

---

## 编译词典 {#build}

| 名称 | 返回类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `acdat.compile(patterns text[], pattern_ids bigint[], replacements text[], priorities integer[], options jsonb)` | `acdat.machine` | `NULL, NULL, NULL, '{}'`¹ | 将一维并行文本数组编译为自动机。省略 ID 时默认按 `1..N` 编号，`NULL` 优先级按 `0` 处理。 |
| `acdat.compile(patterns bytea[], pattern_ids bigint[], replacements bytea[], priorities integer[], options jsonb)` | `acdat.machine` | `NULL, NULL, NULL, '{}'`¹ | 二进制数组重载，完整保留包括 `\x00` 在内的任意字节。 |
| `acdat.compile(text, bigint)` | 聚合 → `acdat.machine` | — | 按行聚合 `(pattern, pattern_id)`；空行输入返回 `NULL`。 |
| `acdat.compile(text, bigint, text, integer)` | 聚合 → `acdat.machine` | — | 按行聚合 `(pattern, pattern_id, replacement, priority)`；空行输入返回 `NULL`。 |
| `acdat.compile(bytea, bigint)` | 聚合 → `acdat.machine` | — | 二进制聚合 `(pattern, pattern_id)`；空行输入返回 `NULL`。 |
| `acdat.compile(bytea, bigint, bytea, integer)` | 聚合 → `acdat.machine` | — | 二进制聚合 `(pattern, pattern_id, replacement, priority)`；空行输入返回 `NULL`。 |
{.fields meta="type default"}

¹ 默认值从 `pattern_ids` 起列出，`patterns` 为必填参数；`options` 接受 `NULL` 或 `{}`。

## 扫描与替换 {#scan}

| 名称 | 返回类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `acdat.contains(text, acdat.machine)` | `boolean` | — | 遇到第一处字面命中即短路返回 `true`。 |
| `acdat.contains(bytea, acdat.machine)` | `boolean` | — | `contains` 的二进制重载。 |
| `acdat.matches(input text, machine acdat.machine, policy text, max_matches integer)` | `setof acdat.hit` | `'all_overlapping', 10000` | 返回每处命中的 1 起算字节与字符偏移。候选上限在重叠消歧前计数。 |
| `acdat.matches(input bytea, machine acdat.machine, policy text, max_matches integer)` | `setof acdat.hit` | `'all_overlapping', 10000` | 二进制重载；`start_char` 与 `char_length` 恒为 `NULL`。 |
| `acdat.replace(input text, machine acdat.machine, policy text, max_replacements integer, max_output_bytes bigint)` | `text` | `'leftmost_longest', 10000, 268435456` | 单趟非递归替换。选中 `NULL` 替换值或超出上限时报错。 |
| `acdat.replace(input bytea, machine acdat.machine, policy text, max_replacements integer, max_output_bytes bigint)` | `bytea` | `'leftmost_longest', 10000, 268435456` | `replace` 的二进制重载。 |
{.fields meta="type default"}

## 重叠策略 {#policies}

| 名称 | 适用函数 | 默认情况 | 消歧顺序 |
| --- | --- | --- | --- |
| `all_overlapping` | `matches` | `matches` 默认 | 枚举全部候选命中，含前缀、后缀及不同 ID 的相同文本。 |
| `leftmost_longest` | `matches` / `replace` | `replace` 默认 | 最左起点 → 更长模式 → 更小 `priority` 数值 → 更小 `pattern_id` → 更小规范序号。 |
| `leftmost_priority` | `matches` / `replace` | — | 最左起点 → 更小 `priority` 数值 → 更长模式 → 更小 `pattern_id` → 更小规范序号。 |
{.fields meta="type default"}

## 检查与跨库传输 {#inspection}

| 名称 | 返回类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `acdat.info(acdat.machine)` | `jsonb` | — | 返回引擎、编译器与格式版本、输入类型、编码、规则数/状态数/槽位数及体积信息。 |
| `acdat.export(acdat.machine)` | `bytea` | — | 将自动机序列化为可移植二进制镜像。 |
| `acdat.validate(bytea)` | `boolean` | — | 校验序列化镜像是否合法（损坏时返回 `false`，不抛错）。 |
| `acdat.import(bytea)` | `acdat.machine` | — | 校验并还原自动机；镜像非法时报错。 |
| `acdat.source_fingerprint(acdat.machine)` | `bytea` | — | 规范规则集与输入类型的 32 字节 SHA-256 指纹。 |
| `acdat.build_fingerprint(acdat.machine)` | `bytea` | — | 包含规则、编译器、引擎与格式版本的 32 字节构建指纹。 |
| `acdat.artifact_checksum(acdat.machine)` | `bytea` | — | 序列化产物的 32 字节完整性校验和。 |
{.fields meta="type default"}

## 托管词典目录 {#catalog}

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `acdat.publish(p_dictionary_name text, p_semantic_version bigint, p_compiled_machine acdat.machine, p_source_reference jsonb)` | `bytea` | `p_source_reference = NULL` | 保存不可变的 `ready` 构建并返回 32 字节词典级 `build_id`。 |
| `acdat.activate(p_dictionary_name text, p_build_id bytea)` | `void` | — | 原子更新 `acdat.active_dictionary` 指向指定 `ready` 构建。 |
| `acdat.retire_build(p_dictionary_name text, p_build_id bytea)` | `void` | — | 将非活跃构建标记为 `retired`。 |
| `acdat.active_machine` | 视图 | — | 当前活跃构建视图：`name`、`input_kind`、`version`、`build_id`、`machine`、`info`。 |
| `acdat.dictionary` | 表 | — | 词典名称与固定 `input_kind`（`text` 或 `bytea`）。 |
| `acdat.dictionary_version` | 表 | — | 语义版本、`source_fingerprint` 及可选 `source_ref` JSON。 |
| `acdat.dictionary_build` | 表 | — | 构建身份、编译器/格式/引擎信息、状态（`ready` / `retired`）与校验和。 |
| `acdat.dictionary_build_data` | 表 | — | 按 `build_id` 存放的编译后 `acdat.machine`（`STORAGE EXTERNAL`）。 |
| `acdat.active_dictionary` | 表 | — | 每个词典当前的活跃 `build_id` 指针。 |
{.fields meta="type default"}

## 类型与内部函数 {#types}

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `acdat.machine` | 基础类型 | — | 变长编译自动机（`STORAGE = external`）。 |
| `acdat.hit` | 复合类型 | — | `(pattern_id bigint, start_byte integer, byte_length integer, start_char integer, char_length integer, priority integer)`。 |
| `machine_in` / `machine_out` / `machine_recv` / `machine_send` | 内部 I/O | — | 已对 `PUBLIC` 撤销权限，由 PostgreSQL 类型系统内部调用。 |
| `_compile_*_trans` / `_compile_final` / `_catalog_build_id` | 内部辅助 | — | 已对 `PUBLIC` 撤销权限，供聚合状态转移与目录摘要计算使用。 |
{.fields meta="type default"}
