Skip to content

Commit

Permalink
cargo: add feature flag for num-bigint v0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
muzarski committed Jan 12, 2024
1 parent 0c0b47f commit 1a1d063
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 61 deletions.
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ futures = "0.3.6"
openssl = "0.10.32"
rustyline = "9"
rustyline-derive = "0.6"
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03"]}
scylla = {path = "../scylla", features = ["ssl", "cloud", "chrono", "time", "num-bigint-03", "num-bigint-04"]}
tokio = {version = "1.1.0", features = ["full"]}
tracing = "0.1.25"
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
Expand Down
4 changes: 3 additions & 1 deletion scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ uuid = "1.0"
thiserror = "1.0"
bigdecimal = "0.2.0"
num-bigint-03 = { package = "num-bigint", version = "0.3", optional = true }
num-bigint-04 = { package = "num-bigint", version = "0.4", optional = true }
chrono = { version = "0.4.27", default-features = false, optional = true }
lz4_flex = { version = "0.11.1" }
async-trait = "0.1.57"
Expand All @@ -41,4 +42,5 @@ secret = ["secrecy"]
time = ["dep:time"]
chrono = ["dep:chrono"]
num-bigint-03 = ["dep:num-bigint-03"]
full-serialization = ["chrono", "time", "secret", "num-bigint-03"]
num-bigint-04 = ["dep:num-bigint-04"]
full-serialization = ["chrono", "time", "secret", "num-bigint-03", "num-bigint-04"]
24 changes: 23 additions & 1 deletion scylla-cql/src/frame/response/cql_to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ impl FromCqlVal<CqlValue> for num_bigint_03::BigInt {
}
}

#[cfg(feature = "num-bigint-04")]
impl FromCqlVal<CqlValue> for num_bigint_04::BigInt {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
match cql_val {
CqlValue::Varint(cql_varint) => Ok(cql_varint.into()),
_ => Err(FromCqlValError::BadCqlType),
}
}
}

