Skip to content

Units

Convert measurements between units, normalize them to SI, and check that two quantities are comparable

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/units/index.md to help me query Units with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

Normalize measurements to consistent units

Example output · captured
Connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
WITH r AS (SELECT txt, units.main.parse_quantity(txt) AS q FROM (VALUES ('5 km'), ('2 GiB'), ('1 kWh'), ('98.6 °F')) AS t(txt)) SELECT r.txt, units.main.to_base((r.q).value, (r.q).unit) AS base_value, s.base_unit
FROM r
JOIN units.main.supported_units AS s ON s.unit = (r.q).unit
ORDER BY r.txt
Result
txt base_value base_unit
1 kWh 3,600,000 J
2 GiB 2,147,483,648 byte
5 km 5000 m
98.6 °F 310.15 K

Quick start

Choose where to run Units, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.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;

2. Attach the connector

Attach the release directly

Downloads the latest release for your platform and caches it locally.

ATTACH 'units' AS units (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/vgi-units'
);

3. Run a query

query.sql
WITH r AS (SELECT txt, units.main.parse_quantity(txt) AS q FROM (VALUES ('5 km'), ('2 GiB'), ('1 kWh'), ('98.6 °F')) AS t(txt)) SELECT r.txt, units.main.to_base((r.q).value, (r.q).unit) AS base_value, s.base_unit
FROM r
JOIN units.main.supported_units AS s ON s.unit = (r.q).unit
ORDER BY r.txt
More example queries →
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach the release directly

Uses the verified release each time you attach the connector.

ATTACH 'units' AS units (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/[email protected]'
);

Overview

Runtime, string-driven physical-unit conversion and dimensional analysis.

Catalog

Everything units exposes once attached — read straight from the worker's released build.

units 1 schema
main 2 tables · 7 functions

Unit-conversion and dimensional-analysis functions.

Schema examples (12)
Examples (12)

Convert a marathon distance from miles to kilometres.

SELECT units.main.convert(26.2, 'mi', 'km') AS km;

Reduce 1 GiB to bytes via the SI base unit.

SELECT units.main.to_base(1, 'GiB') AS bytes;

Look up the physical dimension of a unit.

SELECT units.main.dimension('kWh') AS dim;

Check whether two units can be converted between.

SELECT units.main.compatible('mi', 'km') AS ok;
8 more examples

Parse a free-text quantity string into a (value, unit) struct.

SELECT units.main.parse_quantity('5 km') AS q;

List the recognized units in the length dimension.

SELECT unit, base_unit
FROM units.main.supported_units
WHERE dimension = 'length'
ORDER BY unit;

Parse free-text readings, group them by physical dimension, and total each group in its SI base unit; '7 furlongs' is not a recognized unit, so it parses to NULL and is skipped.

WITH r AS (SELECT units.main.parse_quantity(txt) AS q FROM (VALUES ('5 km'), ('3 mi'), ('800 m'), ('2 kg'), ('12 lb'), ('7 furlongs')) AS t(txt)) SELECT units.main.dimension((q).unit) AS dim, count(*) AS readings, round(sum(units.main.to_base((q).value, (q).unit)), 4) AS total_si
FROM r
WHERE q IS NOT NULL
GROUP BY dim
ORDER BY dim;

Normalize mixed free-text readings to SI and label each with its base unit by joining the parsed unit to supported_units (e.g. '98.6 °F' becomes 310.15 K).

WITH r AS (SELECT txt, units.main.parse_quantity(txt) AS q FROM (VALUES ('5 km'), ('2 GiB'), ('1 kWh'), ('98.6 °F')) AS t(txt)) SELECT r.txt, units.main.to_base((r.q).value, (r.q).unit) AS base_value, s.base_unit
FROM r
JOIN units.main.supported_units AS s ON s.unit = (r.q).unit
ORDER BY r.txt;
Result
txt base_value base_unit
1 kWh 3,600,000 J
2 GiB 2,147,483,648 byte
5 km 5000 m
98.6 °F 310.15 K

