[PATCH v2] box: add support for decimals in update ops

Serge Petrenko sergepetrenko at tarantool.org
Thu Aug 22 11:54:37 MSK 2019





> 22 авг. 2019 г., в 0:37, Konstantin Osipov <kostja at tarantool.org> написал(а):
> 
> * Serge Petrenko <sergepetrenko at tarantool.org> [19/08/21 12:03]:
>> Closes #4413
>> 
>> @TarantoolBot document
>> Title: update operations on decimal fields.
>> 
>> tuple:update and space:update now support deicmal operands for
>> arithmetic operations ('+' and '-'). The syntax is as usual:
> 
> Please document how type arithmetics work, and cover it with
> tests.

Hi! I added the following text to the docbot request in commit message:

«When performing an arithmetic operation ('+', '-'), where either the
updated field or the operand is decimal, the result will be decimal.
    When both the updated field and the operand are decimal, the result
will, of course, be decimal.»



Here’s the tests diff:
diff --git a/test/box/update.result b/test/box/update.result
index a3f731b55..cc8fa7ad0 100644
--- a/test/box/update.result
+++ b/test/box/update.result
@@ -677,6 +677,46 @@ s:update({0}, {{'+', 2, ffi.new("float", 1.2)}}) -- float + float = float 1.2
 ---
 - [0, 1.2000000476837]
 ...
+-- decimal
+decimal = require('decimal')
+---
+...
+s:replace{0, decimal.new("2.000")}
+---
+- [0, 2.000]
+...
+s:update({0}, {{'+', 2, 2ULL}}) -- decimal + unsigned = decimal 4.000
+---
+- [0, 4.000]
+...
+s:update({0}, {{'+', 2, -4LL}}) -- decimal + signed = decimal 0.000
+---
+- [0, 0.000]
+...
+s:update({0}, {{'+', 2, 2}})  -- decimal + number = decimal 2.000
+---
+- [0, 2.000]
+...
+s:update({0}, {{'-', 2, 2}}) -- decimal - number = decimal 0.000
+---
+- [0, 0.000]
+...
+s:update({0}, {{'-', 2, ffi.new('float', 2)}}) -- decimal - float = decimal -2.000
+---
+- [0, -2.000]
+...
+s:update({0}, {{'-', 2, ffi.new('double', 2)}}) -- decimal - double = decimal -4.000
+---
+- [0, -4.000]
+...
+s:update({0}, {{'+', 2, decimal.new(4)}}) -- decimal + decimal = decimal 0.000
+---
+- [0, 0.000]
+...
+s:update({0}, {{'-', 2, decimal.new(2)}}) -- decimal - decimal = decimal -2.000
+---
+- [0, -2.000]
+...
 -- overflow --
 s:replace{0, 0xfffffffffffffffeull}
 ---
diff --git a/test/box/update.test.lua b/test/box/update.test.lua
index c455bc1e1..ac7698ce9 100644
--- a/test/box/update.test.lua
+++ b/test/box/update.test.lua
@@ -210,6 +210,17 @@ s:update({0}, {{'-', 2, ffi.new("float", 1.5)}}) -- float - float = float 5.5
 s:update({0}, {{'+', 2, ffi.new("float", 3.5)}}) -- float + float = float 9
 s:update({0}, {{'-', 2, ffi.new("float", 9)}}) -- float + float = float 0
 s:update({0}, {{'+', 2, ffi.new("float", 1.2)}}) -- float + float = float 1.2
+-- decimal
+decimal = require('decimal')
+s:replace{0, decimal.new("2.000")}
+s:update({0}, {{'+', 2, 2ULL}}) -- decimal + unsigned = decimal 4.000
+s:update({0}, {{'+', 2, -4LL}}) -- decimal + signed = decimal 0.000
+s:update({0}, {{'+', 2, 2}})  -- decimal + number = decimal 2.000
+s:update({0}, {{'-', 2, 2}}) -- decimal - number = decimal 0.000
+s:update({0}, {{'-', 2, ffi.new('float', 2)}}) -- decimal - float = decimal -2.000
+s:update({0}, {{'-', 2, ffi.new('double', 2)}}) -- decimal - double = decimal -4.000
+s:update({0}, {{'+', 2, decimal.new(4)}}) -- decimal + decimal = decimal 0.000
+s:update({0}, {{'-', 2, decimal.new(2)}}) -- decimal - decimal = decimal -2.000
 -- overflow --
 s:replace{0, 0xfffffffffffffffeull}
 s:update({0}, {{'+', 2, 1}}) -- ok



> 
> -- 
> Konstantin Osipov, Moscow, Russia

--
Serge Petrenko
sergepetrenko at tarantool.org




More information about the Tarantool-patches mailing list