InputLayer Builtin Functions Reference


Overview

InputLayer provides 55 builtin functions for vector operations, temporal processing, quantization, string manipulation, and math utilities.


Table of Contents

  1. Distance Functions
  2. Vector Operations
  3. LSH Functions
  4. Quantization Functions
  5. Int8 Distance Functions
  6. Temporal Functions
  7. Math Functions
  8. String Functions
  9. 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
ParameterTypeDescription
v1Vector (f32)First vector
v2Vector (f32)Second vector
ReturnsFloat64Euclidean 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)
ParameterTypeDescription
v1Vector (f32)First vector
v2Vector (f32)Second vector
ReturnsFloat64Cosine 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)
ParameterTypeDescription
v1Vector (f32)First vector
v2Vector (f32)Second vector
ReturnsFloat64Dot 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
ParameterTypeDescription
v1Vector (f32)First vector
v2Vector (f32)Second vector
ReturnsFloat64Manhattan 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)
ParameterTypeDescription
vVector (f32)Input vector
ReturnsVector (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
ParameterTypeDescription
vVector (f32)Input vector
ReturnsInt64Number 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)
ParameterTypeDescription
v1Vector (f32)First vector
v2Vector (f32)Second vector
ReturnsVector (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)
ParameterTypeDescription
vVector (f32)Input vector
scalarFloat64Scale factor
ReturnsVector (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
ParameterTypeDescription
vVector (f32)Input vector
table_idxInt64Hash table index (for multiple tables)
num_hyperplanesInt64Number of hyperplanes (controls granularity)
ReturnsInt64Bucket 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)
ParameterTypeDescription
bucketInt64Original bucket ID
num_hpInt64Number of hyperplanes used
num_probesInt64Number of probe buckets to generate
ReturnsVectorProbe 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)
ParameterTypeDescription
vVector (f32)Query vector
table_idxInt64Hash table index
num_hpInt64Number of hyperplanes
num_probesInt64Number of probes
ReturnsVectorBucket 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)
ParameterTypeDescription
vVector (f32)Input vector
ReturnsVectorInt8Quantized vector

quantize_symmetric(v)

Symmetric quantization: maps [-max_abs, max_abs] to [-127, 127], preserving zero.

QV = quantize_symmetric(V)
ParameterTypeDescription
vVector (f32)Input vector
ReturnsVectorInt8Quantized vector

Note: Better preserves zero values; recommended for normalized vectors.


dequantize(v)

Convert int8 vector back to f32.

FV = dequantize(QV)
ParameterTypeDescription
vVectorInt8Quantized vector
ReturnsVector (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)
ParameterTypeDescription
vVectorInt8Quantized vector
scaleFloat64Scale factor
ReturnsVector (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)
ParameterTypeDescription
v1VectorInt8First quantized vector
v2VectorInt8Second quantized vector
ReturnsFloat64Euclidean distance

cosine_int8(v1, v2)

Cosine distance for int8 vectors.

D = cosine_int8(QV1, QV2)
ParameterTypeDescription
v1VectorInt8First quantized vector
v2VectorInt8Second quantized vector
ReturnsFloat64Cosine distance [0, 2]

dot_int8(v1, v2)

Dot product for int8 vectors.

Score = dot_int8(QV1, QV2)
ParameterTypeDescription
v1VectorInt8First quantized vector
v2VectorInt8Second quantized vector
ReturnsFloat64Dot product

manhattan_int8(v1, v2)

Manhattan distance for int8 vectors.

D = manhattan_int8(QV1, QV2)
ParameterTypeDescription
v1VectorInt8First quantized vector
v2VectorInt8Second quantized vector
ReturnsFloat64Manhattan distance

6. Temporal Functions

Functions for time-based queries and temporal reasoning.

time_now()

Get current Unix timestamp in milliseconds.

Now = time_now()
ParameterTypeDescription
(none)-No parameters
ReturnsInt64Unix timestamp (milliseconds)

time_diff(t1, t2)

Compute difference between two timestamps.

Diff = time_diff(T1, T2)
ParameterTypeDescription
t1Int64First timestamp
t2Int64Second timestamp
ReturnsInt64Difference (t1 - t2) in milliseconds

time_add(ts, duration_ms)

Add duration to timestamp.

NewTime = time_add(Ts, DurationMs)
ParameterTypeDescription
tsInt64Base timestamp
duration_msInt64Duration to add (milliseconds)
ReturnsInt64New timestamp