Express one mile in every length unit the worker recognizes, largest number first.

SELECT unit, units.main.convert(1, 'mi', unit) AS one_mile
FROM units.main.supported_units
WHERE dimension = 'length'
ORDER BY one_mile DESC, unit;

Guard a conversion: convert each row to kilograms only when its unit is compatible with kg, so the length row ('m') yields NULL instead of an incompatible-dimension error.

SELECT id, qty, unit,
  CASE WHEN units.main.compatible(unit, 'kg') THEN units.main.convert(qty, unit, 'kg') END AS kg
FROM (VALUES (1, 150.0, 'lb'), (2, 2.5, 'kg'), (3, 3.0, 'm'), (4, 16.0, 'oz')) AS t(id, qty, unit)
ORDER BY id;

Report each reactor's final temperature in °F with convert_strict and its rise in °F with convert_difference_strict: the 15 °C rise of reactor_a is 27 °F, not the 59 °F an absolute conversion would give.

SELECT sensor,
  round(units.main.convert_strict(end_c, '°C', '°F'), 2) AS end_f,
  round(units.main.convert_difference_strict(end_c - start_c, '°C', '°F'), 2) AS rise_f
FROM (VALUES ('reactor_a', 20.0, 35.0), ('reactor_b', 18.5, 21.0)) AS t(sensor, start_c, end_c)
ORDER BY sensor;

Audit strict temperature conversions: convert 100 of each unit with convert_strict and recompute the same result from the factor and offset published in unit_catalog ((value * f.factor + f.offset - t.offset) / t.factor).

SELECT f.unit AS from_unit, t.unit AS to_unit,
  round(units.main.convert_strict(100, f.unit, t.unit), 6) AS converted,
  round((100 * f.factor + f."offset" - t."offset") / t.factor, 6) AS recomputed
FROM units.main.unit_catalog AS f
JOIN units.main.unit_catalog AS t ON t.dimension = f.dimension AND t.unit <> f.unit
WHERE f.unit IN ('°C', '°F', 'K') AND t.unit IN ('°C', '°F', 'K')
ORDER BY from_unit, to_unit;
Tables
supported_units 9 examples3 cols

Every unit string the worker recognizes, with its physical dimension and SI base unit.

Columns
unitnot null VARCHAR
The recognized unit string, e.g. 'km', 'kWh', '°C'. Valid as the unit argument to convert, to_base, dimension, compatible, and parse_quantity.
dimensionnot null VARCHAR
The physical dimension the unit measures, as a lowercase name such as 'length', 'mass', 'time', 'energy', or 'data'.
base_unitnot null VARCHAR
The SI base unit of the dimension that all conversions in that dimension pass through, e.g. 'm' for length or 'kg' for mass.
ConstraintsUNIQUE (unit) · PRIMARY KEY (unit)
Examples (9)

List the length units and their SI base unit.

SELECT unit, base_unit
FROM units.main.supported_units
WHERE dimension = 'length'
ORDER BY unit;

Count how many units exist in each dimension.

SELECT dimension, count(*) AS n
FROM units.main.supported_units
GROUP BY dimension
ORDER BY n DESC, dimension;

Express one mile in every length unit the worker recognizes, largest number first.

SELECT unit, units.main.convert(1, 'mi', unit) AS one_mile
FROM units.main.supported_units
WHERE dimension = 'length'
ORDER BY one_mile DESC, unit;
6 more examples
SELECT units.main.convert(26.2, 'mi', 'km') AS km;
SELECT units.main.to_base(1, 'GiB') AS bytes;
SELECT units.main.dimension('kWh') AS dim;
SELECT units.main.compatible('mi', 'km') AS ok;
SELECT units.main.parse_quantity('5 km') AS q;
SELECT unit, base_unit
FROM units.main.supported_units()
WHERE dimension = 'length'
ORDER BY unit
LIMIT 5;
Link
unit_catalog 5 examples8 cols

