Validation functions in the GeoSilo DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- check-fixtures.sql for geosilo
-- spatial is needed for ST_GeomFromText, ST_AsText, etc.
INSTALL spatial;
LOAD spatial;
-- raw GEOMETRY source for INSERT INTO parcels SELECT ... FROM raw_parcels;
CREATE OR REPLACE TABLE raw_parcels AS
SELECT * FROM (VALUES
(1, 'a', ST_GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))')),
(2, 'b', ST_GeomFromText('POLYGON((2 2,3 2,3 3,2 3,2 2))'))
) t(id, name, geom);
-- roads(geom) for ST_Length aggregate
CREATE OR REPLACE TABLE roads AS
SELECT * FROM (VALUES
(ST_GeomFromText('LINESTRING(0 0, 1 1, 2 2)')),
(ST_GeomFromText('LINESTRING(0 0, 0 1, 0 2)'))
) t(geom);
-- stations(id, geom) — point geometries for ST_X/ST_Y
CREATE OR REPLACE TABLE stations AS
SELECT * FROM (VALUES
(1, ST_GeomFromText('POINT(-77.0 39.0)')),
(2, ST_GeomFromText('POINT(-76.5 40.5)'))
) t(id, geom);
-- mixed_geom_table(geom) — mixed geometry types
CREATE OR REPLACE TABLE mixed_geom_table AS
SELECT * FROM (VALUES
(ST_GeomFromText('POINT(0 0)')),
(ST_GeomFromText('LINESTRING(0 0, 1 1)')),
(ST_GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))'))
) t(geom);
-- compact_table(silo_blob) — geosilo blobs for geosilo_metadata example
CREATE OR REPLACE TABLE compact_table AS
SELECT geosilo_encode(ST_GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))')) AS silo_blob
UNION ALL
SELECT geosilo_encode(ST_GeomFromText('POINT(1 2)')) AS silo_blob;
-- utm_data(geom) — projected meters geometry source
CREATE OR REPLACE TABLE utm_data AS
SELECT ST_GeomFromText('POINT(500000 4500000)') AS geom;
-- mm_precision_data(geom) and millimeter_data(geom)
CREATE OR REPLACE TABLE mm_precision_data AS
SELECT ST_GeomFromText('POINT(0.001 0.002)') AS geom;
CREATE OR REPLACE TABLE millimeter_data AS
SELECT ST_GeomFromText('POINT(0.001 0.002)') AS geom;
-- parcels_geometry(id, geom) — GEOMETRY-typed parcels for export example
CREATE OR REPLACE TABLE parcels_geometry AS
SELECT id, geom FROM raw_parcels;
-- parcels(id, name, geom GEOSILO) — the table the cookbook defines and fills
-- in earlier blocks. Each in-browser example runs on its own, so it needs to
-- exist up front. The polygons sit inside the cookbook's -78..-76 longitude,
-- 39..41 latitude band, so its bounding-box filters return rows.
CREATE OR REPLACE TABLE parcels (
id INTEGER,
name VARCHAR,
geom GEOSILO('EPSG:4326') USING COMPRESSION zstd
);
INSERT INTO parcels
SELECT * FROM (VALUES
(1, 'north lot', ST_GeomFromText('POLYGON((-77.2 39.8,-77.1 39.8,-77.1 39.9,-77.2 39.9,-77.2 39.8))')),
(2, 'river parcel', ST_GeomFromText('POLYGON((-76.9 40.1,-76.8 40.1,-76.8 40.2,-76.9 40.2,-76.9 40.1))')),
(3, 'west field', ST_GeomFromText('POLYGON((-79.5 38.0,-79.4 38.0,-79.4 38.1,-79.5 38.1,-79.5 38.0))'))
) t(id, name, geom);
-- Source table and input files the cookbook's load examples read.
CREATE OR REPLACE TABLE raw_data AS SELECT id, geom FROM raw_parcels;
COPY (SELECT id, name, ST_AsText(geom) AS wkt FROM raw_parcels)
TO 'parcels.csv' (HEADER true);
COPY (SELECT id, name, ST_AsWKB(geom) AS geom_wkb FROM raw_parcels)
TO 'parcels.parquet' (FORMAT PARQUET);
Function category
Validation
3 functionsDuckDB 2.0 build only. Check GEOSILO blobs from untrusted sources without risking an error mid-query: geosilo_is_valid returns a boolean, geosilo_validate explains the problem, and try_geosilo_decode decodes or returns NULL. TRY_CAST(blob AS GEOSILO) applies the same full validation.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
blob
|
Type
BLOB
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geom
|
Type
GEOSILO
|
Mode Positional | Description |
Description
DuckDB 2.0 build only. Returns TRUE when the value is a structurally valid GEOSILO blob — a known header, element counts consistent with the data, nesting of at most 16 levels and no trailing bytes — and FALSE otherwise. It never raises an error for malformed input, so use it to filter untrusted BLOBs (from files, other systems or user input) before decoding them. Accepts GEOSILO or BLOB; NULL gives NULL.
SELECT geosilo_is_valid(geosilo_encode(ST_Point(1, 2))) AS good,
geosilo_is_valid('\x54\xff\xff\xff\x7f'::BLOB) AS truncated;
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
blob
|
Type
BLOB
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geom
|
Type
GEOSILO
|
Mode Positional | Description |
Description
DuckDB 2.0 build only. Validates a GEOSILO blob and returns NULL when it is valid, otherwise a message describing the first problem found — truncated data, a bad magic byte, an unsupported version, an element count larger than the data, excessive nesting or trailing bytes. Use it to report why a blob is rejected. Accepts GEOSILO or BLOB.
SELECT b, geosilo_validate(b) AS problem
FROM (VALUES (geosilo_encode(ST_Point(1, 2))), ('\x47\x01'::BLOB), ('\x54\xff\xff\xff\x7f'::BLOB)) t(b);
Related functions
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
blob
|
Type
BLOB
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geom
|
Type
GEOSILO
|
Mode Positional | Description |
Description
DuckDB 2.0 build only. Decodes a GEOSILO blob into a GEOMETRY like geosilo_decode, but returns NULL instead of raising an error when the blob is malformed. Accepts GEOSILO or BLOB.
SELECT ST_AsText(try_geosilo_decode(b)) AS wkt
FROM (VALUES (geosilo_encode(ST_Point(1, 2))), ('\x54\xff'::BLOB)) t(b);