Skip to content
Snippets Groups Projects
user avatar
Ilya Verbin authored
Now the default value of a tuple field can be computed as a result of a
function call.

Closes #8609

@TarantoolBot document
Title: Document functional default field values
Product: Tarantool
Since: 3.0
Root document: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/format/
Depends on: tarantool/doc#3520

The format clause contains, for each field, a definition within braces:
`{name='...',type='...'[,default=...][,default_func=...]}`,
where:

* (Optional) The `default_func` string value specifies the name of a
  function, that is used to generate a default value for the field. If
  `default_func` is set, the `default` value is used as the function
  argument. See [field default functions](https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#field-default-functions).

---

Root document: https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#constraint-functions

**Field default functions**

Stored Lua function can be used to generate a default value for the tuple
field. It can take one optional parameter, and must return exactly one
value. To create a field default function, use func.create with function
body. The function must not yield.

Example:

```lua
box.schema.func.create('random_point', {
    language = 'Lua',
    body = 'function(param) return math.random(param.min, param.max) end'
})

box.schema.space.create('test')
box.space.test:create_index('pk')
box.space.test:format({
    {name = 'id', type = 'integer'},
    {name = 'latitude', type = 'number',
     default_func = 'random_point', default = {min = -90, max = 90}},
    {name = 'longitude', type = 'number',
     default_func = 'random_point', default = {min = -180, max = 180}}
})
```

```lua
tarantool> math.randomseed(os.time())
---
...

tarantool> box.space.test:insert{1}
---
- [1, 56, 38]
...
```
b055625f
History

Tarantool

Actions Status Code Coverage OSS Fuzz Telegram GitHub Discussions Stack Overflow

Tarantool is an in-memory computing platform consisting of a database and an application server.

It is distributed under BSD 2-Clause terms.

Key features of the application server:

Key features of the database:

  • MessagePack data format and MessagePack based client-server protocol.
  • Two data engines: 100% in-memory with complete WAL-based persistence and an own implementation of LSM-tree, to use with large data sets.
  • Multiple index types: HASH, TREE, RTREE, BITSET.
  • Document oriented JSON path indexes.
  • Asynchronous master-master replication.
  • Synchronous quorum-based replication.
  • RAFT-based automatic leader election for the single-leader configuration.
  • Authentication and access control.
  • ANSI SQL, including views, joins, referential and check constraints.
  • Connectors for many programming languages.
  • The database is a C extension of the application server and can be turned off.

Supported platforms are Linux (x86_64, aarch64), Mac OS X (x86_64, M1), FreeBSD (x86_64).

Tarantool is ideal for data-enriched components of scalable Web architecture: queue servers, caches, stateful Web applications.

To download and install Tarantool as a binary package for your OS or using Docker, please see the download instructions.

To build Tarantool from source, see detailed instructions in the Tarantool documentation.

To find modules, connectors and tools for Tarantool, check out our Awesome Tarantool list.

Please report bugs to our issue tracker. We also warmly welcome your feedback on the discussions page and questions on Stack Overflow.

We accept contributions via pull requests. Check out our contributing guide.

Thank you for your interest in Tarantool!