### Install and load extension in SQL Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/extension_entrypoint.md Standard SQL commands to install and load the extension, followed by an example usage. ```sql INSTALL ng_postcode FROM community; LOAD ng_postcode; SELECT ng_postcode_parse('ek 01 a03 fk 01'); -- EK01A03FK01 ``` -------------------------------- ### Handle installation failure Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/extension_entrypoint.md Example of the error reported when the extension is not available for the running DuckDB version. ```sql -- On a DuckDB build other than 1.5.6: INSTALL ng_postcode FROM community; -- Error: Binder Error: Failed to install "ng_postcode": extension is not available for ... -- (reported by DuckDB when the community repository has no matching artifact) ``` -------------------------------- ### Install and load the extension Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/README.md Install the extension from the DuckDB community repository and load it into the current session. ```sql INSTALL ng_postcode FROM community; LOAD ng_postcode; ``` -------------------------------- ### Python version reporting Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md Example of using the function in a Python script to verify the installed version. ```python version = db.execute("SELECT ng_postcode_version()").fetchone()[0] print(f"DuckDB {duckdb.__version__}, ng_postcode {version}: {len(failed)} failed") ``` -------------------------------- ### Installation and Loading Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/README.md Commands to install and load the ng_postcode extension from the DuckDB community repository, and to load a locally built copy. ```APIDOC ## Installation and Loading ### Install from Community Repository ```sql INSTALL ng_postcode FROM community; LOAD ng_postcode; ``` ### Load Locally Built Copy Start DuckDB with -unsigned and load the file by its full path: ```sql LOAD '/full/path/to/build/release/ng_postcode.duckdb_extension'; ``` ### Note Until the extension is loaded, none of the eight functions exist. For example: ```sql SELECT ng_postcode_parse('EK01A03FK01'); -- Catalog Error: Scalar Function with name ng_postcode_parse does not exist! ``` ``` -------------------------------- ### Install and load ng_postcode from community Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/configuration.md Use this command to install and load the ng_postcode extension from the community repository. Requires DuckDB version exactly 1.5.6, otherwise the INSTALL command fails to find the extension. ```sql INSTALL ng_postcode FROM community; LOAD ng_postcode; ``` -------------------------------- ### Install and use ng_postcode extension Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Install the extension from the community repository, load it, and use its functions to parse or group data by postcode segments. ```sql INSTALL ng_postcode FROM community; LOAD ng_postcode; SELECT ng_postcode_parse('ek 01 a03 fk 01'); -- EK01A03FK01 SELECT ng_postcode_format(ng_postcode_truncate(postcode, 'district')) AS district, count(*) FROM read_csv('deliveries.csv') GROUP BY 1; ``` -------------------------------- ### Cargo.toml example section for wasm staticlib Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/wasm_lib.md Declares the wasm_lib.rs as an example with crate-type staticlib, required for wasm linking via emcc. The comment explains that crate-type cannot be overridden per target. ```toml [[example]] # crate-type can't be (at the moment) be overriden for specific targets # src/wasm_lib.rs forwards to src/lib.rs so that we can change from cdylib # (that is needed while compiling natively) to staticlib (needed since the # actual linking will be done via emcc name = "ng_postcode" path = "src/wasm_lib.rs" crate-type = ["staticlib"] ``` -------------------------------- ### Load a local build of the extension Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Load a locally built extension file by providing its full path when starting DuckDB with the -unsigned flag. ```sql LOAD '/full/path/to/build/release/ng_postcode.duckdb_extension'; ``` -------------------------------- ### Basic usage of ng_postcode_prefix_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_prefix_parse.md Examples showing how to parse various valid prefix formats into their compact forms. ```sql SELECT ng_postcode_prefix_parse('ek 01'); -- EK01 SELECT ng_postcode_prefix_parse('EK-01-A03'); -- EK01A03 SELECT ng_postcode_prefix_parse('EK-01-A03-FK-01'); -- EK01A03FK01 (a full code is a prefix too) ``` -------------------------------- ### Build command for wasm staticlib Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/wasm_lib.md Builds the wasm example as a staticlib. Linking into the final binary happens later via emcc, not at this step. ```sh cargo build --example ng_postcode # wasm (staticlib) build; linking into the final # binary happens via emcc, not at this step ``` -------------------------------- ### ng_postcode_within(code: VARCHAR, prefix: VARCHAR) -> BOOLEAN Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_within.md Tests whether a postcode starts with a given prefix. Returns true if the code starts with the prefix, false if both parse but the code does not start with the prefix, and NULL if either argument is NULL or fails to parse. ```APIDOC ## ng_postcode_within(code: VARCHAR, prefix: VARCHAR) -> BOOLEAN ### Description Tests whether a postcode starts with a given prefix. Returns true if the code starts with the prefix, false if both parse but the code does not start with the prefix, and NULL if either argument is NULL or fails to parse. ### SQL Signature ```sql ng_postcode_within(code: VARCHAR, prefix: VARCHAR) -> BOOLEAN ``` ### Parameters - **code** (VARCHAR) - Required - Any well-formed postcode - **prefix** (VARCHAR) - Required - Any well-formed prefix the code should start with Argument order is code first, prefix second. ### Return Type BOOLEAN - true if the code starts with the prefix - false if both parse but the code does not start with the prefix - NULL if either argument is NULL or either fails to parse ### Throws / NULL Conditions Never raises a SQL error. Returns NULL when: - Either argument is NULL - prefix does not parse as a prefix - code does not parse as a postcode ### Examples ```sql SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK'); -- true SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK-01-A03'); -- true SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK-01-A04'); -- false SELECT count(*) FROM deliveries WHERE ng_postcode_within(postcode, 'LA-11-W06'); ``` Error handling pattern: ```sql SELECT ng_postcode_within('not-a-code', 'EK') AS bad_code; -- NULL SELECT ng_postcode_within('EK-01-A03-FK-01', 'x') AS bad_prefix; -- NULL SELECT ng_postcode_within(NULL, 'EK') AS n; -- NULL SELECT ng_postcode_within(postcode, 'LA-11-W06') IS TRUE AS in_district, count(*) FROM customers GROUP BY 1; ``` ``` -------------------------------- ### Load local uninstalled build Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/extension_entrypoint.md Example of loading a local build, which fails if the DuckDB version does not match the build. ```sh duckdb -unsigned D LOAD '/build/release/ng_postcode.duckdb_extension'; -- if the DuckDB version does not match the build, DuckDB rejects the binary ``` -------------------------------- ### Basic usage of ng_postcode_parent Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parent.md Examples showing how to walk up the postcode hierarchy. ```sql SELECT ng_postcode_parent('EK-01-A03-FK-01'); -- EK01A03FK SELECT ng_postcode_parent('EK-01-A03'); -- EK01 SELECT ng_postcode_parent('EK-01'); -- EK ``` -------------------------------- ### Basic usage of ng_postcode_compact Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_compact.md Examples showing how to normalize messy but valid postcode strings. ```sql SELECT ng_postcode_compact('ek 01 a03 fk 01'); -- EK01A03FK01 SELECT ng_postcode_compact(' ek-01 a03fk 01 '); -- EK01A03FK01 ``` -------------------------------- ### Basic SQL usage of ng_postcode_version Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md A simple example of calling the function to retrieve the version string. ```sql SELECT ng_postcode_version(); -- 0.1.0 ``` -------------------------------- ### Basic usage of ng_postcode_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parse.md Examples of parsing spaced and hyphenated postcode strings into their compact form. ```sql SELECT ng_postcode_parse('ek 01 a03 fk 01') AS compact; -- EK01A03FK01 SELECT ng_postcode_parse('LA-11-W06-TC-10'); -- LA11W06TC10 ``` -------------------------------- ### Basic ng_postcode_format examples Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_format.md Shows the classic grouping pattern: formatting a compact code and a spaced code, both yielding the canonical hyphenated form EK-01-A03-FK-01. ```sql SELECT ng_postcode_format('ek01a03fk01'); -- EK-01-A03-FK-01 SELECT ng_postcode_format(' ek-01 a03fk 01 '); -- EK-01-A03-FK-01 ``` -------------------------------- ### Error handling with ng_postcode_parent Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parent.md Examples demonstrating NULL returns for state-level prefixes, invalid inputs, and NULL arguments. ```sql SELECT ng_postcode_parent('EK') AS state_has_no_parent; -- NULL SELECT ng_postcode_parent('100001') AS not_a_prefix; -- NULL SELECT ng_postcode_parent(NULL) AS n; -- NULL ``` -------------------------------- ### Load local build of ng_postcode extension Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/configuration.md Use this command to load a locally built ng_postcode extension. Requires starting DuckDB with the -unsigned flag to allow loading unsigned extensions. ```shell duckdb -unsigned ``` ```sql LOAD '/path/to/build/release/ng_postcode.duckdb_extension'; ``` -------------------------------- ### Basic usage of ng_postcode_within Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_within.md Examples showing how to filter rows using the ng_postcode_within function. ```sql SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK'); -- true SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK-01-A03'); -- true SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK-01-A04'); -- false SELECT count(*) FROM deliveries WHERE ng_postcode_within(postcode, 'LA-11-W06'); ``` -------------------------------- ### Error handling for ng_postcode_prefix_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_prefix_parse.md Examples demonstrating how the function returns NULL for incomplete segments or invalid inputs. ```sql SELECT ng_postcode_prefix_parse('EK-0'); -- NULL (3 chars: incomplete lga) SELECT ng_postcode_prefix_parse('EK-01-A03-F1'); -- NULL (area segment not a valid pair) SELECT ng_postcode_prefix_parse(NULL) AS n; -- NULL ``` -------------------------------- ### Basic usage of ng_postcode_truncate Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_truncate.md Examples showing how to truncate postcodes to specific segments and use the function in a GROUP BY clause. ```sql SELECT ng_postcode_truncate('ek 01 a03 fk 01', 'district'); -- EK01A03 SELECT ng_postcode_truncate('LA-11-W06-TC-10', 'state'); -- LA -- Group rows by quarter (area), without a map: SELECT ng_postcode_truncate(s, 'area') AS area, count(*) FROM deliveries GROUP BY 1; ``` -------------------------------- ### Build and test the extension Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Clone the repository with submodules and use the Makefile to configure, build, and test the extension. ```sh git clone --recurse-submodules https://github.com/Adeniyikayodee/ng-postcode-duckdb cd ng-postcode-duckdb make configure make release make test_release ``` -------------------------------- ### Error handling and loading check Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md Demonstrates that the function is unavailable before the extension is loaded and returns the version after loading. ```sql -- Running before LOAD: SELECT ng_postcode_version(); -- Catalog Error: Scalar Function with name ng_postcode_version does not exist! LOAD ng_postcode; SELECT ng_postcode_version(); -- 0.1.0 ``` -------------------------------- ### Configure Windows MSVC target in .cargo/config.toml Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/configuration.md Sets the rustflags to statically link the C runtime when building for the x86_64-pc-windows-msvc target. ```toml # statically linking the C runtime on windows seems sensible? [target.x86_64-pc-windows-msvc] rustflags = ["-Ctarget-feature=+crt-static"] ``` -------------------------------- ### Define extension_entrypoint signature Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/extension_entrypoint.md The entry point function signature using the #[duckdb_entrypoint_c_api] attribute. ```rust #[duckdb_entrypoint_c_api] pub unsafe fn extension_entrypoint(con: Connection) -> Result<(), Box> ``` -------------------------------- ### ng_postcode_version() Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md Returns the version of the ng_postcode extension as a VARCHAR string. The version is baked into the binary at compile time from the crate's Cargo.toml file. ```APIDOC ## ng_postcode_version() ### Description Returns the version of the ng_postcode extension as a VARCHAR string. The version is determined at compile time from the crate's Cargo.toml file and never changes for a given build. ### SQL Signature ```sql ng_postcode_version() -> VARCHAR ``` ### Parameters None. This is a zero-argument function. ### Return Type VARCHAR - The extension version string (e.g., '0.1.0'). ### Throws Never raises. Only available after the extension is loaded. ### Examples Basic use: ```sql SELECT ng_postcode_version(); -- 0.1.0 ``` Error handling - the version call is often the first thing to run after LOAD, so a failure here means the extension did not load: ```sql -- Running before LOAD: SELECT ng_postcode_version(); -- Catalog Error: Scalar Function with name ng_postcode_version does not exist! LOAD ng_postcode; SELECT ng_postcode_version(); -- 0.1.0 ``` Python usage for load sanity check: ```python version = db.execute("SELECT ng_postcode_version()").fetchone()[0] print(f"DuckDB {duckdb.__version__}, ng_postcode {version}: {len(failed)} failed") ``` ``` -------------------------------- ### ng_postcode_version() Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Returns the version of the extension. ```APIDOC ## ng_postcode_version() ### Description Returns the version of the extension. ### Function Signature ng_postcode_version() ### Parameters None ### Returns - **TEXT** - The version of the extension. ### Example SELECT ng_postcode_version(); -- '0.1.0' ``` -------------------------------- ### wasm_lib.rs forwarding module Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/wasm_lib.md The complete contents of src/wasm_lib.rs. It allows the crate type to be overridden for the wasm target by forwarding to lib. Do not change the content of this file. ```rust #![allow(special_module_name)] mod lib; ``` -------------------------------- ### SQL signature for ng_postcode_prefix_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_prefix_parse.md The SQL function signature for parsing a postcode prefix. ```sql ng_postcode_prefix_parse(text: VARCHAR) -> VARCHAR ``` -------------------------------- ### ng_postcode_compact(text) Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Returns the letters and digits in upper case, or NULL if the text holds anything else. ```APIDOC ## ng_postcode_compact(text) ### Description Returns the letters and digits in upper case, or NULL if the text holds anything else. ### Function Signature ng_postcode_compact(text) ### Parameters - **text** (TEXT) - Required - The text to compact. ### Returns - **TEXT** - The letters and digits in upper case, or NULL if the text contains any other characters. ### Example SELECT ng_postcode_compact('ek 01 a03 fk 01'); -- EK01A03FK01 ``` -------------------------------- ### Error handling and table constraints with ng_postcode_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parse.md Demonstrates how the function handles malformed input by returning NULL and how to use it within a CHECK constraint to validate table data. ```sql SELECT ng_postcode_parse('100001') AS not_a_code; -- NULL (too short) SELECT ng_postcode_parse(NULL) AS null_in; -- NULL CREATE TABLE customers ( id bigint PRIMARY KEY, postcode text CHECK (ng_postcode_parse(postcode) IS NOT DISTINCT FROM postcode) ); -- Rows with a malformed postcode are rejected. A plain `=` would not work: -- a malformed code compares with NULL and the check would let it through. ``` -------------------------------- ### Error handling with ng_postcode_compact Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_compact.md Demonstrates how the function returns NULL when encountering invalid characters or NULL inputs. ```sql SELECT ng_postcode_compact('EK_01-A03-FK-01') AS underscore; -- NULL SELECT ng_postcode_compact('ÉK-01-A03-FK-01') AS accent; -- NULL (non-ASCII) SELECT ng_postcode_compact('EK 01 A03 FK 0١') AS arabic; -- NULL (non-ASCII digit) SELECT ng_postcode_compact(NULL) AS n; -- NULL ``` -------------------------------- ### ng_postcode_prefix_parse(text) Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Parses a postcode text and returns the compact prefix (e.g., EK01A03) or NULL if the input is not a well-formed prefix. ```APIDOC ## ng_postcode_prefix_parse(text) ### Description Parses a postcode text and returns the compact prefix (e.g., EK01A03) or NULL if the input is not a well-formed prefix. ### Function Signature ng_postcode_prefix_parse(text) ### Parameters - **text** (TEXT) - Required - The postcode text to parse. ### Returns - **TEXT** - The compact prefix (e.g., EK01A03) or NULL if the input is not well-formed. ### Example SELECT ng_postcode_prefix_parse('ek 01 a03'); -- EK01A03 ``` -------------------------------- ### ng_postcode_format(code or prefix) Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Formats a compact code or prefix into its hyphenated form. ```APIDOC ## ng_postcode_format(code or prefix) ### Description Formats a compact code or prefix into its hyphenated form. ### Function Signature ng_postcode_format(code or prefix) ### Parameters - **code or prefix** (TEXT) - Required - The compact code or prefix to format. ### Returns - **TEXT** - The hyphenated form of the input. ### Example SELECT ng_postcode_format('EK01A03FK01'); -- EK-01-A03-FK-01 ``` -------------------------------- ### Error handling for ng_postcode_truncate Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_truncate.md Demonstrates that unknown segments, invalid postcode formats, and NULL inputs result in a NULL return value. ```sql SELECT ng_postcode_truncate('EK01A03FK01', 'zone'); -- NULL (unknown segment) SELECT ng_postcode_truncate('100001', 'state'); -- NULL (not a postcode) SELECT ng_postcode_truncate(NULL, 'state') AS n; -- NULL SELECT ng_postcode_truncate('EK01A03FK01', NULL); -- NULL ``` -------------------------------- ### SQL signature for ng_postcode_compact Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_compact.md The SQL function signature for the compact operation. ```sql ng_postcode_compact(text: VARCHAR) -> VARCHAR ``` -------------------------------- ### ng_postcode_prefix_parse(text: VARCHAR) -> VARCHAR Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_prefix_parse.md Parses a Nigerian postcode prefix from the given text and returns its compact form. Returns NULL if the input is NULL or not a well-formed prefix. ```APIDOC ## ng_postcode_prefix_parse(text: VARCHAR) -> VARCHAR ### Description Parses a Nigerian postcode prefix from the given text and returns its compact form. The function accepts any text that is a prefix, such as 'EK', 'ek 01', 'EK-01-A03', 'ek01a03fk', or a full 11-character code. Returns NULL when the input is NULL or not a well-formed prefix. ### SQL Signature ```sql ng_postcode_prefix_parse(text: VARCHAR) -> VARCHAR ``` ### Parameters - **text** (VARCHAR) - Required - Any text that is a prefix: 'EK', 'ek 01', 'EK-01-A03', 'ek01a03fk', or a full 11-char code. ### Return Type VARCHAR - The compact form of a valid prefix, e.g., 'EK01A03', or NULL when the input is NULL or not a well-formed prefix. ### NULL Conditions Never raises a SQL error. Returns NULL when: - Argument is NULL - Letters+digits length is not in {2, 4, 7, 9, 11} - Input contains a non ASCII-alphanumeric character other than space/hyphen - A segment value is out of range ### Examples Basic use: ```sql SELECT ng_postcode_prefix_parse('ek 01'); -- EK01 SELECT ng_postcode_prefix_parse('EK-01-A03'); -- EK01A03 SELECT ng_postcode_prefix_parse('EK-01-A03-FK-01'); -- EK01A03FK01 (a full code is a prefix too) ``` Error handling pattern - NULL for prefixes stopped at a partial segment or wrong length: ```sql SELECT ng_postcode_prefix_parse('EK-0'); -- NULL (3 chars: incomplete lga) SELECT ng_postcode_prefix_parse('EK-01-A03-F1'); -- NULL (area segment not a valid pair) SELECT ng_postcode_prefix_parse(NULL) AS n; -- NULL ``` ``` -------------------------------- ### Rust signature implementation Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md The generated signatures() function declaring an empty argument list for the zero-arity function. ```rust fn signatures() -> Vec { vec![ScalarFunctionSignature::exact( varchars(0), // varchars(0) = an empty argument list LogicalTypeId::Varchar.into(), )] } ``` -------------------------------- ### varchars function signature Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/lib.md Builds a vector of LogicalTypeHandle values of type LogicalTypeId::Varchar, used to define argument list lengths for function signatures. ```rust fn varchars(count: usize) -> Vec ``` -------------------------------- ### ng_postcode_parent(prefix) Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Returns the prefix one segment shorter, or NULL for a state. ```APIDOC ## ng_postcode_parent(prefix) ### Description Returns the prefix one segment shorter, or NULL for a state. ### Function Signature ng_postcode_parent(prefix) ### Parameters - **prefix** (TEXT) - Required - The prefix to get the parent of. ### Returns - **TEXT** - The prefix one segment shorter, or NULL if the input is a state. ### Example SELECT ng_postcode_parent('EK01A03'); -- EK01 ``` -------------------------------- ### SQL signature for ng_postcode_version Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md The SQL signature for the function, which takes no arguments and returns a VARCHAR. ```sql ng_postcode_version() -> VARCHAR ``` -------------------------------- ### SQL signature for ng_postcode_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parse.md The function signature for the SQL interface. ```sql ng_postcode_parse(text: VARCHAR) -> VARCHAR ``` -------------------------------- ### ng_postcode_parse(text) Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/README.md Parses a postcode text and returns the compact code (e.g., EK01A03FK01) or NULL if the input is not a well-formed code. ```APIDOC ## ng_postcode_parse(text) ### Description Parses a postcode text and returns the compact code (e.g., EK01A03FK01) or NULL if the input is not a well-formed code. ### Function Signature ng_postcode_parse(text) ### Parameters - **text** (TEXT) - Required - The postcode text to parse. ### Returns - **TEXT** - The compact code (e.g., EK01A03FK01) or NULL if the input is not well-formed. ### Example SELECT ng_postcode_parse('ek 01 a03 fk 01'); -- EK01A03FK01 ``` -------------------------------- ### Error handling pattern for ng_postcode_within Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_within.md Demonstrates how the function handles invalid inputs or NULL values by returning NULL. ```sql SELECT ng_postcode_within('not-a-code', 'EK') AS bad_code; -- NULL SELECT ng_postcode_within('EK-01-A03-FK-01', 'x') AS bad_prefix; -- NULL SELECT ng_postcode_within(NULL, 'EK') AS n; -- NULL -- Count known addresses inside a district, and keep unknowns for follow-up: SELECT ng_postcode_within(postcode, 'LA-11-W06') IS TRUE AS in_district, count(*) FROM customers GROUP BY 1; ``` -------------------------------- ### ng_postcode_parent(prefix) -> VARCHAR Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parent.md Returns the compact prefix one segment shorter than the input prefix, or NULL if the input is a state-level prefix, NULL, or not a well-formed prefix. ```APIDOC ## ng_postcode_parent(prefix) ### Description Returns the compact prefix one segment shorter than the input prefix. For example, 'EK-01-A03-FK-01' becomes 'EK01A03FK'. Returns NULL if the input is a state-level prefix (e.g., 'EK'), NULL, or not a well-formed prefix. ### SQL Signature ```sql ng_postcode_parent(prefix: VARCHAR) -> VARCHAR ``` ### Parameters #### Arguments - **prefix** (VARCHAR) - Required - Any well-formed prefix: 'EK', 'EK-01', 'EK-01-A03', 'EK-01-A03-FK', or a full code. ### Return Type VARCHAR - The compact prefix one segment shorter, or NULL. ### Return Value Examples | Input | Result | | --- | --- | | 'EK-01-A03-FK-01' (unit) | 'EK01A03FK' | | 'EK-01-A03-FK' (area) | 'EK01A03' | | 'EK-01-A03' (district) | 'EK01' | | 'EK-01' (lga) | 'EK' | | 'EK' (state) | NULL | ### NULL Conditions Returns NULL when: - Argument is NULL - Input is not a well-formed prefix (length, characters, segments) - Input is a state-level prefix (length 2) ### Examples Basic use - walk up the hierarchy: ```sql SELECT ng_postcode_parent('EK-01-A03-FK-01'); -- EK01A03FK SELECT ng_postcode_parent('EK-01-A03'); -- EK01 SELECT ng_postcode_parent('EK-01'); -- EK ``` Error handling pattern - the top of the hierarchy and bad input: ```sql SELECT ng_postcode_parent('EK') AS state_has_no_parent; -- NULL SELECT ng_postcode_parent('100001') AS not_a_prefix; -- NULL SELECT ng_postcode_parent(NULL) AS n; -- NULL ``` ``` -------------------------------- ### Create table with CHECK constraint Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/errors.md Use IS NOT DISTINCT FROM in a CHECK constraint to ensure that malformed postcodes are handled correctly, as standard equality comparisons with NULL can lead to unexpected behavior. ```sql CREATE TABLE customers ( id bigint PRIMARY KEY, postcode text CHECK (ng_postcode_parse(postcode) IS NOT DISTINCT FROM postcode) ); ``` -------------------------------- ### Rust definition for ng_postcode_prefix_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_prefix_parse.md The internal Rust implementation using the text_function macro. ```rust // The compact prefix, such as EK01A03. text_function!(PrefixParse, 1, |[text]| Some( Prefix::parse(&text).ok()?.as_str().to_owned() )); ``` -------------------------------- ### ng_postcode_format(code_or_prefix: VARCHAR) -> VARCHAR Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_format.md Hyphenates a postcode or prefix into canonical form with exactly one hyphen between segments, upper-cased. Returns NULL if the input is NULL or does not parse as a prefix. ```APIDOC ## ng_postcode_format(code_or_prefix: VARCHAR) -> VARCHAR ### Description Formats a postcode or prefix into its canonical hyphenated form, with exactly one hyphen between segments and all characters upper-cased. Returns NULL when the input is NULL or cannot be parsed as a prefix. ### Function Signature ng_postcode_format(code_or_prefix: VARCHAR) -> VARCHAR ### Parameters - **code_or_prefix** (VARCHAR) - Required - The input code or prefix. Accepts compact (e.g., 'ek01a03fk01'), spaced (e.g., 'EK 01 A03 FK 01'), or hyphenated (e.g., 'EK-01-A03-FK-01') formats. ### Return Type VARCHAR - The canonical hyphenated form, e.g., 'EK-01-A03-FK-01', 'EK-01-A03', 'EK-01', 'EK'. NULL if input is NULL or does not parse as a prefix. ### NULL Conditions Returns NULL under the same conditions as ng_postcode_prefix_parse: NULL argument, length not in {2, 4, 7, 9, 11}, invalid characters, or out-of-range segments. Never raises a SQL error. ### Examples SELECT ng_postcode_format('ek01a03fk01'); -- EK-01-A03-FK-01 SELECT ng_postcode_format(' ek-01 a03fk 01 '); -- EK-01-A03-FK-01 SELECT ng_postcode_format('100001') AS bad; -- NULL SELECT ng_postcode_format(NULL) AS n; -- NULL SELECT ng_postcode_format(ng_postcode_truncate(s, 'district')) AS district, count(*) FROM (VALUES ('ek 01 a03 fk 01'), ('EK-01-A03-ZZ-14'), ('la11w06tc10'), ('100001')) v(s) GROUP BY 1 ORDER BY 1 NULLS LAST; -- EK-01-A03 2 -- LA-11-W06 1 -- NULL 1 ``` -------------------------------- ### arguments function signature Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/lib.md Reads N text arguments from a data chunk row. Returns None if any argument is NULL, otherwise returns the arguments as an array of strings. ```rust /// The text arguments of one row, or `None` if any of them is NULL. fn arguments(input: &DataChunkHandle, row: usize) -> Option<[String; N]> ``` -------------------------------- ### SQL signature for ng_postcode_parent Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parent.md The function signature for the SQL implementation. ```sql ng_postcode_parent(prefix: VARCHAR) -> VARCHAR ``` -------------------------------- ### Error handling and grouping with ng_postcode_format Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_format.md Shows NULL results for invalid inputs (non-prefix length and NULL) and a GROUP BY query that uses ng_postcode_truncate to group by district, with malformed rows falling into the NULL group. The output shows counts for each district and NULL. ```sql SELECT ng_postcode_format('100001') AS bad; -- NULL (not a prefix length) SELECT ng_postcode_format(NULL) AS n; -- NULL -- Grouping by district with a readable label; malformed rows fall into the NULL group: SELECT ng_postcode_format(ng_postcode_truncate(s, 'district')) AS district, count(*) FROM (VALUES ('ek 01 a03 fk 01'), ('EK-01-A03-ZZ-14'), ('la11w06tc10'), ('100001')) v(s) GROUP BY 1 ORDER BY 1 NULLS LAST; -- EK-01-A03 2 -- LA-11-W06 1 -- NULL 1 ``` -------------------------------- ### Register scalar functions Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/extension_entrypoint.md The function body performs a chain of registrations for scalar functions, mapping structs to SQL names. ```rust con.register_scalar_function::("ng_postcode_version")?; con.register_scalar_function::("ng_postcode_compact")?; con.register_scalar_function::("ng_postcode_parse")?; con.register_scalar_function::("ng_postcode_prefix_parse")?; con.register_scalar_function::("ng_postcode_format")?; con.register_scalar_function::("ng_postcode_truncate")?; con.register_scalar_function::("ng_postcode_parent")?; con.register_scalar_function::("ng_postcode_within")?; Ok(()) ``` -------------------------------- ### SQL signature for ng_postcode_truncate Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_truncate.md The SQL function signature for truncating a postcode. ```sql ng_postcode_truncate(code: VARCHAR, segment: VARCHAR) -> VARCHAR ``` -------------------------------- ### SQL signature of ng_postcode_format Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_format.md Declares the function signature: takes a VARCHAR code or prefix and returns a VARCHAR. ```sql ng_postcode_format(code_or_prefix: VARCHAR) -> VARCHAR ``` -------------------------------- ### Rust definition of ng_postcode_version Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_version.md The exact Rust definition using the text_function! macro. ```rust text_function!(Version, 0, |[]| Some(env!("CARGO_PKG_VERSION").to_owned())); ``` -------------------------------- ### SQL signature for ng_postcode_within Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_within.md The SQL signature for the function, accepting two VARCHAR arguments and returning a BOOLEAN. ```sql ng_postcode_within(code: VARCHAR, prefix: VARCHAR) -> BOOLEAN ``` -------------------------------- ### ng_postcode_compact(text: VARCHAR) -> VARCHAR Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_compact.md Removes all non-alphanumeric characters (spaces and hyphens) from the input text and converts ASCII letters to uppercase. Returns NULL if the input is NULL or contains any character outside the allowed set (ASCII alphanumeric, space, hyphen). ```APIDOC ## ng_postcode_compact(text: VARCHAR) -> VARCHAR ### Description Removes all non-alphanumeric characters (spaces and hyphens) from the input text and converts ASCII letters to uppercase. Returns NULL if the input is NULL or contains any character outside the allowed set (ASCII alphanumeric, space, hyphen). ### SQL Signature ```sql ng_postcode_compact(text: VARCHAR) -> VARCHAR ``` ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | `text` | `VARCHAR` | yes | — | Any text consisting only of ASCII letters/digits, spaces, and hyphens | ### Return Type `VARCHAR` — the input with every non-alphanumeric character (space and hyphen) removed and every ASCII letter upper-cased. `NULL` if the argument is `NULL` or the text contains **anything** outside the allowed set. ### NULL Conditions Never raises a SQL error. Returns `NULL` when: - Argument is `NULL` - Text contains any character that is not ASCII alphanumeric, space, or hyphen ### Examples Basic use — normalize a column of messy-but-valid codes: ```sql SELECT ng_postcode_compact('ek 01 a03 fk 01'); -- EK01A03FK01 SELECT ng_postcode_compact(' ek-01 a03fk 01 '); -- EK01A03FK01 ``` Error handling pattern — anything else in the text is a NULL, not a guess: ```sql SELECT ng_postcode_compact('EK_01-A03-FK-01') AS underscore; -- NULL SELECT ng_postcode_compact('ÉK-01-A03-FK-01') AS accent; -- NULL (non-ASCII) SELECT ng_postcode_compact('EK 01 A03 FK 0١') AS arabic; -- NULL (non-ASCII digit) SELECT ng_postcode_compact(NULL) AS n; -- NULL ``` ``` -------------------------------- ### ng_postcode_parse(text: VARCHAR) -> VARCHAR Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parse.md Parses a postcode from arbitrary text and returns the compact form (letters and digits only, upper case). Returns NULL if the input is NULL or does not parse as a well-formed postcode. ```APIDOC ## ng_postcode_parse(text: VARCHAR) -> VARCHAR ### Description Parses a postcode from arbitrary text and returns the compact form (letters and digits only, upper case). Returns NULL if the input is NULL or does not parse as a well-formed postcode. ### SQL Signature ```sql ng_postcode_parse(text: VARCHAR) -> VARCHAR ``` ### Parameters - **text** (VARCHAR) - Required - Any text: a hyphenated code (EK-01-A03-FK-01), a spaced code (EK 01 A03 FK 01), a compact code (ek01a03fk01), or surrounding junk (- ek-01 a03fk 01 -) ### Return Type VARCHAR - The compact (letters and digits only, upper case) form of a valid postcode, e.g. EK01A03FK01. NULL if the argument is NULL or does not parse as a well-formed postcode. ### NULL Conditions The function never raises a SQL error. Returns NULL when any of these hold: - Argument is NULL - Length of letters+digits is not 11 - Input contains a non-ASCII-alphanumeric character other than space/hyphen - A segment value is out of range (e.g. state not in the Nigeria state set, 00 lga/unit, 0 district digit) ### Examples Basic use - parse, and group rows by place without a map: ```sql SELECT ng_postcode_parse('ek 01 a03 fk 01') AS compact; -- EK01A03FK01 SELECT ng_postcode_parse('LA-11-W06-TC-10'); -- LA11W06TC10 ``` Error handling pattern - malformed values are NULL, count them and follow up, or keep them out of a table with a CHECK: ```sql SELECT ng_postcode_parse('100001') AS not_a_code; -- NULL (too short) SELECT ng_postcode_parse(NULL) AS null_in; -- NULL CREATE TABLE customers ( id bigint PRIMARY KEY, postcode text CHECK (ng_postcode_parse(postcode) IS NOT DISTINCT FROM postcode) ); -- Rows with a malformed postcode are rejected. A plain = would not work: -- a malformed code compares with NULL and the check would let it through. ``` ``` -------------------------------- ### Rust definition for ng_postcode_parse Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parse.md The internal Rust implementation using the text_function macro. ```rust // The compact code, EK01A03FK01. text_function!(Parse, 1, |[text]| Some( Postcode::parse(&text).ok()?.as_str().to_owned() )); ``` -------------------------------- ### Map SQL segment names to Segment enum Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/types.md The segment() function maps SQL string inputs to the corresponding ng-postcode::Segment enum variants. ```rust fn segment(name: &str) -> Option { Some(match name { "state" => Segment::State, "lga" => Segment::Lga, "district" => Segment::District, "area" => Segment::Area, "unit" => Segment::Unit, _ => return None, }) } ``` -------------------------------- ### Rust definition for ng_postcode_parent Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_parent.md The internal Rust implementation using the text_function macro. ```rust // The prefix one segment shorter, or NULL for a state. text_function!(Parent, 1, |[text]| Some( Prefix::parse(&text).ok()?.parent()?.as_str().to_owned() )); ``` -------------------------------- ### Count malformed values Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/errors.md Use this query to identify and count rows where the postcode parsing results in a NULL value. ```sql SELECT ng_postcode_parse(postcode) IS NULL AS malformed, count(*) FROM deliveries GROUP BY 1; ``` -------------------------------- ### segment function signature Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/lib.md Maps SQL segment name strings to the ng_postcode::Segment enum. Returns None for unrecognized names, resulting in a NULL value. ```rust fn segment(name: &str) -> Option ``` -------------------------------- ### Rust definition of ng_postcode_format Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_format.md Exact implementation using the text_function! macro. It parses the input with Prefix::parse and returns the Display form, which is the hyphenated, upper-cased string. Returns None (NULL) when parsing fails. ```rust // The hyphenated form of a code or a prefix. text_function!(Format, 1, |[text]| Some( Prefix::parse(&text).ok()?.to_string() )); ``` -------------------------------- ### Catalog Error: Scalar Function does not exist Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/errors.md This error occurs when attempting to call ng_postcode_parse before the extension has been loaded. Ensure the extension is loaded before executing its functions. ```sql SELECT ng_postcode_parse('EK01A03FK01'); -- Catalog Error: Scalar Function with name ng_postcode_parse does not exist! ``` -------------------------------- ### ng_postcode_truncate(code, segment) Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_truncate.md SQL function that returns the compact prefix of a postcode cut after a specified segment. Returns NULL for invalid inputs or unknown segment names. ```APIDOC ## ng_postcode_truncate(code, segment) ### Description Returns the compact prefix of a postcode cut after a specified segment. The function accepts any well-formed postcode (compact, spaced, or hyphenated) and a segment name, and returns the prefix up to that segment. Returns NULL if the code is not a valid postcode or the segment name is unknown. ### SQL Signature ```sql ng_postcode_truncate(code: VARCHAR, segment: VARCHAR) -> VARCHAR ``` ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | `code` | `VARCHAR` | yes | — | Any well-formed postcode (compact, spaced, or hyphenated) | | `segment` | `VARCHAR` | yes | — | One of the five segment names: 'state', 'lga', 'district', 'area', 'unit'. Anything else yields NULL | ### Return Type `VARCHAR` — the compact prefix of the code cut after the requested segment, e.g. `EK01A03`. NULL when any of the conditions below holds. ### NULL Conditions Never raises a SQL error. Returns NULL when: - `code` is NULL or `segment` is NULL - `code` does not parse as a postcode - `segment` is not one of the five valid names ### Examples Basic use: ```sql SELECT ng_postcode_truncate('ek 01 a03 fk 01', 'district'); -- EK01A03 SELECT ng_postcode_truncate('LA-11-W06-TC-10', 'state'); -- LA -- Group rows by quarter (area), without a map: SELECT ng_postcode_truncate(s, 'area') AS area, count(*) FROM deliveries GROUP BY 1; ``` Error handling pattern: ```sql SELECT ng_postcode_truncate('EK01A03FK01', 'zone'); -- NULL (unknown segment) SELECT ng_postcode_truncate('100001', 'state'); -- NULL (not a postcode) SELECT ng_postcode_truncate(NULL, 'state') AS n; -- NULL SELECT ng_postcode_truncate('EK01A03FK01', NULL); -- NULL ``` ### Valid Segment Values | Segment name | Cuts a code after | Example on `EK-01-A03-FK-01` | | --- | --- | --- | | `'state'` | the state code | `EK` | | `'lga'` | the local government area | `EK01` | | `'district'` | the district | `EK01A03` | | `'area'` | the area | `EK01A03FK` | | `'unit'` | the full code | `EK01A03FK01` | ``` -------------------------------- ### text_function! macro definition Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/lib.md A macro used to declare private unit structs that implement VScalar. It handles binding arguments, computing the result, and writing the output to the data chunk. ```rust /// A scalar function from `$arity` text arguments to text, or NULL. macro_rules! text_function { ($name:ident, $arity:literal, |$arguments:pat_param| $answer:expr) => { ... } } ``` ```rust fn invoke(_: &(), input: &mut DataChunkHandle, output: &mut dyn WritableVector) -> Result<(), Box> ``` -------------------------------- ### Rust definition of ng_postcode_compact Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_compact.md The internal Rust implementation using a text_function macro to filter and transform characters. ```rust // The letters and digits of a code in upper case, or NULL if it holds anything else. text_function!(Compact, 1, |[text]| { let allowed = |c: char| c.is_ascii_alphanumeric() || c == ' ' || c == '-'; text.chars().all(allowed).then(|| { let kept = text.chars().filter(char::is_ascii_alphanumeric); kept.map(|c| c.to_ascii_uppercase()).collect() }) }); ``` -------------------------------- ### Rust definition of ng_postcode_truncate Source: https://github.com/adeniyikayodee/ng-postcode-duckdb/blob/main/_autodocs/api-reference/ng_postcode_truncate.md The internal Rust implementation of the truncation logic. ```rust // The compact prefix of a code, cut off after a segment. NULL for an unknown segment. text_function!(Truncate, 2, |[code, through]| { let prefix = Postcode::parse(&code).ok()?.truncate(segment(&through)?); Some(prefix.as_str().to_owned()) }); ```