Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

steven avatar image
steven asked Erick Ramirez edited

How to use SetType for PreparedStatement in 'cassandra-driver' (Python)

I have a query that operates on column with type `set<tuple<int,int>>`. I want to make a prepared statement for that query and couldn't seem to find the appropriate data type to pass in when executing the query.

Here's the error that I got: `Expected: <class 'cassandra.io.libevreactor.SetType(TupleType(Int32Type, Int32Type))'>`, can I check where I could find that data type? I couldn't seem to pass in `ValueSequence` and I haven't managed to find something useful in `cassandra.query`.

Thank you for your help!

[UPDATE] Here's my table definition:

CREATE TABLE wsupplier.customer_to_item_pairs (
  c_w_id int,
  c_d_id int,
  c_id int,
  i_pairs set<tuple<int,int>>,
  PRIMARY KEY ((c_w_id, c_d_id), c_id)
);

And below is my prepared statement:

session.prepare(
    "UPDATE customer_to_item_pairs "
    "SET i_pairs = i_pairs + ? "
    "WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"
)

I tried to perform the following operation:

batch.add(
    self.statements['updateCustomerToItemPairs'],
    [item_pairs, w_id, d_id, c_id]
)

where `item_pairs` is a set containing tuple of integers.

python driver
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

If you could provide a snippet of your code, I'd be happy to reach out internally here in DataStax and get you an answer. Cheers!

0 Likes 0 ·

1 Answer

aboudreault avatar image
aboudreault answered Erick Ramirez edited

Here is an example:

# create TABLE test.test (id int PRIMARY KEY, val set<tuple<int,int>>);
prepared_stmt = session.prepare("insert into test.test (id, val) values (?, ?)")
session.execute(prepared_stmt, [1, {(42, 42)}])

# also valid but less readable:
# session.execute(prepared_stmt, [45, set([tuple([42, 42])])])
2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

I tried to pass in a set and got this error:

TypeError: Received an argument of invalid type for column "i_pairs". Expected: <class 'cassandra.io.libevreactor.SetType(TupleType(Int32Type, Int32Type))'>, Got: <class 'set'>; (required argument is not an integer)

Also, I tried to perform an addition to a set instead of inserting a completely new value.

[Additional info re-posted in question above]

Thanks a lot for your help!

0 Likes 0 ·

This is not different than your initial case. You have to pass a set of tuple<int, int>


p = s.prepare("update test.test set val = val + ? where id=?")
s.execute(p, [{(1,1)}, 42])


0 Likes 0 ·