tinySQL - Function Examples

Auto-generated from input SQL. Results are shown below; SQL is visible above each result.

tinySQL Function Examples
This file demonstrates all supported SQL functions in tinySQL
Execute these examples to learn the complete SQL dialect

DATE AND TIME FUNCTIONS

Current date and time
current_timestamp
2026-08-04T15:09:08.3860223+02:00

today
2026-08-04T00:00:00+02:00

CURRENT_TIME
2026-08-04T15:09:08.5999875+02:00

Date part extraction
current_year
2026

current_month
8

current_day
4

current_hour
15

current_minute
9

current_second
8

current_quarter
3

day_of_week
3

day_of_year
216

week_of_year
32

NEW: EXTRACT function (alternative to individual functions)
YEAR
2026

MONTH
8

DAY
4

QUARTER
3

week
32

dow
2

Date arithmetic
next_week
2026-08-11T00:00:00+02:00

last_month
2026-07-05T00:00:00+02:00

days_in_2024
365

NEW: ADD_MONTHS function
three_months_later
2026-11-04T00:00:00+01:00

six_months_ago
2026-02-04T00:00:00+01:00

NEW: DATE_TRUNC function (truncate to period start)
start_of_year
2026-01-01T00:00:00+01:00

start_of_quarter
2026-07-01T00:00:00+02:00

start_of_month
2026-08-01T00:00:00+02:00

start_of_week
2026-08-03T02:00:00+02:00

start_of_day
2026-08-04T02:00:00+02:00

start_of_hour
2026-08-04T15:00:00+02:00

NEW: EOMONTH function (end of month)
end_of_current_month
2026-08-31T00:00:00+02:00

end_of_next_month
2026-09-30T00:00:00+02:00

end_of_last_month
2026-07-31T00:00:00+02:00

NEW: IN_PERIOD function (period membership checks)
is_today
true

is_year_to_date
false

is_month_to_date
true

is_quarter_to_date
true

is_last_12_months
true

is_current_quarter
true

was_previous_quarter
true

is_current_year
true

Date formatting
iso_date
2026-08-04

german_date
04.08.2026

long_date
%A, %B 04, 2026

date_only
2026-08-04

time_only
15:09:08

STRING FUNCTIONS

Case conversion
uppercase
HELLO WORLD

lowercase
hello world

title_case
Hello World From Tinysql

String manipulation
concatenated
Hello World

joined_with_separator
Apple, Banana, Cherry

string_length
11

CHAR_LENGTH
5

substring_result
World

substr_result
Hello

left_chars
Hello

right_chars
World

reversed
olleH

repeated
HaHaHa

String trimming
trimmed
Hello

left_trimmed
Hello

right_trimmed
Hello

String padding
left_padded
00042

right_padded
42000

String replacement
replaced
Hello Universe

String search
POSITION
7

locate_position
0

String formatting
formatted
Value: 42, Name: Test

formatted_number
Pi is approximately 3.14

NEW: SPLIT function (returns array)
fruit_array
["apple","banana","cherry"]

Split and extract part (existing function)
second_fruit
banana

Special string functions
ten_spaces

ascii_value
65

char_from_ascii
A

soundex_code
S530

quoted_string
'It''s a test'

NEW: REGEX FUNCTIONS

Pattern matching
is_email
true

is_numeric
true

Pattern extraction
order_number
#12345

email
test@example.com

Pattern replacement
date_with_slashes
2024/11/27

numbers_replaced
HelloXWorldX

FULL-TEXT SEARCH FUNCTIONS

FTS_MATCH — boolean match against a query (term, phrase, boolean, wildcard)

has_fox
true

has_both
true

has_either
true

lacks_cat
true

has_phrase
true

has_prefix
true

? / _ match one character; * / % match zero or more characters.
single_char_wildcard
true

multi_char_wildcard
true

FTS_RANK / BM25: relevance score for the same query syntax (higher = more relevant)
relevance
2

relevance_alias
2

FTS_SNIPPET / FTS_HIGHLIGHT: return the text with matches wrapped in markers
excerpt
the quick brown <em>fox</em>

FTS_WORD_COUNT — number of (stop-word-filtered, stemmed) tokens

token_count
3

(ok)
(ok)
(ok)
(ok)
FTS_SEARCH table-valued function: ranked search across a real table.
With no column arguments it searches the WHOLE ROW (every column, not
just TEXT ones); pass explicit column names to restrict the search.
Repeated calls against an unchanged table reuse a cached tokenization,
so it's cheap for interactive/repeated queries.
idtitle_fts_score_fts_rank
1Go Programming1.84180759835262191

Restrict the search to a single column
idtitle
3Database Design

(ok)
(ok)
(ok)
(ok)

ROW_TO_TEXT — ad-hoc whole-row substring search inside an ordinary WHERE

clause, combinable with other conditions. Cheaper to set up than
FTS_SEARCH (no ranking, no table function) but also less precise; good
for a quick "search everywhere" filter.
id
1

(ok)
(ok)
(ok)
(ok)
(ok)
CONTAINS_ALL / CONTAINS_ANY / CONTAINS_SCORE: a friendlier, case-insensitive
alternative to chaining "ROW_TO_TEXT() LIKE '%term%' AND ROW_TO_TEXT() LIKE
'%other%' ..." for each term. Terms are literal substrings, not patterns —
unlike LIKE, '%' and '_' have no wildcard meaning here.

