-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(hogql): lambdas #14987
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
feat(hogql): lambdas #14987
Conversation
return format(sql, { | ||
language: 'mysql', | ||
tabWidth: 2, | ||
keywordCase: 'preserve', | ||
linesBetweenQueries: 2, | ||
indentStyle: 'tabularRight', | ||
}) | ||
try { | ||
return format(sql, { | ||
language: 'mysql', | ||
tabWidth: 2, | ||
keywordCase: 'preserve', | ||
linesBetweenQueries: 2, | ||
indentStyle: 'tabularRight', | ||
}) | ||
} catch (e) { | ||
return sql | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The x -> x*2
lambda syntax broke the formatter. Hence when using lambdas in the SQL insight editor, the code will no longer be autoformatted. I'll fix this when I can, but would love to just get lambda support in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some other dialect does support this syntax too? https://github.com/sql-formatter-org/sql-formatter/blob/master/docs/dialect.md
I wasn't able to find info about lambdas in other SQL databases though. I guess ideally there'd be a clickhouse
dialect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The closes that worked was Snowflake's dialect, but that converted x -> x*2
into x - > x * 2
(space between -
and >
), which then broke everything.
I think forking the formatter and adding what we need is the way to go... 🤔
|
||
# Each Lambda is a new scope in field name resolution. | ||
# This ref keeps track of all lambda arguments that are in scope. | ||
node.ref = ast.SelectQueryRef() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This SelectQueryRef
should be renamed VariableAccessScope
or something to that extent... but one thing at a time...
return format(sql, { | ||
language: 'mysql', | ||
tabWidth: 2, | ||
keywordCase: 'preserve', | ||
linesBetweenQueries: 2, | ||
indentStyle: 'tabularRight', | ||
}) | ||
try { | ||
return format(sql, { | ||
language: 'mysql', | ||
tabWidth: 2, | ||
keywordCase: 'preserve', | ||
linesBetweenQueries: 2, | ||
indentStyle: 'tabularRight', | ||
}) | ||
} catch (e) { | ||
return sql | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some other dialect does support this syntax too? https://github.com/sql-formatter-org/sql-formatter/blob/master/docs/dialect.md
I wasn't able to find info about lambdas in other SQL databases though. I guess ideally there'd be a clickhouse
dialect.
* master: fix(flags): don't enclose in overall transaction so we get latest reads (#15003) fix(tests): make getEventsByPerson output stable to avoid flakes (#15009) feat(data-exploration): convert funnel correlation to data exploration (#14963) fix: Set hobby deployments to 'latest' by default (#14956) feat(hogql): lambdas (#14987) feat(hogql): arrays and tuples (#14986) fix(funnel): always use total step count for funnel chart label (#14993)
Changes
Adds support for lambdas like
arrayMap(x -> x * 2)
.Only issue: the SQL "pretty printer" won't format code with lambdas. Thus if you use any in your queries, it'll stop pretty printing. I propose to solve this UX issue later, and get lambdas themselves itself in.
How did you test this code?
Wrote tests