Skip to content

Commit

Permalink
fix(python): Fixed typo in file lazy.py (pola-rs#19769)
Browse files Browse the repository at this point in the history
  • Loading branch information
sn0rkmaiden authored and TNieuwdorp committed Nov 14, 2024
1 parent 9f79100 commit a3c0ece
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 14 deletions.
6 changes: 5 additions & 1 deletion crates/polars-plan/src/dsl/function_expr/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl From<ArrayFunction> for SpecialEq<Arc<dyn ColumnsUdf>> {
#[cfg(feature = "array_count")]
CountMatches => map_as_slice!(count_matches),
Shift => map_as_slice!(shift),
Explode => unreachable!(),
Explode => map_as_slice!(explode),
}
}
}
Expand Down Expand Up @@ -253,3 +253,7 @@ pub(super) fn shift(s: &[Column]) -> PolarsResult<Column> {

ca.array_shift(n.as_materialized_series()).map(Column::from)
}

fn explode(c: &[Column]) -> PolarsResult<Column> {
c[0].explode()
}
13 changes: 8 additions & 5 deletions crates/polars-plan/src/plans/aexpr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ impl AExpr {
.get(*expr)
.to_field_impl(schema, ctx, arena, &mut false)?;

if let List(inner) = field.dtype() {
Ok(Field::new(field.name().clone(), *inner.clone()))
} else {
Ok(field)
}
let field = match field.dtype() {
List(inner) => Field::new(field.name().clone(), *inner.clone()),
#[cfg(feature = "dtype-array")]
Array(inner, ..) => Field::new(field.name().clone(), *inner.clone()),
_ => field,
};

Ok(field)
},
Alias(expr, name) => Ok(Field::new(
name.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ pub(super) fn optimize_functions(
expr_arena: &mut Arena<AExpr>,
) -> PolarsResult<Option<AExpr>> {
let out = match function {
#[cfg(feature = "dtype-array")]
// arr.explode() -> explode()
FunctionExpr::ArrayExpr(ArrayFunction::Explode) => {
let input_node = input[0].node();
Some(AExpr::Explode(input_node))
},
// is_null().any() -> null_count() > 0
// is_not_null().any() -> null_count() < len()
// CORRECTNESS: we can ignore 'ignore_nulls' since is_null/is_not_null never produces NULLS
Expand Down
28 changes: 28 additions & 0 deletions py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,31 @@ def __getattr__(name: str) -> Any:

msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)


# fork() breaks Polars thread pool, so warn users who might be doing this.
def __install_postfork_hook() -> None:
message = """\
Using fork() can cause Polars to deadlock in the child process.
In addition, using fork() with Python in general is a recipe for mysterious
deadlocks and crashes.
The most likely reason you are seeing this error is because you are using the
multiprocessing module on Linux, which uses fork() by default. This will be
fixed in Python 3.14. Until then, you want to use the "spawn" context instead.
See https://docs.pola.rs/user-guide/misc/multiprocessing/ for details.
"""

def before_hook() -> None:
import warnings

warnings.warn(message, RuntimeWarning, stacklevel=2)

import os

if hasattr(os, "register_at_fork"):
os.register_at_fork(before=before_hook)


__install_postfork_hook()
4 changes: 3 additions & 1 deletion py-polars/polars/dataframe/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ def write_body(self) -> None:
else:
series = self.df[:, c]
self.elements.append(
html.escape(series._s.get_fmt(r, str_len_limit))
html.escape(
series._s.get_fmt(r, str_len_limit)
).replace(" ", "&nbsp;")
)

def write(self, inner: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/functions/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ def map_groups(
The output for group `1` can be understood as follows:
- group `1` contains Series `'a': [1, 3]` and `'b': [4, 5]`
- group `1` contains Series `'a': [1, 3]` and `'b': [5, 6]`
- applying the function to those lists of Series, one gets the output
`[1 / 4 + 5, 3 / 4 + 6]`, i.e. `[5.25, 6.75]`
"""
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/unit/dataframe/test_repr_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ def test_series_repr_html_max_rows_default() -> None:

expected_rows = 10
assert html.count("<td>") - 2 == expected_rows


def test_html_representation_multiple_spaces() -> None:
df = pl.DataFrame(
{"string_col": ["multiple spaces", " trailing and leading "]}
)
html_repr = df._repr_html_()

assert (
html_repr
== """<div><style>
.dataframe > thead > tr,
.dataframe > tbody > tr {
text-align: right;
white-space: pre-wrap;
}
</style>
<small>shape: (2, 1)</small><table border="1" class="dataframe"><thead><tr><th>string_col</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;multiple&nbsp;&nbsp;&nbsp;spaces&quot;</td></tr><tr><td>&quot;&nbsp;&nbsp;trailing&nbsp;and&nbsp;leading&nbsp;&nbsp;&nbsp;&quot;</td></tr></tbody></table></div>"""
)
10 changes: 10 additions & 0 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,3 +1113,13 @@ def test_join_key_type_coercion_19597() -> None:
left.join(
right, left_on=pl.col("a") * 2, right_on=pl.col("a") * 2
).collect_schema()


def test_array_explode_join_19763() -> None:
q = pl.LazyFrame().select(
pl.lit(pl.Series([[1], [2]], dtype=pl.Array(pl.Int64, 1))).explode().alias("k")
)

q = q.join(pl.LazyFrame({"k": [1, 2]}), on="k")

assert_frame_equal(q.collect().sort("k"), pl.DataFrame({"k": [1, 2]}))
31 changes: 31 additions & 0 deletions py-polars/tests/unit/test_polars_import.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import compileall
import multiprocessing
import os
import subprocess
import sys
from pathlib import Path
Expand Down Expand Up @@ -97,3 +99,32 @@ def test_polars_import() -> None:
import_time_ms = polars_import_time // 1_000
msg = f"Possible import speed regression; took {import_time_ms}ms\n{df_import}"
raise AssertionError(msg)


def run_in_child() -> int:
return 123


@pytest.mark.skipif(not hasattr(os, "fork"), reason="Requires fork()")
def test_fork_safety(recwarn: pytest.WarningsRecorder) -> None:
def get_num_fork_warnings() -> int:
fork_warnings = 0
for warning in recwarn:
if issubclass(warning.category, RuntimeWarning) and str(
warning.message
).startswith("Using fork() can cause Polars"):
fork_warnings += 1
return fork_warnings

assert get_num_fork_warnings() == 0

# Using forkserver and spawn context should not do any of our warning:
for context in ["spawn", "forkserver"]:
with multiprocessing.get_context(context).Pool(1) as pool:
assert pool.apply(run_in_child) == 123
assert get_num_fork_warnings() == 0

# Using fork()-based multiprocessing should raise a warning:
with multiprocessing.get_context("fork").Pool(1) as pool:
assert pool.apply(run_in_child) == 123
assert get_num_fork_warnings() == 1
18 changes: 18 additions & 0 deletions py-polars/tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,21 @@ def test_lf_window_schema(expr: pl.Expr, mapping_strategy: str) -> None:
)

assert q.collect_schema() == q.collect().collect_schema()


def test_lf_explode_schema() -> None:
lf = pl.LazyFrame({"k": [1], "x": pl.Series([[1]], dtype=pl.Array(pl.Int64, 1))})

q = lf.select(pl.col("x").explode())
assert q.collect_schema() == {"x": pl.Int64}

q = lf.select(pl.col("x").arr.explode())
assert q.collect_schema() == {"x": pl.Int64}

lf = pl.LazyFrame({"k": [1], "x": pl.Series([[1]], dtype=pl.List(pl.Int64))})

q = lf.select(pl.col("x").explode())
assert q.collect_schema() == {"x": pl.Int64}

q = lf.select(pl.col("x").list.explode())
assert q.collect_schema() == {"x": pl.Int64}

0 comments on commit a3c0ece

Please sign in to comment.