Skip to content

Commit

Permalink
more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Nov 22, 2023
1 parent 71b7002 commit 82d342a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 12 additions & 2 deletions posthog/hogql/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ def visit_select_query(self, node: ast.SelectQuery):
next_join = next_join.next_join

if node.select:
# Only for ClickHouse: Gather all visible aliases, and/or the last hidden alias for
# each unique alias name. Then make the last hidden aliases visible.
if self.dialect == "clickhouse":
# Gather all visible aliases. Make visible the last occurrence of each unique hidden alias.
visible_aliases = {}
for alias in reversed(node.select):
if isinstance(alias, ast.Alias):
Expand All @@ -247,19 +248,27 @@ def visit_select_query(self, node: ast.SelectQuery):
columns = []
for column in node.select:
if isinstance(column, ast.Alias):
# It's either a visible alias, or the last hidden alias for this name.
if visible_aliases.get(column.alias) == column:
if column.hidden:
if (
isinstance(column.expr, ast.Field)
and isinstance(column.expr.type, ast.FieldType)
and column.expr.type.name == column.alias
):
# Unhide if really the same name for the field and the alias
# Hide the hidden alias only if it's a simple field,
# and we're using the same name for the field and the alias
# E.g. events.event AS event --> events.evnet.
column = column.expr
else:
# Make the hidden alias visible
column = cast(ast.Alias, clone_expr(column))
column.hidden = False
else:
# Always print visible aliases.
pass
else:
# This is not the alias for this unique alias name. Skip it.
column = column.expr
columns.append(self.visit(column))
else:
Expand Down Expand Up @@ -842,6 +851,7 @@ def visit_placeholder(self, node: ast.Placeholder):
raise HogQLException(f"Placeholders, such as {{{node.field}}}, are not supported in this context")

def visit_alias(self, node: ast.Alias):
# Skip hidden aliases completely.
if node.hidden:
return self.visit(node.expr)
expr = node.expr
Expand Down
10 changes: 5 additions & 5 deletions posthog/hogql/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def visit_select_query(self, node: ast.SelectQuery):
else:
select_nodes.append(new_expr)

columns_with_explicit_alias = {}
columns_with_visible_alias = {}
for new_expr in select_nodes:
if isinstance(new_expr.type, ast.FieldAliasType):
alias = new_expr.type.alias
Expand All @@ -139,14 +139,14 @@ def visit_select_query(self, node: ast.SelectQuery):
alias = None

if alias:
# Remember the first visible or last hidden expr for each alias
# Make a reference of the first visible or last hidden expr for each unique alias name.
if isinstance(new_expr, ast.Alias) and new_expr.hidden:
if alias not in node_type.columns or not columns_with_explicit_alias.get(alias, False):
if alias not in node_type.columns or not columns_with_visible_alias.get(alias, False):
node_type.columns[alias] = new_expr.type
columns_with_explicit_alias[alias] = False
columns_with_visible_alias[alias] = False
else:
node_type.columns[alias] = new_expr.type
columns_with_explicit_alias[alias] = True
columns_with_visible_alias[alias] = True

# add the column to the new select query
new_node.select.append(new_expr)
Expand Down

0 comments on commit 82d342a

Please sign in to comment.