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

CQL Vector support #1165

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions scylla-cql/src/types/deserialize/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::fmt::Debug;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use crate::frame::response::result::{ColumnType, CqlValue};
use crate::frame::response::type_parser::TypeParser;
use crate::frame::value::{
Counter, CqlDate, CqlDecimal, CqlDecimalBorrowed, CqlDuration, CqlTime, CqlTimestamp,
CqlTimeuuid, CqlVarint, CqlVarintBorrowed,
Expand All @@ -26,6 +27,16 @@ use super::{
UdtTypeCheckErrorKind,
};

#[test]
fn test_cassandra_type_parser() {
let type_name =
"org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.Int32Type, 5)";
assert_eq!(
TypeParser::parse(Cow::Borrowed(type_name)).unwrap(),
ColumnType::Vector(Box::new(ColumnType::Int), 5)
)
}
Comment on lines +30 to +38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You introduced a beast of a module (type_parser) which is capable of parsing the syntax of any type (be it a primitive type, list, UDT, vector, etc...), but you added only this one, short test. Please add more tests for that module (preferably in the commit which introduced it) in order to increase the coverage.


#[test]
fn test_deserialize_bytes() {
const ORIGINAL_BYTES: &[u8] = &[1, 5, 2, 4, 3];
Expand All @@ -49,6 +60,36 @@ fn test_deserialize_bytes() {
assert_ser_de_identity(&ColumnType::Blob, &(&[] as &[u8]), &mut Bytes::new());
}

#[test]
fn test_deserialize_vector() {
// ser/de identity

assert_ser_de_identity(
&ColumnType::Vector(Box::new(ColumnType::Int), 2),
&vec![1, 2],
&mut Bytes::new(),
);
assert_ser_de_identity(
&ColumnType::Vector(Box::new(ColumnType::Ascii), 3),
&vec!["ala", "ma", "kota"],
&mut Bytes::new(),
);
assert_ser_de_identity(
&ColumnType::Vector(
Box::new(ColumnType::Vector(Box::new(ColumnType::Int), 2)),
2,
),
&vec![vec![1, 2], vec![3, 4]],
&mut Bytes::new(),
);
let vec: Vec<bool> = vec![];
assert_ser_de_identity(
&ColumnType::Vector(Box::new(ColumnType::Boolean), 0),
&vec,
&mut Bytes::new(),
);
}

#[test]
fn test_deserialize_ascii() {
const ASCII_TEXT: &str = "The quick brown fox jumps over the lazy dog";
Expand Down
61 changes: 49 additions & 12 deletions scylla/src/client/session_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,8 @@ async fn test_vector_type_metadata() {
#[cfg(cassandra_tests)]
#[tokio::test]
async fn test_vector_type_unprepared() {
use std::vec;

Comment on lines +3172 to +3173
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ ♻️ Is this used anywhere?

setup_tracing();
let session = create_new_session_builder().build().await.unwrap();
let ks = unique_keyspace_name();
Expand All @@ -3186,16 +3188,32 @@ async fn test_vector_type_unprepared() {

session
.query_unpaged(
format!(
"INSERT INTO {}.t (a, b, c) VALUES (1, [1, 2, 3, 4], ['foo', 'bar'])",
ks
),
&[],
format!("INSERT INTO {}.t (a, b, c) VALUES (?, ?, ?)", ks),
&(1, vec![1, 2, 3, 4], vec!["foo", "bar"]),
)
.await
.unwrap();

// TODO: Implement and test SELECT statements and bind values (`?`)
let query_result = session
.query_unpaged(format!("SELECT * FROM {}.t", ks), &[])
.await
.unwrap();

let rows: Vec<(i32, Vec<i32>, Vec<String>)> = query_result
.into_rows_result()
.unwrap()
.rows::<(i32, Vec<i32>, Vec<String>)>()
.unwrap()
.map(|r| r.unwrap())
.collect();
assert_eq!(
rows[0],
(
1,
vec![1, 2, 3, 4],
vec!["foo".to_string(), "bar".to_string()]
)
);
}

#[cfg(cassandra_tests)]
Expand All @@ -3217,16 +3235,35 @@ async fn test_vector_type_prepared() {
.unwrap();

let prepared_statement = session
.prepare(format!(
"INSERT INTO {}.t (a, b, c) VALUES (?, [11, 12, 13, 14], ['afoo', 'abar'])",
ks
))
.prepare(format!("INSERT INTO {}.t (a, b, c) VALUES (?, ?, ?)", ks))
.await
.unwrap();
session
.execute_unpaged(&prepared_statement, &(2,))
.execute_unpaged(
&prepared_statement,
&(2, vec![11, 12, 13, 14], vec!["afoo", "abar"]),
)
.await
.unwrap();

let query_result = session
.query_unpaged(format!("SELECT * FROM {}.t", ks), &[])
.await
.unwrap();

// TODO: Implement and test SELECT statements and bind values (`?`)
let rows: Vec<(i32, Vec<i32>, Vec<String>)> = query_result
.into_rows_result()
.unwrap()
.rows::<(i32, Vec<i32>, Vec<String>)>()
.unwrap()
.map(|r| r.unwrap())
.collect();
assert_eq!(
rows[0],
(
2,
vec![11, 12, 13, 14],
vec!["afoo".to_string(), "abar".to_string()]
)
);
}