CONTAINS_ALL — every term must be present (case-insensitive substring match)

iddescription
1Widget A2000 - blue, waterproof
3Gadget C100 - blue, waterproof, wireless

CONTAINS_ANY — at least one term must be present

iddescription
2Widget B300 - red
3Gadget C100 - blue, waterproof, wireless

CONTAINS_SCORE — count of matched terms (0..N) - use in ORDER BY to rank

rows by how many search terms they contain
iddescriptionmatch_count
3Gadget C100 - blue, waterproof, wireless3
1Widget A2000 - blue, waterproof2
2Widget B300 - red0

(ok)

NUMERIC FUNCTIONS

Basic math
absolute_value
42

sign_of_number
-1

rounded
3.14

floor_value
3

ceiling_value
4

ceiling_value2
4

truncated
3.14

trunc_value
3.14

Advanced math
power_of_two
256

cube
27

square_root
4

modulo
2

Logarithms and exponentials
natural_log
4.605170185988092

ln_value
0.999999327347282

log_base_10
3

log_base_2
10

euler_number
2.718281828459045

Trigonometry
pi_value
3.141592653589793

sine_90_degrees
1

cosine_0_degrees
1

tangent_45_degrees
1

arcsine
1.5707963267948966

arccosine
1.5707963267948966

arctangent
0.7853981633974483

atan2_value
0.7853981633974483

radians_to_degrees
180

degrees_to_radians
3.141592653589793

Random numbers
random_value
0.09334275200814703

rand_value
0.8887558635612671

NEW: ARRAY FUNCTIONS

Create arrays
colors
["red","green","blue"]

Array element access
first_element
alpha

last_element
gamma

Array properties
array_size
4

Array membership
has_banana
true

has_three
true

Array to string
joined_array
hello world

joined_with_dash
a - b - c

Array manipulation
unique_elements
["a","b","c","d"]

sorted_array
["apple","banana","mango","zebra"]

(ok)
(ok)
(ok)
(ok)

AGGREGATE FUNCTIONS

Create a sample table for aggregate examples

COUNT

group_idtotal_count
13
23
32

group_idnon_null_count
13
23
32

(ok)
(ok)
(ok)
(ok)
(ok)
Basic aggregates (use with GROUP BY in real queries)
SELECT COUNT(*) as total_count FROM table;
SELECT COUNT(column) as non_null_count FROM table;
SELECT SUM(amount) as total FROM table;
SELECT AVG(value) as average FROM table;
SELECT MIN(price) as minimum FROM table;
SELECT MAX(price) as maximum FROM table;
NEW: MIN_BY and MAX_BY - Get value from row with min/max of another column
These are useful for time-series data and finding associated values
Create a sample sales table
Get the product name from the row with the earliest date
earliest_product
Monitor

Get the product name from the row with the latest date
latest_product
Mouse

Get the product name with the lowest amount
cheapest
Mouse

Get the product name with the highest amount
most_expensive
Laptop

Get the amount from the row with the earliest date
earliest_amount
350

Get the amount from the row with the latest date
latest_amount
25

Aliases ARG_MIN and ARG_MAX are also supported
cheapest_via_argmin
Mouse

most_expensive_via_argmax
Laptop

(ok)
(ok)
Clean up

COMPARISON AND RANGE PREDICATES

BETWEEN — inclusive range check, equivalent to (x >= lo AND x <= hi)

in_range
true

out_of_range
false

not_in_range
false

BETWEEN also works on dates and strings (lexicographic range for strings)
in_2024
true

alphabetically_between
true

The comparand is evaluated exactly once even for expensive/non-deterministic
expressions, e.g. RANDOM() BETWEEN 0.0 AND 1.0 is always true, never
comparing two different random draws against each other.
random_is_always_in_unit_range
true

(ok)
(ok)
(ok)
(ok)
(ok)
(ok)

LIMIT / OFFSET

LIMIT ALL means "no limit" (same as omitting LIMIT entirely)
id
1
2
3
4
5

LIMIT/OFFSET accept any constant expression, not just a bare literal
id
3
4
5

SQL — 2008 standard syntax: OFFSET n ROWS FETCH {FIRST|NEXT} m ROWS ONLY

id
2
3

(ok)

CONDITIONAL AND NULL HANDLING

NULL handling
first_non_null
default

nvl_result
replacement

ifnull_result
value

nullif_equal
NULL

nullif_different
5

Conditional
if_result
yes

iif_result
correct

greatest_value
30

least_value
10

CASE expressions
case_result
correct

CRYPTOGRAPHIC AND ENCODING FUNCTIONS

Hashing
md5_hash
5f4dcc3b5aa765d61d8327deb882cf99

sha1_hash
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

sha256_hash
5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

sha512_hash
b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86

Encoding
base64_encoded
SGVsbG8gV29ybGQ=

base64_decoded
Hello World

hex_encoded
48656C6C6F

hex_decoded
Hello

JSON FUNCTIONS

JSON extraction
SELECT JSON_GET('{"name":"John","age":30}', 'name') as json_name;
SELECT JSON_EXTRACT('{"user":{"name":"Jane"}}', 'user.name') as nested_value;

TYPE CONVERSION AND INTROSPECTION

Type casting
string_to_int
123

float_to_int
3

int_to_string
42

Type checking
integer_type
integer

string_type
text

float_type
real

UTILITY FUNCTIONS

UUID generation
unique_id
6c69f735-2079-4959-b6c0-e98a5f089bf0