Authoritative, versioned unit definitions (factor and offset per exact unit string) behind the strict conversion functions.

Columns
unitnot null VARCHAR
The exact recognized unit string, e.g. 'km', 'MB', '°C'. Case-significant.
dimensionnot null VARCHAR
The physical dimension the unit measures, e.g. 'length' or 'temperature'.
base_unitnot null VARCHAR
The SI base unit of the dimension that conversions pass through, e.g. 'm'.
quantity_spacenot null VARCHAR
'affine' for temperature units (offsets apply to absolute values and cancel for differences); 'linear' for every other dimension.
factornot null DOUBLE
Scale factor to the base unit: base = value * factor + offset. Finite and positive.
offsetnot null DOUBLE
Additive offset to the base unit (non-zero only for affine temperature scales); ignored when converting differences.
implementation_versionnot null VARCHAR
The worker software version that serves these definitions.
contract_versionnot null VARCHAR
The strict-conversion contract version these definitions satisfy.
ConstraintsUNIQUE (unit) · PRIMARY KEY (unit)
Examples (5)

Show the factor and offset that define each temperature unit.

SELECT unit, factor, "offset"
FROM units.main.unit_catalog
WHERE dimension = 'temperature'
ORDER BY unit;

Report the worker and contract versions the definitions belong to.

SELECT DISTINCT implementation_version, contract_version
FROM units.main.unit_catalog
ORDER BY ALL;

Audit strict temperature conversions: convert 100 of each unit with convert_strict and recompute the same result from the factor and offset published in unit_catalog ((value * f.factor + f.offset - t.offset) / t.factor).

SELECT f.unit AS from_unit, t.unit AS to_unit,
  round(units.main.convert_strict(100, f.unit, t.unit), 6) AS converted,
  round((100 * f.factor + f."offset" - t."offset") / t.factor, 6) AS recomputed
FROM units.main.unit_catalog AS f
JOIN units.main.unit_catalog AS t ON t.dimension = f.dimension AND t.unit <> f.unit
WHERE f.unit IN ('°C', '°F', 'K') AND t.unit IN ('°C', '°F', 'K')
ORDER BY from_unit, to_unit;
2 more examples
SELECT unit, factor, "offset"
FROM units.main.unit_catalog()
WHERE dimension = 'temperature'
ORDER BY unit;
SELECT DISTINCT implementation_version, contract_version
FROM units.main.unit_catalog();
Link
Scalar functions
compatible (unit_a VARCHAR, unit_b VARCHAR) 2 examples

Whether two units share a dimension (i.e. can be converted between). Unknown units are never compatible

Arguments
unit_a VARCHAR
The first unit to compare, e.g. 'mi'. The result is whether it shares a dimension with unit_b (and is therefore convertible).
unit_b VARCHAR
The second unit to compare, e.g. 'km'. Compared against unit_a to test dimensional compatibility.
ReturnsBOOLEAN
Examples (2)

Check whether two units can be converted between (true).

SELECT units.main.compatible('mi', 'km') AS ok;

Guard a conversion: convert each row to kilograms only when its unit is compatible with kg, so the length row ('m') yields NULL instead of an incompatible-dimension error.

SELECT id, qty, unit,
  CASE WHEN units.main.compatible(unit, 'kg') THEN units.main.convert(qty, unit, 'kg') END AS kg
FROM (VALUES (1, 150.0, 'lb'), (2, 2.5, 'kg'), (3, 3.0, 'm'), (4, 16.0, 'oz')) AS t(id, qty, unit)
ORDER BY id;
Link
convert (value ANY, from_unit VARCHAR, to_unit VARCHAR) 2 examples

Convert a value between two units of the same dimension (NULL if a unit is unknown; ERROR if the dimensions are incompatible)

