Esempio n. 1
0
def test_invalid_query() -> None:
    with pytest.raises(InvalidQueryError,
                       match=re.escape("queries must have a valid dataset")):
        Query(dataset=1, match=Entity("events"))  # type: ignore

    with pytest.raises(InvalidQueryError,
                       match=re.escape("queries must have a valid dataset")):
        Query(dataset="", match=Entity("events"))

    with pytest.raises(InvalidQueryError,
                       match=re.escape("queries must have a valid Entity")):
        Query(dataset="discover", match="events")  # type: ignore

    with pytest.raises(
            InvalidConditionError,
            match=re.escape(
                "invalid condition: LHS of a condition must be a Column, CurriedFunction or Function, not <class 'snuba_sdk.aliased_expression.AliasedExpression'>"
            ),
    ):
        (Query("discover", Entity("events")).set_select(
            [AliasedExpression(Column("transaction"), "tn")]).set_where([
                Condition(AliasedExpression(Column("project_id"), "pi"), Op.IN,
                          (1, ))
            ]  # type: ignore
                                                                        ))
def test_aliased_expression(
    exp: Column,
    alias: Optional[str],
    translated: str,
    exception: Optional[Exception],
) -> None:
    if exception is not None:
        with pytest.raises(type(exception), match=re.escape(str(exception))):
            AliasedExpression(exp, alias)
    else:
        aliased = AliasedExpression(exp, alias)
        assert TRANSLATOR.visit(aliased) == translated
Esempio n. 3
0
    def aliased_column(self, name: str, alias: str) -> SelectType:
        """Given an unresolved sentry name and an expected alias, return a snql
        column that will be aliased to the expected alias.

        :param name: The unresolved sentry name.
        :param alias: The expected alias in the result.
        """

        # TODO: This method should use an aliased column from the SDK once
        # that is available to skip these hacks that we currently have to
        # do aliasing.
        resolved = self.resolve_column_name(name)
        column = Column(resolved)

        # If the expected alias is identical to the resolved snuba column,
        # no need to do this aliasing trick.
        #
        # Additionally, tags of the form `tags[...]` can't be aliased again
        # because it confuses the sdk.
        if alias == resolved:
            return column

        # If the expected aliases differs from the resolved snuba column,
        # make sure to alias the expression appropriately so we get back
        # the column with the correct names.
        return AliasedExpression(column, alias)
Esempio n. 4
0
 def test_array_join_clause(self):
     query = QueryBuilder(
         Dataset.Discover,
         self.params,
         "",
         selected_columns=[
             "spans_op",
             "count()",
         ],
         array_join="spans_op",
     )
     self.assertCountEqual(
         query.columns,
         [
             AliasedExpression(Column("spans.op"), "spans_op"),
             Function("count", [], "count"),
         ],
     )
     assert query.array_join == Column("spans.op")
     query.get_snql_query().validate()
Esempio n. 5
0
    def test_simple_query(self):
        query = QueryBuilder(
            Dataset.Discover,
            self.params,
            "user.email:[email protected] release:1.2.1",
            ["user.email", "release"],
        )

        self.assertCountEqual(
            query.where,
            [
                Condition(Column("email"), Op.EQ, "*****@*****.**"),
                Condition(Column("release"), Op.EQ, "1.2.1"),
                *self.default_conditions,
            ],
        )
        self.assertCountEqual(
            query.select,
            [
                AliasedExpression(Column("email"), "user.email"),
                Column("release"),
            ],
        )
        query.get_snql_query().validate()
Esempio n. 6
0
            )
        ]).set_limit(10).set_offset(1).set_granularity(3600),
        (
            "MATCH (discover)",
            "SELECT arrayMax(array(1, indexOf('a', hierarchical_hashes)))",
            "WHERE event_id IN tuple(group_id, primary_hash)",
            "LIMIT 10",
            "OFFSET 1",
            "GRANULARITY 3600",
        ),
        None,
        id="sequences can mix expressions with literals",
    ),
    pytest.param(
        Query("discover", Entity("events")).set_select([
            AliasedExpression(Column("transaction"), "tn"),
            Function("count", [], "equation[0]"),
        ]).set_groupby([
            AliasedExpression(Column("project_id"), "pi"),
            AliasedExpression(Column("transaction"), "tn"),
        ]).set_where([Condition(Column("project_id"), Op.IN, (1, ))]),
        (
            "MATCH (events)",
            "SELECT transaction AS tn, count() AS equation[0]",
            "BY project_id AS pi, transaction AS tn",
            "WHERE project_id IN tuple(1)",
        ),
        None,
        id="columns can have aliases",
    ),
]