Correct way to map Python classes to Trino tables #346
benrifkind
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I think this is related to using SQLAlchemy but I could not get my table mappings to work properly. My thought is that it should be possible to map a Python DataClass on top of a Trino (Iceberg) table.
So for example, suppose I have these two classes
The idea is that the Purchases class corresponds to an Iceberg table whose schema looks like
I would like to be able to insert an instance of the Purchase class into the Iceberg purchases table. And to be able to fetch a row from the Iceberg table as an instance of the Purchase class. Of course this functionality is not meant to be specific for just these classes. It should work for any Python object as long as the table schema matches the Python object schema.
I was able to write this custom functionality myself but I can't help thinking that this must already exist. I tried using SQLAlchemy but I ran into issues since Iceberg does not support a primary key and SQLAlchemy requires it. Even when I work around this (by creating the table separately from SQLAlchemy), I am unsure how to define a SQLAlchemy table that has a column that is itself a class. The user column must be defined as a tuple and returns as a tuple.
So for example, if I define this SQLAlchemy table
The following attempt to insert data fails with:
And this works but outputs a tuple
The outputs are
If I define a new type function as below and define the SQLAlchemy table using it, I get what I want, but this seems like a complicated way to get what I want. I also could not get it to work if I want a column to be a list of objects which in my mind, in Trino is an array of ROW's
I hope my question makes sense. I'm just looking for the simplest way to map Python objects to Trino tables. And likely using the ORM doesn't make sense for my use case since my tables are not relational. Thanks for any help!
Beta Was this translation helpful? Give feedback.
All reactions