Skip to main content
Version: Next

Functions

Functions appear in expressions and take positional and/or named arguments, producing a value as a result of their computation.

Function signatures have the following notation:

f(arg1:<type>, arg2=<type>, [arg3=type]) -> <type>
  • arg:<type>: positional argument
  • arg=<type>: named argument
  • [arg=type]: optional (named) argument
  • -> <type>: function return type

TQL features the uniform function call syntax (UFCS), which allows you to interchangeably call a function with at least one argument either as free function or method. For example, length(str) and str.length() resolve to the identical function call. The latter syntax is particularly suitable for function chaining, e.g., x.f().g().h() reads left-to-right as "start with x, apply f(), then g() and then h()," compared to h(g(f(x))), which reads "inside out."

Throughout our documentation, we use the free function style in the synopsis but often resort to the method style when it is more idiomatic.

Aggregation

FunctionDescriptionExample
allComputes the conjunction (AND) of all boolean valuesall([true,true,false])
anyComputes the disjunction (OR) of all boolean valuesany([true,false,true])
collectCreates a list of all non-null values, preserving duplicatescollect([1,2,2,3])
countCounts the events or non-null valuescount([1,2,null])
count_distinctCounts all distinct non-null valuescount_distinct([1,2,2,3])
distinctCreates a sorted list without duplicates of non-null valuesdistinct([1,2,2,3])
firstTakes the first non-null valuefirst([null,2,3])
lastTakes the last non-null valuelast([1,2,null])
maxComputes the maximum of all valuesmax([1,2,3])
meanComputes the mean of all valuesmean([1,2,3])
medianComputes the approximate median with a t-digest algorithmmedian([1,2,3,4])
minComputes the minimum of all valuesmin([1,2,3])
modeTakes the most common non-null valuemode([1,1,2,3])
quantileComputes the specified quantile q of valuesquantile([1,2,3,4], q=0.5)
stddevComputes the standard deviation of all valuesstddev([1,2,3])
sumComputes the sum of all valuessum([1,2,3])
value_countsReturns a list of values with their frequencyvalue_counts([1,2,2,3])
varianceComputes the variance of all valuesvariance([1,2,3])

List

FunctionDescriptionExample
appendInserts an element at the back of a listxs.append(y)
prependInserts an element at the front of a listxs.prepend(y)
concatenateMerges two listsconcatenate(xs, ys)
mapMaps each list element to an expressionxs.map(x, x + 3)
whereFilters list elements based on a predicatexs.where(x, x > 5)
zipCombines two lists into a list of pairszip(xs, ys)

String

Inspection

FunctionDescriptionExample
length_bytesReturns the length of a string in bytes"hello".length_bytes()
length_charsReturns the length of a string in characters"hello".length_chars()
starts_withChecks if a string starts with a substring"hello".starts_with("he")
ends_withChecks if a string ends with a substring"hello".ends_with("lo")
is_alnumChecks if a string is alphanumeric"hello123".is_alnum()
is_alphaChecks if a string contains only letters"hello".is_alpha()
is_lowerChecks if a string is in lowercase"hello".is_lower()
is_numericChecks if a string contains only numbers"1234".is_numeric()
is_printableChecks if a string contains only printable characters"hello".is_printable()
is_titleChecks if a string follows title case"Hello World".is_title()
is_upperChecks if a string is in uppercase"HELLO".is_upper()

Transformation

FunctionDescriptionExample
trimTrims whitespace from both ends of a string" hello ".trim()
trim_startTrims whitespace from the start of a string" hello".trim_start()
trim_endTrims whitespace from the end of a string"hello ".trim_end()
capitalizeCapitalizes the first character of a string"hello".capitalize()
replaceReplaces characters within a string"hello".replace("o", "a")
replace_regexReverses the characters of a string"hello".replace("l+o", "y")
reverseReverses the characters of a string"hello".reverse()
to_lowerConverts a string to lowercase"HELLO".to_lower()
to_titleConverts a string to title case"hello world".to_title()
to_upperConverts a string to uppercase"hello".to_upper()
splitSplits a string into substringssplit("a,b,c", ",")
split_regexSplits a string into substrings with a regexsplit_regex("a1b2c", r"\d")
joinJoins a list of strings into a single stringjoin(["a", "b", "c"], ",")