time_sub(ts, duration_ms)

Subtract duration from timestamp.

NewTime = time_sub(Ts, DurationMs)
ParameterTypeDescription
tsInt64Base timestamp
duration_msInt64Duration to subtract (milliseconds)
ReturnsInt64New timestamp

time_decay(ts, now, half_life_ms)

Exponential time decay. Formula: 0.5^(age/half_life).

Weight = time_decay(Ts, Now, HalfLifeMs)
ParameterTypeDescription
tsInt64Event timestamp
nowInt64Current timestamp
half_life_msInt64Half-life in milliseconds
ReturnsFloat64Decay 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)
ParameterTypeDescription
tsInt64Event timestamp
nowInt64Current timestamp
max_age_msInt64Maximum age for decay
ReturnsFloat64Decay 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.

ParameterTypeDescription
t1Int64First timestamp
t2Int64Second timestamp
ReturnsBooltrue if t1 < t2

time_after(t1, t2)

Check if t1 is after t2.

ParameterTypeDescription
t1Int64First timestamp
t2Int64Second timestamp
ReturnsBooltrue if t1 > t2

time_between(ts, start, end)

Check if timestamp is within range [start, end] (inclusive).

ParameterTypeDescription
tsInt64Timestamp to check
startInt64Window start
endInt64Window end
ReturnsBooltrue if start <= ts <= end

within_last(ts, now, duration_ms)

Check if timestamp is within the last duration from now.

ParameterTypeDescription
tsInt64Timestamp to check
nowInt64Current timestamp
duration_msInt64Window size in milliseconds
ReturnsBooltrue if (now - duration_ms) <= ts <= now

intervals_overlap(s1, e1, s2, e2)

Check if two time intervals overlap.

ParameterTypeDescription
s1Int64First interval start
e1Int64First interval end
s2Int64Second interval start
e2Int64Second interval end
ReturnsBooltrue if intervals overlap

interval_contains(s1, e1, s2, e2)

Check if interval [s1, e1] fully contains interval [s2, e2].

ParameterTypeDescription
s1Int64Outer interval start
e1Int64Outer interval end
s2Int64Inner interval start
e2Int64Inner interval end
ReturnsBooltrue if [s1,e1] contains [s2,e2]

interval_duration(start, end)

Get the duration of an interval.

ParameterTypeDescription
startInt64Interval start
endInt64Interval end
ReturnsInt64Duration (end - start) in milliseconds

point_in_interval(ts, start, end)

Check if a point is within an interval [start, end] (inclusive).

ParameterTypeDescription
tsInt64Point timestamp
startInt64Interval start
endInt64Interval end
ReturnsBooltrue 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)
ParameterTypeDescription
xInt64 or Float64Input value
ReturnsSame as inputAbsolute value

abs_int64(x)

Absolute value of an integer.

AbsVal = abs_int64(X)
ParameterTypeDescription
xInt64Input integer
ReturnsInt64Absolute value

Note: Uses saturating arithmetic for i64::MIN.


abs_float64(x)

Absolute value of a float.

AbsVal = abs_float64(X)
ParameterTypeDescription
xFloat64Input float
ReturnsFloat64Absolute value

sqrt(x)

Square root.

R = sqrt(X)
ParameterTypeDescription
xFloat64 or Int64Input value (>= 0)
ReturnsFloat64Square root

Note: Returns Null if x < 0.


pow(base, exp)

Power function.

R = pow(Base, Exp)
ParameterTypeDescription
baseFloat64 or Int64Base value
expFloat64 or Int64Exponent
ReturnsFloat64base^exp

log(x)

Natural logarithm (base e).

R = log(X)
ParameterTypeDescription
xFloat64 or Int64Input value (> 0)
ReturnsFloat64Natural logarithm

Note: Returns Null if x <= 0.


exp(x)

Exponential function (e^x).

R = exp(X)
ParameterTypeDescription
xFloat64 or Int64Input value
ReturnsFloat64e^x

sin(x)

Sine function (radians).

R = sin(X)
ParameterTypeDescription
xFloat64 or Int64Angle in radians
ReturnsFloat64Sine of x

cos(x)

Cosine function (radians).

R = cos(X)
ParameterTypeDescription
xFloat64 or Int64Angle in radians
ReturnsFloat64Cosine of x

tan(x)

