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(devexp): add ability to parse simple storage queries #5776

Closed
wants to merge 24 commits into from

Conversation

volokluev
Copy link
Member

@volokluev volokluev commented Apr 15, 2024

This is the most simple version of this PR:

Work that is needed to make this really work:

  • Add required_time_column to storages so we can do the date alignment logic we use for entities
    • or we just don't care like what we're doing right now
  • Add BasicFunctionsProcessor as a post_process step so it's entity independent

Copy link

sentry-io bot commented Apr 15, 2024

🔍 Existing Issues For Review

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

📄 File: snuba/query/snql/parser.py

Function Unhandled Issue
parse_snql_query PostProcessingError: None snql_dataset_query_view...
Event Count: 4

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

Copy link

codecov bot commented Apr 15, 2024

Test Failures Detected: Due to failing tests, we cannot provide coverage reports at this time.

❌ Failed Test Results:

Completed 1 tests with 1 failed, 0 passed and 0 skipped.

View the full list of failed tests
Test Description Failure message
Testsuite:
pytest

Test name:
::tests.query.snql.test_storage_query

Envs:
- default
No failure message available

@@ -23,18 +47,6 @@ def literal(value: OptionalScalarType, alias: str | None = None) -> Literal:
return Literal(alias, value)


def snuba_tags_raw(indexer_mapping: int) -> SubscriptableReference:
Copy link
Member Author

Choose a reason for hiding this comment

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

was not used

Comment on lines +106 to +108
def equals(
lhs: Expression | OptionalScalarType, rhs: Expression | OptionalScalarType
) -> FunctionCall:
Copy link
Member Author

Choose a reason for hiding this comment

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

happy to change this but I found it useful to not have to specify literals

Copy link
Member

Choose a reason for hiding this comment

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

i like it

@@ -81,3 +79,73 @@ def _transform_expressions_impl(

def _transform_impl(self, visitor: ExpressionVisitor[Expression]) -> None:
pass


class StorageQuery(ProcessableQuery[Storage]):
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: I think this type can be generalized

Copy link
Member

Choose a reason for hiding this comment

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

Why did you decide to parse into a StorageQuery rather than a ClickhouseQuery?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. The main thing is that the Table datasaoure does not resemble the entity in any real way and it's only by the time we get to the StorageProcessing stage that we can process the clickhouse query.

Ideally what I would do is remove Table as a datasource and probably remove ClickhouseQuery later.

Copy link
Member

Choose a reason for hiding this comment

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

Why does it matter that Table doesn't resemble Entity?

@@ -1532,8 +1617,6 @@ def parse_snql_query(
with sentry_sdk.start_span(op="processor", description="time_based_processing"):
_post_process(query, [_replace_time_condition], settings)

# XXX: Select the entity to be used for the query. This step is temporary. Eventually
Copy link
Member Author

Choose a reason for hiding this comment

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

RIP this comment

Copy link
Member

@kylemumma kylemumma left a comment

Choose a reason for hiding this comment

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

My biggest concern would be the introduction of a new StorageQuery class rather than going directly to a ClickhouseQuery.

@@ -81,3 +79,73 @@ def _transform_expressions_impl(

def _transform_impl(self, visitor: ExpressionVisitor[Expression]) -> None:
pass


class StorageQuery(ProcessableQuery[Storage]):
Copy link
Member

Choose a reason for hiding this comment

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

Why did you decide to parse into a StorageQuery rather than a ClickhouseQuery?

Comment on lines +106 to +108
def equals(
lhs: Expression | OptionalScalarType, rhs: Expression | OptionalScalarType
) -> FunctionCall:
Copy link
Member

Choose a reason for hiding this comment

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

i like it



@pytest.mark.parametrize("query_body, expected_query", test_cases)
def test_format_expressions(query_body: str, expected_query: StorageQuery) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

nit: im not sure if this is what you meant to name it but i was expecting something more like test_parse_storage or something

try:
return StorageKey(node.text)
except Exception:
raise ParsingException(f"{node.text} is not a valid Storage name")
Copy link
Member

Choose a reason for hiding this comment

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

unrelated to current PR, but one thought I have in general is that it might be nice to be able to parse StorageKey and EntityKey even if it doesnt exist. And then later on you get an error if you try to access one that doesnt exist. This would make using fake ones in testing easier.


assert isinstance(match, QueryEntity) # mypy
assert isinstance(match, (QueryEntity, QueryStorage)) # mypy
Copy link
Member

Choose a reason for hiding this comment

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

random comment that you can ignore: if you care to read a relevant excerpt of the pragmatic programmer about "assertive programming" https://www.informit.com/articles/article.aspx?p=2982114&seqNum=3

Comment on lines +341 to +345
if not isinstance(match, (QueryEntity, QueryStorage)):
import pdb

pdb.set_trace()
print(match)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if not isinstance(match, (QueryEntity, QueryStorage)):
import pdb
pdb.set_trace()
print(match)

query.transform_expressions(mangle_column_value)
print(query)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
print(query)

@volokluev volokluev closed this May 7, 2024
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

2 participants