Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#27: Added getitem method and test #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions exasol_udf_mock_python/mock_context_run_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

def _disallowed_function(*args, **kw):
raise RuntimeError(
"F-UDF-CL-SL-PYTHON-1107: next(), reset() and emit() functions are not allowed in scalar context")
"F-UDF-CL-SL-PYTHON-1107: next(), reset() and emit() "
"functions are not allowed in scalar context")


class MockContextRunWrapper:

def __init__(self, mock_context: MockContext, input_type: str, output_type: str):
def __init__(
self, mock_context: MockContext, input_type: str, output_type: str):
self._output_type = output_type
self._input_type = input_type
self._mock_context = mock_context
Expand All @@ -24,6 +27,8 @@ def __init__(self, mock_context: MockContext, input_type: str, output_type: str)
self.get_dataframe = self._mock_context.get_dataframe
self.size = self._mock_context.size


def __getattr__(self, name):
return self._mock_context.__getattr__(name)

def __getitem__(self, item):
return self._mock_context._data[item]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@umitbuyuksahin I am not sure, if this is correct for all cases, because I think, getitem is only available in case you have a variadic udf and in that case, you don't have getattr. Also, the columns names for variadic udfs are only number, in the current implementation they can be anything. A proper solution would have been to introduce an option to the MockMetadata that you want to mock a variadic udf. Please, add an issue to this project to fix this.

Copy link
Contributor Author

@umitbuyuksahin umitbuyuksahin May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getattr is not available for variadic while getitem is available for both case.
The ticket(#30) is created for that

31 changes: 31 additions & 0 deletions tests/test_executor_context_set_emits.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,34 @@ def run(ctx):
exa = MockExaEnvironment(meta)
with pytest.raises(TypeError):
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)


def test_context_parameters():
def udf_wrapper():
def run(ctx):
ctx.emit(ctx[0], ctx.t1)
ctx.emit(ctx[1], ctx.t2)
ctx.emit(ctx[2], ctx.t3)

input_columns = [Column("t1", int, "INTEGER"),
Column("t2", int, "INTEGER"),
Column("t3", int, "INTEGER")]
output_columns = [Column("o1", int, "INTEGER"),
Column("o2", int, "INTEGER")]
meta = MockMetaData(
script_code_wrapper_function=udf_wrapper,
input_type="SET",
input_columns=input_columns,
output_type="EMITS",
output_columns=output_columns
)
input_data = [(1, 2, 3), (4, 5, 6)]
exa = MockExaEnvironment(meta)
executor = UDFMockExecutor()
result = executor.run([Group(input_data)], exa)
for i, group in enumerate(result):
result_row = group.rows
assert len(result_row) == len(input_columns)
for j in range(len(result_row)):
assert len(result_row[j]) == len(output_columns)
assert input_data[i][j] == result_row[j][0] == result_row[j][1]