Closed
Description
Decimals may now be stored in spaces. A corresponding field type is
introduced: 'decimal'. Decimal values are also allowed in 'scalar',
'any' and 'number' fields.
'decimal' field type is appropriate for both memtx HASH and TREE
indices, as well as for vinyl TREE index.
To create an index 'pk' over a decimal field, say
tarantool> box.space.test:create_index('pk', {parts={1, 'decimal'}})
- unique: true
parts:- type: decimal
is_nullable: false
fieldno: 1
id: 0
space_id: 512
type: TREE
name: pk
...
- type: decimal
Now you can insert some decimal values:
tarantool> for i = 1,10 do
> box.space.test:insert{decimal.new((i-5)/10)}
> end
...
tarantool> box.space.test:select{}
---
- - [-0.4]
- [-0.3]
- [-0.2]
- [-0.1]
- [0]
- [0.1]
- [0.2]
- [0.3]
- [0.4]
- [0.5]
...
Decimals may alse be inserted into scalar
and number
fields. In this
case all the number values are sorted correctly:
tarantool> box.schema.space.create('test')
tarantool> box.space.test:create_index('pk', {parts={1, 'number'}})
tarantool> box.space.test:insert{-1.0001, 'number'}
---
- [-1.0001, 'number']
...
tarantool> box.space.test:insert{decimal.new(-1.00001), 'decimal'}
---
- [-1.00001, 'decimal']
...
tarantool> box.space.test:insert{-1, 'number'}
---
- [-1, 'number']
...
tarantool> box.space.test:insert{decimal.new(-0.999), 'decimal'}
---
- [-0.999, 'decimal']
...
tarantool> box.space.test:insert{-0.998, 'number'}
---
- [-0.998, 'number']
...
tarantool> box.space.test:insert{-0.9, 'number'}
---
- [-0.9, 'number']
...
tarantool> box.space.test:insert{-0.95, 'number'}
---
- [-0.95, 'number']
...
tarantool> box.space.test:insert{decimal.new(-0.92), 'decimal'}
---
- [-0.92, 'decimal']
...
tarantool> box.space.test:insert{decimal.new(-0.971), 'decimal'}
---
- [-0.971, 'decimal']
...
tarantool> box.space.test:select{}
---
- - [-1.0001, 'number']
- [-1.00001, 'decimal']
- [-1, 'number']
- [-0.999, 'decimal']
- [-0.998, 'number']
- [-0.971, 'decimal']
- [-0.95, 'number']
- [-0.92, 'decimal']
- [-0.9, 'number']
...
Uniqueness is also preserved between decimals and other number types:
tarantool> box.space.test:insert{-0.92}
---
- error: Duplicate key exists in unique index 'pk' in space 'test'
...
tarantool> box.space.test:insert{decimal.new(-0.9)}
---
- error: Duplicate key exists in unique index 'pk' in space 'test'
...
You can also set decimal fields in space format:
tarantool> _ = box.schema.space.create('test')
---
...
tarantool> _ = box.space.test:create_index('pk')
---
...
tarantool> box.space.test:format{{name='id', type='unsigned'}, {name='balance', type='decimal'}}
---
...
tarantool> box.space.test:insert{1}
---
- error: Tuple field 2 required by space format is missing
...
tarantool> box.space.test:insert{1, 'string'}
---
- error: 'Tuple field 2 type does not match one required by operation: expected decimal'
...
tarantool> box.space.test:insert{1, 1.2345}
---
- error: 'Tuple field 2 type does not match one required by operation: expected decimal'
...
tarantool> box.space.test:insert{1, decimal.new('1337.420')}
---
- [1, 1337.420]
...
Requested by @sergepetrenko in tarantool/tarantool@8ba7267.