File Paths

FunctionDescriptionExample
file_nameExtracts the file name from a file pathfile_name("/path/to/log.json")
parent_dirExtracts the parent directory from a file pathparent_dir("/path/to/log.json")

Time & Date

FunctionDescriptionExample
as_secsConverts a duration into secondsas_secs(42ms)
from_epochInterprets a duration as Unix timefrom_epoch(time_ms * 1ms)
nowGets the current wallclock timenow()
since_epochTurns a time into a duration since the Unix epochsince_epoch(2021-02-24)
parse_timeParses a timestamp following a given format"10/11/2012".parse_time("%d/%m/%Y")

Math

FunctionDescriptionExample
ceilTakes the ceilingceil(4.2), ceil(3.2s, 1m)
floorTakes the floorfloor(4.2), floor(32h, 1d)
randomGenerates a random numberrandom()
roundRounds a valueround(4.2), round(31m, 1h)
sqrtCalculates the square rootsqrt(49)

Networking

FunctionDescriptionExample
community_idComputes a Community IDcommunity_id(src_ip=1.2.3.4, dst_ip=4.5.6.7, proto="tcp")
decapsulateDecapsulates PCAP packetsdecapsulate(this)
encrypt_cryptopanEncrypts IPs via Crypto-PAnencrypt_cryptopan(1.2.3.4)
is_v4Checks if an IP is IPv4is_v4(1.2.3.4)
is_v6Checks if an IP is IPv6is_v6(::1)

Hashing

FunctionDescriptionExample
hash_md5Computes a MD5 hash digesthash_md5("foo")
hash_sha1Computes a SHA1 hash digesthash_sha1("foo")
hash_sha224Computes a SHA224 hash digesthash_sha224("foo")
hash_sha256Computes a SHA256 hash digesthash_sha256("foo")
hash_sha384Computes a SHA384 hash digesthash_sha384("foo")
hash_sha512Computes a SHA512 hash digesthash_sha512("foo")
hash_xxh3Computes a XXH3 hash digesthash_xxh3("foo")

Encoding

functiondescriptionexample
encode_base64Encodes bytes as Base64encode_base64("Tenzir")
encode_hexEncodes bytes as their hexadecimal representationencode_hex("Tenzir")

Decoding

functiondescriptionexample
decode_base64Decodes bytes as Base64decode_base64("VGVuemly")
decode_hexDecodes bytes from their hexadecimal representationdecode_hex("4e6f6E6365")

Type System

Introspection

FunctionDescriptionExample
type_idRetrieves the type of an expressiontype_id(1 + 3.2)
hasChecks whether a record has a fieldrecord.has("field")
lengthRetrieves the length of a list[1,2,3].length()
networkRetrieves the network address of a subnet10.0.0.0/8.network()

Conversion

FunctionDescriptionExample
intCasts an expression to a signed integerint(-4.2)
uintCasts an expression to an unsigned integeruint(4.2)
floatCasts an expression to a floatfloat(42)
timeCasts an expression to a time valuetime("2020-03-15")
stringCasts an expression to stringstring(1.2.3.4)
ipCasts an expression to an IPip("1.2.3.4")
subnetCasts an expression to a subnetsubnet("1.2.3.4/16")

Transposition

FunctionDescriptionExample
flattenFlattens nested dataflatten(this)
unflattenUnflattens nested structuresunflatten(this)

Runtime

FunctionDescriptionExample
envReads an environment variableenv("PATH")
secretReads a secret from a storesecret("PATH")