Skip to content

Commit

Permalink
feat(flink): implement array operators
Browse files Browse the repository at this point in the history
  • Loading branch information
mfatihaktas committed Jan 11, 2024
1 parent 38aaf8f commit 1e22248
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 35 deletions.
49 changes: 48 additions & 1 deletion ibis/backends/flink/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ def _floor_divide(translator: ExprTranslator, op: ops.Node) -> str:
return f"FLOOR(({left}) / ({right}))"


def _array_column(translator: ExprTranslator, op: ops.arrays.ArrayColumn) -> str:
return "ARRAY[{}]".format(", ".join(map(translator.translate, op.cols)))

Check warning on line 259 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L259

Added line #L259 was not covered by tests


def _array_contains(translator: ExprTranslator, op: ops.arrays.ArrayContains) -> str:
arg = translator.translate(op.arg)
other = translator.translate(op.other)
return f"ARRAY_CONTAINS({arg}, {other})"

Check warning on line 265 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L263-L265

Added lines #L263 - L265 were not covered by tests


def _array_distinct(translator: ExprTranslator, op: ops.arrays.ArrayDistinct) -> str:
arg = translator.translate(op.arg)
return f"ARRAY_DISTINCT({arg})"

Check warning on line 270 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L269-L270

Added lines #L269 - L270 were not covered by tests


def _array_index(translator: ExprTranslator, op: ops.arrays.ArrayIndex):
table_column = op.arg
index = op.index
Expand All @@ -269,6 +284,31 @@ def _array_length(translator: ExprTranslator, op: ops.arrays.ArrayLength) -> str
return f"CARDINALITY({translator.translate(op.arg)})"


def _array_position(translator: ExprTranslator, op: ops.arrays.ArrayPosition) -> str:
arg = translator.translate(op.arg)
other = translator.translate(op.other)
return f"ARRAY_POSITION({arg}, {other}) - 1"

Check warning on line 290 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L288-L290

Added lines #L288 - L290 were not covered by tests


def _array_slice(translator: ExprTranslator, op: ops.arrays.ArraySlice) -> str:
array = translator.translate(op.arg)
start = op.start.value

Check warning on line 295 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L294-L295

Added lines #L294 - L295 were not covered by tests
# Note (mehmet): The offsets are 1-based for ARRAY_SLICE.
# Ref: https://nightlies.apache.org/flink/flink-docs-master/docs/dev/table/functions/systemfunctions
if start >= 0:
start += 1

Check warning on line 299 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L299

Added line #L299 was not covered by tests

if op.stop is None:
return f"ARRAY_SLICE({array}, {start})"

Check warning on line 302 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L302

Added line #L302 was not covered by tests

stop = op.stop.value

Check warning on line 304 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L304

Added line #L304 was not covered by tests
if stop >= 0:
return f"ARRAY_SLICE({array}, {start}, {stop})"

Check warning on line 306 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L306

Added line #L306 was not covered by tests
else:
# Note (mehmet): To imitate the behavior of pandas array slicing.
return f"ARRAY_SLICE({array}, {start}, CARDINALITY({array}) - {abs(stop)})"

Check warning on line 309 in ibis/backends/flink/registry.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/flink/registry.py#L309

Added line #L309 was not covered by tests


def _json_get_item(translator: ExprTranslator, op: ops.json.JSONGetItem) -> str:
arg_translated = translator.translate(op.arg)
if op.index.dtype.is_integer():
Expand Down Expand Up @@ -442,9 +482,16 @@ def _struct_field(translator, op):
# Binary operations
ops.Power: fixed_arity("power", 2),
ops.FloorDivide: _floor_divide,
# Collection functions
# Collection operations
ops.ArrayColumn: _array_column,
ops.ArrayContains: _array_contains,
ops.ArrayDistinct: _array_distinct,
ops.ArrayIndex: _array_index,
ops.ArrayLength: _array_length,
ops.ArrayPosition: _array_position,
ops.ArrayRemove: fixed_arity("ARRAY_REMOVE", 2),
ops.ArraySlice: _array_slice,
ops.ArrayUnion: fixed_arity("ARRAY_UNION", 2),
ops.JSONGetItem: _json_get_item,
ops.Map: _map,
ops.MapGet: _map_get,
Expand Down
3 changes: 2 additions & 1 deletion ibis/backends/flink/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ def connect(*, tmpdir, worker_id, **kw: Any):
def _load_data(self, **_: Any) -> None:
import pandas as pd

from ibis.backends.tests.data import json_types, struct_types
from ibis.backends.tests.data import array_types, json_types, struct_types

for table_name in TEST_TABLES:
path = self.data_dir / "parquet" / f"{table_name}.parquet"
self.connection.create_table(table_name, pd.read_parquet(path), temp=True)

self.connection.create_table("json_t", json_types, temp=True)
self.connection.create_table("struct", struct_types, temp=True)
self.connection.create_table("array_types", array_types, temp=True)


class TestConfForStreaming(TestConf):
Expand Down
Loading

0 comments on commit 1e22248

Please sign in to comment.