Repository navigation
Replies: 3 comments 3 replies
|
While this is not exactly what you're looking for, repeating the table names can be avoided by using replacement scans: iris = duckdb.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
iris = duckdb.sql("from iris select species, sepal_length as sl, sepal_width as sw")
iris = duckdb.sql("from iris where sl > 4.5")
... |
|
Using |
|
Thanks! I had looked at those options as well, and they're good to know about, but they still don't quite achieve what I was hoping for.
Like I said, I don't think this is too hard for a user to cook up a way to do this in their own code. It just seems that a method like the Would it be much work to add? Or are there reasons not to add such a method? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi folks, I'm really enjoying playing around with DuckDB Relational API in Python as a way to incrementally build up queries.
However, the
DuckDBPyRelation.query()function requires avirtual_table_namestring that can be a little cumbersome to work with, and I'm wondering if it would be easy to add functionality to the API to make it unnecessary in some situations.For example, I can load the iris data set and chain several SQL statements together with something like the following:
However, each statement requires the somewhat repetitive pattern of
.query(<unique_table_name>, 'from <unique_table_name> ...'). When I've done this in the past, I've found it error-prone and tricker to reorder or refactor statements.Ideally, I'm thinking we could drop the
from <unique_table_name>part, making it implied and unique so that we avoid name conflicts. For example, we could do some monkey patching to add aDuckDBPyRelation.sql()function (I'm surprised this name is still free!) like the following:The chaining example above then simplifies to the following, without the need to come up with and specify table names:
While this works as a hack, it would be nice if functionality like this was included in DuckDB by default. I'm also assuming you could do something more intelligent to uniquely identify tables than my
temp_table_name = '_tbl_' + ''.join(random.choices(string.ascii_lowercase, k=10)).I've found the pattern above super ergonomic to work with, especially when I'm doing exploratory data analysis and can just write short SQL snippets like
where sl > 4.5,order by 2, orlimit 2. This functionality would also be helpful to someone who wanted to do something funky with Python magic methods :)Would it make sense to add a function along these lines to the relational API?
All reactions