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

Cursor unable to find Pandas df registered in parent connection #1736

Closed
2 tasks done
Alex-Monahan opened this issue May 8, 2021 · 9 comments
Closed
2 tasks done
Labels

Comments

@Alex-Monahan
Copy link
Contributor

Alex-Monahan commented May 8, 2021

What does happen?
First, I create a connection to DuckDB in Python, then I register a Pandas dataframe on that connection. Then I create a cursor from that same connection and attempt to execute a SQL statement that refers to the Pandas dataframe I had just registered. However, I receive a "Catalog Error: Table with name main_table does not exist!". This behavior did not occur on 0.2.5.

What should happen?
Cursors made from a connection object should inherit the registered dataframes on that connection.

I do have a workaround where I can build the cursor first and then register the dataframes, but this still felt like a bug worth reporting!

To Reproduce

import pandas as pd
import duckdb

main_table = pd.DataFrame([{"join_column":"text"}])

sql = "select main_table.* from main_table"

try:
    duckdb_conn = duckdb.connect()
    duckdb_conn.execute("PRAGMA threads=4")
    duckdb_conn.register('main_table',main_table)
    cur = duckdb_conn.cursor()
    output_df = cur.execute(sql).fetchdf()
except Exception as err:
    print(err)
finally:
    duckdb_conn.close()
    
print(output_df)

Environment (please complete the following information):

  • OS: Windows 10
  • DuckDB Version 0.2.6

Before submitting

  • Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?
  • Have you tried this on the latest master branch? In case you cannot compile, you may find some binaries here: https://github.com/duckdb/duckdb/releases/tag/master-builds
  • (In this case the latest master branch on PyPi is the same as 0.2.6). I used pip install duckdb --pre --upgrade and no new version was installed. Looks like I'm pretty early on testing this version! :-)
@hannes
Copy link
Member

hannes commented May 8, 2021

Did you know that the cursor() call there is completely optional? You can directly call execute() on the duckdb_conn. Never understood this whole cursor business in the Python DB API..

@Alex-Monahan
Copy link
Contributor Author

Alex-Monahan commented May 8, 2021 via email

@hannes
Copy link
Member

hannes commented May 9, 2021

Yes should be no difference with temp tables.

@hannes
Copy link
Member

hannes commented May 11, 2021

In addition, the whole registration step should no longer be required with 0.2.6, there you can simply use the name of a data frame in your query and it should auto-register.

@Alex-Monahan
Copy link
Contributor Author

Alex-Monahan commented May 11, 2021 via email

@akdor1154
Copy link
Contributor

akdor1154 commented Dec 4, 2021

@hannesmuehleisen would it be possible for cursor() to return the connection itself?

I know when using ddb it's not required, but people will run into this when using it the "normal" way you'd use a database with pandas:

ddb = duckdb.connect()

df = DataFrame()
ddb.register('my_table', df)

pd.read_sql('select * from my_table', ddb) # error, my_table doesn't exist

so it's a bit of a nasty introduction for newcomers.

If cursor() just returned self, is that a quick fix that would avoid this?

EDIT: oh i see, all well and good until someone calls cursor.close().

@omo
Copy link
Contributor

omo commented May 6, 2023

Probably a duplicate of #1848.

@github-actions
Copy link

github-actions bot commented Aug 5, 2023

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions
Copy link

github-actions bot commented Sep 4, 2023

This issue was closed because it has been stale for 30 days with no activity.

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

No branches or pull requests

4 participants