Version info
tinysql_version
tinySQL 1.0

(ok)
(ok)
(ok)
(ok)
(ok)
(ok)

WINDOW FUNCTIONS - FULLY IMPLEMENTED

Window functions operate on a set of rows (window) relative to the current row
Syntax: function() OVER (PARTITION BY ... ORDER BY ... frame_clause)
Create sample table for window function examples

ROW_NUMBER — Assign unique row numbers

productamountrow_num
Mouse251
Keyboard752
Chair2003
Desk3504
Laptop12005

ROW_NUMBER with PARTITION BY: Restart numbering per partition
productcategoryamountrow_in_category
MouseElectronics251
KeyboardElectronics752
LaptopElectronics12003
ChairFurniture2001
DeskFurniture3502

RANK — like ROW_NUMBER, but tied rows share a rank and the next rank

skips ahead by the tie-group size (e.g. 1, 1, 3)
productamountrank_by_amount
Laptop12001
Desk3502
Chair2003
Keyboard754
Mouse255

DENSE_RANK — like RANK, but no gaps after ties (e.g. 1, 1, 2)

productamountdense_rank_by_amount
Laptop12001
Desk3502
Chair2003
Keyboard754
Mouse255

PERCENT_RANK and CUME_DIST: relative standing within the partition, as a
fraction in [0, 1]
productamountpercent_rankcume_dist
Mouse2500.2
Keyboard750.250.4
Chair2000.50.6
Desk3500.750.8
Laptop120011

NTILE — split the partition into N (approximately) equal-sized buckets

productamounttercile
Mouse251
Keyboard751
Chair2002
Desk3502
Laptop12003

LAG — Access previous row value

productamountprevious_amount
Laptop1200NULL
Mouse251200
Desk35025
Keyboard75350
Chair20075

LAG with default value for first row
productamountprevious_or_zero
Laptop12000
Mouse251200
Desk35025
Keyboard75350
Chair20075

LEAD — Access next row value

productamountnext_amount
Laptop120025
Mouse25350
Desk35075
Keyboard75200
Chair200NULL

FIRST_VALUE — Get first value in window

productamountcheapest_product
Mouse25Mouse
Keyboard75Mouse
Chair200Mouse
Desk350Mouse
Laptop1200Mouse

FIRST_VALUE with PARTITION BY
productcategoryamountcheapest_in_category
MouseElectronics25Mouse
KeyboardElectronics75Mouse
LaptopElectronics1200Mouse
ChairFurniture200Chair
DeskFurniture350Chair

LAST_VALUE — Get last value in window (up to current row by default)

productamountmost_expensive_so_far
Mouse25Laptop
Keyboard75Laptop
Chair200Laptop
Desk350Laptop
Laptop1200Laptop

MOVING_SUM — Calculate moving/rolling sum

productsale_dateamountrolling_sum_3days
Laptop2025-01-1512001200
Mouse2025-01-20251225
Desk2025-01-253501575
Keyboard2025-02-1075450
Chair2025-02-15200625

MOVING_AVG — Calculate moving/rolling average

productsale_dateamountrolling_avg_3days
Laptop2025-01-1512001200
Mouse2025-01-2025612.5
Desk2025-01-25350525
Keyboard2025-02-1075150
Chair2025-02-15200208.33333333333334

Complex example: LAG with PARTITION BY
productcategoryamountprev_in_category
LaptopElectronics1200NULL
MouseElectronics251200
KeyboardElectronics7525
DeskFurniture350NULL
ChairFurniture200350

(ok)
Clean up

COMPLEX QUERIES COMBINING MULTIPLE FUNCTIONS

Example 1: Date range analysis with period check
todaymonth_startmonth_endis_month_to_datedays_into_month
2026-08-04T00:00:00+02:002026-08-01T00:00:00+02:002026-08-31T00:00:00+02:00true3

Example 2: String processing with regex
order_idusernameitem_count
#12345TEST3

Example 3: Numeric calculations
circle_area_r5angle_degreesabsolute_signed
78.5436.86989764584402100

Example 4: Array operations
first_sorted_uniquesorted_numbers
apple1 -> 1 -> 3 -> 4 -> 5 -> 9

Example 5: Conditional logic with dates
period_classification
Today

Example 6: NULL handling with coalesce chain
coalesce_result
Final Default

(ok)
(ok)
(ok)
(ok)
(ok)

PIVOT

PIVOT spreads the distinct values of one column into new output columns,
aggregating another column into each. Every other selected column becomes
an implicit GROUP BY key. Scope: one aggregate function and a static
(literal) value list.
One row per region, one column per category, aliased for clean output names
regionelectronicsfurniture
East10050
West20075

WHERE filters the source rows before pivoting
regionelectronics
East100
West200

COUNT works too, not just SUM
regionelectronics_countfurniture_count
East11
West11

(ok)

PRACTICAL USE CASES

Use Case 1: Email validation and extraction
is_valid_emailusernamedomain
trueuserexample.com

Use Case 2: Date-based filtering helper
in_last_yearin_current_quartercurrent_quarter_number
truetrue3

