sfackler / rust-postgres

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

supplying &[f32] as params #1167

Closed guspower closed 1 month ago

guspower commented 1 month 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 1 month ago

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

guspower commented 1 month ago

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

sfackler commented 1 month 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 1 month ago

TIL: query_raw

works perfectly, thank you +!