Skip to content

Commit

Permalink
Add support for query conditions on enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
eddelbuettel committed Jul 31, 2023
1 parent 9e973e1 commit ffee01e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export(tiledb_query_buffer_alloc_ptr)
export(tiledb_query_condition)
export(tiledb_query_condition_combine)
export(tiledb_query_condition_init)
export(tiledb_query_condition_set_use_enumeration)
export(tiledb_query_create_buffer_ptr)
export(tiledb_query_create_buffer_ptr_char)
export(tiledb_query_ctx)
Expand Down
20 changes: 18 additions & 2 deletions R/ArraySchema.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ setClass("tiledb_array_schema",
tiledb_array_schema.from_ptr <- function(ptr, arrptr=NULL) {
stopifnot("The 'ptr' argument must be an external pointer to a tiledb_array_schema instance"
= !missing(ptr) && is(ptr, "externalptr") && !is.null(ptr))
new("tiledb_array_schema", ptr = ptr, arrptr=arrptr)
new("tiledb_array_schema", ptr = ptr, arrptr = arrptr)
}

#' Constructs a `tiledb_array_schema` object
Expand Down Expand Up @@ -150,7 +150,7 @@ setMethod("show", signature(object = "tiledb_array_schema"),
nfo <- nfilters(fl$offsets)
nfv <- nfilters(fl$validity)
cat("tiledb_array_schema(\n domain=", .as_text_domain(domain(object)), ",\n",
" attrs=c(\n ", paste(sapply(attrs(object), .as_text_attribute), collapse=",\n "), "\n ),\n",
" attrs=c(\n ", paste(sapply(attrs(object), .as_text_attribute, arrptr=object@arrptr), collapse=",\n "), "\n ),\n",
" cell_order=\"", cell_order(object), "\", ",
"tile_order=\"", tile_order(object), "\", ",
"capacity=", capacity(object), ", ",
Expand Down Expand Up @@ -543,6 +543,22 @@ tiledb_schema_get_dim_attr_status <- function(sch) {
return(c(rep(1L, length(dims)), rep(2L, length(attrs))))
}

##' Get Dimension or Attribute Status
##'
##' Note that this function is an unexported internal function.
##'
##' @param sch A TileDB Schema object
##' @return An integer vector where each element corresponds to a schema entry,
##' and a value of one signals dimension and a value of two an attribute.
tiledb_schema_get_enumeration_status <- function(sch) {
stopifnot("The 'sch' argument must be a schema" = is(sch, "tiledb_array_schema"))
dom <- tiledb::domain(sch)
dims <- tiledb::dimensions(dom)
attrs <- tiledb::attrs(sch)
return(c(rep(FALSE, length(dims)),
sapply(attrs, tiledb_attribute_has_enumeration)))
}


# -- get and set tile capacity

Expand Down
27 changes: 27 additions & 0 deletions R/QueryCondition.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ parse_query_condition <- function(expr, ta=NULL, debug=FALSE, strict=TRUE, use_i
attr <- as.character(x[2])
ch <- as.character(x[3])
dtype <- .getType(ch, use_int64)
is_enum <- FALSE # default is no
if (.hasArray) {
ind <- match(attr, ta@sil$names)
if (!is.finite(ind)) {
Expand All @@ -193,10 +194,19 @@ parse_query_condition <- function(expr, ta=NULL, debug=FALSE, strict=TRUE, use_i
return(NULL)
}
dtype <- ta@sil$types[ind]
is_enum <- ta@sil$enum[ind]
}
if (debug) cat(" [", attr,"] ",
op, " (aka ", .mapOpToCharacter(op), ")",
" [",ch, "] ", dtype, "\n", sep="")

## take care of factor (aka "enum" case) and set the daat type to ASCII
if (dtype == "INT32" && is_enum) {
if (debug) cat(" [factor column] ", ch, " ", attr, " ", dtype, " --> ASCII", " ", is_enum, "\n")
dtype <- "ASCII"
}

## general case of extracting appropriate value give type info
tiledb_query_condition_init(attr = attr,
value = switch(dtype,
ASCII = ch,
Expand All @@ -215,3 +225,20 @@ parse_query_condition <- function(expr, ta=NULL, debug=FALSE, strict=TRUE, use_i
e <- substitute(expr)
.makeExpr(e, debug)
}

#' Enable use of enumeration in query condition
#'
#' Set a boolean toggle to signal use of enumeration in query condtion (TileDB 2.17 or later)
#' @param qc A 'tiledb_query_condition' object
#' @param use_enum A boolean to set (if TRUE) or unset (if FALSE) enumeration use
#' @param ctx (optional) A TileDB Ctx object; if not supplied the default
#' context object is retrieved
#' @return Nothing is retuned, the function is invoked for the side effect
#' @export
tiledb_query_condition_set_use_enumeration <- function(qc, use_enum, ctx = tiledb_get_context()) {
stopifnot("Argument 'qc' must be a query condition object" = is(qc, "tiledb_query_condition"),
"Argument 'use_enum' must be logical" = is.logical(use_enum),
"The argument must be a ctx object" = is(ctx, "tiledb_ctx"),
"This function needs TileDB 2.17.0 or later" = tiledb_version(TRUE) >= "2.17.0")
libtiledb_query_condition_set_use_enumeration(ctx@ptr, qc@ptr, use_enum)
}
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,10 @@ libtiledb_query_condition_combine <- function(lhs, rhs, str) {
.Call(`_tiledb_libtiledb_query_condition_combine`, lhs, rhs, str)
}

libtiledb_query_condition_set_use_enumeration <- function(ctx, cond, use_enumeration) {
invisible(.Call(`_tiledb_libtiledb_query_condition_set_use_enumeration`, ctx, cond, use_enumeration))
}

libtiledb_zip_coords_numeric <- function(coords, coord_length) {
.Call(`_tiledb_libtiledb_zip_coords_numeric`, coords, coord_length)
}
Expand Down
7 changes: 4 additions & 3 deletions R/TileDBArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -2121,11 +2121,12 @@ setMethod("tdb_collect", signature("tiledb_array"), function(x, ...) {
})

# unexported helper
.fill_schema_info_list <- function(uri) {
sch <- schema(uri)
.fill_schema_info_list <- function(ta) {
sch <- schema(ta)
list(names=tiledb_schema_get_names(sch),
types=tiledb_schema_get_types(sch),
status=tiledb_schema_get_dim_attr_status(sch))
status=tiledb_schema_get_dim_attr_status(sch),
enum=tiledb_schema_get_enumeration_status(sch))
}


Expand Down
1 change: 1 addition & 0 deletions inst/include/tiledb.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <tiledb/array_experimental.h>
#include <tiledb/attribute_experimental.h>
#include <tiledb/enumeration_experimental.h>
#include <tiledb/query_condition_experimental.h>
#endif

// Use the 'finalizer on exit' toggle in the XPtr template to ensure
Expand Down
26 changes: 26 additions & 0 deletions man/tiledb_query_condition_set_use_enumeration.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions man/tiledb_stats_raw_dump.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// libtiledb_query_condition_set_use_enumeration
void libtiledb_query_condition_set_use_enumeration(XPtr<tiledb::Context> ctx, XPtr<tiledb::QueryCondition> cond, bool use_enumeration);
RcppExport SEXP _tiledb_libtiledb_query_condition_set_use_enumeration(SEXP ctxSEXP, SEXP condSEXP, SEXP use_enumerationSEXP) {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< XPtr<tiledb::Context> >::type ctx(ctxSEXP);
Rcpp::traits::input_parameter< XPtr<tiledb::QueryCondition> >::type cond(condSEXP);
Rcpp::traits::input_parameter< bool >::type use_enumeration(use_enumerationSEXP);
libtiledb_query_condition_set_use_enumeration(ctx, cond, use_enumeration);
return R_NilValue;
END_RCPP
}
// libtiledb_zip_coords_numeric
NumericVector libtiledb_zip_coords_numeric(List coords, R_xlen_t coord_length);
RcppExport SEXP _tiledb_libtiledb_zip_coords_numeric(SEXP coordsSEXP, SEXP coord_lengthSEXP) {
Expand Down Expand Up @@ -3575,6 +3587,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_tiledb_libtiledb_query_condition", (DL_FUNC) &_tiledb_libtiledb_query_condition, 1},
{"_tiledb_libtiledb_query_condition_init", (DL_FUNC) &_tiledb_libtiledb_query_condition_init, 5},
{"_tiledb_libtiledb_query_condition_combine", (DL_FUNC) &_tiledb_libtiledb_query_condition_combine, 3},
{"_tiledb_libtiledb_query_condition_set_use_enumeration", (DL_FUNC) &_tiledb_libtiledb_query_condition_set_use_enumeration, 3},
{"_tiledb_libtiledb_zip_coords_numeric", (DL_FUNC) &_tiledb_libtiledb_zip_coords_numeric, 2},
{"_tiledb_libtiledb_zip_coords_integer", (DL_FUNC) &_tiledb_libtiledb_zip_coords_integer, 2},
{"_tiledb_libtiledb_create_group", (DL_FUNC) &_tiledb_libtiledb_create_group, 2},
Expand Down
11 changes: 11 additions & 0 deletions src/libtiledb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,17 @@ XPtr<tiledb::QueryCondition> libtiledb_query_condition_combine(XPtr<tiledb::Quer
return query_cond;
}

// [[Rcpp::export]]
void libtiledb_query_condition_set_use_enumeration(XPtr<tiledb::Context> ctx,
XPtr<tiledb::QueryCondition> cond,
bool use_enumeration) {
check_xptr_tag<tiledb::Context>(ctx);
check_xptr_tag<tiledb::QueryCondition>(cond);
#if TILEDB_VERSION >= TileDB_Version(2,17,0)
tiledb::QueryConditionExperimental::set_use_enumeration(*ctx.get(), *cond.get(), use_enumeration);
#endif
}


/**
* Array helper functions
Expand Down

0 comments on commit ffee01e

Please sign in to comment.