(ok)
(ok)
Complex Example: DISTINCT ON + ROW_NUMBER, string concatenation,
column names with spaces, and timestamp helpers.
Use DISTINCT ON to pick the most recent order per customer.
Note: DISTINCT ON keeps the first row per key; ORDER BY controls which row is first.
STRING CONCAT via + operator (also works with CONCAT)
ROW_NUMBER demonstrates window function numbering per PARTITION
TIMESTAMP helpers
customercustomer nametotalorder_datesummaryrnnow_tstoday_dateepoch_zerots_epoch
aliceAlice A2002025-02-05 14:20:00alice - 20012026-08-04T15:09:08.6093595+02:002026-08-04T00:00:00+02:001970-01-01T01:00:00+01:001682899200
bobRobert B502025-03-01 08:30:00bob - 5012026-08-04T15:09:08.6093595+02:002026-08-04T00:00:00+02:001970-01-01T01:00:00+01:001682899200
carolCarol C3002025-02-20 16:45:00carol - 30012026-08-04T15:09:08.6093595+02:002026-08-04T00:00:00+02:001970-01-01T01:00:00+01:001682899200

Show all rows with ROW_NUMBER and LAG for context
order_idcustomercustomer nameorder_datetotalrn_ascprev_total
1aliceAlice A2025-01-10 09:15:001201NULL
2aliceAlice A2025-02-05 14:20:002002120
3bobRobert B2025-01-12 11:05:00751NULL
4bobRobert B2025-03-01 08:30:0050275
5carolCarol C2025-02-20 16:45:003001NULL

(ok)
Cleanup demo table
Use Case 3: String normalization
normalized_string
HELLO WORLD

Use Case 4: Array processing from CSV
color_counthas_greensorted_unique
4truea,b,c

Use Case 5: Complex date arithmetic
mid_yearend_of_quarterdays_in_year
2026-07-01T00:00:00+02:002026-11-30T00:00:00+01:00364

CTE (Common Table Expression) EXAMPLES
Non-recursive CTE: useful for organizing complex subqueries
sale_dateproduct
2025-01-01A

Recursive CTE example: generate numbers 1..5
n
1
2
3
4
5

Fibonacci via recursive CTE: generate first 10 Fibonacci numbers
n: index, a: current fib value, b: next fib value
nfib_value
00
11
21
32
43
55
68
713
821
934

IO AND TRANSFORM FUNCTIONS (safe runnable examples enabled)
GZIP/GUNZIP: Compression functions (safe examples)
gzip_compressed_length
48

gunzip_roundtrip
roundtrip test

BASE64 encoding/decoding (safe examples)
base64_encoded_example
aGVsbG8gd29ybGQ=

base64_decoded_roundtrip
hello world

file_content
[ {"id": 1, "name": "Alice", "email": "alice@example.com"}, {"id": 2, "name": "Bob", "email": "bob@example.com"}, {"id": 3, "name": "Carol", "email": "carol@example.com"} ]

HTTP — Fetch HTTP GET response (30 second timeout)

http_response
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

Combined example: Read and decompress a gzipped file
decompressed_content
id,name,email 1,Alice,alice@example.com 2,Bob,bob@example.com 3,Carol,carol@example.com

Combined example: Fetch and decode base64 data from HTTP
decoded_api_response
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

idnameemail
1Alicealice@example.com
2Bobbob@example.com
3Carolcarol@example.com

idnameemail
1Alicealice@example.com
2Bobbob@example.com
3Carolcarol@example.com

TABLE-VALUED FUNCTIONS (TVF)

Note: Table-valued functions are currently in development.
The infrastructure is implemented, but parser integration is pending.
These will be available in a future release:

TABLE_FROM_JSON — Parse JSON array into table

nameid
Alice1
Bob2

TABLE_FROM_JSON_LINES — Parse JSON Lines (JSONL) format

idnameemail
1Alicealice@example.com
2Bobbob@example.com
3Carolcarol@example.com

TABLE_FROM_CSV — Parse CSV data with configurable delimiter

emailidname
alice@example.com1Alice
bob@example.com2Bob
carol@example.com3Carol

Combined example: Query JSON data from HTTP endpoint
Use local demo JSON instead of external HTTP endpoint
emailidname
alice@example.com1Alice
bob@example.com2Bob
carol@example.com3Carol

(ok)
Combined example: Join CSV file with database table
Ensure `users` demo table exists before JOIN examples
ERR: parse error near "ON": unexpected token after statement
ERR: parse error near "ON": unexpected token after statement
ERR: parse error near "ON": unexpected token after statement
Combined example: Join CSV file with database table

SYSTEM CATALOG

Note: System catalog tables are implemented and available via the Go API.
SQL syntax for querying catalog is planned for future release:
List all tables
namefull_namecolumnsrowsis_tempversiontypetenantschema
usersusers30false0TABLEdefaultmain

Find columns for a specific table

View all registered functions
ERR: unknown column "return_type"

Check scheduled jobs

(ok)
(ok)
(ok)
(ok)
(ok)
(ok)

JOB SCHEDULER

Note: Job scheduler is fully implemented and operational via the Go API.
SQL syntax for job management is planned for future release:
Create a CRON job
Create an INTERVAL job
Create a ONCE job
Enable/disable jobs
Drop a job

PRACTICAL EXAMPLES WITH NEW FUNCTIONS

Example 1: Load and process JSON data from a file
nameemailid
Bobbob@example.com2
Carolcarol@example.com3

Materialize JSON into a demo table for downstream examples
ERR: parse error near "INTO": unexpected token after statement

Example 2: Fetch and parse CSV from HTTP endpoint
producttotal
Mouse2
Keyboard1
Laptop3
Chair1
Desk1

Example 3: Process compressed log files
idnameemail
2Bobbob@example.com
3Carolcarol@example.com

