sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.52k stars 446 forks source link

supplying &[f32] as params #1167

Closed guspower closed 3 months ago

guspower commented 4 months ago

I am trying to provide a &[f32] as params to execute (an output from a Pearson correlation in ndarray-stats). I get the following build error:

expected `&[&dyn ToSql + Sync]`, found `&[f32]`

I considered wrapping f32 in a custom type that implements ToSql but that seems like overkill. After re-reading the docs I enabled the array-impls feature to see if that would get me any further. I can see that there is a ToSql impl for f32, but I could do with some pointers about the most sensible way forward!

Thanks for your help + super useful library.

sfackler commented 4 months ago

What are those f32's supposed to correspond to? Are they individual parameters to the query? A single array parameter?

guspower commented 4 months ago

They are individual parameters to the query (not an array parameter).

sfackler commented 4 months ago

f32 already implements ToSql. The issue is that you are passing a &[f32] to a function taking &[&dyn ToSql], and those are not the same type.

You can either make a new slice with that type, or use the lower-level query_raw method which should be able to work with the float slice.

guspower commented 3 months ago

TIL: query_raw

works perfectly, thank you +!