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

Cannot annotate type of unnest for postgres #2927

Closed
eric-zhu opened this issue Feb 7, 2024 · 5 comments
Closed

Cannot annotate type of unnest for postgres #2927

eric-zhu opened this issue Feb 7, 2024 · 5 comments
Assignees

Comments

@eric-zhu
Copy link

eric-zhu commented Feb 7, 2024

Version

$ pip list | grep sqlglot
sqlglot               21.0.1

Fully reproducible code snippet

import sqlglot
from sqlglot.optimizer.annotate_types import annotate_types
from sqlglot.optimizer.qualify import qualify

schema = {
    "customers": {
        "access_errors": "text[]",
    }
}
sql_query = "select unnest(access_errors) as errors from customers"
expression = sqlglot.parse_one(sql_query, dialect="postgres")
qualified_expr = qualify(expression, dialect="postgres", schema=schema)
annotated_expr = annotate_types(qualified_expr, schema=schema)
ex = annotated_expr.expressions[0]
print(f"column '{ex.alias_or_name}' type: {ex.type}")

Output: column 'errors' type: UNKNOWN.
Expected: column 'errors' type: TEXT

import sqlglot
from sqlglot.optimizer.annotate_types import annotate_types
from sqlglot.optimizer.qualify import qualify

sql_query = "select unnest('{A,B,C,D}'::text[]) category"
expression = sqlglot.parse_one(sql_query, dialect="postgres")
qualified_expr = qualify(expression, dialect="postgres")
annotated_expr = annotate_types(qualified_expr)
ex = annotated_expr.expressions[0]
print(f"column '{ex.alias_or_name}' type: {ex.type}")

Output: column 'category' type: UNKNOWN
Expected: column 'category' type: TEXT

@tobymao tobymao self-assigned this Feb 8, 2024
@tobymao
Copy link
Owner

tobymao commented Feb 8, 2024

unnest is currently unsupported but your code snippet has an error, you need to pass schema to annotate types

@eric-zhu
Copy link
Author

eric-zhu commented Feb 8, 2024

unnest is currently unsupported but your code snippet has an error, you need to pass schema to annotate types

Thanks for pointing out the error. I've update the code snippet. Output is the same.

Is it easy to add support for unnest? or do you have a plan to add it? Thanks.

@tobymao
Copy link
Owner

tobymao commented Feb 8, 2024

it’s easy, but i ran into an obscure bug that i need to fix first before i can get it in

@tobymao tobymao closed this as completed in 9241858 Feb 8, 2024
@eric-zhu
Copy link
Author

eric-zhu commented Feb 8, 2024

Thanks a lot for the quick fix.

@tobymao
Copy link
Owner

tobymao commented Feb 8, 2024

you're welcome!

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

No branches or pull requests

2 participants