Skip to content

Commit

Permalink
perf(sql): don't compile pretty sql by default (ibis-project#8616)
Browse files Browse the repository at this point in the history
Co-authored-by: Phillip Cloud <[email protected]>
  • Loading branch information
kszucs and cpcloud authored Mar 19, 2024
1 parent 3e945e9 commit af402f9
Show file tree
Hide file tree
Showing 99 changed files with 425 additions and 894 deletions.
8 changes: 4 additions & 4 deletions ibis/backends/clickhouse/tests/test_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
@pytest.mark.parametrize(
"reduction", ["sum", "count", "mean", "max", "min", "std", "var"]
)
def test_reduction_where(alltypes, reduction, snapshot):
def test_reduction_where(alltypes, reduction, assert_sql):
method = getattr(alltypes.double_col, reduction)
cond = alltypes.bigint_col < 70
expr = method(where=cond)

snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)


@pytest.mark.parametrize("method", ["var", "std"])
def test_std_var_pop(con, alltypes, method, snapshot):
def test_std_var_pop(con, alltypes, method, assert_sql):
cond = alltypes.bigint_col < 70
col = alltypes.double_col
expr = getattr(col, method)(where=cond, how="pop")
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert isinstance(con.execute(expr), float)


Expand Down
5 changes: 5 additions & 0 deletions ibis/backends/clickhouse/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
cc = pytest.importorskip("clickhouse_connect")


def test_connection(con):
assert con is not None
assert isinstance(con, ibis.backends.clickhouse.Backend)


def test_run_sql(con):
query = "SELECT * FROM ibis_testing.functional_alltypes"
table = con.sql(query)
Expand Down
83 changes: 41 additions & 42 deletions ibis/backends/clickhouse/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@


@pytest.mark.parametrize("to_type", ["int8", "int16", "float32", "float", "!float64"])
def test_cast_double_col(alltypes, to_type, snapshot):
def test_cast_double_col(alltypes, to_type, assert_sql):
expr = alltypes.double_col.cast(to_type)
result = expr.compile()
snapshot.assert_match(result, "out.sql")
assert_sql(expr)


@pytest.mark.parametrize(
Expand All @@ -37,9 +36,9 @@ def test_cast_double_col(alltypes, to_type, snapshot):
"!struct<a: string, b: int64>",
],
)
def test_cast_string_col(alltypes, to_type, snapshot):
def test_cast_string_col(alltypes, to_type, assert_sql):
expr = alltypes.string_col.cast(to_type)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)


@pytest.mark.parametrize(
Expand All @@ -60,35 +59,35 @@ def test_cast_string_col(alltypes, to_type, snapshot):
"month",
],
)
def test_noop_cast(alltypes, column, snapshot):
def test_noop_cast(alltypes, column, assert_sql):
col = alltypes[column]
result = col.cast(col.type())
assert result.equals(col)
snapshot.assert_match(result.compile(), "out.sql")
assert_sql(result)


def test_timestamp_cast(alltypes, snapshot):
def test_timestamp_cast(alltypes, assert_sql):
target = dt.Timestamp(nullable=False)
result1 = alltypes.timestamp_col.cast(target)
result2 = alltypes.int_col.cast(target)

assert isinstance(result1, ir.TimestampColumn)
assert isinstance(result2, ir.TimestampColumn)

snapshot.assert_match(result1.compile(), "out1.sql")
snapshot.assert_match(result2.compile(), "out2.sql")
assert_sql(result1, "out1.sql")
assert_sql(result2, "out2.sql")


def test_timestamp_now(con, snapshot):
def test_timestamp_now(con, assert_sql):
expr = ibis.now()
snapshot.assert_match(con.compile(expr), "out.sql")
assert_sql(expr)


@pytest.mark.parametrize("unit", ["y", "m", "d", "w", "h", "minute"])
def test_timestamp_truncate(con, unit, snapshot):
def test_timestamp_truncate(con, unit, assert_sql):
stamp = ibis.timestamp("2009-05-17 12:34:56")
expr = stamp.truncate(unit)
snapshot.assert_match(con.compile(expr), "out.sql")
assert_sql(expr)


@pytest.mark.parametrize(("value", "expected"), [(0, None), (5.5, 5.5)])
Expand Down Expand Up @@ -185,13 +184,13 @@ def test_string_substring(con, op, expected):
assert con.execute(op(value)) == expected