Example 4: Combine multiple data sources

Example 5: Base64-encoded JSON data
idnameemail
1Alicealice@example.com
2Bobbob@example.com

VECTOR / EMBEDDING FUNCTIONS (RAG Vector Database Support)
tinySQL supports a native VECTOR data type for storing float64 embeddings,
plus a rich set of functions for similarity search, distance computation,
and vector manipulation — enabling RAG (Retrieval-Augmented Generation)
workloads directly in SQL.
---- Vector Creation & Conversion ----
Parse a JSON array into a VECTOR
EMBEDDING
[1,2,3]

Serialize a VECTOR back to JSON
json_vec
[1.5,2.5,3.5]

---- Vector Introspection ----
Number of dimensions
dimensions
5

L2 (Euclidean) norm
norm_is_5
5

---- Vector Normalization ----
Normalize to unit length
unit_vec
[0.6,0.8]

---- Element-wise Arithmetic ----
Addition
sum_vec
[5,7,9]

Subtraction
diff_vec
[4,5,6]

Hadamard (element-wise) product
prod_vec
[10,18,28]

Scalar multiplication
scaled_vec
[2.5,5,7.5]

---- Similarity & Distance Functions ----
Dot product (inner product)
dot_product
32

Cosine similarity ∈ [-1, 1] (1 = identical direction)
same_dir
1

orthogonal
0

Cosine distance ∈ [0, 2] (0 = identical)
zero_dist
0

Euclidean (L2) distance
dist_5
5

Manhattan (L1) distance
dist_12
12

Generic distance function with selectable metric
cosine_dist
1

l2_dist
5

l1_dist
7

dot_dist
-0

---- Vector Manipulation ----
Extract a sub-vector (0-based start, length)
sub_vec
[20,30,40]

Concatenate two vectors
concat_vec
[1,2,3,4,5]

Element-wise average of two vectors
avg_vec
[3,5,7]

---- Quantization ----
Simulate 8-bit quantization (reduces precision, saves storage)
quantized_8bit
[0,0.5019607843137255,1]

Simulate 16-bit quantization
quantized_16bit
[0,0.3300068665598535,0.6699931334401464,1]

---- Random Vector Generation ----
Generate a random unit vector of 5 dimensions
random_vec
[-0.14274490614762214,-0.7160125291820697,-0.523714526457873,0.39095634406149465,-0.19956498099185213]

Deterministic random vector with a seed
seeded_vec
[0.7546115837811923,0.06083794731271071,-0.24012205373772932,0.6042286795046568,0.06410307295949322]

(ok)
(ok)
(ok)
(ok)
(ok)
(ok)
---- VECTOR Column Type ----
Tables can have VECTOR columns for storing embeddings
Query embeddings with inline similarity scoring
titlesimilarity
apple1
banana0.9938837346736189
elderberry0.9701425001453318
cherry0
date fruit0

Filter by similarity threshold
titledistance
apple0
banana0.006116265326381098
elderberry0.029857499854668235

---- k-NN Vector Search (Table-Valued Function) ----
VEC_SEARCH(table, column, query_vector, k [, metric [, index]])
Returns the k nearest neighbours with _vec_distance and _vec_rank columns
idtitleembedding_vec_distance_vec_rank_vec_similarity
1apple[1,0,0]011
2banana[0.9,0.1,0]0.00611626532638109820.9938837346736189
5elderberry[0.8,0.2,0]0.02985749985466823530.9701425001453318

Search with L2 distance metric
_vec_rankidtitleembedding_vec_distance_vec_similarity
11apple[1,0,0]0-0
22banana[0.9,0.1,0]0.1414213562373095-0.1414213562373095
35elderberry[0.8,0.2,0]0.282842712474619-0.282842712474619

Search with cached approximate ANN indexes for larger RAG tables
_vec_distance_vec_similarity_vec_rankidtitleembedding
0111apple[1,0,0]
0.0061162653263810980.993883734673618922banana[0.9,0.1,0]
0.0298574998546682350.970142500145331835elderberry[0.8,0.2,0]

idtitleembedding_vec_distance_vec_similarity_vec_rank
1apple[1,0,0]011
2banana[0.9,0.1,0]0.0061162653263810980.99388373467361892
5elderberry[0.8,0.2,0]0.0298574998546682350.97014250014533183

VEC_WARM(table, column [, metric [, index]]) prebuilds the vector column
cache and the requested ANN index (ivf/hnsw) ahead of time, instead of
paying that one-time O(n log n) build cost on whichever query happens to
run first. Building an HNSW index is real, non-trivial work (each row
insertion does its own approximate-nearest-neighbor search against the
graph built so far) — worth doing explicitly right after a bulk load
(e.g. a nightly re-embed) rather than surprising the first live query with
it. Returns one row describing what was warmed.
dimsdistinct_dimstable_namerow_countcolumn_nameindex_modevector_countmetricexcluded_rows
31vec_demo_docs5embeddinghnsw5cosine0

VEC_TOP_K is an alias for VEC_SEARCH
embedding_vec_distance_vec_similarityidtitle_vec_rank
[0,1,0]013cherry1
[0.8,0.2,0]0.7574643749636670.242535625036332975elderberry2

