Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Index

The index operation is denoted with square brackets and can be applied to any indexable data type and has the form:

<entity> [ <index> ]

where <entity> is an expression resulting in an indexable entity and <index> is an expression resulting in a value that is used to index the indexable entity.

Indexable entities include records, arrays, sets, maps, strings, and bytes.

If <entity> is a record, then the <index> operand must be coercible to a string and the result is the record’s field of that name.

If <entity> is an array, then the <index> operand must be coercible to an integer and the result is the value in the array of that index.

If <entity> is a set, then the <index> operand must be coercible to an integer and the result is the value in the set of that index ordered by total order of values.

If <entity> is a map, then the <index> operand is presumed to be a key and the corresponding value for that key is the result of the operation. If no such key exists in the map, then the result is error("missing").

If <entity> is a string, then the <index> operand must be coercible to an integer and the result is an integer representing the unicode code point at that offset in the string.

If <entity> is type bytes, then the <index> operand must be coercible to an integer and the result is an unsigned 8-bit integer representing the byte value at that offset in the bytes sequence.

Note

Indexing of strings and bytes is not yet implemented.

Index Base

Indexing in SuperSQL are 0-based meaning the first element is at index 0 and the last element is at index n-1 for an entity of size n.

If 1-based indexing is desired, a scoped language pragma may be used to specify either 1-based indexing or mixed indexing. In mixed indexing, 0-based indexing is used for expressions appearing in pipe operators and 1-based indexing is used for expressions appearing in SQL operators.

Note

Mixed indexing is not yet implemented.

Examples


Simple index

# spq
values a[2]
# input
{a:[1,2,3,4]}
{a:|[1,2,3,4]|}
{a:"1234"}
{a:0x01020304}
# expected output
3
3
error("missing")
error("missing")

One-based indexing

# spq
pragma index_base = 1
values a[2]
# input
{a:[1,2,3,4]}
{a:|[1,2,3,4]|}
{a:"1234"}
{a:0x01020304}
# expected output
2
2
error("missing")
error("missing")

Index from end of entity

# spq
values a[-1]
# input
{a:[1,2,3,4]}
{a:|[1,2,3,4]|}
{a:"1234"}
{a:0x01020304}
# expected output
4
4
error("missing")
error("missing")