Skip to content

Java type convertion for integer #11

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

Open
PaoloAchdjian opened this issue Jan 29, 2016 · 9 comments
Open

Java type convertion for integer #11

PaoloAchdjian opened this issue Jan 29, 2016 · 9 comments

Comments

@PaoloAchdjian
Copy link

Hi,
I see that when getting a tuple of integers the JAVA type is List of objects whose type is Integer if the value is < 65535 and a Long if the value is >= 65535, also if the Integer type can contains number until 2^31
This generate a weird behavior: I create a tuple of Integer and I get a mix of Integer and Long.

@dmitry-grytsovets
Copy link

I will fix this in next release.
At this moment I recommend u use List with Number generic and .intValue то get int cause tarantool 'num' can be double or float too.

@PaoloAchdjian
Copy link
Author

Thanks

@dmitry-grytsovets
Copy link

I've researched this problem. You could look at https://github.com/tarantool/tarantool-java/blob/master/src/main/java/org/tarantool/MsgPackLite.java from line 92.

Msgpack stores number in smallest number type possible.
Writes positive integers more than 65535 as UINT32. But reads UINT32 as Long because UNIT32 can be greater then Integer.MAX_VALUE.

@PaoloAchdjian
Copy link
Author

So it should enough to save the value < MAX_UINT31 (equivalent to Integer.MAX_VALUE) as MP_INT32 instead of MP_UINT32,if I well understood the code.

@dmitry-grytsovets
Copy link

But if u store long with value = 1 u will read int :) it's a problem

@PaoloAchdjian
Copy link
Author

Ok, now I understood your point: you don't save (or better, you can't save) the original type so you can't restore it.
Obviously you can't change the behavior of the method for my needs, but, if you want, you can add a new method (or a new parameter) in order to force the returned list of a specific type: for example to force all the type to Integer or Long or also a String.
In any way, I understood that is not a bug but at most a new request.

Thanks,
Paolo

@Totktonada
Copy link
Member

Raw idea: after #7 we can use space format to automatically retrieve results with appropriate types.

@Totktonada
Copy link
Member

It seems that the only working way to handle this flaw is to provide a ResultSet object to allow a user to acquire a value in a certain Java type with getInt() / getLong() / getString() getter. Implicit conversion should be performed when it is possible and there is no precision loss. Otherwise a runtime exception should be fired up.

This way we also gracefully handle scalar type.

@Totktonada
Copy link
Member

Connected with #92.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants