spyql icon indicating copy to clipboard operation
spyql copied to clipboard

[Feature request] Define data access interface

Open ivaigult opened this issue 1 year ago • 1 comments

I really appreciate spyql flexibility and the fact that it supports many formats, but what if I want to add my own format support?:slightly_smiling_face:

My use case: I have custom data format, and I parse it to dataclasses. For example:

@dataclass
class MyData:
    field_one: str
    field_two: str

Now, I want to be able to run queries on a list of these dataclasses instances:

data = [DataClass("one", "1"), DataClass("two", "2"), DataClass("three", "3")]

# ...

query = Query("""SELECT row.field_one FROM data""")
query(data)

Of course, this does not work, since the attributes are queried directly with dict.__getitem__.

I understand, I can use dataclasses.asdict, but this approach is not flexible enough, because I also want to use arbitrary properties and functions in my queries. This would mean that everything should be converted to dictionaries ahead of time, what might be costly.

Could you please add some abstraction to allow custom types in queries? Something like:

@dataclass
class MyData(spyql.Row):
   # ...

   def get_column(self, name: str) -> types.Any:
       # ...

ivaigult avatar Oct 23 '23 10:10 ivaigult

Hi! Thank you so much for the feedback.

The idea is great! I cannot implement it right now (no bandwidth at the moment), but you are welcomed to submit a PR :-) Let me know before start coding if you would to like to contribute. Thanks!

dcmoura avatar Oct 25 '23 17:10 dcmoura