Skip to content

Commit

Permalink
test(arrays): add array repeat test on column expression (#9182)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored May 13, 2024
1 parent 474855f commit bee1da5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions ibis/backends/snowflake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
return Object.assign(...keys.map((key, j) => ({[key]: arrays[j][i]})));
})""",
},
"ibis_udfs.public.array_repeat": {
# Integer inputs are not allowed because JavaScript only supports
# doubles
"inputs": {"value": "ARRAY", "count": "DOUBLE"},
"returns": "ARRAY",
"source": """return Array(count).fill(value).flat();""",
},
}


Expand Down
7 changes: 1 addition & 6 deletions ibis/backends/snowflake/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,7 @@ def visit_TypeOf(self, op, *, arg):
return self.f.typeof(self.f.to_variant(arg))

def visit_ArrayRepeat(self, op, *, arg, times):
return self.f.array_flatten(
self.f.transform(
self.f.array_generate_range(0, times),
sge.Lambda(this=arg, expressions=[sg.to_identifier("__arg__")]),
)
)
return self.f.udf.array_repeat(arg, times)

def visit_ArrayUnion(self, op, *, left, right):
return self.f.array_distinct(self.f.array_cat(left, right))
Expand Down
11 changes: 11 additions & 0 deletions ibis/backends/tests/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import functools
from collections import Counter
from datetime import datetime

import numpy as np
Expand Down Expand Up @@ -117,6 +118,16 @@ def test_array_repeat(con):
assert np.array_equal(result, expected)


@pytest.mark.notimpl(["flink", "polars"], raises=com.OperationNotDefinedError)
def test_array_repeat_column(con):
t = ibis.memtable({"x": [[1.0, 2.0]]}, schema=ibis.schema({"x": "array<float64>"}))
expr = (t.x * 2).name("tmp")

result = con.execute(expr.name("tmp")).iat[0]
expected = np.array([1.0, 2.0, 1.0, 2.0])
assert Counter(result) == Counter(expected)


def test_array_concat(con):
left = ibis.literal([1, 2, 3])
right = ibis.literal([2, 1])
Expand Down

0 comments on commit bee1da5

Please sign in to comment.