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

Add support for creating 32-bit float values #73

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ are pure, i.e. invoking them multiple times with the same parameters yields alwa
#### Numeric conversions

Rune represents integers as 64-bit signed values. Therefore, it is possible to directly pass a Rune integer to
a Cassandra column of type `bigint`. However, binding a 64-bit value to smaller integer column types, like
`int`, `smallint` or `tinyint` will result in a runtime error. As long as an integer value does not exceed the bounds,
a Cassandra column of type `bigint`, and to pass a Rune float to a Cassandra column of type `double`.
However, binding a 64-bit value to smaller integer or float column types, like
`int`, `smallint`, `tinyint` or `float` will result in a runtime error.
As long as an integer value does not exceed the bounds,
you can convert it to smaller signed integer types by using the following instance functions:

- `x.to_i32()` – converts a float or integer to a 32-bit signed integer, compatible with Cassandra `int` type
- `x.to_i16()` – converts a float or integer to a 16-bit signed integer, compatible with Cassandra `smallint` type
- `x.to_i8()` – converts a float or integer to an 8-bit signed integer, compatible with Cassandra `tinyint` type
- `x.to_f32()` - converts a float or integer value to a 32-bit float, compatible with Cassandra `float` type
- `x.clamp(min, max)` – restricts the range of an integer or a float value to given range

You can also convert between floats and integers by calling `to_integer` or `to_float` instance functions.
Expand Down
15 changes: 15 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ impl Context {
.statements
.get(key)
.ok_or_else(|| CassError(CassErrorKind::PreparedStatementNotFound(key.to_string())))?;

let params = bind::to_scylla_query_params(&params)?;
for current_attempt_num in 0..self.retry_number + 1 {
let start_time = self.stats.try_lock().unwrap().start_request();
Expand Down Expand Up @@ -611,6 +612,9 @@ mod bind {
} else if h == Int8::type_hash() {
let int8: &Int8 = obj.downcast_borrow_ref().unwrap();
Ok(CqlValue::TinyInt(int8.0))
} else if h == Float32::type_hash() {
let float32: &Float32 = obj.downcast_borrow_ref().unwrap();
Ok(CqlValue::Float(float32.0))
} else {
Err(CassError(CassErrorKind::UnsupportedType(
v.type_info().unwrap(),
Expand Down Expand Up @@ -684,6 +688,9 @@ pub struct Int16(pub i16);
#[derive(Clone, Debug, Any)]
pub struct Int32(pub i32);

#[derive(Clone, Debug, Any)]
pub struct Float32(pub f32);

/// Returns the literal value stored in the `params` map under the key given as the first
/// macro arg, and if not found, returns the expression from the second arg.
pub fn param(
Expand Down Expand Up @@ -737,6 +744,14 @@ pub fn float_to_i32(value: f64) -> Option<Int32> {
int_to_i32(value as i64)
}

pub fn int_to_f32(value: i64) -> Option<Float32> {
Some(Float32(value as f32))
}

pub fn float_to_f32(value: f64) -> Option<Float32> {
Some(Float32(value as f32))
}

/// Computes a hash of an integer value `i`.
/// Returns a value in range `0..i64::MAX`.
pub fn hash(i: i64) -> i64 {
Expand Down
5 changes: 5 additions & 0 deletions src/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ impl Program {
.unwrap();
latte_module.inst_fn("to_i8", context::int_to_i8).unwrap();
latte_module.inst_fn("to_i8", context::float_to_i8).unwrap();
latte_module.inst_fn("to_f32", context::int_to_f32).unwrap();
latte_module
.inst_fn("to_f32", context::float_to_f32)
.unwrap();

latte_module.inst_fn("clamp", context::clamp_float).unwrap();
latte_module.inst_fn("clamp", context::clamp_int).unwrap();

Expand Down
Loading