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

improve error message when passing a non-identifier to start a connection #764

Closed
edublancas opened this issue Jul 27, 2023 · 1 comment · Fixed by #821
Closed

improve error message when passing a non-identifier to start a connection #764

edublancas opened this issue Jul 27, 2023 · 1 comment · Fixed by #821
Assignees

Comments

@edublancas
Copy link

we allow users to pass a connection variable to connect to a db. example:

%load_ext sql

import duckdb
conn = duckdb.connect()

%sql conn

however, in some cases, the connection object might be part of a dictionary, list or object. examples:

%sql object.property
%sql dictionary["key"]
%sql some_list[0]

Trying any of the above throws an error because our parser thinks it's SQL code. Instead, we should provide a more informative error:

"object.property" is not a valid connection identifier. Please pass the variable's name directly, as passing object attributes, dictionaries `some_dict[key]` or lists `some_list[0]` won't work. Example: `%sql connection`

so we need a way to identity such cases, we can use the following heuristics:

  • the magic gets a single argument (no spaces between characters) - because valid SQL code will always have spaces
  • we can check if the argument is not a valid identifier using argument.isidentifier() but it is valid Python code (using ast.parse)

if those conditions hold, then we can raise the error.

@edublancas edublancas changed the title improve error message improve error message when passing a non-identifier to start a connection Jul 27, 2023
@bbeat2782
Copy link

Acceptance Criteria

  1. Raise a more informative error when a connection object that is a part of object, dictionary, or list (eg. %sql object.property, %sql dictionary["key"], %sql some_list[0]) is passed
    a. error example :
"object.property" is not a valid connection identifier. Please pass the variable's name directly, as passing object attributes, dictionaries `some_dict[key]` or lists `some_list[0]` won't work. Example: `%sql connection`
  1. Add test functions

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

Successfully merging a pull request may close this issue.

2 participants