Skip to content

Filter expressions ​

Workspace Key draft workspace layer filter sql security

JSON filter expressions which the XYZ API turns into SQL conditions; use when writing layer.filter.default or a filter on a role.

Resource and sources

A filter expression is a JSON object whose keys are table columns and whose values select a filter type. The XYZ API converts it to a SQL condition and passes every value as a query parameter, never as SQL text.[1]

json
{ "retailer": { "in": ["Tesco", "Aldi"] }, "open_date": { "null": false } }
TypeValueSQL
eq, gt, gte, lt, ltenumber or numeric string"col" = $1, >, >=, <, <=
in / niarray, or one value"col" = ANY ($1) / NOT "col" = ANY ($1)
likestring; commas separate alternatives("col" ILIKE $1 OR …) with a trailing %
matchstring"col"::text = $1, an exact text match
booleanboolean"col" IS true / IS false
nullbooleantrue: IS NULL; false: IS NOT NULL
emailany"col"::text = the requesting user's email

Combining ​

  • Several columns in one object, and several types for one column, are joined with AND.
  • An array of objects is joined with OR: [{"a": {"eq": 1}}, {"b": {"eq": 2}}].
  • A column name must match ^[a-zA-Z_]\w*$. Otherwise the whole object is dropped with a warning.
  • An unknown type is ignored silently, so a misspelt type filters nothing.
  • The numeric types only check that the value is numeric; a value which is not fails the query. The schema accepts numbers and decimal strings. On a text column PostgreSQL compares text, so {"col": {"gte": 50}} matches 'Rooftop'. Use numeric types on numeric columns only.

Where expressions are used ​

  • layer.filter.default in the workspace, always applied; see layer filter.
  • layer.filter.current, sent by the MAPP library as the filter request param of layer queries.[2]
  • The sqlFilter request param of query templates without a layer.

Only query templates containing ${filter} apply a filter. A custom query template must include ${filter} for the default and current filters of its layer to apply.

Review notes ​

  • Whether a filter inside layer.roles is merged into filter.default and applied by queries is not yet confirmed against getLayer.js and composeObj.js.
  • An array value nested under a column ({"col": [ … ]}) calls sqlfilter without the request and throws. Use a top-level array for OR.

Examples ​

A complete workspace with a default filter on the devdb retail points. It shows only Tesco and Aldi stores with a known open date.

json
{
  "dbs": "DEVDB",
  "locale": {
    "layers": {
      "retail_points": {
        "display": true,
        "format": "wkt",
        "table": "geodata.uk_glx_open_retail_points",
        "geom": "geom_p_4326",
        "srid": "4326",
        "qID": "id",
        "filter": {
          "default": {
            "retailer": { "in": ["Tesco", "Aldi"] },
            "open_date": { "null": false }
          }
        }
      }
    }
  }
}

  1. sqlFilter.js ↩︎

  2. query.js layerQuery ↩︎

XYZ v5.1.0. Generated from knowledge/ at dd7a6fa27736502733499c1bf485f15659b10338. Concepts marked draft are not reviewed.