Beispiel #1
0
def test_format_expressions(
    expression: Expression, expected_clickhouse: str, expected_anonymized: str
) -> None:
    visitor = ClickhouseExpressionFormatter()
    anonymized_visitor = ClickHouseExpressionFormatterAnonymized()
    assert expression.accept(visitor) == expected_clickhouse
    assert expression.accept(anonymized_visitor) == expected_anonymized
Beispiel #2
0
def replace_in_expression(
        body: Expression, replace_lookup: Mapping[str,
                                                  Expression]) -> Expression:
    ret = body.transform(lambda exp: replace_lookup[exp.column_name]
                         if isinstance(exp, Column) and exp.column_name in
                         replace_lookup else exp)
    return ret
def test_expand_aliases(
    expression: Expression,
    lookup: Mapping[str, Expression],
    nested_resolution: bool,
    expected: Expression,
) -> None:
    assert (expression.accept(
        AliasExpanderVisitor(lookup, [], nested_resolution)) == expected)
Beispiel #4
0
def test_translation(
    mappings: TranslationMappers,
    expression: Expression,
    expected: ClickhouseExpression,
) -> None:
    translator = SnubaClickhouseMappingTranslator(mappings)
    translated = expression.accept(translator)

    assert translated == expected
Beispiel #5
0
def test_transaction_translation(
    expression: Expression, expected: ClickhouseExpression,
) -> None:
    translator = SnubaClickhouseMappingTranslator(
        transaction_translation_mappers.concat(null_function_translation_mappers)
    )
    translated = expression.accept(translator)

    assert translated == expected
Beispiel #6
0
def test_default_translation(expression: Expression) -> None:
    """
    Ensures that a translation that relies on the default translation rules
    produces a deep copy of the original expression.
    """

    translated = expression.accept(
        SnubaClickhouseMappingTranslator(TranslationMappers()))

    assert translated == expression
Beispiel #7
0
def _process_root(
    expression: Expression,
    subqueries: Mapping[str, SubqueryDraft],
    alias_generator: AliasGenerator,
) -> Expression:
    """
    Takes a root expression in the main query, runs the branch cutter
    and pushes down the subexpressions.
    """
    subexpressions = expression.accept(BranchCutter(alias_generator))
    return _push_down_branches(subexpressions, subqueries, alias_generator)
def test_branch_cutter(expression: Expression, expected: SubExpression,
                       main_expr: MainQueryExpression) -> None:
    def alias_generator() -> Generator[str, None, None]:
        i = 0
        while True:
            i += 1
            yield f"_snuba_gen_{i}"

    subexpression = expression.accept(BranchCutter(alias_generator()))
    assert subexpression == expected
    assert subexpression.cut_branch(alias_generator()) == main_expr
Beispiel #9
0
def test_escaping(expression: Expression, expected: str) -> None:
    visitor = ClickhouseExpressionFormatter()
    assert expression.accept(visitor) == expected
Beispiel #10
0
def test_format_expressions(expression: Expression, expected_clickhouse: str,
                            expected_tracing: TExpression) -> None:
    visitor = ClickhouseExpressionFormatter()
    assert expression.accept(visitor) == expected_clickhouse
    assert expression.accept(TracingExpressionFormatter()) == expected_tracing
Beispiel #11
0
 def transform_aggregate_functions_with_mismatched_nullable_parameters(
     exp: Expression, ) -> Expression:
     if (isinstance(exp, FunctionCall)
             and exp.function_name in AGGREGATION_FUNCTIONS):
         return exp.transform(cast_column_to_nullable)
     return exp