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

Using user defined types #166

Open
triscuitcircuit opened this issue Apr 17, 2023 · 2 comments
Open

Using user defined types #166

triscuitcircuit opened this issue Apr 17, 2023 · 2 comments
Labels
awaiting info Awaiting further info from raiser question

Comments

@triscuitcircuit
Copy link

I defined a table and a type and added via a session to Cassandra-rs like so:

    let create_type = r#"
        CREATE TYPE moviedb.person (
            name text,
            gender text
        );
    "#;
    let create_table = r#"
    CREATE TABLE IF NOT EXISTS moviedb.movies (
        name text,
        mid int,
        year int,
        rank float,
        director text,
        cast list<frozen<map<person,text>>>, // lists person and their role 
        PRIMARY KEY (name, mid)
        );"#;
            let result = {
        session.execute(create_keyspace).await.unwrap();
        session.execute(create_type).await.unwrap();
        session.execute(create_table).await.unwrap()
    };
    Ok(result)

and I have a Rust Person struct defined like so:

#[derive( Debug)]
struct Person {
    name: String,
    gender: String,
}

I have a Vec<(Person, String)> containing a person and their role, defined as cast, and is itererated through with this code:

let mut final_cast = List::new();
            for (person,role) in cast {
                let mut set = Set::new();
                let mut user_type = DataType::new(ValueType::UDT).new_user_type();
                user_type.set_stringl(0,person.name)?;
                user_type.set_stringl(1,person.gender)?;

                set.append_user_type(&user_type)?;
                set.append_string(role.as_str())?;
                final_cast.append_set(set)?;
            }

and I get this error: Cassandra error LIB_INDEX_OUT_OF_BOUNDS: Index out of bounds
(the List:new() was used because I followed the example file, however, I can't figure out how to use UDT or define a UDT in code.)

The goal with this code is to add a person to a cast list, along with their listed role. For the life of me, I cant figure this out. My apologies about the code styling, if there's anything you guys see wrong feel free to suggest corrections :)

@triscuitcircuit
Copy link
Author

My apologies for using the issue tracker, I dont know where else to ask

@kw217 kw217 added question awaiting info Awaiting further info from raiser labels Apr 17, 2023
@kw217
Copy link
Collaborator

kw217 commented Apr 17, 2023

There are several things that seem odd about your code, I'm afraid :-/. Hopefully this will help:

  • In the table schema, why is cast a list of maps, rather than just a map?
  • In the code, why do you create a set, rather than a list or a map? There are no sets in your CQL schema.
  • You are setting values into the UDT but you haven't defined the fields yet. There's some sample code in https://github.com/Metaswitch/cassandra-rs/blob/main/examples/disabled/udt.rs (it's disabled because it's not been updated for recent API changes, but should give you the idea).

I haven't used UDTs so can't confirm it works, but you could try addressing the above and see if it helps.

No problem using the issue tracker, that's the right place to report that something isn't working for you :-) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting info Awaiting further info from raiser question
Projects
None yet
Development

No branches or pull requests

2 participants