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

Support mapping from np.int and np.float #39

Open
caniko opened this issue Dec 5, 2023 · 2 comments
Open

Support mapping from np.int and np.float #39

caniko opened this issue Dec 5, 2023 · 2 comments

Comments

@caniko
Copy link

caniko commented Dec 5, 2023

I have a NumPy array of values that I would like to put into Scylla. When I use np.ndarray.astype(SmallInt) it succeeds, but I get a marshalling error when I try to upload the data.

I am guessing that there is a lack of support for these types in scyllapy. I expect it to be a simple solution considering NumPy has Rust bindings.

@s3rius
Copy link
Member

s3rius commented Dec 15, 2023

Hi. I don't think that supporting such types is inside the scope of the driver. Here's an example of how you can insert numpy array in your database without any issue.

import asyncio
from scyllapy import Scylla
from scyllapy import extra_types
import numpy as np


async def main():
    scylla = Scylla(
        contact_points=["localhost"],
    )
    await scylla.startup()
    await scylla.execute(
        "CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"
    )
    await scylla.execute(
        "CREATE TABLE IF NOT EXISTS test.test (id int, data LIST<SMALLINT>, PRIMARY KEY (id));"
    )
    await scylla.execute("TRUNCATE test.test;")

    arr = np.array(range(10), dtype=np.int8)
    data = list(map(extra_types.SmallInt, arr))      # <-- Here's how you can convert numpy array to Scylla type

    await scylla.execute("INSERT INTO test.test (id, data) VALUES (1, ?);", (data,))
    res = await scylla.execute("SELECT * FROM test.test;")
    print(res.all())


if __name__ == "__main__":
    asyncio.run(main())

@caniko
Copy link
Author

caniko commented Dec 18, 2023

Yeah, that is what I do right now.

I think scope is purely defined by you; if you say it is within scope it would be. We wouldn't depend on numpy on the Python side, but on the rust side only. The type mapping would be in scope because you are mapping to types defined in this package.

Please reconsider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants