Skip to content

Commit

Permalink
[python] QueryCondition should error out if given empty set (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenv authored Dec 11, 2023
1 parent df7eefd commit 1b6688b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 9 additions & 6 deletions apis/python/src/tiledbsoma/_query_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def visit_Compare(self, node: ast.Compare) -> clib.PyQueryCondition:

variable = node.left.id
values = [self.get_val_from_node(val) for val in self.visit(rhs)]
if len(values) == 0:
raise tiledb.TileDBError(
"At least one value must be provided to the set membership"
)

if self.schema.has_attr(variable):
enum_label = self.schema.attr(variable).enum_label
Expand Down Expand Up @@ -325,14 +329,14 @@ def get_att_from_node(self, node: QueryConditionNodeElem) -> Any:
att_node = att_node.args[0]

if isinstance(att_node, ast.Name):
att = att_node.id
att = str(att_node.id)
elif isinstance(att_node, ast.Constant) or isinstance(
att_node, ast.NameConstant
):
att = att_node.value
att = str(att_node.value)
elif isinstance(att_node, ast.Str) or isinstance(att_node, ast.Bytes):
# deprecated in 3.8
att = att_node.s
att = str(att_node.s)
else:
raise tiledb.TileDBError(
f"Incorrect type for attribute name: {ast.dump(att_node)}"
Expand Down Expand Up @@ -437,9 +441,8 @@ def create_pyqc(self, dtype: str) -> Callable:
) from ae

def visit_BinOp(self, node: ast.BinOp) -> clib.PyQueryCondition:
try:
op = self.visit(node.op)
except KeyError:
op = self.visit(node.op)
if op is None:
raise tiledb.TileDBError(
f"Unsupported binary operator: {ast.dump(node.op)}. Only & is currently supported."
)
Expand Down
9 changes: 8 additions & 1 deletion libtiledbsoma/test/test_query_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def test_parsing_error_conditions(malformed_condition):
"n_genes == val()",
"attr() > 20",
"n_genes < -val(-1)",
"louvain in []",
],
)
def test_eval_error_conditions(malformed_condition):
Expand All @@ -220,14 +221,20 @@ def test_eval_error_conditions(malformed_condition):

# TODO: these raise the wrong error - it should be SOMAError. Change the test
# when https://github.com/single-cell-data/TileDB-SOMA/issues/783 is fixed
#

with pytest.raises(RuntimeError):
qc = QueryCondition(malformed_condition)
schema = tiledb.open(uri).schema
sr = clib.SOMAArray(uri)
sr.set_condition(qc, schema)
sr.read_next()

with pytest.raises(tiledb.TileDBError):
qc = QueryCondition(malformed_condition)
schema = tiledb.open(uri).schema
# test function directly for codecov
qc.init_query_condition(schema, {}, [])


if __name__ == "__main__":
test_query_condition_select_columns()

0 comments on commit 1b6688b

Please sign in to comment.