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

feat(hogql): support join USING #21916

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

nikitaevg
Copy link
Contributor

@nikitaevg nikitaevg commented Apr 28, 2024

Problem

#20660

Changes

Does this work well for both Cloud and self-hosted?

Yes

How did you test this code?

Automated tests

Copy link

sentry-io bot commented Apr 28, 2024

🔍 Existing Issues For Review

Your pull request is modifying functions with the following pre-existing issues:

📄 File: posthog/hogql/printer.py

Function Unhandled Issue
visit_join_expr QueryError: Table function 'numbers' requires arguments posthog.tasks.tasks.process_que...
Event Count: 1
📄 File: posthog/hogql/resolver.py (Click to Expand)
Function Unhandled Issue
visit_join_expr QueryError: Unknown table "e". posthog.tasks.task...
Event Count: 8
visit_join_expr QueryError: Unknown table "customer_value". posth...
Event Count: 2
visit_join_expr QueryError: Unknown table "stripe_invoice". posth...
Event Count: 2
visit_join_expr QueryError: Already have joined a table called "polls". Can't join another one with the same name. ...
Event Count: 2
---

Did you find this useful? React with a 👍 or 👎

@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@nikitaevg
Copy link
Contributor Author

Unstale please

@posthog-bot posthog-bot removed the stale label May 7, 2024
@@ -514,6 +510,13 @@ posthog/hogql/test/test_resolver.py:0: error: Incompatible types in assignment (
posthog/hogql/test/test_resolver.py:0: error: "TestResolver" has no attribute "snapshot" [attr-defined]
posthog/hogql/test/test_resolver.py:0: error: Incompatible types in assignment (expression has type "Expr", variable has type "SelectQuery") [assignment]
posthog/hogql/test/test_resolver.py:0: error: "TestResolver" has no attribute "snapshot" [attr-defined]
posthog/hogql/test/test_resolver.py:0: error: Item "None" of "JoinExpr | None" has no attribute "next_join" [union-attr]
Copy link
Contributor Author

@nikitaevg nikitaevg May 10, 2024

Choose a reason for hiding this comment

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

IMO it doesn't make sense and will reduce readability if I asserted in tests that these fields are not None, but I could do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup that's fine. I agree that additional asserts wouldn't help code quality.

node = cast(ast.JoinExpr, clone_expr(node))
if node.constraint and node.constraint.constraint_type == "USING":
# visit USING constraint before adding the table to avoid ambiguous names
node.constraint = self.visit_join_constraint(node.constraint)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

visit_join_constraint instead of visit to have correct types - visit returns Expr while visit_join_contraint returns JoinContraint

@nikitaevg nikitaevg marked this pull request as ready for review May 10, 2024 07:01
@nikitaevg
Copy link
Contributor Author

nikitaevg commented May 10, 2024

@thmsobrmlr PTAL!

About the failing tests - it seems like e2e tests don't recompile the c++ parser. These tests fail with the following error

2024-05-10T07:14:19.988440Z [error    ] JoinConstraint.__init__() missing 1 required keyword-only argument: 'constraint_type' [posthog.exceptions] host=localhost:8000 ip=127.0.0.1 path=/api/projects/1/query/ pid=141 request_id=20271ca5-215d-47fd-82af-c9df1a267eee team_id=1 tid=139837938559296 x_forwarded_for=
Traceback (most recent call last):
  File "/python-runtime/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/code/./posthog/api/query.py", line 92, in create
    raise e
  File "/code/./posthog/api/query.py", line 79, in create
    result = process_query_model(
  File "/code/./posthog/api/services/query.py", line 92, in process_query_model
    result = query_runner.run(execution_mode=execution_mode)
  File "/code/./posthog/hogql_queries/query_runner.py", line 377, in run
    fresh_response_dict = self.calculate().model_dump()
  File "/code/./posthog/hogql_queries/insights/retention_query_runner.py", line 309, in calculate
    query = self.to_query()
  File "/code/./posthog/hogql_queries/insights/retention_query_runner.py", line 245, in to_query
    "actor_query": self.actor_query(),
  File "/code/./posthog/hogql_queries/insights/retention_query_runner.py", line 207, in actor_query
    return parse_select(
  File "/code/./posthog/hogql/parser.py", line 100, in parse_select
    node = RULE_TO_PARSE_FUNCTION[backend]["select"](statement)
  File "/code/./posthog/hogql/parser.py", line 32, in <lambda>
    "select": lambda string: _parse_select_cpp(string),
TypeError: JoinConstraint.__init__() missing 1 required keyword-only argument: 'constraint_type'

But they finish successfully for me locally. Maybe there's a way to fix this?

@thmsobrmlr
Copy link
Contributor

@thmsobrmlr PTAL!

About the failing tests - it seems like e2e tests don't recompile the c++ parser. These tests fail with the following error

2024-05-10T07:14:19.988440Z [error    ] JoinConstraint.__init__() missing 1 required keyword-only argument: 'constraint_type' [posthog.exceptions] host=localhost:8000 ip=127.0.0.1 path=/api/projects/1/query/ pid=141 request_id=20271ca5-215d-47fd-82af-c9df1a267eee team_id=1 tid=139837938559296 x_forwarded_for=
Traceback (most recent call last):
  File "/python-runtime/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/code/./posthog/api/query.py", line 92, in create
    raise e
  File "/code/./posthog/api/query.py", line 79, in create
    result = process_query_model(
  File "/code/./posthog/api/services/query.py", line 92, in process_query_model
    result = query_runner.run(execution_mode=execution_mode)
  File "/code/./posthog/hogql_queries/query_runner.py", line 377, in run
    fresh_response_dict = self.calculate().model_dump()
  File "/code/./posthog/hogql_queries/insights/retention_query_runner.py", line 309, in calculate
    query = self.to_query()
  File "/code/./posthog/hogql_queries/insights/retention_query_runner.py", line 245, in to_query
    "actor_query": self.actor_query(),
  File "/code/./posthog/hogql_queries/insights/retention_query_runner.py", line 207, in actor_query
    return parse_select(
  File "/code/./posthog/hogql/parser.py", line 100, in parse_select
    node = RULE_TO_PARSE_FUNCTION[backend]["select"](statement)
  File "/code/./posthog/hogql/parser.py", line 32, in <lambda>
    "select": lambda string: _parse_select_cpp(string),
TypeError: JoinConstraint.__init__() missing 1 required keyword-only argument: 'constraint_type'

But they finish successfully for me locally. Maybe there's a way to fix this?

@Twixes Any hints on what's going on here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants