Skip to content

Commit

Permalink
feat(bigquery): implement ops.ArraySlice
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-kwitt authored and cpcloud committed Jan 4, 2023
1 parent 53fc6cb commit 49414ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 19 additions & 0 deletions ibis/backends/bigquery/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,24 @@ def _array_repeat(t, op):
return f"ARRAY(SELECT {arg}[SAFE_ORDINAL({idx})] FROM UNNEST({series}) AS i)"


def _neg_idx_to_pos(array, idx):
return f"IF({idx} < 0, ARRAY_LENGTH({array}) + {idx}, {idx})"


def _array_slice(t, op):
arg = t.translate(op.arg)
cond = [f"index >= {_neg_idx_to_pos(arg, t.translate(op.start))}"]
if op.stop:
cond.append(f"index < {_neg_idx_to_pos(arg, t.translate(op.stop))}")
return (
f"ARRAY("
f"SELECT el "
f"FROM UNNEST({arg}) AS el WITH OFFSET index "
f"WHERE {' AND '.join(cond)}"
f")"
)


OPERATION_REGISTRY = {
**operation_registry,
# Literal
Expand Down Expand Up @@ -561,6 +579,7 @@ def _array_repeat(t, op):
ops.ArrayIndex: _array_index,
ops.ArrayLength: unary("ARRAY_LENGTH"),
ops.ArrayRepeat: _array_repeat,
ops.ArraySlice: _array_slice,
ops.Log: _log,
ops.Log2: _log2,
ops.Arbitrary: _arbitrary,
Expand Down
3 changes: 0 additions & 3 deletions ibis/backends/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,6 @@ def test_unnest_default_name(con):
],
)
@pytest.mark.notimpl(["dask", "datafusion", "polars", "snowflake"])
@pytest.mark.notyet(
["bigquery"], reason="BigQuery doesn't have native array slicing functionality"
)
def test_array_slice(con, start, stop):
array_types = con.tables.array_types
expr = array_types.select(sliced=array_types.y[start:stop])
Expand Down

0 comments on commit 49414ef

Please sign in to comment.