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

feat(event-explorer): column aliases #15861

Closed
wants to merge 15 commits into from
Closed

Conversation

mariusandra
Copy link
Collaborator

Problem

Some column titles can get really long unless using the (undocumented) hidden comment syntax like ... -- Url / Screen. It would be great if this would also work with aliases.

image

Changes

  1. Gives each column in the query (SQL) an alias. This helps if we ever want to nest the query further in a subquery.
  2. Updates the frontend "extract comment" functions to work with aliases.
image

How did you test this code?

In the browser. WIP

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

select_input.append(f"tuple({', '.join(SELECT_STAR_FROM_EVENTS_FIELDS)})")
elif col.split("--")[0].strip() == "person":
select_expr = f"tuple({', '.join(SELECT_STAR_FROM_EVENTS_FIELDS)})"
elif col.split("--")[0].split(" as ")[0].strip() == "person":
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 as is the main change in this file. You can now alias person as "whatever" if you want to

Comment on lines +79 to +85
# Make sure each column has an alias
if isinstance(expr, ast.Alias):
select.append(expr)
elif isinstance(expr, ast.Field):
select.append(ast.Alias(expr=expr, alias=expr.chain[-1]))
else:
select.append(ast.Alias(expr=expr, alias=col))
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 is the second fix in this file.

Sorting order by bla as x gives an error. Thus we must extract the expression from the alias. However there's a benefit in explicitly providing an alias for every selected column: we can use the generated AST in a subquery. I ran into it when writing the test that is/was #15859 . Making everything an alias here also simplifies the order by code below.

@mariusandra mariusandra marked this pull request as ready for review June 13, 2023 09:56
Copy link
Member

@Twixes Twixes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, but why not use just ... AS ... for column display names?

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

1 snapshot changes in total. 0 added, 1 modified, 0 deleted:

  • chromium: 0 added, 1 modified, 0 deleted (diff for shard 2)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

1 snapshot changes in total. 0 added, 1 modified, 0 deleted:

  • chromium: 0 added, 1 modified, 0 deleted (diff for shard 2)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

@posthog-bot posthog-bot removed the stale label Jun 14, 2023
@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@mariusandra mariusandra removed the request for review from yakkomajuri June 28, 2023 22:32
@mariusandra
Copy link
Collaborator Author

I watered this PR. Ready for a re-review.

This makes sense, but why not use just ... AS ... for column display names?

Hmm.. Few reasons why I didn't go this route:

  • Legacy. With the HogQL beta there might be users with -- in saved columns that'll now fail. We could work around it though, but then why not have the workaround keep working forever?
  • Should users still be able to add a comment in a column? It is valid SQL if you squint. And if so, then if they do, why not be nice and use that as the column title?
  • If someone uses both an alias and a comment, I'd expect the comment then to override the alias, as a "presentation level alias".

🤷

@mariusandra mariusandra requested a review from Twixes June 28, 2023 22:37
select_input.append(f"tuple({', '.join(SELECT_STAR_FROM_EVENTS_FIELDS)})")
elif col.split("--")[0].strip() == "person":
select_expr = f"tuple({', '.join(SELECT_STAR_FROM_EVENTS_FIELDS)})"
elif col.split("--")[0].split(" as ")[0].strip() == "person":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nittest of nits: AS

person_indices: List[int] = []
for index, col in enumerate(select_input_raw):

select: List[ast.Alias] = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This would be a bit clearer as select_columns

Comment on lines 165 to +167
# Convert star field from tuple to dict in each result
if "*" in select_input_raw:
star_idx = select_input_raw.index("*")
if "*" in select_input:
star_idx = select_input.index("*")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"asterisk" would be more consistent with the rest of the code than "star"

Comment on lines +43 to +66
if (query.match(/ as (`[^`]+`|"[^"]+"|[a-zA-Z$][a-zA-Z0-9_$]*)\s*$/)) {
const comment = query.split(' as ').pop()?.trim() || query
if ((comment.startsWith('`') || comment.startsWith('"')) && comment.endsWith(comment[0])) {
return comment.slice(1, -1)
}
return comment
}
if (query.includes('--')) {
return query.split('--').pop()?.trim() || query
}
return query
}

export function removeExpressionComment(query: string): string {
/**
* Removes the comment and/or alias from a query, keeping just the SQL. For display only!
*
* This is a rather naive implementation using basic string operations.
* Its output is not cleaned for usage in queries!
*/
export function removeCommentOrAlias(query: string): string {
if (query.includes('--')) {
return query.split('--').slice(0, -1).join('--').trim()
query = query.split('--').slice(0, -1).join('--').trim()
}
if (query.match(/ as (`[^`]+`|"[^"]+"|[a-zA-Z\$][a-zA-Z0-9\_\$]*)$/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two regexes only seem to differ by "_" and "$" being escaped in character sets, and whitespace being allowed at the end of the extraction one. Are these differences important, or should these be the same? I think this would be clearer if the regexes were put in file-level constants.
BTW I think the case insensitivity flag is missing, this wouldn't match the valid "AS" or "As"

@Twixes
Copy link
Member

Twixes commented Jun 29, 2023

Also some tests are failing

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@thmsobrmlr thmsobrmlr removed the stale label Aug 31, 2023
@mariusandra mariusandra deleted the event-explorer-aliases branch August 31, 2023 12:18
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.

4 participants