Ejemplo n.º 1
0
def test_complex_conditions_expr(actual, expected) -> None:
    entity = get_entity(EntityKey.EVENTS)
    assert (
        parse_function_to_expr(
            actual, entity.get_data_model(), {"sdk_integrations", "tags.key"}
        )
        == expected
    ), actual
Ejemplo n.º 2
0
def parse_expression(val: Any, dataset_columns: ColumnSet,
                     arrayjoin: Set[str]) -> Expression:
    """
    Parse a simple or structured expression encoded in the Snuba query language
    into an AST Expression.
    """
    if is_function(val, 0):
        return parse_function_to_expr(val, dataset_columns, arrayjoin)
    if isinstance(val, str):
        return parse_string_to_expr(val)
    raise ParsingException(
        f"Expression to parse can only be a function or a string: {val}")
Ejemplo n.º 3
0
def parse_expression(val: Any) -> Expression:
    if is_function(val, 0):
        return parse_function_to_expr(val)
    # TODO: This will use the schema of the dataset to decide
    # if the expression is a column or a literal.
    if val.isdigit():
        return Literal(None, int(val))
    else:
        try:
            return Literal(None, float(val))
        except Exception:
            if QUOTED_LITERAL_RE.match(val):
                return Literal(None, val[1:-1])
            else:
                return Column(None, val, None)
Ejemplo n.º 4
0
def test_complex_conditions_expr() -> None:
    assert parse_function_to_expr(tuplify(["count", []]),) == FunctionCall(
        None, "count", ()
    )
    assert parse_function_to_expr(tuplify(["notEmpty", ["foo"]]),) == FunctionCall(
        None, "notEmpty", (Column(None, "foo", None),)
    )
    assert parse_function_to_expr(
        tuplify(["notEmpty", ["arrayElement", ["foo", 1]]]),
    ) == FunctionCall(
        None,
        "notEmpty",
        (
            FunctionCall(
                None, "arrayElement", (Column(None, "foo", None), Literal(None, 1))
            ),
        ),
    )
    assert parse_function_to_expr(
        tuplify(["foo", ["bar", ["qux"], "baz"]]),
    ) == FunctionCall(
        None,
        "foo",
        (
            FunctionCall(None, "bar", (Column(None, "qux", None),)),
            Column(None, "baz", None),
        ),
    )
    assert parse_function_to_expr(tuplify(["foo", [], "a"]),) == FunctionCall(
        "a", "foo", ()
    )
    assert parse_function_to_expr(tuplify(["foo", ["b", "c"], "d"]),) == FunctionCall(
        "d", "foo", (Column(None, "b", None), Column(None, "c", None))
    )
    assert parse_function_to_expr(tuplify(["foo", ["b", "c", ["d"]]]),) == FunctionCall(
        None,
        "foo",
        (Column(None, "b", None), FunctionCall(None, "c", (Column(None, "d", None),))),
    )

    assert parse_function_to_expr(
        tuplify(["emptyIfNull", ["project_id"]]),
    ) == FunctionCall(None, "emptyIfNull", (Column(None, "project_id", None),))

    assert parse_function_to_expr(
        tuplify(["or", [["or", ["a", "b"]], "c"]]),
    ) == binary_condition(
        None,
        BooleanFunctions.OR,
        binary_condition(
            None, BooleanFunctions.OR, Column(None, "a", None), Column(None, "b", None)
        ),
        Column(None, "c", None),
    )
    assert parse_function_to_expr(
        tuplify(["and", [["and", ["a", "b"]], "c"]]),
    ) == binary_condition(
        None,
        BooleanFunctions.AND,
        binary_condition(
            None, BooleanFunctions.AND, Column(None, "a", None), Column(None, "b", None)
        ),
        Column(None, "c", None),
    )
    # (A OR B) AND C
    assert parse_function_to_expr(
        tuplify(["and", [["or", ["a", "b"]], "c"]]),
    ) == binary_condition(
        None,
        BooleanFunctions.AND,
        binary_condition(
            None, BooleanFunctions.OR, Column(None, "a", None), Column(None, "b", None)
        ),
        Column(None, "c", None),
    )
    # A OR B OR C OR D
    assert parse_function_to_expr(
        tuplify(["or", [["or", [["or", ["c", "d"]], "b"]], "a"]]),
    ) == binary_condition(
        None,
        BooleanFunctions.OR,
        binary_condition(
            None,
            BooleanFunctions.OR,
            binary_condition(
                None,
                BooleanFunctions.OR,
                Column(None, "c", None),
                Column(None, "d", None),
            ),
            Column(None, "b", None),
        ),
        Column(None, "a", None),
    )

    assert parse_function_to_expr(
        tuplify(
            [
                "if",
                [["in", ["release", "tuple", ["'foo'"]]], "release", "'other'"],
                "release",
            ]
        ),
    ) == FunctionCall(
        "release",
        "if",
        (
            FunctionCall(
                None,
                "in",
                (
                    Column(None, "release", None),
                    FunctionCall(None, "tuple", (Literal(None, "foo"),)),
                ),
            ),
            Column(None, "release", None),
            Literal(None, "other"),
        ),
    )

    # TODO once search_message is filled in everywhere, this can be just 'message' again.
    assert parse_function_to_expr(
        tuplify(["positionCaseInsensitive", ["message", "'lol 'single' quotes'"]]),
    ) == FunctionCall(
        None,
        "positionCaseInsensitive",
        (Column(None, "message", None), Literal(None, "lol 'single' quotes")),
    )
Ejemplo n.º 5
0
def test_complex_conditions_expr(actual, expected) -> None:
    dataset = get_dataset("events")
    assert (parse_function_to_expr(
        actual, dataset.get_abstract_columnset(),
        {"sdk_integrations", "tags.key"}) == expected), actual