igorcoding / asynctnt

A fast Tarantool Database connector for Python/asyncio.

Home Page:https://igorcoding.github.io/asynctnt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for decimal and float types for arithmetic operations in update

first-muchacho opened this issue · comments

TypeError when trying to update a numeric or decimal field using an addition or subtraction operation using the Decimal or float type.

Example:
init.lua

box.cfg{listen = 3301}

space_list = box.space

require "os"

if not box.schema.user.exists(os.getenv("TARANTOOL_USER_NAME")) then
    box.schema.user.create(os.getenv("TARANTOOL_USER_NAME"), {password = os.getenv("TARANTOOL_USER_PASSWORD"),
                                             if_not_exists = true})
    box.schema.user.grant(os.getenv("TARANTOOL_USER_NAME"),'read,write,execute,create,drop','universe')
end

if not space_list['customer'] then
    s = box.schema.space.create('customer')

    s:format({
        {name = 'customer_id', type = 'unsigned'},
        {name = 'username', type = 'string', is_nullable=true},
        {name = 'is_active', type = 'boolean'},
        {name = 'balance', type = 'number', is_nullable=true}
    })

    s:create_index('primary', {
        type = 'hash',
        parts = {'customer_id'}
    })
end

example.py

import os
import asynctnt
import asyncio


async def main():
    conn = asynctnt.Connection(
        host='127.0.0.1',
        port=3301,
        username=os.getenv('TARANTOOL_USER_NAME'),
        password=os.getenv('TARANTOOL_USER_PASSWORD')
    )
    await conn.connect()

    result = await conn.update('customer', [1], [('+', 'balance', 23.65)])

    print(f'RESULT: {result}')

    await conn.disconnect()


asyncio.run(main())

stderr

...
File "asynctnt\iproto\db.pyx", line 403, in asynctnt.iproto.protocol.Db.update
File "asynctnt\iproto\db.pyx", line 206, in asynctnt.iproto.protocol.Db._update
File "asynctnt\iproto\protocol.pyx", line 490, in asynctnt.iproto.protocol.BaseProtocol._execute_normal
File "asynctnt\iproto\requests/base.pyx", line 18, in asynctnt.iproto.protocol.BaseRequest.encode
File "asynctnt\iproto\requests/update.pyx", line 7, in asynctnt.iproto.protocol.UpdateRequest.encode_body
File "asynctnt\iproto\requests/update.pyx", line 220, in asynctnt.iproto.protocol.encode_request_update
File "asynctnt\iproto\requests/update.pyx", line 91, in asynctnt.iproto.protocol.encode_update_ops
TypeError: int argument required for Arithmetic and Delete operations

Hi! Thank you for the issue! Sorry for the delay, I see the problem. As a workaround while the fix is not out yet I would suggest rewriting query as a call, something like this (didn't check the result, but essentially provide proper arguments as you will in the call method in lua:

await conn.call('box.space.customer:update', [1, [('+', 'balance', Decimal("23.65"))]])

Just released version 2.2.0 that fixes this issue. The complete changelog is here.