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

Support New Set Membership Query Condition #1837

Merged
merged 4 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
84 changes: 77 additions & 7 deletions tiledb/query_condition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,34 @@ class PyQueryCondition {
use_enumeration);
}

template <typename T>
static PyQueryCondition create(py::object pyctx,
const std::string &field_name,
const std::vector<T> &values) {
auto pyqc = PyQueryCondition(pyctx);

const Context ctx = std::as_const(pyqc.ctx_);

auto set_membership_qc =
QueryConditionExperimental::create(ctx, field_name, values, TILEDB_IN);

pyqc.qc_ = std::make_shared<QueryCondition>(std::move(set_membership_qc));

return pyqc;
}

PyQueryCondition
combine(PyQueryCondition rhs,
combine(PyQueryCondition qc,
tiledb_query_condition_combination_op_t combination_op) const {

auto pyqc = PyQueryCondition(nullptr, ctx_.ptr().get());

tiledb_query_condition_t *combined_qc = nullptr;
ctx_.handle_error(
tiledb_query_condition_alloc(ctx_.ptr().get(), &combined_qc));

ctx_.handle_error(tiledb_query_condition_combine(
ctx_.ptr().get(), qc_->ptr().get(), rhs.qc_->ptr().get(),
combination_op, &combined_qc));
ctx_.ptr().get(), qc_->ptr().get(), qc.qc_->ptr().get(), combination_op,
&combined_qc));

pyqc.qc_ = std::shared_ptr<QueryCondition>(
new QueryCondition(pyqc.ctx_, combined_qc));
Expand Down Expand Up @@ -112,7 +127,6 @@ void init_query_condition(py::module &m) {
static_cast<void (PyQueryCondition::*)(
const string &, const string &, tiledb_query_condition_op_t)>(
&PyQueryCondition::init))

.def("init_uint64",
static_cast<void (PyQueryCondition::*)(const string &, uint64_t,
tiledb_query_condition_op_t)>(
Expand Down Expand Up @@ -145,7 +159,6 @@ void init_query_condition(py::module &m) {
static_cast<void (PyQueryCondition::*)(const string &, int8_t,
tiledb_query_condition_op_t)>(
&PyQueryCondition::init))

.def("init_float32",
static_cast<void (PyQueryCondition::*)(const string &, float,
tiledb_query_condition_op_t)>(
Expand All @@ -157,7 +170,63 @@ void init_query_condition(py::module &m) {

.def("__capsule__", &PyQueryCondition::__capsule__)

.def("combine", &PyQueryCondition::combine);
.def("combine", &PyQueryCondition::combine)

.def_static(
"create_string",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<std::string> &)>(
&PyQueryCondition::create))
.def_static(
"create_uint64",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<uint64_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_int64",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<int64_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_uint32",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<uint32_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_int32",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<int32_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_uint16",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<uint16_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_int8",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<int8_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_uint16",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<uint16_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_int8",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<int8_t> &)>(
&PyQueryCondition::create))
.def_static(
"create_float32",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<float> &)>(
&PyQueryCondition::create))
.def_static(
"create_float64",
static_cast<PyQueryCondition (*)(py::object, const std::string &,
const std::vector<double> &)>(
&PyQueryCondition::create));

py::enum_<tiledb_query_condition_op_t>(m, "tiledb_query_condition_op_t",
py::arithmetic())
Expand All @@ -167,6 +236,7 @@ void init_query_condition(py::module &m) {
.value("TILEDB_GE", TILEDB_GE)
.value("TILEDB_EQ", TILEDB_EQ)
.value("TILEDB_NE", TILEDB_NE)
.value("TILEDB_IN", TILEDB_IN)
.export_values();

py::enum_<tiledb_query_condition_combination_op_t>(
Expand Down
34 changes: 27 additions & 7 deletions tiledb/query_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,20 @@ def visit_Compare(self, node: Type[ast.Compare]) -> qc.PyQueryCondition:
"`in` operator syntax must be written as `variable in ['l', 'i', 's', 't']`"
)

consts = self.visit(rhs)
result = self.aux_visit_Compare(
self.visit(node.left), qc.TILEDB_EQ, consts[0]
)
variable = node.left.id
values = [self.get_value_from_node(val) for val in self.visit(rhs)]

for val in consts[1:]:
value = self.aux_visit_Compare(self.visit(node.left), qc.TILEDB_EQ, val)
result = result.combine(value, qc.TILEDB_OR)
if self.array.schema.has_attr(variable):
enum_label = self.array.attr(variable).enum_label
if enum_label is not None:
dt = self.array.enum(enum_label).dtype
else:
dt = self.array.attr(variable).dtype
else:
dt = self.array.schema.attr_or_dim_dtype(variable)

dtype = "string" if dt.kind in "SUa" else dt.name
result = self.create_pyqc(dtype)(self.ctx, node.left.id, values)

return result

Expand Down Expand Up @@ -401,6 +407,20 @@ def init_pyqc(self, pyqc: qc.PyQueryCondition, dtype: str) -> Callable:

return getattr(pyqc, init_fn_name)

def create_pyqc(self, dtype: str) -> Callable:
if dtype != "string":
if np.issubdtype(dtype, np.datetime64):
dtype = "int64"
elif np.issubdtype(dtype, bool):
dtype = "uint8"

create_fn_name = f"create_{dtype}"

if not hasattr(qc.PyQueryCondition, create_fn_name):
raise TileDBError(f"PyQueryCondition.{create_fn_name}() not found.")

return getattr(qc.PyQueryCondition, create_fn_name)

def visit_BinOp(self, node: ast.BinOp) -> qc.PyQueryCondition:
try:
op = self.visit(node.op)
Expand Down