Skip to content

Commit 224bfc3

Browse files
IndexSeekcpcloud
authored andcommitted
feat(polars): add ArraySlice operation
1 parent a4fe3ee commit 224bfc3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

ibis/backends/polars/compiler.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,3 +1488,29 @@ def execute_array_index(op, **kw):
14881488
arg = translate(op.arg, **kw)
14891489
index = translate(op.index, **kw)
14901490
return arg.list.get(index)
1491+
1492+
1493+
@translate.register(ops.ArraySlice)
1494+
def visit_ArraySlice(op, **kw):
1495+
arg = translate(op.arg, **kw)
1496+
arg_length = arg.list.len()
1497+
1498+
start = translate(op.start, **kw) if op.start is not None else 0
1499+
stop = translate(op.stop, **kw) if op.stop is not None else arg_length
1500+
1501+
def normalize_index(index, length):
1502+
index = pl.when(index < 0).then(length + index).otherwise(index)
1503+
return (
1504+
pl.when(index < 0)
1505+
.then(0)
1506+
.when(index > length)
1507+
.then(length)
1508+
.otherwise(index)
1509+
)
1510+
1511+
start = normalize_index(start, arg_length)
1512+
stop = normalize_index(stop, arg_length)
1513+
1514+
slice_len = pl.when(stop > start).then(stop - start).otherwise(0)
1515+
1516+
return arg.list.slice(start, slice_len)

ibis/backends/tests/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def test_unnest_default_name(backend):
406406
),
407407
],
408408
)
409-
@pytest.mark.notimpl(["datafusion", "polars"], raises=com.OperationNotDefinedError)
409+
@pytest.mark.notimpl(["datafusion"], raises=com.OperationNotDefinedError)
410410
def test_array_slice(backend, start, stop):
411411
array_types = backend.array_types
412412
expr = array_types.select(sliced=array_types.y[start:stop])

0 commit comments

Comments
 (0)