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

chore(data-warehouse): joins on views #21151

Merged
merged 20 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions posthog/hogql/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,24 @@
class SelectQueryAliasType(Type):
alias: str
select_query_type: SelectQueryType | SelectUnionQueryType
view_name: Optional[str] = None

def get_child(self, name: str, context: HogQLContext) -> Type:
if name == "*":
return AsteriskType(table_type=self)
if self.view_name:
field = context.database.get_table(self.view_name).get_field(name)

Check failure on line 196 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "None" of "Database | None" has no attribute "get_table"

Check failure on line 196 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "None" of "Database | None" has no attribute "get_table"
if isinstance(field, LazyJoin):
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: this resolution logic is repeated from BaseTableType

return LazyJoinType(table_type=self, field=name, lazy_join=field)

Check failure on line 198 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "table_type" to "LazyJoinType" has incompatible type "SelectQueryAliasType"; expected "BaseTableType"

Check failure on line 198 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "table_type" to "LazyJoinType" has incompatible type "SelectQueryAliasType"; expected "BaseTableType"
if isinstance(field, LazyTable):
return LazyTableType(table=field)
if isinstance(field, FieldTraverser):
return FieldTraverserType(table_type=self, chain=field.chain)

Check failure on line 202 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "chain" to "FieldTraverserType" has incompatible type "list[str]"; expected "list[str | int]"

Check failure on line 202 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

"List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance

Check failure on line 202 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Consider using "Sequence" instead, which is covariant

Check failure on line 202 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "chain" to "FieldTraverserType" has incompatible type "list[str]"; expected "list[str | int]"

Check failure on line 202 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

"List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance

Check failure on line 202 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Consider using "Sequence" instead, which is covariant
if isinstance(field, VirtualTable):
return VirtualTableType(table_type=self, field=name, virtual_table=field)

Check failure on line 204 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "table_type" to "VirtualTableType" has incompatible type "SelectQueryAliasType"; expected "BaseTableType"

Check failure on line 204 in posthog/hogql/ast.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "table_type" to "VirtualTableType" has incompatible type "SelectQueryAliasType"; expected "BaseTableType"
if isinstance(field, ExpressionField):
return ExpressionFieldType(table_type=self, name=name, expr=field.expr)
return FieldType(name=name, table_type=self)
if self.select_query_type.has_child(name, context):
return FieldType(name=name, table_type=self)
raise HogQLException(f"Field {name} not found on query with alias {self.alias}")
Expand Down Expand Up @@ -575,6 +589,7 @@
limit_with_ties: Optional[bool] = None
offset: Optional[Expr] = None
settings: Optional[HogQLQuerySettings] = None
view_name: Optional[str] = None


@dataclass(kw_only=True)
Expand Down
8 changes: 8 additions & 0 deletions posthog/hogql/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def visit_select_query(self, node: ast.SelectQuery):
{name: self.visit(expr) for name, expr in node.window_exprs.items()} if node.window_exprs else None
)
new_node.settings = node.settings.model_copy() if node.settings is not None else None
new_node.view_name = node.view_name

self.scopes.pop()

Expand Down Expand Up @@ -276,6 +277,10 @@ def visit_join_expr(self, node: ast.JoinExpr):
raise ResolverException("Nested views are not supported")

node.table = parse_select(str(database_table.query))

if isinstance(node.table, ast.SelectQuery):
node.table.view_name = database_table.name

node.alias = table_alias or database_table.name
node = self.visit(node)

Expand Down Expand Up @@ -334,6 +339,9 @@ def visit_join_expr(self, node: ast.JoinExpr):
f'Already have joined a table called "{node.alias}". Can\'t join another one with the same name.'
)
node.type = ast.SelectQueryAliasType(alias=node.alias, select_query_type=node.table.type)
if isinstance(node.table, ast.SelectQuery):
node.type.view_name = node.table.view_name

scope.tables[node.alias] = node.type
else:
node.type = node.table.type
Expand Down
1 change: 1 addition & 0 deletions posthog/hogql/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ def visit_select_query(self, node: ast.SelectQuery):
if node.window_exprs
else None,
settings=node.settings.model_copy() if node.settings is not None else None,
view_name=node.view_name,
)

def visit_select_union_query(self, node: ast.SelectUnionQuery):
Expand Down
Loading