Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RethinkDB converts float 0.0 to int 0 #263

Open
EugeniuZ opened this issue Sep 2, 2021 · 3 comments
Open

RethinkDB converts float 0.0 to int 0 #263

EugeniuZ opened this issue Sep 2, 2021 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@EugeniuZ
Copy link

EugeniuZ commented Sep 2, 2021

Hi,

Seems that RethinkDB is converting float value of 0.0 to integer value of 0.
In my scenario the problem is that python dictionaries are saved in RethinkDB and upon retrieval this (undesired) conversion happens. Some of the upstream systems check for the type of the field (float is expected but an int is received).

Is there a way to avoid this conversion? Is it a feature or a bug?

task_info = {'a': 0.0, 'b': +0.0, 'c': -0.0}
ta = task_info['a']
tb = task_info['b']
tc = task_info['c']
print(f'ta - {type(ta)}, {ta}')
print(f'tb - {type(tb)}, {tb}')
print(f'tc - {type(tc)}, {tc}')
result = r.table(table).insert(task_info).run(connection)
task_id = result['generated_keys'][0]

Output is:

ta - <class 'float'>, 0.0
tb - <class 'float'>, 0.0
tc - <class 'float'>, -0.0
document = r.table(table).get(task_id).run(connection)
print(document)
a = document['a']
b = document['b']
c = document['c']
print(f'a - {type(a)}, {a}')
print(f'b - {type(b)}, {b}')
print(f'b - {type(c)}, {c}')

Output is:

{'a': 0, 'b': 0, 'c': -0.0, 'id': 'd8df59a5-330f-4cd0-84ac-394b70a05b9d'}
a - <class 'int'>, 0
b - <class 'int'>, 0
b - <class 'float'>, -0.0

Using the latest container (rethinkdb 2.4.1~0buster)
Python client version (rethinkdb==2.4.8)

@sefgit
Copy link

sefgit commented Sep 2, 2021 via email

@srh srh transferred this issue from rethinkdb/rethinkdb Sep 3, 2021
@srh
Copy link
Contributor

srh commented Sep 3, 2021

This is a behavior of the Python driver, not RethinkDB itself, which only has one numeric type, finite 64-bit floats.

I think the related code is here:

https://github.com/rethinkdb/rethinkdb-python/blob/master/rethinkdb/ast.py#L780

and that this behavior is a result of JSONDecoder: https://docs.python.org/3/library/json.html#json.JSONDecoder

There is no way to avoid this behavior. I don't think it's a bug. I'd describe the behavior as a decision rather than a feature.

@EugeniuZ
Copy link
Author

EugeniuZ commented Sep 3, 2021

Hi @srh ,

If i try conversion to an from a string i get this result:

>>> task_info
{'a': 0.0, 'b': 0.0, 'c': -0.0}
>>> s = json.dumps(task_info)
>>> s
'{"a": 0.0, "b": 0.0, "c": -0.0}'
>>> json.loads(s)
{'a': 0.0, 'b': 0.0, 'c': -0.0}

@sefgit - the code handling the python dictionaries doesn't make any assumption about the structure of received the documents (which can have various levels of nesting) so using explicit conversion would require to make assumptions about the structure and data types of the fields in received documents.

@gabor-boros gabor-boros added bug Something isn't working good first issue Good for newcomers labels Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants