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

expressions.Filter with a sg.func clips the first letter of the function name in generated sql #2180

Closed
gforsyth opened this issue Sep 7, 2023 · 3 comments
Assignees

Comments

@gforsyth
Copy link
Contributor

gforsyth commented Sep 7, 2023

Hi! sqlglot is great! I'm building up some expressions manually and then generating sql from there. This SQL should be valid in DuckDB (minus the missing leading letter in the function name) if it gets included with a select, etc...

Fully reproducible code snippet

[ins] In [1]: import sqlglot as sg

[ins] In [2]: sg.__version__
Out[2]: '18.2.1.dev11'

[ins] In [3]: sg.expressions.Filter(
         ...:     this=sg.expressions.Count(this="int_col"), expression=sg.func("list_contains", 1, 2)
         ...: ).sql(dialect="duckdb")
Out[3]: 'COUNT(int_col) FILTER(IST_CONTAINS(1, 2))'

I expect to get:

'COUNT(int_col) FILTER(LIST_CONTAINS(1, 2))'

Official Documentation
Please include links to official SQL documentation related to your issue.

@tobymao tobymao self-assigned this Sep 7, 2023
@tobymao tobymao closed this as completed in 6d761f9 Sep 7, 2023
@tobymao
Copy link
Owner

tobymao commented Sep 7, 2023

you're not building the ast correctly when you do it by hand, the filter() node expects a WHERE node in expression which is why it was generating weird sql. i've made it a bit more robust, but the way you're doing it has various errors.

this="int_col" is wrong, it needs to be a column expression for example.

it's going to be easier to instead do like

exp.parse_one(...), building complex AST node expressions by hand is error prone and not going to be API stable

@tobymao
Copy link
Owner

tobymao commented Sep 7, 2023

happy to chat more deeply with you about best practices. hit me up on slack https://tobikodata.com/slack

@gforsyth
Copy link
Contributor Author

gforsyth commented Sep 7, 2023

thanks @tobymao ! yeah, it was a cobbled together example just to show the clipping. I'll definitely ping on slack when I've got something a bit more robust to show off.

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