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

BI-5110: validate filter argument count #155

Merged
merged 4 commits into from
Dec 11, 2023
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
23 changes: 23 additions & 0 deletions lib/dl_api_lib/dl_api_lib_tests/db/data_api/result/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ def test_filter_errors(self, saved_dataset, data_api):
assert result_resp.status_code == 400
assert result_resp.bi_status_code == "ERR.DS_API.FIELD.NOT_FOUND"

@pytest.mark.parametrize(
("filter_name", "filter_values"),
(
("eq", []),
("isnull", [None]),
),
)
def test_invalid_argument_count_filter_error(self, saved_dataset, data_api, filter_name, filter_values):
ds = saved_dataset
field = ds.result_schema[0]
expected_count = 1 if filter_name == "eq" else 0
error_msg = f"Invalid argument count for {filter_name}: expected {expected_count}, got {len(filter_values)}"

result_resp = data_api.get_result(
dataset=ds,
fields=[field],
filters=[field.filter(filter_name.upper(), values=filter_values)],
fail_ok=True,
)
assert result_resp.status_code == 400
assert result_resp.bi_status_code == "ERR.DS_API.FILTER.ARGUMENT_COUNT_ERROR"
assert result_resp.json["message"] == error_msg

def test_invalid_group_by_configuration(self, saved_dataset, data_api):
ds = saved_dataset
ds.result_schema["Measure"] = ds.field(title="Measure", aggregation=AggregationFunction.sum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ def _remove_timezone(arg: Any) -> Any:
if add_arg_cnt is None: # for list lookup operations (IN, NOT IN)
args = [formula_nodes.ExpressionList.make(*args_nodes)]
else:
args = args_nodes[:add_arg_cnt] # type: ignore # TODO: fix
if len(args_nodes) != add_arg_cnt:
raise dl_query_processing.exc.FilterArgumentCountError(
f"Invalid argument count for {operation.value}: expected {add_arg_cnt}, got {len(args_nodes)}"
)
args = args_nodes # type: ignore # TODO: fix

# 3. Create formula object
formula_obj = formula_nodes.Formula.make(expr=expr_callable(field_formula_obj.expr, *args))
Expand Down
5 changes: 5 additions & 0 deletions lib/dl_query_processing/dl_query_processing/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class MeasureFilterUnsupportedError(FilterError):
default_message = "Measure filter is unsupported for this type of query"


class FilterArgumentCountError(FilterError):
err_code = FilterError.err_code + ["ARGUMENT_COUNT_ERROR"]
default_message = "Invalid argument count for filter"


class ParameterError(DLBaseException):
err_code = DLBaseException.err_code + ["PARAMETER"]
default_message = "Invalid parameter"
Expand Down
Loading