Arguments
value ANY
The quantity to convert, expressed in the source unit (e.g. 26.2 for a marathon in miles).
from_unit VARCHAR
The unit the value is currently in, e.g. 'mi'. Must be a unit string the worker recognizes (see supported_units); an unknown unit yields NULL.
to_unit VARCHAR
The unit to convert the value into, e.g. 'km'. Must share a dimension with from_unit, otherwise the call errors.
ReturnsDOUBLE
Examples (2)

Convert a marathon distance from miles to kilometres.

SELECT units.main.convert(26.2, 'mi', 'km') AS km;

Convert a body temperature from Celsius to Fahrenheit (affine, offset-aware).

SELECT units.main.convert(37, '°C', '°F') AS fahrenheit;
Link
convert_difference_strict (value DOUBLE, from_unit VARCHAR, to_unit VARCHAR) 3 examples

Convert a finite quantity difference using scale factors without offsets; reject invalid units, nonfinite values and overflow; preserve valid NULL values

Arguments
value DOUBLE
The finite quantity to convert, in the source unit. NULL yields NULL (after the units are validated); NaN or infinity raises an error.
from_unit VARCHAR
The unit the value is in, e.g. 'mi'. Must be a recognized unit string (see supported_units or unit_catalog); unknown or NULL raises an error.
to_unit VARCHAR
The unit to convert into, e.g. 'km'. Must be recognized and share a dimension with from_unit; otherwise the call raises an error.
ReturnsDOUBLE
Examples (3)

Convert a 10 degree Celsius temperature rise into Fahrenheit degrees.

SELECT units.main.convert_difference_strict(10, '°C', '°F') AS delta_f;

Convert a 5 km difference in distance into miles.

SELECT units.main.convert_difference_strict(5, 'km', 'mi') AS delta_mi;

Report each reactor's final temperature in °F with convert_strict and its rise in °F with convert_difference_strict: the 15 °C rise of reactor_a is 27 °F, not the 59 °F an absolute conversion would give.

SELECT sensor,
  round(units.main.convert_strict(end_c, '°C', '°F'), 2) AS end_f,
  round(units.main.convert_difference_strict(end_c - start_c, '°C', '°F'), 2) AS rise_f
FROM (VALUES ('reactor_a', 20.0, 35.0), ('reactor_b', 18.5, 21.0)) AS t(sensor, start_c, end_c)
ORDER BY sensor;
Link
convert_strict (value DOUBLE, from_unit VARCHAR, to_unit VARCHAR) 2 examples

Convert a finite absolute quantity using scale factors and offsets; reject invalid units, nonfinite values and overflow; preserve valid NULL values

Arguments
value DOUBLE
The finite quantity to convert, in the source unit. NULL yields NULL (after the units are validated); NaN or infinity raises an error.
from_unit VARCHAR
The unit the value is in, e.g. 'mi'. Must be a recognized unit string (see supported_units or unit_catalog); unknown or NULL raises an error.
to_unit VARCHAR
The unit to convert into, e.g. 'km'. Must be recognized and share a dimension with from_unit; otherwise the call raises an error.
ReturnsDOUBLE
Examples (2)

Strictly convert a marathon distance from miles to kilometres.

SELECT units.main.convert_strict(26.2, 'mi', 'km') AS km;

Strictly convert the freezing point of water from Celsius to Fahrenheit (offset-aware).

SELECT units.main.convert_strict(0, '°C', '°F') AS fahrenheit;
Link
dimension (unit VARCHAR) 2 examples

Return the physical dimension of a unit ('length'|'mass'|'time'|…), or NULL if the unit is unknown

Arguments
unit VARCHAR
The unit to classify, e.g. 'mi'. Returns the unit's physical dimension; an unrecognized unit yields NULL.
ReturnsVARCHAR
Examples (2)

Identify the physical dimension of a unit (energy).

SELECT units.main.dimension('kWh') AS dim;

Group a column of unit strings by the dimension they measure.

SELECT units.main.dimension(u) AS dim, count(*) AS n
FROM (VALUES ('mi'), ('km'), ('kg'), ('lb')) AS t(u)
GROUP BY dim
ORDER BY dim;
Link
parse_quantity (text VARCHAR) 3 examples

