diff --git a/posthog/hogql/printer.py b/posthog/hogql/printer.py index 7f5121ec8fab7..ab588239b012d 100644 --- a/posthog/hogql/printer.py +++ b/posthog/hogql/printer.py @@ -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): @@ -247,6 +248,7 @@ 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 ( @@ -254,12 +256,19 @@ def visit_select_query(self, node: ast.SelectQuery): 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: @@ -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 diff --git a/posthog/hogql/resolver.py b/posthog/hogql/resolver.py index a70eefa8ef9dd..315e9c62c1a1d 100644 --- a/posthog/hogql/resolver.py +++ b/posthog/hogql/resolver.py @@ -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 @@ -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)