Skip to content

Commit

Permalink
fix(polars): ensure t.select(col=scalar) results in len(t) rows (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored Mar 17, 2024
1 parent c2b06f6 commit 6c00579
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
18 changes: 15 additions & 3 deletions ibis/backends/polars/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,30 @@ def project(op, **kw):

selections = []
unnests = []
scalars = []
for name, arg in op.values.items():
if isinstance(arg, ops.Unnest):
translated = translate(arg.arg, **kw).alias(name)
unnests.append(name)
translated = translate(arg.arg, **kw)
selections.append(translated)
elif isinstance(arg, ops.Value):
translated = translate(arg, **kw)
translated = translate(arg, **kw).alias(name)
if arg.shape.is_scalar():
scalars.append(translated)
selections.append(pl.col(name))
else:
selections.append(translated)
else:
raise com.TranslationError(
"Polars backend is unable to compile selection with "
f"operation type of {type(arg)}"
)
selections.append(translated.alias(name))

if scalars:
# Scalars need to first be projected to columns with `with_columns`,
# otherwise if a downstream select only selects out the scalar-backed
# columns they'll return with only a single row.
lf = lf.with_columns(scalars)

if selections:
lf = lf.select(selections)
Expand Down
6 changes: 6 additions & 0 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,12 @@ def test_select_mutate_with_dict(backend):
backend.assert_frame_equal(result, expected)


def test_select_scalar(alltypes):
res = alltypes.select(y=ibis.literal(1)).limit(3).execute()
assert len(res.y) == 3
assert (res.y == 1).all()


@pytest.mark.broken(["mssql", "oracle"], reason="incorrect syntax")
def test_isnull_equality(con, backend, monkeypatch):
monkeypatch.setattr(ibis.options, "default_backend", con)
Expand Down
4 changes: 0 additions & 4 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,6 @@ def test_now(con):
assert isinstance(result, datetime.datetime)


@pytest.mark.notimpl(["polars"], reason="assert 1 == 5", raises=AssertionError)
@pytest.mark.notimpl(["datafusion"], raises=com.OperationNotDefinedError)
def test_now_from_projection(alltypes):
n = 2
Expand All @@ -1579,9 +1578,6 @@ def test_today(con):
assert isinstance(result, datetime.date)


@pytest.mark.notimpl(
["polars"], reason="polars fails to project literal", raises=AssertionError
)
def test_today_from_projection(alltypes):
expr = alltypes.select(today=ibis.today()).limit(2).today
ts = expr.execute()
Expand Down

0 comments on commit 6c00579

Please sign in to comment.