Skip to content

Conversation

mariusandra
Copy link
Collaborator

@mariusandra mariusandra commented Apr 5, 2023

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

Comment on lines -8 to +18
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
}
Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Collaborator Author

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()
Copy link
Collaborator Author

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...

@mariusandra mariusandra marked this pull request as ready for review April 6, 2023 15:04
@mariusandra mariusandra requested review from thmsobrmlr and Twixes April 6, 2023 15:13
Base automatically changed from hogql-tupleware to master April 6, 2023 15:32
Comment on lines -8 to +18
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
}
Copy link
Member

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.

@mariusandra mariusandra merged commit c4bcfcd into master Apr 6, 2023
@mariusandra mariusandra deleted the hogql-lambdas branch April 6, 2023 16:29
fuziontech added a commit that referenced this pull request Apr 7, 2023
* 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)
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 this pull request may close these issues.

2 participants