Skip to main content


This page uses Wasm code blocks so you can run the examples directly in the browser.

The order of the objects in the bag returned by a regular set query is not predetermined unless an order by clause is specified. However, even if order by is specified the system will not preserve the order of the result of a set query if it is inside in other operations.

If it is required to maintain the order of a set of data values the data type Vector has to be used. The collection data type Vector represents ordered collections of any kinds of objects; a common case of a vector is a numerical vector of numbers. In case the order of a query result is significant you can specify vector queries that preserve the order in a query result by returning vectors rather than the bags returned by regular set queries. This is particularly important when working with numerical vectors. A vector query can be one of the following:

  1. It can be a vector construction expression that creates a new vector from other objects.

  2. It can be a call to vector function returning vectors as result.

  3. It can be a vector indexing expression that accesses vector elements by their indexes.

  4. It can be a select vector query that returns an ordered vector rather than an unordered bag as the regular set query.

  5. It can be a regular set query returning a set of constructed vectors.

Vector construction

The vector constructor [...] notation creates a single vector with explicit contents. The following query constructs a vector of three numbers:


The following query constructs a bag of vectors holding the natural numbers up to ten together with their square roots:

select [n,sqrt(n)]
from Integer n
where n in range(10)

Vector functions

Functions over vectors are divided into:

Numerical vector functions

The following infix operators over numeric vectors v and w, matrices a and b, and numbers lambda are defined:

operationdescriptionimplemented as function
v + welement-wise additionplus
v .+ w-"-plus
lambda + wadd number to elementsplus
v + lambda-"-plus
v - welement-wise subtractionsminus
v .- w-"-minus
-vnegate elementsuminus
v - lambdasubtract number from elementsminus
lambda - wsubtract elements from numberminus
v * wscalar producttimes
a * bmatrix producttimes
v * bscalar productstimes
a * wscalar productstimes
v .* welement-wise multiplicationselemtimes
lambda * wmultiply elements with numbertimes
v * lambda-"-times
v ./ welement-wise divisionelemdiv
v / lambdadivide elements with numberdiv
lambda / wdivide number with elementsdiv
v .^ lambdaelement-wise exponentelempower


Vector addition:

Add vector elements:



Number and vector addition:



Add nested vector elements:



Vector subtraction:

Subtract vector elements:



Number and vector subtraction:



Vector division:

Number and vector division:



Element division:


Nested element division:


Multiply vector elements:

Scalar product:


Number and vector multiplication:



Element multiplication:


Nested element multiplication:


Raise each element to a given exponent:

[1,2,3] .^ 2

Round each element in a vector of numbers to two decimals:


Compute the max value of vector elements:



Compute the min value of vector elements:



Vector aggregate functions

Dimension-wise aggregates over bags of vectors can be computed using the function aggv().


   aggv((select [i, i + 10]
from Integer i
where i in range(1, 10)), thefunction('mean'))

Notice that the function mean() is overloaded and thefunction() has to be use to get the generic mean() function.

Each dimension in a bag of vector of number can be normalized using one of the normalization functions meansub(), zscore(), or maxmin():

   meansub(Bag of Vector of Number b) -> Bag of Vector of Number
zscore(Bag of Vector of Number b) -> Bag of Vector of Number
maxmin(Bag of Vector of Number b) -> Bag of Vector of Number

meansub() transforms each dimension to a N(0, s) distribution (assuming that the dimension was N(u, s) distributed) by subtracting the mean u of each dimension.

zscore() transforms each dimension to a N(0, 1) distribution by also dividing by the standard deviation of each dimension.

maxmin() transforms each dimension to be on the [0, 1] interval by applying the transformation (w - min) ./ (max - min) to each vector w in bag b where max and min are computed using aggv(b, #' max') and aggv(b, #'min') respectively.


   meansub((select [i, i/2 + 10]
from Integer i
where i in range(1, 5)))

Sequence functions

The function section(v,l,u), returns a splice of a vector v, e.g.:


The function skip(v,n) returns the part of vector v starting at position n+1, e.g.:


The function permute(v,indl) permutes the elements of a vector, e.g.:


The select vector query

A select vector query provides a powerful way to construct new vectors by queries. It has the same syntax as a set query except for the keywords Vector of following the select clause. The difference is that whereas a set query returns a bag of objects, a select vector query returns a single vector of objects.


select Vector of i*2
from Integer i
where i in [1,2,3]
order by i

Notice that the order by clause normally should be present when constructing vectors with a select vector query in order to exactly specify the order of the elements in the vector. If no order by clause is present the order of the elements in the vector is arbitrarily chosen by the system being the order that is the most efficient to produce.

The built-in function range()is often used constructing vectors.

For example:

select Vector of i
from Integer i
where i in range(-4,5)
order by i

Vector functions and operators can be used in queries.

For example:

select lambda
from Number lambda
where [1, 2] - lambda = [11, 12]

If the equation has no solution, the query will have no result, for example:

select lambda
from Integer lambda
where [1, 3] - lambda = [11, 12]

By contrast, this query returns the vector [-10,-10]

select lambda
from Vector of Integer lambda
where [1, 2] - lambda = [11, 12]

returns [-10,-10].

Accessing vector elements

Vector elements can be accessed using the [..] syntax. The first element in a vector has index 1.


select a[1] + a[2]
from Vector of Integer a
where a = [1,2,3]

Index variables as integers can be used in queries to specify iteration over all possibles index positions of a vector.


select Vector of a[i]
from Vector a, Integer i
where a[i] != 2
and a = [1,2,3]
order by i

The query should be read as "Make a vector v of all vector elements ai in a where ai is not two and order the elements in v on index i."

The following query multiplies the elements of the vectors bound to the session variables :u and :v:

set :u = [1,2,3];
set :v = [4,5,6];

select Vector of :u[i] * :v[i]
from Integer i
order by i


Vector functions