Skip to content

Commit 53cf06c

Browse files
committed
cql: wrap CqlVarint with CqlValue::Varint
1 parent f701d8f commit 53cf06c

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

scylla-cql/src/frame/response/cql_to_rust.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::result::{CqlValue, Row};
2-
use crate::frame::value::{Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid};
2+
use crate::frame::value::{
3+
Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid, CqlVarint,
4+
};
35
use bigdecimal::BigDecimal;
46
use num_bigint::BigInt;
57
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
@@ -125,7 +127,7 @@ impl_from_cql_value_from_method!(i32, as_int); // i32::from_cql<CqlValue>
125127
impl_from_cql_value_from_method!(i64, as_bigint); // i64::from_cql<CqlValue>
126128
impl_from_cql_value_from_method!(Counter, as_counter); // Counter::from_cql<CqlValue>
127129
impl_from_cql_value_from_method!(i16, as_smallint); // i16::from_cql<CqlValue>
128-
impl_from_cql_value_from_method!(BigInt, into_varint); // BigInt::from_cql<CqlValue>
130+
impl_from_cql_value_from_method!(CqlVarint, into_cql_varint); // CqlVarint::from_cql<CqlValue>
129131
impl_from_cql_value_from_method!(i8, as_tinyint); // i8::from_cql<CqlValue>
130132
impl_from_cql_value_from_method!(f32, as_float); // f32::from_cql<CqlValue>
131133
impl_from_cql_value_from_method!(f64, as_double); // f64::from_cql<CqlValue>
@@ -148,6 +150,15 @@ impl<const N: usize> FromCqlVal<CqlValue> for [u8; N] {
148150
}
149151
}
150152

153+
impl FromCqlVal<CqlValue> for BigInt {
154+
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
155+
match cql_val {
156+
CqlValue::Varint(cql_varint) => Ok(cql_varint.into()),
157+
_ => Err(FromCqlValError::BadCqlType),
158+
}
159+
}
160+
}
161+
151162
#[cfg(feature = "chrono")]
152163
impl FromCqlVal<CqlValue> for NaiveDate {
153164
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
@@ -463,7 +474,7 @@ mod tests {
463474
let big_int = 0.to_bigint().unwrap();
464475
assert_eq!(
465476
Ok(big_int),
466-
BigInt::from_cql(CqlValue::Varint(0.to_bigint().unwrap()))
477+
BigInt::from_cql(CqlValue::Varint(0.to_bigint().unwrap().into()))
467478
);
468479
}
469480

scylla-cql/src/frame/response/result.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::cql_to_rust::{FromRow, FromRowError};
22
use crate::frame::response::event::SchemaChangeEvent;
33
use crate::frame::types::vint_decode;
4-
use crate::frame::value::{Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid};
4+
use crate::frame::value::{
5+
Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid, CqlVarint,
6+
};
57
use crate::frame::{frame_errors::ParseError, types};
68
use bigdecimal::BigDecimal;
79
use byteorder::{BigEndian, ReadBytesExt};
810
use bytes::{Buf, Bytes};
9-
use num_bigint::BigInt;
1011
use std::{
1112
convert::{TryFrom, TryInto},
1213
net::IpAddr,
@@ -113,7 +114,7 @@ pub enum CqlValue {
113114
Timeuuid(CqlTimeuuid),
114115
Tuple(Vec<Option<CqlValue>>),
115116
Uuid(Uuid),
116-
Varint(BigInt),
117+
Varint(CqlVarint),
117118
}
118119

119120
impl ColumnType {
@@ -363,7 +364,7 @@ impl CqlValue {
363364
}
364365
}
365366

366-
pub fn into_varint(self) -> Option<BigInt> {
367+
pub fn into_cql_varint(self) -> Option<CqlVarint> {
367368
match self {
368369
Self::Varint(i) => Some(i),
369370
_ => None,
@@ -810,7 +811,7 @@ pub fn deser_cql_value(typ: &ColumnType, buf: &mut &[u8]) -> StdResult<CqlValue,
810811
let uuid = uuid::Uuid::from_slice(buf).expect("Deserializing Uuid failed.");
811812
CqlValue::Uuid(uuid)
812813
}
813-
Varint => CqlValue::Varint(num_bigint::BigInt::from_signed_bytes_be(buf)),
814+
Varint => CqlValue::Varint(CqlVarint::from_signed_bytes_be(buf.to_vec())),
814815
List(type_name) => {
815816
let len: usize = types::read_int(buf)?.try_into()?;
816817
let mut res = Vec::with_capacity(len);
@@ -1086,7 +1087,7 @@ mod tests {
10861087

10871088
for t in tests.iter() {
10881089
let value = super::deser_cql_value(&ColumnType::Varint, &mut &*t.encoding).unwrap();
1089-
assert_eq!(CqlValue::Varint(t.value.clone()), value);
1090+
assert_eq!(CqlValue::Varint(t.value.clone().into()), value);
10901091
}
10911092
}
10921093

scylla-cql/src/frame/value.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,18 @@ impl Value for CqlTimeuuid {
995995
}
996996
}
997997

998+
impl Value for CqlVarint {
999+
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1000+
let serialized = &self.0;
1001+
let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;
1002+
1003+
buf.put_i32(serialized_len);
1004+
buf.extend_from_slice(serialized);
1005+
1006+
Ok(())
1007+
}
1008+
}
1009+
9981010
impl Value for BigInt {
9991011
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
10001012
let serialized = self.to_signed_bytes_be();

scylla-cql/src/types/serialize/value.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use secrecy::{ExposeSecret, Secret, Zeroize};
2020
use crate::frame::response::result::{ColumnType, CqlValue};
2121
use crate::frame::types::vint_encode;
2222
use crate::frame::value::{
23-
Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid, MaybeUnset, Unset, Value,
23+
Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid, CqlVarint, MaybeUnset,
24+
Unset, Value,
2425
};
2526

2627
#[cfg(feature = "chrono")]
@@ -233,6 +234,14 @@ impl SerializeCql for CqlTimeuuid {
233234
writer.set_value(me.as_bytes().as_ref()).unwrap()
234235
});
235236
}
237+
impl SerializeCql for CqlVarint {
238+
impl_serialize_via_writer!(|me, typ, writer| {
239+
exact_type_check!(typ, Varint);
240+
writer
241+
.set_value(me.as_signed_bytes_be_slice())
242+
.map_err(|_| mk_ser_err::<Self>(typ, BuiltinSerializationErrorKind::SizeOverflow))?
243+
});
244+
}
236245
impl SerializeCql for BigInt {
237246
impl_serialize_via_writer!(|me, typ, writer| {
238247
exact_type_check!(typ, Varint);

scylla/src/utils/pretty.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ where
5151
CqlValue::Inet(i) => write!(f, "'{}'", i)?,
5252
CqlValue::SmallInt(si) => write!(f, "{}", si)?,
5353
CqlValue::TinyInt(ti) => write!(f, "{}", ti)?,
54-
CqlValue::Varint(vi) => write!(f, "{}", vi)?,
54+
CqlValue::Varint(vi) => write!(
55+
f,
56+
"blobAsVarint(0x{:x})",
57+
HexBytes(vi.as_signed_bytes_be_slice())
58+
)?,
5559
CqlValue::Counter(c) => write!(f, "{}", c.0)?,
5660
CqlValue::Date(CqlDate(d)) => {
5761
let days_since_epoch = chrono::Duration::days(*d as i64 - (1 << 31));

0 commit comments

Comments
 (0)