def test_string_column_substring(con, alltypes, snapshot):
def test_string_column_substring(con, alltypes, assert_sql):
expr = alltypes.string_col.substr(2)
snapshot.assert_match(expr.compile(), "out1.sql")
assert_sql(expr, "out1.sql")
assert len(con.execute(expr))

expr = alltypes.string_col.substr(0, 3)
snapshot.assert_match(expr.compile(), "out2.sql")
assert_sql(expr, "out2.sql")
assert len(con.execute(expr))


Expand Down Expand Up @@ -243,12 +242,12 @@ def test_find_in_set(con, value, expected):
assert con.execute(expr) == expected


def test_string_column_find_in_set(con, alltypes, snapshot):
def test_string_column_find_in_set(con, alltypes, assert_sql):
s = alltypes.string_col
vals = list("abc")

expr = s.find_in_set(vals)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


Expand All @@ -271,25 +270,25 @@ def test_string_find_like(con, expr, expected):
assert con.execute(expr) == expected


def test_string_column_like(con, alltypes, snapshot):
def test_string_column_like(con, alltypes, assert_sql):
expr = alltypes.string_col.like("foo%")
snapshot.assert_match(expr.compile(), "out1.sql")
assert_sql(expr, "out1.sql")
assert len(con.execute(expr))

expr = alltypes.string_col.like(["foo%", "%bar"])
snapshot.assert_match(expr.compile(), "out2.sql")
assert_sql(expr, "out2.sql")
assert len(con.execute(expr))


def test_string_column_find(con, alltypes, snapshot):
def test_string_column_find(con, alltypes, assert_sql):
s = alltypes.string_col

expr = s.find("a")
snapshot.assert_match(expr.compile(), "out1.sql")
assert_sql(expr, "out1.sql")
assert len(con.execute(expr))

expr = s.find(s)
snapshot.assert_match(expr.compile(), "out2.sql")
assert_sql(expr, "out2.sql")
assert len(con.execute(expr))


Expand All @@ -309,9 +308,9 @@ def test_string_column_find(con, alltypes, snapshot):
param(methodcaller("sign"), id="sign"),
],
)
def test_translate_math_functions(con, alltypes, call, snapshot):
def test_translate_math_functions(con, alltypes, call, assert_sql):
expr = call(alltypes.double_col)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr, "out.sql")
assert len(con.execute(expr))


Expand Down Expand Up @@ -352,21 +351,21 @@ def test_math_functions(con, expr, expected):
assert con.execute(expr) == expected


def test_greatest_least(con, alltypes, snapshot):
def test_greatest_least(con, alltypes, assert_sql):
expr = ibis.greatest(alltypes.int_col, 10)
snapshot.assert_match(expr.compile(), "out1.sql")
assert_sql(expr, "out1.sql")
assert len(con.execute(expr))

expr = ibis.greatest(alltypes.int_col, alltypes.bigint_col)
snapshot.assert_match(expr.compile(), "out2.sql")
assert_sql(expr, "out2.sql")
assert len(con.execute(expr))

expr = ibis.least(alltypes.int_col, 10)
snapshot.assert_match(expr.compile(), "out3.sql")
assert_sql(expr, "out3.sql")
assert len(con.execute(expr))

expr = ibis.least(alltypes.int_col, alltypes.bigint_col)
snapshot.assert_match(expr.compile(), "out4.sql")
assert_sql(expr, "out4.sql")
assert len(con.execute(expr))


Expand Down Expand Up @@ -397,15 +396,15 @@ def test_regexp_extract(con, expr, expected):
assert con.execute(expr) == expected


def test_column_regexp_extract(con, alltypes, snapshot):
def test_column_regexp_extract(con, alltypes, assert_sql):
expr = alltypes.string_col.re_extract(r"[\d]+", 3)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr, "out.sql")
assert len(con.execute(expr))


def test_column_regexp_replace(con, alltypes, snapshot):
def test_column_regexp_replace(con, alltypes, assert_sql):
expr = alltypes.string_col.re_replace(r"[\d]+", "aaa")
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr, "out.sql")
assert len(con.execute(expr))


Expand Down Expand Up @@ -437,10 +436,10 @@ def test_literal_none_to_nullable_column(alltypes):
tm.assert_series_equal(result, expected)


def test_timestamp_from_integer(con, alltypes, snapshot):
def test_timestamp_from_integer(con, alltypes, assert_sql):
# timestamp_col has datetime type
expr = alltypes.int_col.to_timestamp()
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr, "out.sql")
assert len(con.execute(expr))


