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

Fix handling of prepared collections #38

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ classifiers = [
"Topic :: Database :: Front-Ends",
]


[tool.maturin]
python-source = "python"
module-name = "scyllapy._internal"
Expand Down Expand Up @@ -105,4 +104,4 @@ convention = "pep257"
ignore-decorators = ["typing.overload"]

[tool.ruff.pylint]
allow-magic-value-types = ["int", "str", "float", "tuple"]
allow-magic-value-types = ["int", "str", "float"]
48 changes: 48 additions & 0 deletions python/tests/test_prepared.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Callable

import pytest
from tests.utils import random_string

Expand All @@ -16,3 +18,49 @@ async def test_prepared(scylla: Scylla) -> None:
prepared_res = await scylla.execute(prepared)

assert res.all() == prepared_res.all()


@pytest.mark.anyio
@pytest.mark.parametrize(
("type_name", "test_val", "cast_func"),
[
("SET<TEXT>", ["one", "two"], set),
("SET<TEXT>", {"one", "two"}, set),
("SET<TEXT>", ("one", "two"), set),
("LIST<TEXT>", ("1", "2"), list),
("LIST<TEXT>", ["1", "2"], list),
("LIST<TEXT>", {"1", "2"}, list),
("MAP<TEXT, TEXT>", {"one": "two"}, dict),
("MAP<INT, BIGINT>", {1: 2}, dict),
("SET<BIGINT>", {1, 2}, set),
("TUPLE<INT, INT>", (1, 2), tuple),
("TUPLE<INT, TEXT, FLOAT>", (1, "two", 3.0), tuple),
("SET<TEXT>", [], lambda x: set(x) if x else None),
("LIST<TEXT>", [], lambda x: list(x) if x else None),
("MAP<TEXT, TEXT>", {}, lambda x: dict(x) if x else None),
("SET<INT>", [1], set),
("LIST<INT>", [1], list),
("MAP<TEXT, INT>", {"key": 1}, dict),
("SET<INT>", list(range(1000)), set),
("SET<INT>", [2147483647], set),
],
)
async def test_prepared_collections(
scylla: Scylla,
type_name: str,
test_val: Any,
cast_func: Callable[[Any], Any],
) -> None:
table_name = random_string(4)
await scylla.execute(
f"CREATE TABLE {table_name} (id INT, coll {type_name}, PRIMARY KEY (id))",
)

insert_query = f"INSERT INTO {table_name}(id, coll) VALUES (?, ?)"
prepared = await scylla.prepare(insert_query)
await scylla.execute(prepared, [1, test_val])

