InputLayer Builtin Functions Reference
Overview
InputLayer provides 55 builtin functions for vector operations, temporal processing, quantization, string manipulation, and math utilities.
Table of Contents
- Distance Functions
- Vector Operations
- LSH Functions
- Quantization Functions
- Int8 Distance Functions
- Temporal Functions
- Math Functions
- String Functions
- Scalar Min/Max Functions
1. Distance Functions
Distance functions for f32 vectors. All return Float64.
euclidean(v1, v2)
Euclidean (L2) distance between two vectors.
// Syntax
D = euclidean(V1, V2)
// Example
similar(Id1, Id2, Dist) <-
vectors(Id1, V1),
vectors(Id2, V2),
Id1 < Id2,
Dist = euclidean(V1, V2),
Dist < 1.0
| Parameter | Type | Description |
|---|---|---|
| v1 | Vector (f32) | First vector |
| v2 | Vector (f32) | Second vector |
| Returns | Float64 | Euclidean distance (>= 0) |
cosine(v1, v2)
Cosine distance (1 - cosine similarity) between two vectors.
// Syntax
D = cosine(V1, V2)
// Example - Find similar documents
similar_docs(Id1, Id2) <-
doc_embedding(Id1, V1),
doc_embedding(Id2, V2),
D = cosine(V1, V2),
D < 0.1 // Very similar (close to 0)
| Parameter | Type | Description |
|---|---|---|
| v1 | Vector (f32) | First vector |
| v2 | Vector (f32) | Second vector |
| Returns | Float64 | Cosine distance (range [0, 2]) |
Note: Returns 0 for identical directions, 1 for orthogonal, 2 for opposite directions.
dot(v1, v2)
Dot product of two vectors.
// Syntax
Score = dot(V1, V2)
// Example - Compute relevance scores
relevance(QueryId, DocId, Score) <-
query_vector(QueryId, Q),
doc_vector(DocId, D),
Score = dot(Q, D)
| Parameter | Type | Description |
|---|---|---|
| v1 | Vector (f32) | First vector |
| v2 | Vector (f32) | Second vector |
| Returns | Float64 | Dot product |
manhattan(v1, v2)
Manhattan (L1) distance between two vectors. Good for sparse vectors.
// Syntax
D = manhattan(V1, V2)
// Example
nearby(Id1, Id2) <-
location(Id1, V1),
location(Id2, V2),
D = manhattan(V1, V2),
D < 10.0
| Parameter | Type | Description |
|---|---|---|
| v1 | Vector (f32) | First vector |
| v2 | Vector (f32) | Second vector |
| Returns | Float64 | Manhattan distance (>= 0) |
2. Vector Operations
Operations for manipulating vectors.
normalize(v)
Normalize vector to unit length.
// Syntax
Normalized = normalize(V)
// Example - Normalize before cosine similarity
normalized_embedding(Id, NormV) <-
raw_embedding(Id, V),
NormV = normalize(V)
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Input vector |
| Returns | Vector (f32) | Unit vector (length = 1) |
Note: Returns zero vector if input is zero vector.
vec_dim(v)
Get the dimension (length) of a float32 vector.
// Syntax
Dim = vec_dim(V)
// Example - Filter by dimension
valid_embedding(Id, V) <-
embedding(Id, V),
Dim = vec_dim(V),
Dim = 128
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Input vector |
| Returns | Int64 | Number of dimensions |
vec_add(v1, v2)
Element-wise addition of two vectors.
// Syntax
Sum = vec_add(V1, V2)
// Example - Combine embeddings
combined(Id, SumV) <-
text_embedding(Id, T),
image_embedding(Id, I),
SumV = vec_add(T, I)
| Parameter | Type | Description |
|---|---|---|
| v1 | Vector (f32) | First vector |
| v2 | Vector (f32) | Second vector |
| Returns | Vector (f32) | Element-wise sum |
Note: Vectors must have same dimension.
vec_scale(v, scalar)
Scale vector by a scalar value.
// Syntax
Scaled = vec_scale(V, S)
// Example - Apply weight
weighted(Id, ScaledV) <-
embedding(Id, V),
weight(Id, W),
ScaledV = vec_scale(V, W)
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Input vector |
| scalar | Float64 | Scale factor |
| Returns | Vector (f32) | Scaled vector |
3. LSH (Locality Sensitive Hashing) Functions
Functions for approximate nearest neighbor search.
lsh_bucket(v, table_idx, num_hyperplanes)
Compute LSH bucket ID for a float32 vector.
// Syntax
Bucket = lsh_bucket(V, TableIdx, NumHyperplanes)
// Example - Build LSH index
lsh_index(Id, Table, Bucket) <-
embedding(Id, V),
Table = 0,
Bucket = lsh_bucket(V, Table, 8) // 8 hyperplanes = 256 buckets
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Input vector |
| table_idx | Int64 | Hash table index (for multiple tables) |
| num_hyperplanes | Int64 | Number of hyperplanes (controls granularity) |
| Returns | Int64 | Bucket ID |
Note: More hyperplanes = more buckets = higher precision but lower recall.
lsh_probes(bucket, num_hp, num_probes)
Generate multi-probe sequence for a bucket by Hamming distance.
// Syntax
Probes = lsh_probes(Bucket, NumHyperplanes, NumProbes)
// Example - Get probe sequence
probe_buckets(OrigBucket, Probes) <-
query_bucket(OrigBucket),
Probes = lsh_probes(OrigBucket, 8, 4)
| Parameter | Type | Description |
|---|---|---|
| bucket | Int64 | Original bucket ID |
| num_hp | Int64 | Number of hyperplanes used |
| num_probes | Int64 | Number of probe buckets to generate |
| Returns | Vector | Probe bucket IDs |
lsh_multi_probe(v, table_idx, num_hp, num_probes)
Compute LSH bucket and probes in one call for float32 vectors.
// Syntax
Buckets = lsh_multi_probe(V, TableIdx, NumHyperplanes, NumProbes)
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Query vector |
| table_idx | Int64 | Hash table index |
| num_hp | Int64 | Number of hyperplanes |
| num_probes | Int64 | Number of probes |
| Returns | Vector | Bucket IDs to probe |
4. Quantization Functions
Functions for int8 vector quantization (memory-efficient storage).
quantize_linear(v)
Linear quantization: maps [min, max] to [-128, 127].
// Syntax
QV = quantize_linear(V)
// Example - Compress embeddings
compressed(Id, QV) <-
embedding(Id, V),
QV = quantize_linear(V)
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Input vector |
| Returns | VectorInt8 | Quantized vector |
quantize_symmetric(v)
Symmetric quantization: maps [-max_abs, max_abs] to [-127, 127], preserving zero.
QV = quantize_symmetric(V)
| Parameter | Type | Description |
|---|---|---|
| v | Vector (f32) | Input vector |
| Returns | VectorInt8 | Quantized vector |
Note: Better preserves zero values; recommended for normalized vectors.
dequantize(v)
Convert int8 vector back to f32.
FV = dequantize(QV)
| Parameter | Type | Description |
|---|---|---|
| v | VectorInt8 | Quantized vector |
| Returns | Vector (f32) | Dequantized vector |
Note: Lossy conversion - original precision not fully recovered.
dequantize_scaled(v, scale)
Dequantize with explicit scale factor.
FV = dequantize_scaled(QV, Scale)
| Parameter | Type | Description |
|---|---|---|
| v | VectorInt8 | Quantized vector |
| scale | Float64 | Scale factor |
| Returns | Vector (f32) | Dequantized vector |
5. Int8 Distance Functions
Native (Fast) Int8 Distance
Direct computation on int8 values for maximum speed.
euclidean_int8(v1, v2)
Euclidean distance for int8 vectors.
D = euclidean_int8(QV1, QV2)
| Parameter | Type | Description |
|---|---|---|
| v1 | VectorInt8 | First quantized vector |
| v2 | VectorInt8 | Second quantized vector |
| Returns | Float64 | Euclidean distance |
cosine_int8(v1, v2)
Cosine distance for int8 vectors.
D = cosine_int8(QV1, QV2)
| Parameter | Type | Description |
|---|---|---|
| v1 | VectorInt8 | First quantized vector |
| v2 | VectorInt8 | Second quantized vector |
| Returns | Float64 | Cosine distance [0, 2] |
dot_int8(v1, v2)
Dot product for int8 vectors.
Score = dot_int8(QV1, QV2)
| Parameter | Type | Description |
|---|---|---|
| v1 | VectorInt8 | First quantized vector |
| v2 | VectorInt8 | Second quantized vector |
| Returns | Float64 | Dot product |
manhattan_int8(v1, v2)
Manhattan distance for int8 vectors.
D = manhattan_int8(QV1, QV2)
| Parameter | Type | Description |
|---|---|---|
| v1 | VectorInt8 | First quantized vector |
| v2 | VectorInt8 | Second quantized vector |
| Returns | Float64 | Manhattan distance |
6. Temporal Functions
Functions for time-based queries and temporal reasoning.
time_now()
Get current Unix timestamp in milliseconds.
Now = time_now()
| Parameter | Type | Description |
|---|---|---|
| (none) | - | No parameters |
| Returns | Int64 | Unix timestamp (milliseconds) |
time_diff(t1, t2)
Compute difference between two timestamps.
Diff = time_diff(T1, T2)
| Parameter | Type | Description |
|---|---|---|
| t1 | Int64 | First timestamp |
| t2 | Int64 | Second timestamp |
| Returns | Int64 | Difference (t1 - t2) in milliseconds |
time_add(ts, duration_ms)
Add duration to timestamp.
NewTime = time_add(Ts, DurationMs)
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Base timestamp |
| duration_ms | Int64 | Duration to add (milliseconds) |
| Returns | Int64 | New timestamp |
time_sub(ts, duration_ms)
Subtract duration from timestamp.
NewTime = time_sub(Ts, DurationMs)
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Base timestamp |
| duration_ms | Int64 | Duration to subtract (milliseconds) |
| Returns | Int64 | New timestamp |
time_decay(ts, now, half_life_ms)
Exponential time decay. Formula: 0.5^(age/half_life).
Weight = time_decay(Ts, Now, HalfLifeMs)
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Event timestamp |
| now | Int64 | Current timestamp |
| half_life_ms | Int64 | Half-life in milliseconds |
| Returns | Float64 | Decay factor [0, 1] |
Note: Returns 1.0 at ts=now, 0.5 at age=half_life, approaches 0 for old events.
time_decay_linear(ts, now, max_age_ms)
Linear time decay. Formula: max(0, 1 - age/max_age).
Weight = time_decay_linear(Ts, Now, MaxAgeMs)
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Event timestamp |
| now | Int64 | Current timestamp |
| max_age_ms | Int64 | Maximum age for decay |
| Returns | Float64 | Decay factor [0, 1] |
Note: Returns 1.0 at ts=now, 0.5 at age=max_age/2, 0.0 at age>=max_age.
time_before(t1, t2)
Check if t1 is before t2.
| Parameter | Type | Description |
|---|---|---|
| t1 | Int64 | First timestamp |
| t2 | Int64 | Second timestamp |
| Returns | Bool | true if t1 < t2 |
time_after(t1, t2)
Check if t1 is after t2.
| Parameter | Type | Description |
|---|---|---|
| t1 | Int64 | First timestamp |
| t2 | Int64 | Second timestamp |
| Returns | Bool | true if t1 > t2 |
time_between(ts, start, end)
Check if timestamp is within range [start, end] (inclusive).
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Timestamp to check |
| start | Int64 | Window start |
| end | Int64 | Window end |
| Returns | Bool | true if start <= ts <= end |
within_last(ts, now, duration_ms)
Check if timestamp is within the last duration from now.
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Timestamp to check |
| now | Int64 | Current timestamp |
| duration_ms | Int64 | Window size in milliseconds |
| Returns | Bool | true if (now - duration_ms) <= ts <= now |
intervals_overlap(s1, e1, s2, e2)
Check if two time intervals overlap.
| Parameter | Type | Description |
|---|---|---|
| s1 | Int64 | First interval start |
| e1 | Int64 | First interval end |
| s2 | Int64 | Second interval start |
| e2 | Int64 | Second interval end |
| Returns | Bool | true if intervals overlap |
interval_contains(s1, e1, s2, e2)
Check if interval [s1, e1] fully contains interval [s2, e2].
| Parameter | Type | Description |
|---|---|---|
| s1 | Int64 | Outer interval start |
| e1 | Int64 | Outer interval end |
| s2 | Int64 | Inner interval start |
| e2 | Int64 | Inner interval end |
| Returns | Bool | true if [s1,e1] contains [s2,e2] |
interval_duration(start, end)
Get the duration of an interval.
| Parameter | Type | Description |
|---|---|---|
| start | Int64 | Interval start |
| end | Int64 | Interval end |
| Returns | Int64 | Duration (end - start) in milliseconds |
point_in_interval(ts, start, end)
Check if a point is within an interval [start, end] (inclusive).
| Parameter | Type | Description |
|---|---|---|
| ts | Int64 | Point timestamp |
| start | Int64 | Interval start |
| end | Int64 | Interval end |
| Returns | Bool | true if start <= ts <= end |
7. Math Functions
General-purpose math functions. All accept Int64 or Float64 inputs (coerced to f64 internally unless noted).
abs(x)
Generic absolute value. Preserves input type.
A = abs(X)
| Parameter | Type | Description |
|---|---|---|
| x | Int64 or Float64 | Input value |
| Returns | Same as input | Absolute value |
abs_int64(x)
Absolute value of an integer.
AbsVal = abs_int64(X)
| Parameter | Type | Description |
|---|---|---|
| x | Int64 | Input integer |
| Returns | Int64 | Absolute value |
Note: Uses saturating arithmetic for i64::MIN.
abs_float64(x)
Absolute value of a float.
AbsVal = abs_float64(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 | Input float |
| Returns | Float64 | Absolute value |
sqrt(x)
Square root.
R = sqrt(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Input value (>= 0) |
| Returns | Float64 | Square root |
Note: Returns Null if x < 0.
pow(base, exp)
Power function.
R = pow(Base, Exp)
| Parameter | Type | Description |
|---|---|---|
| base | Float64 or Int64 | Base value |
| exp | Float64 or Int64 | Exponent |
| Returns | Float64 | base^exp |
log(x)
Natural logarithm (base e).
R = log(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Input value (> 0) |
| Returns | Float64 | Natural logarithm |
Note: Returns Null if x <= 0.
exp(x)
Exponential function (e^x).
R = exp(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Input value |
| Returns | Float64 | e^x |
sin(x)
Sine function (radians).
R = sin(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Angle in radians |
| Returns | Float64 | Sine of x |
cos(x)
Cosine function (radians).
R = cos(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Angle in radians |
| Returns | Float64 | Cosine of x |
tan(x)
Tangent function (radians).
R = tan(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Angle in radians |
| Returns | Float64 | Tangent of x |
floor(x)
Round down to nearest integer.
R = floor(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Input value |
| Returns | Int64 | Largest integer <= x |
ceil(x)
Round up to nearest integer.
R = ceil(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Input value |
| Returns | Int64 | Smallest integer >= x |
sign(x)
Sign function. Returns -1, 0, or 1.
S = sign(X)
| Parameter | Type | Description |
|---|---|---|
| x | Float64 or Int64 | Input value |
| Returns | Int64 | -1 (negative), 0 (zero), or 1 (positive) |
Note: For float NaN, returns 0.
8. String Functions
Functions for string manipulation.
len(s)
Get string length (byte count).
L = len(S)
// Example
long_names(Name, L) <-
names(Name),
L = len(Name),
L > 10
| Parameter | Type | Description |
|---|---|---|
| s | String | Input string |
| Returns | Int64 | Byte length of string |
upper(s)
Convert string to uppercase (Unicode-aware).
U = upper(S)
| Parameter | Type | Description |
|---|---|---|
| s | String | Input string |
| Returns | String | Uppercase string |
lower(s)
Convert string to lowercase (Unicode-aware).
L = lower(S)
| Parameter | Type | Description |
|---|---|---|
| s | String | Input string |
| Returns | String | Lowercase string |
trim(s)
Remove leading and trailing whitespace.
T = trim(S)
| Parameter | Type | Description |
|---|---|---|
| s | String | Input string |
| Returns | String | Trimmed string |
substr(s, start, len)
Extract a substring.
Sub = substr(S, Start, Len)
// Example - Get first 3 characters
prefix(Name, P) <-
names(Name),
P = substr(Name, 0, 3)
| Parameter | Type | Description |
|---|---|---|
| s | String | Input string |
| start | Int64 | Start byte index (0-based) |
| len | Int64 | Maximum length to extract |
| Returns | String | Extracted substring |
Note: Returns empty string if start > string length. Clamps to string bounds.
replace(s, find, replacement)
Replace all occurrences of a substring.
R = replace(S, Find, Replacement)
// Example
cleaned(R) <-
raw("hello-world"),
R = replace("hello-world", "-", " ")
| Parameter | Type | Description |
|---|---|---|
| s | String | Input string |
| find | String | Substring to find |
| replacement | String | Replacement string |
| Returns | String | String with all occurrences replaced |
concat(s1, s2, ...)
Concatenate multiple values into a string. Variable arity (2+ arguments).
R = concat(S1, S2)
// Example - Build full name
full_name(First, Last, Full) <-
person(First, Last),
Full = concat(First, " ", Last)
| Parameter | Type | Description |
|---|---|---|
| s1, s2, ... | String/Int64/Float64 | Values to concatenate |
| Returns | String | Concatenated string |
Note: Non-string values are automatically converted to their string representation.
9. Scalar Min/Max Functions
Scalar comparison functions returning the minimum or maximum of two values.
min_val(a, b)
Return the smaller of two values.
M = min_val(A, B)
// Example - Clamp to range
clamped(X, C) <-
values(X),
C = max_val(0, min_val(X, 100))
| Parameter | Type | Description |
|---|---|---|
| a | Int64/Float64/String | First value |
| b | Int64/Float64/String | Second value |
| Returns | Same as input | Smaller of the two values |
Note: Do not confuse with the min aggregation. min_val is a scalar function comparing two values. Mixed numeric types (Int64 + Float64) return Float64. Strings compare lexicographically.
max_val(a, b)
Return the larger of two values.
M = max_val(A, B)
| Parameter | Type | Description |
|---|---|---|
| a | Int64/Float64/String | First value |
| b | Int64/Float64/String | Second value |
| Returns | Same as input | Larger of the two values |
Note: Do not confuse with the max aggregation. max_val is a scalar function comparing two values. Mixed numeric types (Int64 + Float64) return Float64. Strings compare lexicographically.
Appendix: Function Quick Reference
| Function | Parameters | Returns | Category |
|---|---|---|---|
euclidean | (v1, v2) | Float64 | Distance |
cosine | (v1, v2) | Float64 | Distance |
dot | (v1, v2) | Float64 | Distance |
manhattan | (v1, v2) | Float64 | Distance |
normalize | (v) | Vector | Vector Ops |
vec_dim | (v) | Int64 | Vector Ops |
vec_add | (v1, v2) | Vector | Vector Ops |
vec_scale | (v, s) | Vector | Vector Ops |
lsh_bucket | (v, table, hp) | Int64 | LSH |
lsh_probes | (bucket, hp, n) | Vector | LSH |
lsh_multi_probe | (v, table, hp, n) | Vector | LSH |
quantize_linear | (v) | VectorInt8 | Quantization |
quantize_symmetric | (v) | VectorInt8 | Quantization |
dequantize | (qv) | Vector | Quantization |
dequantize_scaled | (qv, s) | Vector | Quantization |
euclidean_int8 | (qv1, qv2) | Float64 | Int8 Distance |
cosine_int8 | (qv1, qv2) | Float64 | Int8 Distance |
dot_int8 | (qv1, qv2) | Float64 | Int8 Distance |
manhattan_int8 | (qv1, qv2) | Float64 | Int8 Distance |
time_now | () | Int64 | Temporal |
time_diff | (t1, t2) | Int64 | Temporal |
time_add | (ts, dur) | Int64 | Temporal |
time_sub | (ts, dur) | Int64 | Temporal |
time_decay | (ts, now, hl) | Float64 | Temporal |
time_decay_linear | (ts, now, max) | Float64 | Temporal |
time_before | (t1, t2) | Bool | Temporal |
time_after | (t1, t2) | Bool | Temporal |
time_between | (ts, s, e) | Bool | Temporal |
within_last | (ts, now, dur) | Bool | Temporal |
intervals_overlap | (s1, e1, s2, e2) | Bool | Temporal |
interval_contains | (s1, e1, s2, e2) | Bool | Temporal |
interval_duration | (s, e) | Int64 | Temporal |
point_in_interval | (ts, s, e) | Bool | Temporal |
abs | (x) | same type | Math |
abs_int64 | (x) | Int64 | Math |
abs_float64 | (x) | Float64 | Math |
sqrt | (x) | Float64 | Math |
pow | (base, exp) | Float64 | Math |
log | (x) | Float64 | Math |
exp | (x) | Float64 | Math |
sin | (x) | Float64 | Math |
cos | (x) | Float64 | Math |
tan | (x) | Float64 | Math |
floor | (x) | Int64 | Math |
ceil | (x) | Int64 | Math |
sign | (x) | Int64 | Math |
len | (s) | Int64 | String |
upper | (s) | String | String |
lower | (s) | String | String |
trim | (s) | String | String |
substr | (s, start, len) | String | String |
replace | (s, find, repl) | String | String |
concat | (s1, s2, ...) | String | String |
min_val | (a, b) | same type | Min/Max |
max_val | (a, b) | same type | Min/Max |