Tangent function (radians).

R = tan(X)
ParameterTypeDescription
xFloat64 or Int64Angle in radians
ReturnsFloat64Tangent of x

floor(x)

Round down to nearest integer.

R = floor(X)
ParameterTypeDescription
xFloat64 or Int64Input value
ReturnsInt64Largest integer <= x

ceil(x)

Round up to nearest integer.

R = ceil(X)
ParameterTypeDescription
xFloat64 or Int64Input value
ReturnsInt64Smallest integer >= x

sign(x)

Sign function. Returns -1, 0, or 1.

S = sign(X)
ParameterTypeDescription
xFloat64 or Int64Input value
ReturnsInt64-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
ParameterTypeDescription
sStringInput string
ReturnsInt64Byte length of string

upper(s)

Convert string to uppercase (Unicode-aware).

U = upper(S)
ParameterTypeDescription
sStringInput string
ReturnsStringUppercase string

lower(s)

Convert string to lowercase (Unicode-aware).

L = lower(S)
ParameterTypeDescription
sStringInput string
ReturnsStringLowercase string

trim(s)

Remove leading and trailing whitespace.

T = trim(S)
ParameterTypeDescription
sStringInput string
ReturnsStringTrimmed 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)
ParameterTypeDescription
sStringInput string
startInt64Start byte index (0-based)
lenInt64Maximum length to extract
ReturnsStringExtracted 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", "-", " ")
ParameterTypeDescription
sStringInput string
findStringSubstring to find
replacementStringReplacement string
ReturnsStringString 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)
ParameterTypeDescription
s1, s2, ...String/Int64/Float64Values to concatenate
ReturnsStringConcatenated 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))
ParameterTypeDescription
aInt64/Float64/StringFirst value
bInt64/Float64/StringSecond value
ReturnsSame as inputSmaller 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)
ParameterTypeDescription
aInt64/Float64/StringFirst value
bInt64/Float64/StringSecond value
ReturnsSame as inputLarger 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

FunctionParametersReturnsCategory
euclidean(v1, v2)Float64Distance
cosine(v1, v2)Float64Distance
dot(v1, v2)Float64Distance
manhattan(v1, v2)Float64Distance
normalize(v)VectorVector Ops
vec_dim(v)Int64Vector Ops
vec_add(v1, v2)VectorVector Ops
vec_scale(v, s)VectorVector Ops
lsh_bucket(v, table, hp)Int64LSH
lsh_probes(bucket, hp, n)VectorLSH
lsh_multi_probe(v, table, hp, n)VectorLSH
quantize_linear(v)VectorInt8Quantization
quantize_symmetric(v)VectorInt8Quantization
dequantize(qv)VectorQuantization
dequantize_scaled(qv, s)VectorQuantization
euclidean_int8(qv1, qv2)Float64Int8 Distance
cosine_int8(qv1, qv2)Float64Int8 Distance
dot_int8(qv1, qv2)Float64Int8 Distance
manhattan_int8(qv1, qv2)Float64Int8 Distance
time_now()Int64Temporal
time_diff(t1, t2)Int64Temporal
time_add(ts, dur)Int64Temporal
time_sub(ts, dur)Int64Temporal
time_decay(ts, now, hl)Float64Temporal
time_decay_linear(ts, now, max)Float64Temporal
time_before(t1, t2)BoolTemporal
time_after(t1, t2)BoolTemporal
time_between(ts, s, e)BoolTemporal
within_last(ts, now, dur)BoolTemporal
intervals_overlap(s1, e1, s2, e2)BoolTemporal
interval_contains(s1, e1, s2, e2)BoolTemporal
interval_duration(s, e)Int64Temporal
point_in_interval(ts, s, e)BoolTemporal
abs(x)same typeMath
abs_int64(x)Int64Math
abs_float64(x)Float64Math
sqrt(x)Float64Math
pow(base, exp)Float64Math
log(x)Float64Math
exp(x)Float64Math
sin(x)Float64Math
cos(x)Float64Math
tan(x)Float64Math
floor(x)Int64Math
ceil(x)Int64Math
sign(x)Int64Math
len(s)Int64String
upper(s)StringString
lower(s)StringString
trim(s)StringString
substr(s, start, len)StringString
replace(s, find, repl)StringString
concat(s1, s2, ...)StringString
min_val(a, b)same typeMin/Max
max_val(a, b)same typeMin/Max