result = await scylla.execute(f"SELECT * FROM {table_name}")
rows = result.all()
assert len(rows) == 1
assert rows[0] == {"id": 1, "coll": cast_func(test_val)}
62 changes: 50 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub enum ScyllaPyCQLDTO {
Uuid(uuid::Uuid),
Inet(IpAddr),
List(Vec<ScyllaPyCQLDTO>),
Set(Vec<ScyllaPyCQLDTO>),
Tuple(Vec<ScyllaPyCQLDTO>),
Map(Vec<(ScyllaPyCQLDTO, ScyllaPyCQLDTO)>),
// UDT holds serialized bytes according to the protocol.
Udt(Vec<u8>),
Expand All @@ -118,6 +120,8 @@ impl Value for ScyllaPyCQLDTO {
ScyllaPyCQLDTO::Uuid(uuid) => uuid.serialize(buf),
ScyllaPyCQLDTO::Inet(inet) => inet.serialize(buf),
ScyllaPyCQLDTO::List(list) => list.serialize(buf),
ScyllaPyCQLDTO::Set(set) => set.serialize(buf),
ScyllaPyCQLDTO::Tuple(tuple) => tuple.serialize(buf),
ScyllaPyCQLDTO::Counter(counter) => counter.serialize(buf),
ScyllaPyCQLDTO::TinyInt(tinyint) => tinyint.serialize(buf),
ScyllaPyCQLDTO::Date(date) => date.serialize(buf),
Expand Down Expand Up @@ -169,14 +173,22 @@ pub fn py_to_value(
Some(ColumnType::SmallInt) => Ok(ScyllaPyCQLDTO::SmallInt(item.extract::<i16>()?)),
Some(ColumnType::BigInt) => Ok(ScyllaPyCQLDTO::BigInt(item.extract::<i64>()?)),
Some(ColumnType::Counter) => Ok(ScyllaPyCQLDTO::Counter(item.extract::<i64>()?)),
Some(_) | None => Ok(ScyllaPyCQLDTO::Int(item.extract::<i32>()?)),
Some(ColumnType::Int) | None => Ok(ScyllaPyCQLDTO::Int(item.extract::<i32>()?)),
Some(_) => Err(ScyllaPyError::BindingError(format!(
"Unsupported type for parameter binding: {column_type:?}"
))),
}
} else if item.is_instance_of::<PyFloat>() {
match column_type {
Some(ColumnType::Double) => Ok(ScyllaPyCQLDTO::Double(eq_float::F64(
item.extract::<f64>()?,
))),
Some(_) | None => Ok(ScyllaPyCQLDTO::Float(eq_float::F32(item.extract::<f32>()?))),
Some(ColumnType::Float) | None => {
Ok(ScyllaPyCQLDTO::Float(eq_float::F32(item.extract::<f32>()?)))
}
Some(_) => Err(ScyllaPyError::BindingError(format!(
"Unsupported type for parameter binding: {column_type:?}"
))),
}
} else if item.is_instance_of::<SmallInt>() {
Ok(ScyllaPyCQLDTO::SmallInt(
Expand Down Expand Up @@ -258,15 +270,34 @@ pub fn py_to_value(
#[allow(clippy::cast_possible_truncation)]
let timestamp = Duration::milliseconds(milliseconds.trunc() as i64);
Ok(ScyllaPyCQLDTO::Timestamp(timestamp))
} else if item.is_instance_of::<PyList>()
|| item.is_instance_of::<PyTuple>()
|| item.is_instance_of::<PySet>()
{
} else if item.is_instance_of::<PyList>() || item.is_instance_of::<PySet>() {
let mut items = Vec::new();
for inner in item.iter()? {
items.push(py_to_value(inner?, column_type)?);
if let Some(ColumnType::List(actual_type)) | Some(ColumnType::Set(actual_type)) =
column_type.as_ref()
{
items.push(py_to_value(inner?, Some(actual_type))?);
} else {
items.push(py_to_value(inner?, column_type)?);
}
}
Ok(ScyllaPyCQLDTO::List(items))
} else if item.is_instance_of::<PyTuple>() {
let tuple = item
.downcast::<PyTuple>()
.map_err(|err| ScyllaPyError::BindingError(format!("Cannot cast to tuple: {err}")))?;
let mut items = Vec::new();
if let Some(ColumnType::Tuple(types)) = column_type {
for (index, r#type) in types.iter().enumerate() {
let value = tuple.get_item(index)?;
items.push(py_to_value(value, Some(r#type))?);
}
} else {
for value in tuple.iter() {
items.push(py_to_value(value, column_type)?);
}
}
Ok(ScyllaPyCQLDTO::Tuple(items))
} else if item.is_instance_of::<PyDict>() {
let dict = item
.downcast::<PyDict>()
Expand All @@ -276,10 +307,17 @@ pub fn py_to_value(
let item_tuple = dict_item.downcast::<PyTuple>().map_err(|err| {
ScyllaPyError::BindingError(format!("Cannot cast to tuple: {err}"))
})?;
items.push((
py_to_value(item_tuple.get_item(0)?, column_type)?,
py_to_value(item_tuple.get_item(1)?, column_type)?,
));
if let Some(ColumnType::Map(key_type, val_type)) = column_type {
items.push((
py_to_value(item_tuple.get_item(0)?, Some(key_type.as_ref()))?,
py_to_value(item_tuple.get_item(1)?, Some(val_type.as_ref()))?,
));
} else {
items.push((
py_to_value(item_tuple.get_item(0)?, column_type)?,
py_to_value(item_tuple.get_item(1)?, column_type)?,
));
}
}
Ok(ScyllaPyCQLDTO::Map(items))
} else {
Expand All @@ -304,7 +342,7 @@ pub fn py_to_value(
/// # Errors
///
/// This function can throw an error, if it was unable
/// to parse thr type, or if type is not supported.
/// to parse the type, or if type is not supported.
#[allow(clippy::too_many_lines)]
pub fn cql_to_py<'a>(
py: Python<'a>,
Expand Down
Loading