#[cfg(feature = "chrono")]
impl FromCqlVal<CqlValue> for NaiveDate {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
Expand Down Expand Up @@ -467,7 +477,7 @@ mod tests {

#[cfg(feature = "num-bigint-03")]
#[test]
fn varint_from_cql() {
fn varint03_from_cql() {
use num_bigint_03::ToBigInt;

let big_int = 0.to_bigint().unwrap();
Expand All @@ -477,6 +487,18 @@ mod tests {
);
}

#[cfg(feature = "num-bigint-04")]
#[test]
fn varint04_from_cql() {
use num_bigint_04::ToBigInt;

let big_int = 0.to_bigint().unwrap();
assert_eq!(
Ok(big_int),
num_bigint_04::BigInt::from_cql(CqlValue::Varint(0.to_bigint().unwrap().into()))
);
}

#[test]
fn decimal_from_cql() {
let decimal = BigDecimal::from_str("123.4").unwrap();
Expand Down
91 changes: 55 additions & 36 deletions scylla-cql/src/frame/response/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,16 +1025,14 @@ mod tests {
assert_eq!(double_serialize, CqlValue::Double(double));
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn test_varint() {
use num_bigint_03::ToBigInt;

struct Test<'a> {
value: num_bigint_03::BigInt,
encoding: &'a [u8],
}
#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
struct VarintTestCase {
value: i32,
encoding: Vec<u8>,
}

#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
fn varint_test_cases_from_spec() -> Vec<VarintTestCase> {
/*
Table taken from CQL Binary Protocol v4 spec
Expand All @@ -1049,44 +1047,65 @@ mod tests {
-128 | 0x80
-129 | 0xFF7F
*/
let tests = [
Test {
value: 0.to_bigint().unwrap(),
encoding: &[0x00],
vec![
VarintTestCase {
value: 0,
encoding: vec![0x00],
},
Test {
value: 1.to_bigint().unwrap(),
encoding: &[0x01],
VarintTestCase {
value: 1,
encoding: vec![0x01],
},
Test {
value: 127.to_bigint().unwrap(),
encoding: &[0x7F],
VarintTestCase {
value: 127,
encoding: vec![0x7F],
},
Test {
value: 128.to_bigint().unwrap(),
encoding: &[0x00, 0x80],
VarintTestCase {
value: 128,
encoding: vec![0x00, 0x80],
},
Test {
value: 129.to_bigint().unwrap(),
encoding: &[0x00, 0x81],
VarintTestCase {
value: 129,
encoding: vec![0x00, 0x81],
},
Test {
value: (-1).to_bigint().unwrap(),
encoding: &[0xFF],
VarintTestCase {
value: -1,
encoding: vec![0xFF],
},
Test {
value: (-128).to_bigint().unwrap(),
encoding: &[0x80],
VarintTestCase {
value: -128,
encoding: vec![0x80],
},
Test {
value: (-129).to_bigint().unwrap(),
encoding: &[0xFF, 0x7F],
VarintTestCase {
value: -129,
encoding: vec![0xFF, 0x7F],
},
];
]
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn test_bigint03() {
use num_bigint_03::ToBigInt;

let tests = varint_test_cases_from_spec();

for t in tests.iter() {
let value = super::deser_cql_value(&ColumnType::Varint, &mut &*t.encoding).unwrap();
assert_eq!(CqlValue::Varint(t.value.to_bigint().unwrap().into()), value);
}
}

#[cfg(feature = "num-bigint-04")]
#[test]
fn test_bigint04() {
use num_bigint_04::ToBigInt;

let tests = varint_test_cases_from_spec();

for t in tests.iter() {
let value = super::deser_cql_value(&ColumnType::Varint, &mut &*t.encoding).unwrap();
assert_eq!(CqlValue::Varint(t.value.clone().into()), value);
assert_eq!(CqlValue::Varint(t.value.to_bigint().unwrap().into()), value);
}
}

Expand Down
27 changes: 27 additions & 0 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ impl From<CqlVarint> for num_bigint_03::BigInt {
}
}

#[cfg(feature = "num-bigint-04")]
impl From<num_bigint_04::BigInt> for CqlVarint {
fn from(value: num_bigint_04::BigInt) -> Self {
Self(value.to_signed_bytes_be())
}
}

#[cfg(feature = "num-bigint-04")]
impl From<CqlVarint> for num_bigint_04::BigInt {
fn from(val: CqlVarint) -> Self {
num_bigint_04::BigInt::from_signed_bytes_be(&val.0)
}
}

/// Native CQL date representation that allows for a bigger range of dates (-262145-1-1 to 262143-12-31).
///
/// Represented as number of days since -5877641-06-23 i.e. 2^31 days before unix epoch.
Expand Down Expand Up @@ -784,6 +798,19 @@ impl Value for num_bigint_03::BigInt {
}
}

#[cfg(feature = "num-bigint-04")]
impl Value for num_bigint_04::BigInt {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
let serialized = self.to_signed_bytes_be();
let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;

buf.put_i32(serialized_len);
buf.extend_from_slice(&serialized);

Ok(())
}
}

impl Value for &str {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
let str_bytes: &[u8] = self.as_bytes();
Expand Down
41 changes: 25 additions & 16 deletions scylla-cql/src/frame/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ fn cql_varint_serialization() {
}
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn bigint03_serialization() {
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[
fn varint_test_cases_from_spec() -> Vec<(i64, Vec<u8>)> {
vec![
(0, vec![0x00]),
(1, vec![0x01]),
(127, vec![0x7F]),
Expand All @@ -109,10 +107,18 @@ fn bigint03_serialization() {
(-1, vec![0xFF]),
(-128, vec![0x80]),
(-129, vec![0xFF, 0x7F]),
];
]
}

#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
fn generic_num_bigint_serialization<B>()
where
B: From<i64> + Value + SerializeCql,
{
let cases_from_the_spec: &[(i64, Vec<u8>)] = &varint_test_cases_from_spec();

for (i, b) in cases_from_the_spec {
let x = num_bigint_03::BigInt::from(*i);
let x = B::from(*i);
let b_with_len = (b.len() as i32)
.to_be_bytes()
.iter()
Expand All @@ -123,19 +129,22 @@ fn bigint03_serialization() {
}
}

#[cfg(feature = "num-bigint-03")]
#[test]
fn bigint03_serialization() {
generic_num_bigint_serialization::<num_bigint_03::BigInt>()
}

#[cfg(feature = "num-bigint-04")]
#[test]
fn bigint04_serialization() {
generic_num_bigint_serialization::<num_bigint_04::BigInt>()
}

#[test]
fn bigdecimal_serialization() {
// Bigint cases
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[
(0, vec![0x00]),
(1, vec![0x01]),
(127, vec![0x7F]),
(128, vec![0x00, 0x80]),
(129, vec![0x00, 0x81]),
(-1, vec![0xFF]),
(-128, vec![0x80]),
(-129, vec![0xFF, 0x7F]),
];
let cases_from_the_spec: &[(i64, Vec<u8>)] = &varint_test_cases_from_spec();

for exponent in -10_i32..10_i32 {
for (digits, serialized_digits) in cases_from_the_spec {
Expand Down
10 changes: 10 additions & 0 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ impl SerializeCql for num_bigint_03::BigInt {
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
});
}
#[cfg(feature = "num-bigint-04")]
impl SerializeCql for num_bigint_04::BigInt {
impl_serialize_via_writer!(|me, typ, writer| {
exact_type_check!(typ, Varint);
// TODO: See above comment for num-bigint-03.
writer
.set_value(me.to_signed_bytes_be().as_slice())
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
});
}
impl SerializeCql for &str {
impl_serialize_via_writer!(|me, typ, writer| {
exact_type_check!(typ, Ascii, Text);
Expand Down
4 changes: 3 additions & 1 deletion scylla/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ secret = ["scylla-cql/secret"]
chrono = ["scylla-cql/chrono"]
time = ["scylla-cql/time"]
num-bigint-03 = ["scylla-cql/num-bigint-03"]
full-serialization = ["chrono", "time", "secret", "num-bigint-03"]
num-bigint-04 = ["scylla-cql/num-bigint-04"]
full-serialization = ["chrono", "time", "secret", "num-bigint-03", "num-bigint-04"]

[dependencies]
scylla-macros = { version = "0.3.0", path = "../scylla-macros" }
Expand Down Expand Up @@ -58,6 +59,7 @@ socket2 = { version = "0.5.3", features = ["all"] }

[dev-dependencies]
num-bigint-03 = { package = "num-bigint", version = "0.3" }
num-bigint-04 = { package = "num-bigint", version = "0.4" }
scylla-proxy = { version = "0.0.3", path = "../scylla-proxy" }
ntest = "0.9.0"
criterion = "0.4" # Note: v0.5 needs at least rust 1.70.0
Expand Down
21 changes: 16 additions & 5 deletions scylla/src/transport/cql_types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ where
}
}

#[cfg(feature = "num-bigint-03")]
#[tokio::test]
async fn test_varint() {
let tests = [
#[cfg(any(feature = "num-bigint-03", feature = "num-bigint-04"))]
fn varint_test_cases() -> Vec<&'static str> {
vec![
"0",
"1",
"127",
Expand All @@ -117,11 +116,23 @@ async fn test_varint() {
"-129",
"123456789012345678901234567890",
"-123456789012345678901234567890",
];
]
}

#[cfg(feature = "num-bigint-03")]
#[tokio::test]
async fn test_varint03() {
let tests = varint_test_cases();
run_tests::<num_bigint_03::BigInt>(&tests, "varint").await;
}

#[cfg(feature = "num-bigint-04")]
#[tokio::test]
async fn test_varint04() {
let tests = varint_test_cases();
run_tests::<num_bigint_04::BigInt>(&tests, "varint").await;
}

#[tokio::test]
async fn test_cql_varint() {
let tests = [
Expand Down

0 comments on commit 1a1d063

Please sign in to comment.