(ok)
(ok)
(ok)
(ok)
(ok)
---- RAG Workflow Example ----
1. Create a knowledge base with embeddings
2. Semantic search: find docs closest to a query embedding
idcontent_vec_distance_vec_similarity_vec_rankembedding
1Go is a statically typed language0.0037184723731137350.99628152762688631[0.9,0.1,0,0]
2Python is dynamically typed0.0044206089509798430.99557939104902022[0.8,0.2,0.1,0]

3. Inline similarity ranking
contentrelevance
Go is a statically typed language0.9962815276268863
Python is dynamically typed0.9955793910490202
Vector search enables AI applications0.11426095898687248
Databases store structured data0.05747778044001267

4. Vector arithmetic for query expansion (average two embeddings)
expanded_query
[0.8500000000000001,0.15000000000000002,0.05,0]

(ok)
(ok)
(ok)
(ok)
Cleanup demo tables
---- RAG_SEARCH: composed retrieval (one call) ----
RAG_SEARCH(table, vector_column, query_vector, k [, options_json]) composes
VEC_SEARCH + FTS_SEARCH (hybrid reciprocal rank fusion) + RAG_CONTEXT_FROM
(neighbor-chunk expansion) into a single table-valued function, instead of
hand-assembling that pipeline from the three primitives above.
1. Vector-only search (equivalent to VEC_SEARCH, one call)
doc_idchunk_indexchunk_text_vec_distance_vec_similarity_vec_rank
doc-10TinySQL is a lightweight embeddable SQL engine1.1102230246251565e-160.99999999999999991
doc-11It supports vector search and full-text search0.0037184723731137350.99628152762688632

2. Hybrid vector + BM25 keyword search fused via reciprocal rank fusion
(RRF). key_columns is required in hybrid mode: it's how RAG_SEARCH matches
a row across the independently-fetched vector and text candidate sets.
doc_idchunk_indexchunk_text_rrf_score_rrf_rank
doc-11It supports vector search and full-text search0.032522474881015341
doc-20Full-text search uses BM25 ranking0.032002048131080392
doc-10TinySQL is a lightweight embeddable SQL engine0.016393442622950823

3. Hybrid search plus neighbor-chunk context expansion, in one call
(replaces wrapping the hybrid result in RAG_CONTEXT_FROM separately).
doc_idchunk_indexchunk_text_hit_rank_context_offset_context_rank
doc-10TinySQL is a lightweight embeddable SQL engine1-11
doc-11It supports vector search and full-text search102
doc-12RAG pipelines combine retrieval with generation113
doc-20Full-text search uses BM25 ranking204

(ok)
(ok)
(ok)
---- HYBRID_SEARCH: one search term, semantic + full-text ----
The query vector is the embedding of the same search term, generated with
the model used to populate the VECTOR column. The PRIMARY KEY is used
automatically to fuse vector and BM25 candidate lists.
idcontent_vec_similarity_fts_score_rrf_score_rrf_rank
1database timeout retry guide0.99388373467361890.90664888933857080.032786885245901641
2general systems handbook0.9909924304103231NULL0.0161290322580645162
3unrelated cooking notes0.11043152607484652NULL0.0158730158730158723

(ok)
(ok)
(ok)
(ok)
VEC_HYBRID_SEARCH is an alias. Tables without a PRIMARY KEY provide the
identity columns via the optional JSON object:
'{"key_columns":["doc_id","chunk_index"],"index":"hnsw","candidate_k":50}'

GEOSPATIAL FUNCTIONS

Geometry is stored and returned as GeoJSON, either in a plain TEXT/JSON
column or in the dedicated GEOMETRY column type (validated on write and
canonicalized to stable text). Raw four-number coordinate arguments use
(lat, lon, lat, lon); GeoJSON itself stays [lon, lat].

GEO_POINT — build a GeoJSON Point from (lon, lat[, z])

berlin
{"coordinates":[13.405,52.52],"type":"Point"}

ST_MAKEPOINT / ST_POINT: aliases of GEO_POINT
berlinalso_berlin
{"coordinates":[13.405,52.52],"type":"Point"}{"coordinates":[13.405,52.52],"type":"Point"}

GEO_LON / GEO_X: read a Point's longitude. ST_X is the alias.
lonlon_alias
13.40513.405

GEO_LAT / GEO_Y: read a Point's latitude. ST_Y is the alias.
latlat_alias
52.5252.52

GEO_DISTANCE — great-circle distance in meters between two points.

HAVERSINE and ST_DISTANCE are aliases.
a.nameb.namemeters
BerlinMunich504307.226096815

GEO_DISTANCE also accepts four raw (lat1, lon1, lat2, lon2) numbers
meters
504307.226096815

GEO_DWITHIN — true if two points are within a given radius (meters).

ST_DWITHIN is the alias.
within_200m
true

GEO_WITHIN_BBOX — true if a point falls inside [minLon, minLat, maxLon, maxLat].

ST_WITHIN_BBOX is the alias.
in_germany_ish
true

GEO_BEARING — initial compass bearing (0-360 clockwise from north) from the

first point toward the second. ST_AZIMUTH is the alias.
a.nameb.namebearing_deg
BerlinMunich195.63069050650938

GEO_MIDPOINT — great-circle midpoint between two points. ST_MIDPOINT is the alias.

a.nameb.namemidpoint
BerlinMunich{"coordinates":[12.448041477751076,50.33218058133119],"type":"Point"}

GEO_DESTINATION — project a point along a bearing for a distance (meters).

ST_PROJECT is the alias.
ten_km_east
{"coordinates":[13.552796765540279,52.51990795285367],"type":"Point"}