Parse quantity text such as the string 5 km into a STRUCT(value DOUBLE, unit VARCHAR); NULL if unparseable or the unit is unknown

Arguments
text VARCHAR
Free-form quantity text: a leading number immediately or space-separated from a recognized unit — for instance the string `5 km`, parsed into value 5 and unit `km`. The result is a NULL struct when the text has no number or the unit is unrecognized.
ReturnsSTRUCT("value" DOUBLE, unit VARCHAR)
Examples (3)

Parse a quantity string into a (value, unit) struct.

SELECT units.main.parse_quantity('5 km') AS q;

Parse a free-text quantity and convert it to another unit in one query.

WITH q AS (SELECT units.main.parse_quantity('26.2 mi') AS p) SELECT units.main.convert((p).value, (p).unit, 'km') AS km
FROM q;

Parse free-text readings, group them by physical dimension, and total each group in its SI base unit; '7 furlongs' is not a recognized unit, so it parses to NULL and is skipped.

WITH r AS (SELECT units.main.parse_quantity(txt) AS q FROM (VALUES ('5 km'), ('3 mi'), ('800 m'), ('2 kg'), ('12 lb'), ('7 furlongs')) AS t(txt)) SELECT units.main.dimension((q).unit) AS dim, count(*) AS readings, round(sum(units.main.to_base((q).value, (q).unit)), 4) AS total_si
FROM r
WHERE q IS NOT NULL
GROUP BY dim
ORDER BY dim;
Link
to_base (value ANY, unit VARCHAR) 3 examples

Express a value in the SI base unit of its dimension (NULL if the unit is unknown)

Arguments
value ANY
The quantity to express in its SI base unit, given in the unit named by `unit` (e.g. 100 for 100 centimetres).
unit VARCHAR
The unit the value is given in, e.g. 'GiB'. Must be recognized by the worker; an unknown unit yields NULL.
ReturnsDOUBLE
Examples (3)

Express 100 centimetres in the SI base unit (metres).

SELECT units.main.to_base(100, 'cm') AS metres;

Reduce 1 GiB to bytes so mixed data sizes can be summed on a common base.

SELECT units.main.to_base(1, 'GiB') AS bytes;

Normalize mixed free-text readings to SI and label each with its base unit by joining the parsed unit to supported_units (e.g. '98.6 °F' becomes 310.15 K).

WITH r AS (SELECT txt, units.main.parse_quantity(txt) AS q FROM (VALUES ('5 km'), ('2 GiB'), ('1 kWh'), ('98.6 °F')) AS t(txt)) SELECT r.txt, units.main.to_base((r.q).value, (r.q).unit) AS base_value, s.base_unit
FROM r
JOIN units.main.supported_units AS s ON s.unit = (r.q).unit
ORDER BY r.txt;
Result
txt base_value base_unit
1 kWh 3,600,000 J
2 GiB 2,147,483,648 byte
5 km 5000 m
98.6 °F 310.15 K
Link

Try these queries

Convert a marathon distance from miles to kilometres.

SELECT units.main.convert(26.2, 'mi', 'km') AS km

Uses convert

Reduce 1 GiB to bytes via the SI base unit.

SELECT units.main.to_base(1, 'GiB') AS bytes

Uses to_base

Look up the physical dimension of a unit.

SELECT units.main.dimension('kWh') AS dim

Uses dimension

Check whether two units can be converted between.

SELECT units.main.compatible('mi', 'km') AS ok

Uses compatible

Parse a free-text quantity string into a (value, unit) struct.

SELECT units.main.parse_quantity('5 km') AS q

Uses parse_quantity

List the recognized units in the length dimension.

SELECT unit, base_unit
FROM units.main.supported_units
WHERE dimension = 'length'
ORDER BY unit

Uses supported_units

19 more examples in the Catalog, each listed under the table or function it uses.

Units is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.