diff --git a/posthog/hogql/ast.py b/posthog/hogql/ast.py index 70d6163934de6..806226b8f1b9e 100644 --- a/posthog/hogql/ast.py +++ b/posthog/hogql/ast.py @@ -46,11 +46,15 @@ def resolve_constant_type(self, context: HogQLContext): def resolve_database_field(self, context: HogQLContext): if isinstance(self.type, FieldType): return self.type.resolve_database_field(context) + if isinstance(self.type, PropertyType): + return self.type.field_type.resolve_database_field(context) raise NotImplementedException("FieldAliasType.resolve_database_field not implemented") def resolve_table_type(self, context: HogQLContext): if isinstance(self.type, FieldType): return self.type.table_type + if isinstance(self.type, PropertyType): + return self.type.field_type.table_type raise NotImplementedException("FieldAliasType.resolve_table_type not implemented") diff --git a/posthog/hogql/database/schema/util/session_where_clause_extractor.py b/posthog/hogql/database/schema/util/session_where_clause_extractor.py index cc02cfeb8b78c..a36dbfa8d0606 100644 --- a/posthog/hogql/database/schema/util/session_where_clause_extractor.py +++ b/posthog/hogql/database/schema/util/session_where_clause_extractor.py @@ -350,9 +350,11 @@ def visit_alias(self, node: ast.Alias) -> bool: if node.type and isinstance(node.type, ast.FieldAliasType): resolved_field = node.type.resolve_database_field(self.context) - table = node.type.resolve_table_type(self.context).table - return (isinstance(table, EventsTable) and resolved_field.name == "timestamp") or ( - isinstance(table, SessionsTable) and resolved_field.name == "min_timestamp" + table_type = node.type.resolve_table_type(self.context) + if not table_type or not isinstance(table_type, ast.TableType): + return False + return (isinstance(table_type.table, EventsTable) and resolved_field.name == "timestamp") or ( + isinstance(table_type.table, SessionsTable) and resolved_field.name == "min_timestamp" ) return self.visit(node.expr)