You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add Support for Returning Query Results as List of Tuples #146
Currently, psqlpy returns query results as a list of dictionaries (e.g., [{"a": "a1", "b": "b1"}, {"a": "a2", "b": "b2"}]). While this format is useful for accessing fields by name, it would be beneficial to also support returning results as a list of tuples (e.g., [("a1", "b1"), ("a2", "b2")]), similar to the default behavior of psycopg2's fetchall() method.
I propose adding an option to configure psqlpy to return query results as a list of tuples.
My apologies, I overlooked this...
Does this operation ultimately return the same data? I haven’t had a chance to test it yet.
Perhaps adding a preview of the return value format to the documentation would be helpful?
let pydict: pyo3::Bound<'_,PyDict> = row_to_dict(py, row,&custom_decoders)?;
let row_factory_class = row_factory.call(py,(pydict,),None)?;
result.push(row_factory_class);
}
Ok(result.into_py_any(py)?)
}
It seems like this operation might not be ideal, as it first converts the result to a dictionary before transforming it into another type. Could this be somewhat inefficient?
I mean.. would it be better to provide a native method that doesn’t involve converting to a dictionary before transforming into a tuple?
I’m not very familiar with Rust, so I’m just speculating based on reading the code.
@bymoye In general, you are right. It’s not as efficient as it could be.
There you see such implementation cuz I made it mostly for row factories which are passed from python. And dict was chosen by me as a primary type for transforming Result into std python type
I’ll think how we can speed up the performance in this case.
@chandr-andr No, I think this is great! I was just wondering if we could consider adding an additional function that solely returns the result. Something like this, perhaps:
@chandr-andr ok, but I don’t know much about Rust, though I can give it a try. I’ll start attempting it after I wake up (it’s already midnight in China, haha).
Thanks for reviewing. Just in case, is ((field_value, field_value, ...), (field_value, field_value, ...)) expected behavior? Could you please provide an example when it can be used? I know only one place where it can be used this way - in the SQLAlchemy dialect.
Activity
chandr-andr commentedon Jun 30, 2025
@bymoye
Hello!
Can you check this functionality? https://psqlpy-python.github.io/usage/row_factories/predefined_row_factories.html#tuple-row
bymoye commentedon Jun 30, 2025
My apologies, I overlooked this...
Does this operation ultimately return the same data? I haven’t had a chance to test it yet.
Perhaps adding a preview of the return value format to the documentation would be helpful?
chandr-andr commentedon Jun 30, 2025
If I remember correctly (I made it maybe more than a year ago), yes, it returns the same data. I'll check it today.
bymoye commentedon Jun 30, 2025
hi,I reviewed the implementation code:
psqlpy/src/query_result.rs
Lines 97 to 112 in 91aa22e
It seems like this operation might not be ideal, as it first converts the result to a dictionary before transforming it into another type. Could this be somewhat inefficient?
I mean.. would it be better to provide a native method that doesn’t involve converting to a dictionary before transforming into a tuple?
I’m not very familiar with Rust, so I’m just speculating based on reading the code.
chandr-andr commentedon Jun 30, 2025
@bymoye In general, you are right. It’s not as efficient as it could be.
There you see such implementation cuz I made it mostly for row factories which are passed from python. And dict was chosen by me as a primary type for transforming Result into std python type
I’ll think how we can speed up the performance in this case.
If you have any ideas, please share them)
bymoye commentedon Jun 30, 2025
@chandr-andr No, I think this is great! I was just wondering if we could consider adding an additional function that solely returns the result. Something like this, perhaps:
chandr-andr commentedon Jun 30, 2025
@bymoye Looks like some awesome idea.
Maybe you can create a PR with this function?
As an idea, you can add method to QueryResult class named .tuple() (we can think through the naming), without row_factory parameter and return tuple.
I’ll review it when I come home
bymoye commentedon Jun 30, 2025
@chandr-andr ok, but I don’t know much about Rust, though I can give it a try. I’ll start attempting it after I wake up (it’s already midnight in China, haha).
chandr-andr commentedon Jun 30, 2025
@bymoye I've found some time and implemented what you want.
Could you please look at the PR and tell me (in the PR) if I missed something?
#147
bymoye commentedon Jul 1, 2025
@chandr-andr Good morning, I feel something is off. From the code:
psqlpy/src/query_result.rs
Line 49 in ee26552
The final tuple content is:
(((column_name, field_value), (column_name, field_value), ,,,), ((column_name, field_value), (column_name, field_value),... ) ,...).The expected value should be:
((field_value, field_value, ...), (field_value, field_value, ...)).chandr-andr commentedon Jul 1, 2025
Thanks for reviewing. Just in case, is
((field_value, field_value, ...), (field_value, field_value, ...))expected behavior? Could you please provide an example when it can be used? I know only one place where it can be used this way - in the SQLAlchemy dialect.bymoye commentedon Jul 1, 2025
@chandr-andr
For example, I currently have a database table:
The data returned by the result is:
[{"field_one": "a1", "field_two": "b1", "field_three": "c1"}, {"field_one": "a2", "field_two": "b2", "field_three": "c2"}, {"field_one": "a3", "field_two": "b3", "field_three": "c3"}]
However, we can see that during the process of passing to Python, the field names (i.e., the table headers) are repeated.
The desired data result for this request is:
[("a1", "b1", "c1"), ("a2", "b2", "c2"), ("a3", "b3", "c3")]
In other words, the table headers are not included in the result.
bymoye commentedon Jul 1, 2025
https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.connection.Connection.fetch
Actually I'm writing
tfbSo what is actually returned should be a
list[tuple(PyAny)]Now I have done it like this:
chandr-andr commentedon Jul 1, 2025
Looks good to me, agree with u