Expand All @@ -460,15 +459,15 @@ def test_count_distinct_with_filter(alltypes):
param(",", 0, id="comma_zero"),
],
)
def test_group_concat(alltypes, sep, where_case, snapshot):
def test_group_concat(alltypes, sep, where_case, assert_sql):
where = None if where_case is None else alltypes.bool_col == where_case
expr = alltypes.string_col.group_concat(sep, where)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr, "out.sql")


def test_hash(alltypes, snapshot):
def test_hash(alltypes, assert_sql):
expr = alltypes.string_col.hash()
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)


def test_udf_in_array_map(alltypes):
Expand Down
13 changes: 6 additions & 7 deletions ibis/backends/clickhouse/tests/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
ibis.timestamp("2015-01-01 12:34:56"),
],
)
def test_timestamp_literals(con, expr, snapshot):
snapshot.assert_match(con.compile(expr), "out.sql")
def test_timestamp_literals(con, expr, assert_sql):
assert_sql(expr)
assert con.execute(expr) == Timestamp("2015-01-01 12:34:56")


Expand All @@ -32,8 +32,8 @@ def test_timestamp_literals(con, expr, snapshot):
param(ibis.timestamp("2015-01-01 12:34:56.789321 UTC"), id="micros_tz"),
],
)
def test_fine_grained_timestamp_literals(con, expr, snapshot):
snapshot.assert_match(con.compile(expr), "out.sql")
def test_fine_grained_timestamp_literals(con, expr, assert_sql):
assert_sql(expr)
assert con.execute(expr) == expr.op().value


Expand All @@ -49,10 +49,9 @@ def test_fine_grained_timestamp_literals(con, expr, snapshot):
param(False, id="false"),
],
)
def test_string_numeric_boolean_literals(con, value, snapshot):
def test_string_numeric_boolean_literals(con, value, assert_sql):
expr = ibis.literal(value)
result = con.compile(expr)
snapshot.assert_match(result, "out.sql")
assert_sql(expr)
assert con.execute(expr) == value


Expand Down
24 changes: 12 additions & 12 deletions ibis/backends/clickhouse/tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def test_string_temporal_compare(con, op, left, right, type):
"op",
["add", "sub", "mul", "truediv", "pow", "lt", "le", "gt", "ge", "eq", "ne"],
)
def test_binary_infix_operators(con, alltypes, op, snapshot):
def test_binary_infix_operators(con, alltypes, op, assert_sql):
func = getattr(operator, op)
a, b = alltypes.int_col, alltypes.tinyint_col
expr = func(a, b)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


Expand All @@ -78,19 +78,19 @@ def test_binary_infix_operators(con, alltypes, op, snapshot):
lambda a, b, c: (b + (-(a + c))),
],
)
def test_binary_infix_parenthesization(con, alltypes, op, snapshot):
def test_binary_infix_parenthesization(con, alltypes, op, assert_sql):
a = alltypes.int_col
b = alltypes.tinyint_col
c = alltypes.double_col

expr = op(a, b, c)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


def test_between(con, alltypes, snapshot):
def test_between(con, alltypes, assert_sql):
expr = alltypes.int_col.between(0, 10)
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


Expand Down Expand Up @@ -143,9 +143,9 @@ def test_field_in_literals(con, alltypes, df, container):


@pytest.mark.parametrize("column", ["int_col", "float_col", "bool_col"])
def test_negate(con, alltypes, column, snapshot):
def test_negate(con, alltypes, column, assert_sql):
expr = -alltypes[column]
snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


Expand Down Expand Up @@ -199,17 +199,17 @@ def test_ifelse(alltypes, df, op, pandas_op):
tm.assert_series_equal(result, expected)


def test_simple_case(con, alltypes, snapshot):
def test_simple_case(con, alltypes, assert_sql):
t = alltypes
expr = (
t.string_col.case().when("foo", "bar").when("baz", "qux").else_("default").end()
)

snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


def test_search_case(con, alltypes, snapshot):
def test_search_case(con, alltypes, assert_sql):
t = alltypes
expr = (
ibis.case()
Expand All @@ -219,7 +219,7 @@ def test_search_case(con, alltypes, snapshot):
.end()
)

snapshot.assert_match(expr.compile(), "out.sql")
assert_sql(expr)
assert len(con.execute(expr))


Expand Down
Loading

0 comments on commit af402f9

Please sign in to comment.