duck_pinyin
Chinese pinyin conversion, polyphonic phrase disambiguation, and ordered pinyin matching
Compatibility Matrix
This table covers all DuckDB releases that have not reached end-of-life, including LTS releases, and shows the 2 most recent patch releases of each. Older patch releases are not listed, but the extension may be available for them as well.
| Platform | 1.5.5 | 1.5.6 | 2.0.0 | 2.0.1 |
|---|---|---|---|---|
| Linux AMD64 | ||||
| Linux ARM64 | ||||
| macOS AMD64 | ||||
| macOS ARM64 | ||||
| Windows AMD64 | ||||
| Windows ARM64 | ||||
| WebAssembly |
Installing and Loading
INSTALL duck_pinyin FROM community;
LOAD duck_pinyin;
Example
LOAD duck_pinyin;
SELECT pinyin_char_romanize('我ABC们123');
SELECT pinyin_word_romanize('重庆银行');
SELECT pinyin_matches('wu han huang he lou', 'whh');
About duck_pinyin
Native Chinese pinyin conversion and ordered phrase matching, ported from pg_pinyin with pinned embedded dictionaries. Includes character conversion, longest-phrase polyphonic disambiguation, batch documents, pre-tokenized input, and explicit per-call dictionary overrides.
Query modes include full syllables, initials, autocomplete, automatic, and legacy compatibility parsing. Ordered matching supports a total skipped-token budget. The core extension is independent of PostgreSQL and FTS.
An optional official FTS/BM25 SQL example is provided separately. Its current dynamic-term macro has a documented correlated-query scaling limitation; consult the repository benchmarks before using that example on large tables.
Extension code is MIT licensed. Embedded phrase dictionary data includes CC-BY-SA-4.0 material. Source attribution, pinned data manifests, and complete data license texts are included in the repository and release archives. Community builds register English descriptions, parameter names and SQL examples for each overload in duckdb_functions(), using the native API for the matching DuckDB patch. The portable upstream v0.1.1 archives use the stable V1 C API. The project tests DuckDB 2.0.0 and 2.0.1 on x86-64 and ARM64 for Linux, macOS and Windows.
Added Functions
| function_name | function_type | description | comment | examples |
|---|---|---|---|---|
| pinyin_char_romanize | scalar | Read characters with per-call character overrides. Keys are single characters and values must be non-NULL. | Returns lowercase, toneless syllables separated by spaces. NULL propagates. | [SELECT pinyin_char_romanize('单', MAP {'单': 'shan'})] |
| pinyin_char_romanize | scalar | Read each Han character using its first dictionary pronunciation. | Returns lowercase, toneless syllables separated by spaces. NULL propagates. | [SELECT pinyin_char_romanize('银行')] |
| pinyin_matches | scalar | Allow at most slope skipped syllables in total. A negative slope raises an error. | Match ordered syllables anywhere in a romanized field. NULL propagates. Empty or invalid queries return false. | [SELECT pinyin_matches('chong qing shang ye yin hang', 'chongqingyinhang', 'full', 2)] |
| pinyin_matches | scalar | Match a legacy prefix query with no skipped syllables. | Match ordered syllables anywhere in a romanized field. NULL propagates. Empty or invalid queries return false. | [SELECT pinyin_matches('wu han huang he lou', 'whh')] |
| pinyin_matches | scalar | Match in compat, full, initials, autocomplete or auto mode. | Match ordered syllables anywhere in a romanized field. NULL propagates. Empty or invalid queries return false. | [SELECT pinyin_matches('xi an', 'xi''an', 'full')] |
| pinyin_matches | scalar | Match in the chosen mode with a total skip budget and optional legacy pipe-encoded readings. | Match ordered syllables anywhere in a romanized field. NULL propagates. Empty or invalid queries return false. | [SELECT pinyin_matches('|an|ang| |lv|lu|', 'an lv', 'full', 0, true)] |
| pinyin_phrase | scalar | Build a full-pinyin phrase regex with an explicit zero skip budget. | Returns a DuckDB RE2 expression for contiguous full syllables. Apostrophes mark syllable boundaries. | [SELECT pinyin_phrase('xi''an', 0)] |
| pinyin_phrase | scalar | Build an exact full-pinyin phrase regex. | Returns a DuckDB RE2 expression for contiguous full syllables. Apostrophes mark syllable boundaries. | [SELECT pinyin_phrase('xi''an')] |
| pinyin_regex_phrase | scalar | Build a compat phrase regex for plain or pipe-encoded fields at slope zero. | Produces a DuckDB RE2 expression for contiguous syllables in compat mode. Only slope zero is supported. | [SELECT pinyin_regex_phrase('whh', 0, true)] |
| pinyin_regex_phrase | scalar | Build a compat phrase regex with an explicit zero skip budget. | Produces a DuckDB RE2 expression for contiguous syllables in compat mode. Only slope zero is supported. | [SELECT pinyin_regex_phrase('whh', 0)] |
| pinyin_regex_phrase | scalar | Build a contiguous phrase regex from a legacy prefix query. | Produces a DuckDB RE2 expression for contiguous syllables in compat mode. Only slope zero is supported. | [SELECT pinyin_regex_phrase('whh')] |
| pinyin_regex_phrase_mode | scalar | Build a contiguous phrase regex in the chosen query mode. | Returns a DuckDB RE2 expression. Use pinyin_matches for positive skip budgets. | [SELECT pinyin_regex_phrase_mode('whh', 'initials')] |
| pinyin_regex_phrase_mode | scalar | Build a mode-specific regex for plain or pipe-encoded readings. Slope must be zero. | Returns a DuckDB RE2 expression. Use pinyin_matches for positive skip budgets. | [SELECT pinyin_regex_phrase_mode('an lv', 'full', 0, true)] |
| pinyin_regex_phrase_mode | scalar | Build a phrase regex in the chosen mode with explicit slope zero. | Returns a DuckDB RE2 expression. Use pinyin_matches for positive skip budgets. | [SELECT pinyin_regex_phrase_mode('xi''an', 'full', 0)] |
| pinyin_regex_phrase_patterns | scalar | Parse a legacy prefix query for plain or pipe-encoded pinyin fields. | Returns one regex pattern per syllable. Invalid or empty queries return an empty list. | [SELECT pinyin_regex_phrase_patterns('whh', true)] |
| pinyin_regex_phrase_patterns | scalar | Parse a legacy prefix query into per-syllable patterns. | Returns one regex pattern per syllable. Invalid or empty queries return an empty list. | [SELECT pinyin_regex_phrase_patterns('whh')] |
| pinyin_regex_phrase_patterns_mode | scalar | Parse a query in the chosen mode into individual syllable patterns. | Modes are compat, full, initials, autocomplete and auto. NULL propagates. | [SELECT pinyin_regex_phrase_patterns_mode('xi''an', 'full')] |
| pinyin_regex_phrase_patterns_mode | scalar | Parse in the chosen mode and optionally target legacy pipe-encoded readings. | Modes are compat, full, initials, autocomplete and auto. NULL propagates. | [SELECT pinyin_regex_phrase_patterns_mode('an lv', 'full', true)] |
| pinyin_word_romanize | scalar | Apply character and phrase overrides for this call. A custom phrase wins ties of equal length. | Uses the longest matching phrase and falls back to character readings. NULL propagates. | [SELECT pinyin_word_romanize('重庆', MAP {}::MAP(VARCHAR,VARCHAR), MAP {'重庆': 'zhong qing'})] |
| pinyin_word_romanize | scalar | Resolve polyphonic characters using the embedded phrase dictionary. | Uses the longest matching phrase and falls back to character readings. NULL propagates. | [SELECT pinyin_word_romanize('重庆银行')] |
| pinyin_word_romanize_batch | scalar | Romanize each document independently using longest-phrase readings. | Keeps document order and NULL entries. An empty list returns an empty list. | [SELECT pinyin_word_romanize_batch(['重庆', NULL, '音乐'])] |
| pinyin_word_romanize_tokens | scalar | Romanize the tokens of one document, preserving segmented labels and resolving phrases across Han tokens. | A NULL list returns NULL. A NULL token inside a list raises an error. | [SELECT pinyin_word_romanize_tokens(['重', '庆ABC'])] |
Overloaded Functions
This extension does not add any function overloads.
Added Types
This extension does not add any types.
Added Settings
| name | description | input_type | scope | aliases | typed_value |
|---|---|---|---|---|---|
| temp_directory | Set the directory to which temporary files are written. | VARCHAR | GLOBAL | [] | post.db.tmp |