GEO_WITHIN_POLYGON — point-in-polygon(-or-multipolygon) test.

ST_WITHIN is the alias; ST_CONTAINS takes the same two arguments reversed
(polygon, point), matching PostGIS's ST_Contains(A, B) = "A contains B".
point_insidepoint_outside
truefalse

GEO_POLYGON_AREA — area in square meters, exterior ring minus holes.

ST_AREA is the alias. Accepts a Polygon or MultiPolygon.
square_meters
7.603723971920487e+07

GEO_LENGTH — sum of great-circle segment lengths of a LineString, in meters.

ST_LENGTH is the alias.
meters
1788.5531141424217

GEO_INTERSECTS — true if two geometries (point/line/polygon, any combination)

share at least one point. ST_INTERSECTS is the alias. Respects polygon
holes: a shape nested in another's hole is NOT reported as intersecting.
overlapping_squares
true

GEO_DISJOINT — the exact negation of GEO_INTERSECTS. ST_DISJOINT is the alias.

far_apart_squares
true

GEO_EQUALS — same coordinates, allowing for a different start vertex,

winding direction, or Polygon-vs-single-part-MultiPolygon wrapping (NOT
full OGC point-set equality -- two differently-vertexized polygons
covering the same area are not detected as equal). ST_EQUALS is the alias.
same_square_rotated_start
true

GEO_BUFFER — approximate a circular buffer (meters) around a point as a

regular polygon; the optional 3rd argument sets the vertex count (8-256,
default 32). ST_BUFFER is the alias.
half_km_circle
{"coordinates":[[[13.404999999999973,52.524496608029594],[13.407828240070671,52.524154290420434],[13.410225790262416,52.523179466961025],[13.411827597276556,52.52172058096743],[13.412389848573184,52.51999976988185],[13.41182706244581,52.51827902619631],[13.410225033897405,52.51682030292081],[13.407827705239924,52.515845642179514],[13.404999999999973,52.515503391970405],[13.402172294760021,52.515845642179514],[13.399774966102541,52.51682030292081],[13.39817293755425,52.51827902619631],[13.397610151426761,52.51999976988185],[13.39817240272339,52.52172058096743],[13.399774209737643,52.523179466961025],[13.402171759929274,52.524154290420434],[13.404999999999973,52.524496608029594]]],"type":"Polygon"}

GEO_CONVEX_HULL — convex hull of every vertex in a geometry, as a Polygon.

ST_CONVEXHULL is the alias. Computed in plain lon/lat space (a standard
planar approximation, not a rigorous spherical hull).
hull
{"coordinates":[[[0,0],[2,0],[2,2],[0,2],[0,0]]],"type":"Polygon"}

GEO_ENVELOPE — a geometry's bounding box as a Polygon (vs. GEO_BBOX's plain

array). ST_ENVELOPE is the alias.
bbox_polygon
{"coordinates":[[[0,0],[3,0],[3,4],[0,4],[0,0]]],"type":"Polygon"}

GEO_LINE_INTERPOLATE — the point a fraction (0-1) of the way along a

LineString, by actual distance, not vertex count. ST_LINE_INTERPOLATE_POINT
is the alias.
midway
{"coordinates":[13.45,52.56],"type":"Point"}

GEO_CLIP — Sutherland-Hodgman clip of a geometry to a convex boundary

polygon. ST_CLIP is the alias. Rejects a non-convex boundary unless the
optional 3rd argument (allow_nonconvex) is true -- a best-effort,
not-guaranteed-correct result in that case.
clipped_to_inner_square
{"coordinates":[[[1,3],[1,1],[3,1],[3,3],[1,3]]],"type":"Polygon"}

GEOSPATIAL — EDITING AND QUALITY

GEO_SIMPLIFY — reduce vertex count. Accepts Douglas-Peucker ('dp', the

default), 'visvalingam-effective', and 'visvalingam-weighted'.
ST_SIMPLIFY is the alias.
simplified
{"coordinates":[[0,0],[2,0],[3,2],[4,2]],"type":"LineString"}

GEO_BBOX — bounding box as [minLon, minLat, maxLon, maxLat]. ST_BBOX is the alias.

bbox
[-2,-2,2,2]

GEO_CENTROID — area/length-weighted centroid of a geometry. ST_CENTROID is the alias.

centroid
{"coordinates":[0,0],"type":"Point"}

GEO_AFFINE — shift, scale, and rotate a geometry around an anchor (default

anchor: the geometry's own bbox center; here given explicitly as the last
two arguments). ST_AFFINE is the alias.
rotated_90deg_around_origin
{"coordinates":[6.123233995736757e-17,1],"type":"Point"}

GEO_SMOOTH — Chaikin corner-cutting smoothing, 0-8 iterations. ST_SMOOTH is the alias.

smoothed
{"coordinates":[[0,0],[0.25,0.25],[0.75,0.75],[1.25,0.75],[1.75,0.25],[2,0]],"type":"LineString"}

GEO_DROP_HOLES — remove every hole from a Polygon/MultiPolygon, keeping only

exterior rings. ST_REMOVE_HOLES is the alias.
outer_ring_only
{"coordinates":[[[-2,-2],[-2,2],[2,2],[2,-2],[-2,-2]]],"type":"Polygon"}

GEO_CLEAN — remove repeated consecutive vertices and normalize ring closure.

