Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
EDsCODE committed Mar 26, 2024
1 parent 92e0375 commit f07b4f8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions posthog/hogql/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def get_child(self, name: str, context: HogQLContext) -> Type:
raise HogQLException(f"Field not found: {name}")


TableOrSelectType = Union[BaseTableType, "SelectUnionQueryType", "SelectQueryType", "SelectQueryAliasType"]


@dataclass(kw_only=True)
class TableType(BaseTableType):
table: Table
Expand All @@ -104,7 +107,7 @@ def resolve_database_table(self, context: HogQLContext) -> Table:

@dataclass(kw_only=True)
class LazyJoinType(BaseTableType):
table_type: BaseTableType
table_type: TableOrSelectType
field: str
lazy_join: LazyJoin

Expand All @@ -122,7 +125,7 @@ def resolve_database_table(self, context: HogQLContext) -> Table:

@dataclass(kw_only=True)
class VirtualTableType(BaseTableType):
table_type: BaseTableType
table_type: TableOrSelectType
field: str
virtual_table: VirtualTable

Expand All @@ -133,9 +136,6 @@ def has_child(self, name: str, context: HogQLContext) -> bool:
return self.virtual_table.has_field(name)


TableOrSelectType = Union[BaseTableType, "SelectUnionQueryType", "SelectQueryType", "SelectQueryAliasType"]


@dataclass(kw_only=True)
class SelectQueryType(Type):
"""Type and new enclosed scope for a select query. Contains information about all tables and columns in the query."""
Expand Down Expand Up @@ -193,7 +193,11 @@ def get_child(self, name: str, context: HogQLContext) -> Type:
if name == "*":
return AsteriskType(table_type=self)
if self.view_name:
if context.database is None:
raise HogQLException("Database must be set for queries with views")

field = context.database.get_table(self.view_name).get_field(name)

if isinstance(field, LazyJoin):
return LazyJoinType(table_type=self, field=name, lazy_join=field)
if isinstance(field, LazyTable):
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ExpressionField(DatabaseField):
class FieldTraverser(FieldOrTable):
model_config = ConfigDict(extra="forbid")

chain: List[str]
chain: List[str | int]


class Table(FieldOrTable):
Expand Down

0 comments on commit f07b4f8

Please sign in to comment.