AlexPikalov / cdrs

Cassandra DB native client written in Rust language. Find 1.x versions on https://github.com/AlexPikalov/cdrs/tree/v.1.x Looking for an async version? - Check WIP https://github.com/AlexPikalov/cdrs-async

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to query with a vec of binary keys?

rw opened this issue · comments

I have a vec of binary keys (Vec<Vec<u8>>), and I want to provide them as a query value in the following select statement:

SELECT value FROM mycollection WHERE key IN ?

I think I'm getting tripped up on encoding with query_values! here. I know how to do the single-key case, by wrapping the key in cdrs::types::value::Bytes::new(mykey). But, I don't understand how to do that with a Vec of bytes keys. One issue I've run into is that I can get the conversions to compile, but queries don't work as expected -- that's why I think I don't understand the encoding process.

Edit:

Here's what does work, for a single key lookup:

let key: Vec<u8> = ...;
let qv = cdrs::query_values!(cdrs::types::value::Bytes::new(key));
let _ = conn.query_with_values(r#"SELECT value FROM mycollection WHERE key = ?"#, qv);

Here's what I tried for a vec of keys, that compiles but I think produces a wrong encoding:

let keys: Vec<Vec<u8>> = ...;
let qv = cdrs::query_values!(keys);
let _ = conn.query_with_values(r#"SELECT value FROM mycollection WHERE key IN ?"#, qv);

Here's my schema:

CREATE TABLE IF NOT EXISTS mykeyspace.mycollection (key blob PRIMARY KEY, value blob)

Thanks!

cc @AlexPikalov

I tried using the example code below, but it seems to work differently with Vec<u8> than with String:

let criteria = vec!["1".to_string(), "3".to_string()];
let rows = session
.query_with_values(cql, query_values!(criteria.clone()))

hi @rw,

I guess there was a similar issue. Let me first try to find it. I'll try to get back soon.

@AlexPikalov Thanks! Anything I can do to help? Getting this batching working will significantly speed up our application, because we fetch thousands of objects from Cassandra.

Works fine for me. Some pseudo code:

#[test]
fn test_temp_test() {
    let connection = setup_test_keyspace();

    query(&connection, "drop TABLE if EXISTS v");
    query(&connection, "CREATE TABLE IF NOT EXISTS v (key blob PRIMARY KEY)");

    let val1 = "Val1";
    let val2 = "Val2";

    let insert = |v: &str| connection.query_with_values("insert into v(key) values (?)", query_values!(v)).unwrap();

    insert(val1);
    insert(val2);

    let result = query_with_values(&connection, "SELECT * FROM v WHERE key IN ?", query_values!(vec![val1, val2]));

    assert_eq!(2, result.get_body().unwrap().into_rows().unwrap().len());
}

can you provide a reproduction project with a test that fails? That makes it more easy to fix the problem, because the problem is nowhere described in the issue (I don't read there is 'maybe' a problem with the encoding, but no actual problem has been described.

I made a test case reproduction of what I think the encoding problem is: #341

@Jasperav Thanks for the example code, but I'm using Vec<Vec<u8>>, not Vec<String>.

Fixed by the suggestion in #341