ST_CLEAN is the alias. Rejects a result collapsing below the GeoJSON
minimum vertex count instead of returning invalid output.
deduplicated
{"coordinates":[[0,0],[1,1],[2,2]],"type":"LineString"}

GEO_SNAP — round coordinates to a grid, then clean. ST_SNAPTOGRID is the alias.

snapped
{"coordinates":[13.405000000000001,52.52],"type":"Point"}

GEO_IS_VALID — structural GeoJSON check (not full topology validation --

no self-intersection detection). ST_ISVALID is the alias.
validinvalid_too_few_points
truefalse

(ok)
(ok)
CREATE TABLE ... GEOMETRY: a first-class, validated, canonicalizing column
type (not just TEXT/JSON). A bare number or a Feature/FeatureCollection is
rejected -- a GEOMETRY column holds a Geometry.
idshape
1{"coordinates":[13.4,52.52],"type":"Point"}

CAST(x AS GEOMETRY) validates and canonicalizes the same way a column write does
canonical
{"coordinates":[1,2],"type":"Point"}

(ok)
(ok)
(ok)
(ok)
(ok)

GEOSPATIAL — REGION OPERATIONS, SEARCH, AND CLASSIFICATION

Mapshaper-inspired region-editing verbs and BI-oriented helpers for
turning raw geometry into location-based KPIs and choropleth dashboards.

GEO_DISSOLVE — merge every geometry in a group into one, by cancelling

shared directed edges between adjacent polygons. GEO_UNION_AGG and
ST_UNION are the same operation under aggregate-style names. Correct for
topologically-clean, vertex-aligned adjacent input (e.g. this project's
own output, or real GIS boundary data) -- NOT a general polygon-boolean-
union for overlapping-but-misaligned input.
regionboundary
north{"coordinates":[[[0,0],[1,0],[2,0],[2,1],[1,1],[0,1],[0,0]]],"type":"Polygon"}
south{"coordinates":[[[10,10],[11,10],[11,11],[10,11],[10,10]]],"type":"Polygon"}

GEO_UNION_AGG / ST_UNION: aliases of GEO_DISSOLVE
regionboundary
north{"coordinates":[[[0,0],[1,0],[2,0],[2,1],[1,1],[0,1],[0,0]]],"type":"Polygon"}
south{"coordinates":[[[10,10],[11,10],[11,11],[10,11],[10,10]]],"type":"Polygon"}

GEO_BBOX_AGG — bounding box across every geometry in a group.

regionbbox
north[0,0,2,1]
south[10,10,11,11]

GEO_CENTROID_AGG — (optionally weighted) centroid across a group. The

optional weight combines with GEO_CENTROID's own area/length weighting --
here, a population-weighted centroid of already-area-weighted per-row centroids.
regionweighted_centroid
north{"coordinates":[1.25,0.5],"type":"Point"}
south{"coordinates":[10.5,10.5],"type":"Point"}

GEO_SEARCH — an indexed bbox/radius search over a table, backed by a lazy,

per-table grid index invalidated automatically on writes. Exact for Point
columns; for polygon/line columns it indexes by centroid (a large shape
whose edge -- not centroid -- clips into the window is a false negative;
use GEO_INTERSECTS for exact shape overlap).
region
north
north

region
north
north

(ok)
(ok)
(ok)
(ok)
(ok)
(ok)
(ok)
EQUAL_INTERVAL / NATURAL_BREAKS: choropleth classification window
functions, bucketing a KPI column into N legend classes. Quantile
classification needs no new function -- NTILE(n) OVER (ORDER BY kpi)
already does that.
districtbuildingsequal_interval_classnatural_breaks_classquantile_class
a5111
b12111
c48222
d51222
e95333

(ok)
(ok)

TILE FUNCTIONS

Web Mercator XYZ tile addressing, for working with MBTiles tilesets in
SQL. XYZ (web clients, /{z}/{x}/{y}.png) counts rows from the top; TMS
(what MBTiles stores in tiles.tile_row) counts rows from the bottom.
TILE_X / TILE_Y: the XYZ tile column/row containing (lon, zoom) / (lat, zoom)
colROW
88025373

TILE_ZXY — the covering tile at one call, including the MBTiles TMS row

tile
{"tile_row":11010,"x":8802,"y":5373,"z":14}

TILE_FLIP_Y — convert an XYZ row to/from the MBTiles TMS tile_row (its own

inverse). TILE_ROW_TMS is the alias.
tms_row
11010

TILE_LON / TILE_LAT: the (west, north) edge of a tile
west_edgenorth_edge
13.403320312552.522905940278065

TILE_BBOX — a tile's geographic bounds as [west, south, east, north]

bounds
[13.4033203125,52.50953477032728,13.42529296875,52.522905940278065]

TILE_QUADKEY / TILE_FROM_QUADKEY: Bing Maps quadkey encoding, and its inverse
quadkey
12021023322212

tile
{"x":8500,"y":5286,"z":14}

TILE_PARENT — the containing tile one zoom level up (NULL at zoom 0)

parent
{"x":4401,"y":2686,"z":13}

TILE_COUNT — how many tiles a fully populated zoom level holds (4^zoom)

tiles_at_zoom_10
1048576

TILE_CONTAINS — whether a tile covers a point (edges belong to the tile on

their north/west sides, matching TILE_X/TILE_Y's own assignment)
covers_point
true

(ok)
(ok)
A tiles table lookup, converting the client's XYZ row to the MBTiles TMS
row stored on disk:
tile_data
[0]

(ok)