# Charset

> Detect text encodings, convert legacy bytes to and from UTF-8, and repair mojibake

This page includes setup, release pins, credentials, hosting and verification evidence. [JSON alternative](https://query.farm/products/orchard/api/v1/connectors/charset.json) contains the same setup facts for structured readers; choose one format.

## Runtime and release

Use DuckDB or Haybarn **2.0.1** with **VGI protocol 3.4**.
Catalog alias: `charset`. Release: [v0.5.0](https://github.com/Query-farm/vgi-charset/releases/tag/v0.5.0). Metadata generated: 2026-12-01T18:22:16+00:00.

## Choose where it runs

- On your machine: use an installation method below. No Query Farm account is required for local execution.
- Self-host: follow [the connector deployment instructions](https://github.com/Query-farm/vgi-charset) and [connection guide](https://query.farm/vgi/connect-to-a-worker/). Use your real endpoint and its authentication settings.
- Query Farm hosted: [create a deployment](https://auth.queryfarm.cloud/console/new?worker=charset). Hosting has a $10 monthly minimum per organization; usage and license fees may apply. Copy the deployment-specific ATTACH from the console.

## Service credentials and prerequisites

No service credentials are declared by this connector. Individual datasets, input files or deployments may have their own prerequisites.

Secret parameter required flags describe the upstream declaration; they do not override the service-level mode above. Keep secrets in environment variables or the deployment secret store. SQL values in angle brackets are placeholders, not automatic environment lookups. Native DuckDB and Haybarn 2.0.1 do not provide a SQL getenv() function, even when worker-supplied snippets mention it. Read environment variables in your application and pass credential values through client parameter bindings (for example, Python con.execute("CREATE SECRET stripe (TYPE stripe, api_key ?)", [os.environ["STRIPE_API_KEY"]]) after attaching the connector. Omit unused optional parameters and keep real values out of saved SQL and logs.

## Install the extension

In stock DuckDB:

```sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
```

In Haybarn:

```sql
INSTALL vgi FROM community;
LOAD vgi;
```

## Attach the connector

The local recipes below pin the catalog release for repeatability. A public demo endpoint follows its deployed release.

### From GitHub

Attach the release directly

Uses the verified release each time you attach the connector.

```sql
ATTACH 'charset' AS charset (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/vgi-charset@v0.5.0'
);
```

### Docker

Attach the signed image

Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.

```sql
ATTACH 'charset' AS charset (
  TYPE vgi,
  LOCATION 'oci://ghcr.io/query-farm/vgi-charset:0.5.0'
);
```

## See what you can query

Service credentials required for this example: not declared by this example; check the connector requirements above.

```sql
WITH raw AS (SELECT id, charset.main.transcode(txt, enc) AS bytes FROM (VALUES (1, 'Grüße aus München', 'windows-1252'), (2, 'Привет, как дела?', 'windows-1251'), (3, 'こんにちは世界', 'shift_jis'), (4, 'naïve café', 'utf-8')) AS t(id, txt, enc)) SELECT id, charset.main.detect_encoding(bytes) AS detected, charset.main.detect_confidence(bytes) AS confidence, charset.main.is_valid_utf8(bytes) AS already_utf8, charset.main.to_utf8(bytes) AS text FROM raw ORDER BY id
```

- [charset.main.transcode — scalar_function](https://query.farm/products/orchard/connectors/charset/objects/scalar_function.bWFpbg.dHJhbnNjb2Rl.md): exact declaration for this example.
- [charset.main.detect_encoding — scalar_function](https://query.farm/products/orchard/connectors/charset/objects/scalar_function.bWFpbg.ZGV0ZWN0X2VuY29kaW5n.md): exact declaration for this example.
- [charset.main.detect_confidence — scalar_function](https://query.farm/products/orchard/connectors/charset/objects/scalar_function.bWFpbg.ZGV0ZWN0X2NvbmZpZGVuY2U.md): exact declaration for this example.
- [charset.main.is_valid_utf8 — scalar_function](https://query.farm/products/orchard/connectors/charset/objects/scalar_function.bWFpbg.aXNfdmFsaWRfdXRmOA.md): exact declaration for this example.
- [charset.main.to_utf8 — scalar_function](https://query.farm/products/orchard/connectors/charset/objects/scalar_function.bWFpbg.dG9fdXRmOA.md): exact declaration for this example.

Execution evidence: **captured_result**.

```json
{
  "status": "captured_result",
  "capturedAt": "2026-12-01T06:12:39+00:00",
  "engineVersion": "2.0.1",
  "protocolVersion": "3.4.0",
  "releaseTag": "v0.5.0",
  "sample": {
    "columns": [
      "id",
      "detected",
      "confidence",
      "already_utf8",
      "text"
    ],
    "types": [
      "INTEGER",
      "VARCHAR",
      "DOUBLE",
      "BOOLEAN",
      "VARCHAR"
    ],
    "rows": [
      [
        1,
        "windows-1252",
        1,
        false,
        "Grüße aus München"
      ],
      [
        2,
        "windows-1251",
        1,
        false,
        "Привет, как дела?"
      ],
      [
        3,
        "Shift_JIS",
        1,
        false,
        "こんにちは世界"
      ],
      [
        4,
        "UTF-8",
        1,
        true,
        "naïve café"
      ]
    ],
    "more": false
  }
}
```

## Connector reference

### charset — detect text encoding and transcode to UTF-8 in SQL

Detect the character encoding of raw bytes and transcode legacy or mojibake-garbled text into clean UTF-8 directly in DuckDB SQL — no Python preprocessing step and no manual `iconv` passes.

## What it does

`charset` is a [VGI](https://query.farm) worker that adds character-encoding detection and UTF-8 transcoding to DuckDB over Apache Arrow. It is built for data engineers and analysts wrangling text of unknown or mixed provenance: CSV and log dumps in legacy Windows code pages, scraped HTML, email archives, and columns that arrived double-encoded — the classic `CafÃ©`-instead-of-`Café` mojibake. Rather than shelling out to external tools, you detect, repair, and normalize encodings inline with ordinary SQL queries.

## How it works

Encoding detection is powered by Mozilla's [`chardetng`](https://github.com/hsivonen/chardetng) ([docs](https://docs.rs/chardetng)), the same heuristic Firefox applies to unlabelled legacy text. Decoding and encoding use [`encoding_rs`](https://github.com/hsivonen/encoding_rs) ([docs](https://docs.rs/encoding_rs)), the pure-Rust implementation of the [WHATWG Encoding Standard](https://encoding.spec.whatwg.org/) that Firefox ships for every web-platform codec. A byte-order-mark (BOM) check runs first; undecodable bytes within a known encoding become the U+FFFD replacement character rather than raising an error, while an unknown encoding *label* is rejected so typos surface immediately.

## When to reach for it

Reach for `charset` whenever a text column's bytes are of unknown, mixed, or legacy provenance: sniff the likely encoding and score how trustworthy that guess is, decode legacy bytes to UTF-8 either automatically or with an explicit codec label, re-encode UTF-8 back into a legacy codec for export, repair double-encoded mojibake, and check whether bytes are already clean UTF-8. Because detection is heuristic, confidence-check short or ambiguous samples before relying on a result. Empty or NULL input yields NULL everywhere.

## Find the exact SQL object

[Browse 8 object declarations](https://query.farm/products/orchard/api/v1/connectors/charset/objects.json) or [search within this connector](https://query.farm/products/orchard/api/v1/search.json?kind=object&connector=charset&q=charset).

Follow one format of each needed object for complete argument types, defaults, named flags, return columns and examples. Existing links above can replace another search. For questions about hosting, credentials or supported operations, stop once the documentation answers the question; SQL and unrelated object lookups are unnecessary.

## Verification and interpretation

- Missing, null, or undeclared metadata means unknown; do not invent defaults, credential scope, platform support or endpoint URLs.
- Search is keyword retrieval, not proof of a capability. Read the exact object declaration before generating SQL.
- A table and a table function can have the same name. Preserve parentheses and use the matching overload and schema.
- Service credentials, external data availability and deployment authentication are separate requirements.
- Catalog verification records the harvest environment. It is not proof that every query or current live API call succeeds. Only an example with captured_result has a saved result for that exact SQL.

```json
{
  "generatedAt": "2026-12-01T18:22:16+00:00",
  "repository": "https://github.com/Query-farm/vgi-charset",
  "harvest": {
    "status": "ok",
    "method": "github-auto",
    "error": null,
    "vgi_version": "9773130dde"
  },
  "catalogVerification": {
    "verified": true,
    "status": "passed",
    "total": 10,
    "passed": 10,
    "failed": 0,
    "needs_credentials": 0,
    "location": "github-auto://Query-farm/vgi-charset@v0.5.0",
    "transport": "github-auto",
    "environment": {
      "vgi_extension_version": "9773130dde",
      "haybarn_version": "2.0.1rc3",
      "engine_version": "v2.0.1",
      "python": "3.12.4",
      "platform": "darwin_arm64",
      "protocol_version": "3.4.0"
    },
    "checked_at": "2026-12-01T06:10:50+00:00"
  },
  "interpretation": "Catalog verification records the harvest environment. It is not proof that every query or current live API call succeeds. Only an example with captured_result has a saved result for that exact SQL."
}
```
