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

Bug: object with decimals are converted to strings #121

Closed
2 tasks done
bakman2 opened this issue Sep 11, 2022 · 8 comments
Closed
2 tasks done

Bug: object with decimals are converted to strings #121

bakman2 opened this issue Sep 11, 2022 · 8 comments
Labels
bug Something isn't working topic:surrealql This is related to the SurrealQL query language

Comments

@bakman2
Copy link

bakman2 commented Sep 11, 2022

Describe the bug

When setting an object, floating numbers are converted to strings

Steps to reproduce

$ DATA="create device:test1 set data = {'humidity':86.15,'last_seen':'2022-09-11T15:24:21.578Z','linkquality':127}"

$ curl -X POST -u "root:root" -H "NS: test" -H "DB: test" -H "Content-Type: application/json" -d "${DATA}" http://10.0.0.224:8000/sql

[{"time":"14.15968ms","status":"OK","result":[{"data":{"humidity":"86.15","last_seen":"2022-09-11T15:24:21.000000578Z",...

Tested via both REST/curl and node.js (using node-red).

Expected behaviour

A floating number

SurrealDB version

surreal 1.0.0-beta.7 for linux on x86_64

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bakman2 bakman2 added the bug Something isn't working label Sep 11, 2022
@clawcastle
Copy link
Contributor

This seems to be an issue related to how the bigdecimal crate represents the floating point numbers using strings. I've made a pull request that might fix it, but if anyone has input/ideas on how to do it better that is very welcome :)

@finnbear
Copy link
Contributor

finnbear commented Sep 27, 2022

This issue actually applies to decimals, but SurrealDB currently defaults non-integers to decimals not floats:

> create thing:float set num = <float> 4.2;
[{"time":"543.52µs","status":"OK","result":[{"id":"thing:float","num":4.2}]}]
> create thing:decimal set num = <decimal> 4.2;
[{"time":"453.885µs","status":"OK","result":[{"id":"thing:decimal","num":"4.2"}]}]

As a workaround, you can cast to <float> like the above :)

@clawcastle
Copy link
Contributor

As a workaround, you can cast to <float> like the above :)

But wouldn't this potentially truncate the decimal value, since float is represented internally by the f64 type while decimal is represented by the bigdecimal type?

@finnbear
Copy link
Contributor

As a workaround, you can cast to <float> like the above :)

But wouldn't this potentially truncate the decimal value, since float is represented internally by the f64 type while decimal is represented by the bigdecimal type?

Yes, and this isn't ideal, but I think that is part of the reason why BigDecimal likes to serialize to string. No matter what language you use SurrealDB with, the value wont be truncated. Serializing the BigDecimal as a float would lead to truncation if deserialized in a language like JavaScript which treats JSON numbers as floats.

@clawcastle
Copy link
Contributor

I see your point - unless the client somehow deserializes it to some arbitrary-length decimal type like bigdecimal it will potentially get truncated. I think I'm going to decline my PR (#171 ) then, as it doesn't solve the problem after all

@bakman2 bakman2 changed the title Bug: object with floats are converted to strings Bug: object with decimals are converted to strings Oct 1, 2022
@bakman2
Copy link
Author

bakman2 commented Oct 1, 2022

@finnbear I've updated the title

@patrickoppel
Copy link

Ran into the same issue when using the Rust client library with the latest nightly version of the database (v1.0.0-beta.8

`#[derive(Debug,Serialize,Deserialize)]
struct Eps {
bat: Battery,
}

#[derive(Debug,Serialize,Deserialize)]
struct Battery {
volt: u16,
curr: i16,
power: f64,
temp: f64,
}

#[tokio::main]
async fn main() -> Result<()> {
static db: Surreal = Surreal::init();

db.connect::<Ws>("localhost:8000").await?;

db.signin(Root {
    username: "root",
    password: "root",
}).await?;

db.use_ns("namespace").use_db("database").await?;

let b = Battery {
    volt: 15000,
    curr: 250,
    power: 3.75,
    temp: 20.1,
};

let mut eps: Eps = db
    .create("eps")
    .content(Eps {
        bat: b,
    })
    .await?;`

And I get the following error:
Error: Api(FromValue { value: Object(Object({"bat": Object(Object({"curr": Number(Int(250)), "power": Number(Decimal(BigDecimal("3.75"))), "temp": Number(Decimal(BigDecimal("20.100000381469727"))), "volt": Number(Int(15000))})), "id": Thing(Thing { tb: "eps", id: String("jc3mpnl87dwdszfkeqby") })})), error: "invalid type: string "3.75", expected f32" })

As described above here the f64 inputs are casted as decimals instead of f64.

Is there any work on this, or anything I can do?

@rushmorem
Copy link
Collaborator

This is no longer an issue on v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working topic:surrealql This is related to the SurrealQL query language
Projects
None yet
Development

No branches or pull requests

6 participants