ogc-cql2
v1.7.1
Published
OGC CQL2 1.0.0 (OGC 21-065r2) for JavaScript and TypeScript: validation, CQL2-text (written and read), in-memory evaluation and SQL (SQLite / GeoPackage, PostgreSQL / PostGIS, DuckDB / GeoParquet, Oracle, SQL Server) for CQL2-JSON filters, in browsers and
Maintainers
Readme
ogc-cql2
OGC CQL2 1.0.0, the Common Query Language (OGC 21-065r2), for JavaScript and TypeScript. It works on filters in the standard's JSON encoding, CQL2-JSON:
ogc-cql2/validatechecks a filter, with a code, a location and a reason for each problem;ogc-cql2/textwrites it as CQL2-text, for thefilterparameter of OGC API Features Part 3;ogc-cql2/parsereads CQL2-text into CQL2-JSON, for a server that receives it;ogc-cql2/evaluatetests features against it in memory, with CQL2's three-valued logic, so it selects the same features a CQL2 server would;ogc-cql2/sqlturns it into a parameterizedWHEREclause for SQLite / GeoPackage, PostgreSQL / PostGIS, DuckDB (GeoParquet and other files, in Node and in the browser), Oracle Database (Oracle Spatial) or SQL Server, that selects exactly whatevaluateselects. It builds the SQL; your own driver runs it.
It runs in browsers and in Node 22 or later and has no dependencies. Each part is its own entry point, so an application loads only what it uses.
Developer guide: how to use each part, task by task, with every rule in detail.
npm install ogc-cql2What it covers
The package implements these conformance classes:
| Conformance class | Operators | validate, text, parse | evaluate | sql |
|---|---|---|---|---|
| Basic CQL2 | = <> < <= > >=, isNull, and or not, true / false, { "date" } and { "timestamp" } literals | ✅ | ✅ | ✅ |
| Advanced Comparison Operators | like, between, in | ✅ | ✅ | ✅ |
| Case-insensitive Comparison | casei | ✅ | ✅ | ✅ |
| Accent-insensitive Comparison | accenti | ✅ | ✅ | ✅ |
| Basic Spatial Functions | s_intersects with a point or a bbox | ✅ | ✅ | ✅ |
| Basic Spatial Functions with additional Spatial Literals | s_intersects with any geometry | ✅ | ✅ | ✅ |
| Spatial Functions | s_intersects s_equals s_disjoint s_touches s_within s_overlaps s_crosses s_contains | ✅ | ✅ with an engine plugged in | ✅ PostgreSQL, DuckDB, Oracle; SQLite with SpatiaLite; not SQL Server |
| Property-Property Comparisons | a property or a literal on either side: comparisons, like (a string on the left), between, in, the spatial functions | ✅ | ✅ | ✅ |
| Arithmetic Expressions | + - * / % div ^ wherever a number goes | ✅ | ✅ | ✅ |
| Temporal Functions | t_after t_before t_disjoint t_equals t_intersects t_contains t_during t_finishedBy t_finishes t_meets t_metBy t_overlappedBy t_overlaps t_startedBy t_starts, { "interval" } literals | ✅ | ✅ | ✅ |
| Array Functions | a_equals a_contains a_containedBy a_overlaps, array literals | ✅ | ✅ | ✅ |
- Spatial:
evaluatetestss_intersectsin memory, and the Spatial Functions class (s_disjointand the other relations) with a geometry engine you plug in (GEOS in WebAssembly, for one);sqltranslates them for PostGIS, DuckDB (with its spatial extension), SQLite / GeoPackage (s_intersectswith the package's own functions, the Spatial Functions class with SpatiaLite), Oracle Spatial (every relation, with a documented tolerance), and SQL Server (s_intersectsonly). - Property-Property Comparisons: either side may be a property or a literal (
name = nameascii,'København' < name,S_WITHIN(BBOX(7,50,8,51), geom)). Arithmetic:area > population * 0.001. Temporal:T_DURING(INTERVAL(start, end), INTERVAL('2023-01-01', '2023-12-31')). - Arrays:
A_CONTAINS(tags, ('a', 'b')), compared as sets. The arrays can be held natively (PostgreSQL arrays, DuckDB lists) or as JSON (PostgreSQLjsonb, GeoPackage text as GDAL writes list fields, Oracle, SQL Server) (guide). - Each engine says what it supports:
evaluateConformance(options)andsqlConformance(options)return the classes, as conformance URIs, to pass tovalidate; a filter valid against them is never refused asunsupported(toSql'sliteralsin another SRID aside:sqlReprojectssays where). - Anything else that is valid CQL2 (custom functions) is refused as
outside-profile, with the conformance class it would need.
The parts at a glance
A filter is a plain CQL2-JSON value (in TypeScript, Cql2Filter, from ogc-cql2):
const filter = {
op: "and",
args: [
{ op: ">", args: [{ property: "population" }, 1000000] },
{ op: "like", args: [{ op: "casei", args: [{ property: "name" }] }, { op: "casei", args: ["s%"] }] },
],
};Validate it, against a server's conformance classes and a collection's queryables (guide):
import { validate } from "ogc-cql2/validate";
validate(filter, { conformance: conformsTo, queryables: properties });
// { valid: true, filter }, or { valid: false, problems: [{ code, path, message }, …] }Write and read CQL2-text (guide):
import { toText } from "ogc-cql2/text";
import { fromText } from "ogc-cql2/parse";
toText(filter); // "(population > 1000000) AND (CASEI(name) LIKE CASEI('s%'))"
fromText("population > 1000000 AND CASEI(name) LIKE CASEI('s%')"); // the same filter, as CQL2-JSONFilter features in memory, as a CQL2 server would (guide):
import { evaluateConformance, toPredicate } from "ogc-cql2/evaluate";
const selected = features.filter(toPredicate(filter));
evaluateConformance(); // the classes it supports: all but Spatial Functions, without a geometry engineTurn it into SQL that selects exactly what evaluate selects (guide):
import { toSql } from "ogc-cql2/sql";
const columns = { name: { type: "string" }, population: { type: "integer" } }; // the table's queryables
const { where, params } = toSql(filter, { dialect: "sqlite", columns });
// run `SELECT … WHERE ${where}` with params, with your own driver| Database | Dialect | What you get |
|---|---|---|
| SQLite, GeoPackage | sqlite | ? placeholders, GeoPackage's types, its R-tree for spatial filters; Spatial Functions with SpatiaLite; arrays as JSON text (guide) |
| PostgreSQL, PostGIS | postgres | $1 placeholders, code point order, PostGIS's GiST index, exact on arm64 too, any SRID, literals in another CRS (filter-crs); native and jsonb arrays, GIN-indexable (guide) |
| DuckDB, GeoParquet | duckdb | the same SQL in Node and in the browser (DuckDB-WASM), straight on files, GeoParquet's bbox covering; lists as arrays (guide) |
| Oracle Database 23ai+, Oracle Spatial | oracle | :1 placeholders, code point order whatever the session's NLS settings, every spatial relation planar with a planar index; arrays as JSON (guide) |
| SQL Server 2022+ | sqlserver | @p1 placeholders, code point order without blank padding, S_INTERSECTS on geometry with the spatial index; arrays as JSON; no Spatial Functions (guide) |
Every refusal is a Cql2Error with a stable code (guide).
Playground and demo server
In a clone of the repository:
npm run demoopens a page where you type CQL2-JSON and see the problems, the conformance classes (ticked as the chosen source supports them, fromevaluateConformanceandsqlConformance), the CQL2-text and which sample features match. Under "Data", the demo's two GeoParquet files can also be filtered in the page itself, by DuckDB-WASM in a Web Worker, with no server (the engine, about 8 MB compressed, and its spatial extension download when one is chosen).demo-server/is a small Node backend (Express) that answers the same filters from data files, so the page can query a server instead of filtering in the browser. Start it withnpm install && npm startindemo-server/(it listens on port 3100;PORT=… npm startfor another), keep that terminal open, runnpm run demoin a second one, and choose a server collection under "Data" in the page. For a server on another port, start the page withDEMO_SERVER=http://localhost:… npm run demo.
The server's collections are listed in demo-server/collections.json. A collection is either a GeoJSON
FeatureCollection file, filtered in memory with ogc-cql2/evaluate, or a table of a GeoPackage (OGC's SQLite
format), filtered in the database with ogc-cql2/sql: one entry serves every table the GeoPackage lists in
gpkg_contents. A GeoParquet (or Parquet) file is read in place by DuckDB (@duckdb/node-api), filtered with the
duckdb dialect, its bbox covering used when the file has one. A PostgreSQL entry serves the tables it lists, from the database in demo-server/.env
(copy .env_sample), filtered with the postgres dialect; Oracle and SQL Server entries likewise, with the
oracle and sqlserver dialects (a database left out of .env is reported, and its collections aren't served). A table's
JSON columns that hold arrays are listed under "arrays", with their elements' type. The samples: the states, the
census sub-regions, a "kitchen sink" of every type and edge case, and an arrays table of the values where engines
disagree, the same rows in every source (demo-server/data/README.md says how to load
them into PostgreSQL, Oracle and SQL Server). Its routes:
| Route | Answer |
|---|---|
| GET /collections | the collections |
| GET /collections/{id}/queryables | the properties and their types (a JSON Schema) |
| POST /collections/{id}/query with { "filter": …, "limit": 50, "offset": 0 } | the matching features, as GeoJSON |
| … with "filter-lang": "cql2-text" and the filter as a CQL2-text string | the same, read with ogc-cql2/parse |
| … with "filter-crs": EPSG:4326 (latitude first) on the SQL stores, EPSG:3857 on PostgreSQL | the same, its geometries read in that CRS |
An invalid filter, or one naming a property the collection doesn't have, is refused with HTTP 400 and the problems as
application/problem+json. Neither the page nor the server is part of the npm package.
Versioning and stability
Since 1.0.0 the package follows semantic versioning: a breaking change comes only in a major version, and something is removed only after a minor version has deprecated it.
Stable:
- the entry points and the signatures of their functions;
- the options objects, which only gain optional keys;
Cql2Error(name,code,path,details), the meaning of each code,details.problemsanddetails.position;toSql's result (where,params) and each dialect's placeholders;- the
SpatialEngineinterface; CQL2_CONFORMANCE(all of CQL2 1.0's filter classes, fixed as the standard is) and each key ofCONFORMANCE.
Added in minor versions, as the package grows:
- filter shapes the standard allows, as a class is implemented: new members of
Cql2Filter, wider arguments (a property on both sides of a comparison, for example); - new codes in
Cql2ErrorCode,ProblemCodeandTextProblemCode; - new keys in
CONFORMANCE; - new SQL dialects (
SqlDialect), keys inSqlColumnand the options, and helpers such assqliteFunctions.
So code that reads filters, codes or dialects keeps a default case for what it doesn't know. And validate's default
conformance (every implemented class) accepts a newly implemented class after an upgrade: a server passes the
classes it declares (see the guide's Validating).
Not covered: messages (test the codes), the SQL text itself (what it selects is covered), and anything the entry points don't export. Nor is accepting what the standard doesn't allow: when the package accepts input the standard refuses, or refuses input it allows, a patch version fixes that, as 1.6.1 and 1.6.2 did (below).
Upgrading to 1.6.1 and 1.6.2
Both come from a review of the package against the standard's latest text (OGC 21-065r2, its grammar and JSON schema, the 1.0.1 draft, and its open issues), and from users' reports. Where the package read more, or less, than the standard allows, it now follows the standard. What may need action on your side:
| Since | What changed | What to do |
|---|---|---|
| 1.6.1 | fromText refuses parentheses the grammar doesn't have. CQL2 puts them only around a boolean or an arithmetic expression, so x = ('a'), x = (TRUE), x = (DATE('2020-01-01')), S_INTERSECTS(g, (POINT(1 2))) and (name) LIKE 'a%' are now invalid-text | Remove those parentheses. toText never wrote them, so text it produced is unaffected |
| 1.6.1 | A list in parentheses outside the array functions (x = ('a', 'b'), x = ()) is invalid-text | Use IN (…), or an array function |
| 1.6.1 | Inside an array, ( always opens a nested array: A_EQUALS(tags, (('a'))) is an array holding ('a'), which the profile refuses (invalid-filter); it used to be read as ('a') | Write ('a') |
| 1.6.1 | interval is a property name where no ( follows (INTERVAL isn't in the standard's list of keywords); lowercase t and z are read in text timestamps | Nothing: more valid text is read |
| 1.6.2 | SQL Server: text with trailing U+0000 is no longer equal to the same text without it ('a' ≠ 'a\u0000') | Nothing, unless you relied on that equality |
| 1.6.2 | A leap second, TIMESTAMP('…T23:59:60Z'), is accepted and compared as …:59.999, in literals, evaluate's data and the dialects' reading of text | Run postgresFunctions, oracleFunctions or sqlserverFunctions again, so cql2_instant reads it too |
| 1.6.2 | validate's message for an array element of the wrong kind reads "tags" holds strings, and this is a number. | Nothing, unless you match on messages (test the codes) |
Decided against, and why. Requests and readings considered and not adopted; each can be raised again, and will be weighed again then:
- Refusing a decimal against an
integerqueryable (storeys = 1.5). CQL2 has one number type, and a queryable's type describes the property's values, not the filter's literals:storeys > 1.5is a meaningful filter. Refusing it would be stricter than the standard. A UI that wants whole numbers checks its own inputs. - Converting between dates and timestamps in an interval (
INTERVAL('2020-01-01', '2020-01-02T12:00:00Z')). The standard lets an implementation either raise an error or cast (Permission 2, §7.8.3); the package keeps the error (type-mismatch) rather than guess a conversion. A cast could come later as an explicit option, never as the default. MULTIPOINT(1 2, 3 4)without each point's parentheses. The grammar requires them (MULTIPOINT((1 2), (3 4))), as OGC's WKT grammar does; the form appears only in the standard's test data files, not in filters.
Tested versions
"Tested" means the differential tests (the same answers as evaluate, filter by filter) ran on that exact version.
Older versions may well work, but haven't been tested.
| Component | Tested | Notes |
|---|---|---|
| Node.js | 22.23.4, 24.15.0 | 22 or later. node:sqlite needs no flag from Node 22.13 |
| Browsers | Chrome 156 | the DuckDB-WASM checks; fromText on the standard's text examples |
| PostgreSQL + PostGIS (GEOS) | 13 / 3.1.4 (3.9.0), 15 / 3.3.4 (3.9.0), 16 / 3.4.3 (3.9.0), 17 / 3.5.2 (3.9.0), 18 / 3.6.4 (3.14.1) on amd64; 17 / 3.5.3 (3.11.1), 17.10 / 3.5.6 (3.13.1), 18 / 3.6.1 (3.13.1), 18 / 3.7.0 (3.15.0) on arm64 | the spatial filters on all of them; every other filter on 17.10 / 3.5.6. A column holding GeometryCollections: GEOS 3.13 or later. Literals in another CRS (literals): all of them, and 14 / 3.2.3 (3.9.0), 18 / 3.7.0 (3.15.0) on amd64 |
| pg (node-postgres) | 8.23.1 | |
| SQLite (node:sqlite) | 3.51.3 (Node 24.15), 3.53.4 (Node 22.23) | GeoPackage 1.4 |
| SpatiaLite | 5.1.0 (GEOS 3.15.0, 3.14.1) | for Spatial Functions in sqlite; GEOS 3.13 or later for GeometryCollection literals |
| DuckDB, Node (@duckdb/node-api) | 2.0.0, 1.5.4 | 1.5.4 is DuckDB-WASM's engine; npm run check:duckdb tests the newest releases |
| DuckDB-WASM (@duckdb/duckdb-wasm) | 1.33.1-dev57.0 (DuckDB 1.5.4) | with the spatial extension |
| geos.js (as spatialEngine) | 0.1.5 (GEOS 3.14.0) | |
| jsts (as spatialEngine) | 2.12.1 (JTS 1.17) | the standard's Table 14, and the US states against GEOS; not in CI (guide) |
| Oracle Database | 26ai Free 23.26.3 (arm64) | every filter, arrays as text and in the JSON type, every spatial relation, against GEOS. Not in CI |
| oracledb | 26.0.0 (thin mode) | |
| SQL Server | 2022 (16.0.4305.5), 2025 (17.0.5035.5), amd64 (under emulation) | every filter, arrays as text and in 2025's json type, S_INTERSECTS against GEOS. Not in CI |
| mssql (tedious) | 12.7.13 | |
More
- Developer guide: using the package, task by task.
- docs/vision.md: what the package is for, and its principles.
- docs/architecture.md: how it is built and tested.
- docs/spatial-research.md: spatial support: the standard, the engines measured, the decisions.
- docs/accent-research.md: accent-insensitive comparison: the standard, the engines, the decisions.
- docs/array-research.md: Array Functions: the standard, the engines measured, the decisions.
- docs/duckdb-case.md: why the package has a DuckDB dialect, and what was measured first.
- CHANGELOG.md
Donations & sponsoring
Building and maintaining open-source libraries takes time, and ogc-cql2 is tested against real databases and engines before every release. If it saves you work, please consider supporting its development: it keeps the project active and moving forward.
Every bit of support is appreciated, whether it is sponsoring on GitHub, a coffee, a star, or a mention. Thank you for being part of the community!
Licence
MIT, © 2026 Felipe Carrillo Romero. The case-folding table is derived from Unicode data, under the Unicode License: see LICENSE-UNICODE.


