--- warning: DO NOT CHANGE THIS MANUALLY, THIS IS GENERATED BY https://github.com/duckdb/community-extensions repository, check README there title: charset excerpt: | DuckDB Community Extensions Character-encoding detection, transcoding & mojibake repair in SQL — chardetng detection, WHATWG/encoding_rs codecs, UTF-8 validity extension: name: charset description: "Character-encoding detection, transcoding & mojibake repair in SQL — chardetng detection, WHATWG/encoding_rs codecs, UTF-8 validity" version: 0.1.0 language: Rust build: cargo license: MIT requires_toolchains: rust maintainers: - alitrack excluded_platforms: "linux_arm64;wasm_mvp;wasm_eh;wasm_threads" repo: github: alitrack/duckdb-charset ref: 09d85c919e0a73081e8131ca1fe96eb00a324f49 docs: hello_world: | LOAD charset; -- Guess the encoding of a blob of windows-1252 "cafe" bytes and read it back SELECT detect_encoding(from_hex('636166e9')) AS detected; -- windows-1252 SELECT to_utf8(from_hex('636166e9')) AS text; -- cafe -- Explicit-label decode (e.g. a Shift_JIS byte stream) SELECT to_utf8_from(from_hex('93fa967b8cea'), 'shift_jis') AS text; -- Encode for export (cafe -> windows-1252 bytes, round-trips losslessly) SELECT to_utf8_from(transcode('cafe', 'windows-1252'), 'windows-1252') AS back; -- Repair garble: "Cafe" with a mojibake e is what you get when -- UTF-8 "Cafe" is misread as 1252 SELECT fix_mojibake('Caf\u00c3\u00a9') AS fixed; -- Validate and enumerate SELECT is_valid_utf8(from_hex('636166e9')); -- false (0xE9 alone is not UTF-8) SELECT count(*) FROM supported_encodings(); -- 40 extended_description: | ## charset — Character-encoding detection & transcoding in SQL Guess the encoding of a `BLOB` of raw bytes, decode it to UTF-8, encode a string back to a target codec, check UTF-8 validity, and repair the classic "UTF-8 misread as Windows-1252" garble. Built on two battle-tested pure-Rust libraries that ship with Firefox: - **`chardetng`** — the heuristic encoding *detector* (a Rust port of the Mozilla chardet-style guesser). - **`encoding_rs`** — the WHATWG Encoding Standard *codec* library (decodes/encodes every web-platform label, with lossy U+FFFD / HTML-ref handling for unmappable input). The engine is ported verbatim from the MIT-licensed [Query-farm `vgi-charset`](https://github.com/Query-farm/vgi-charset) VGI worker; only the Arrow IPC transport layer was replaced with direct DuckDB C-API vector I/O (via [quack-rs](https://github.com/tomtom215/quack-rs)). No VGI, no Arrow IPC, no JSON shims. **8 Functions:** | Function | Signature | Returns | Notes | |----------|-----------|---------|-------| | `detect_encoding` | `(bytes BLOB)` | `VARCHAR` | BOM first, then chardetng. Empty/NULL -> NULL | | `detect_confidence` | `(bytes BLOB)` | `DOUBLE` | `[0,1]` decode-cleanliness proxy. Empty -> 0.0 | | `is_valid_utf8` | `(bytes BLOB)` | `BOOLEAN` | strict `from_utf8` check | | `to_utf8` | `(bytes BLOB)` | `VARCHAR` | decode with *detected* encoding. Undecodable -> U+FFFD | | `to_utf8_from` | `(bytes BLOB, label VARCHAR)` | `VARCHAR` | decode with *explicit* label. Unknown label -> SQL error | | `transcode` | `(text VARCHAR, to_encoding VARCHAR)` | `BLOB` | encode a string to bytes. Unknown label -> SQL error | | `fix_mojibake` | `(text VARCHAR)` | `VARCHAR` | repair UTF-8 -> 1252 double-encoding; no-op if unsure | | `supported_encodings` | `()` | `table(encoding)` | the 40 WHATWG/`encoding_rs` canonical labels | NULL/error policy: `to_utf8` / `to_utf8_from` never error on *dirty* bytes — undecodable bytes become U+FFFD. Only an **unknown encoding label** raises a SQL error. `fix_mojibake` is conservative: it repairs only when the re-decode provably reduces mojibake markers, otherwise it returns the input unchanged. extension_star_count: 0 extension_star_count_pretty: 0 extension_download_count: 37 extension_download_count_pretty: 37 image: '/images/community_extensions/social_preview/preview_community_extension_charset.png' layout: community_extension_doc --- ### Compatibility Matrix This table covers all DuckDB releases that have not reached [end-of-life](https://duckdb.org/release_calendar), 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 ```sql INSTALL {{ page.extension.name }} FROM community; LOAD {{ page.extension.name }}; ``` {% if page.docs.hello_world %} ### Example ```sql {{ page.docs.hello_world }}``` {% endif %} {% if page.docs.extended_description %} ### About {{ page.extension.name }} {{ page.docs.extended_description }} {% endif %} ### Added Functions
| function_name | function_type | description | comment | examples | |---------------------|---------------|-------------|---------|----------| | detect_confidence | scalar | NULL | NULL | | | detect_encoding | scalar | NULL | NULL | | | fix_mojibake | scalar | NULL | NULL | | | is_valid_utf8 | scalar | NULL | NULL | | | supported_encodings | table | NULL | NULL | | | to_utf8 | scalar | NULL | NULL | | | to_utf8_from | scalar | NULL | NULL | | | transcode | scalar | NULL | NULL | | ### 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 |