Skip to content

Commit

Permalink
tests/testthat/test-query-condition.R
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Oct 10, 2024
1 parent f95efe5 commit e1788fb
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 15 deletions.
13 changes: 8 additions & 5 deletions apis/r/R/QueryCondition.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ parse_query_condition_new <- function(
somactx
) {

stopifnot("The schema argument must be an Arrow Schema" =
is(schema, "ArrowObject") &&
is(schema, "Schema"))
stopifnot(
"The schema argument must be an Arrow Schema" =
is(schema, "ArrowObject") &&
is(schema, "Schema"),
"The argument must be a somactx object" =
is(somactx, "externalptr"))

# ----------------------------------------------------------------
# Helpers for walking the parse tree
Expand Down Expand Up @@ -201,12 +204,12 @@ setClass(
# ================================================================
#' Creates a 'tiledbsoma_query_condition' object
#'
#' @param ctx (optional) A TileDB Ctx object; if not supplied the default
#' @param somactx (optional) A TileDB Ctx object; if not supplied the default
#' context object is retrieved
#' @return A 'tiledbsoma_query_condition' object
#' @export
tiledbsoma_empty_query_condition <- function(somactx) {
stopifnot("The argument must be a ctx object" = is(ctx, "externalptr"))
stopifnot("The argument must be a somactx object" = is(somactx, "externalptr"))
ptr <- libtiledbsoma_empty_query_condition(somactx)
query_condition <- new("tiledbsoma_query_condition", ptr = ptr, init = FALSE)
invisible(query_condition)
Expand Down
91 changes: 91 additions & 0 deletions apis/r/tests/testthat/test-query-condition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 15: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 16: arrow::field("i32", arrow::int32(), nullable = FALSE),
# 17: arrow::field("f64", arrow::float64(), nullable = FALSE),
# 18: arrow::field("utf8", arrow::large_utf8(), nullable = FALSE)
# 5: arrow::field("foo", arrow::int32(), nullable = FALSE),
# 6: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 7: arrow::field("bar", arrow::float64(), nullable = FALSE),
# 8: arrow::field("baz", arrow::large_utf8(), nullable = FALSE)
# 6:schema <- arrow::schema(arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 7: arrow::field("int", arrow::int32(), nullable = FALSE),
# 8: arrow::field("A", arrow::dictionary(index_type = arrow::int8(), value_type = arrow::utf8(), ordered = FALSE), nullable=FALSE)
# 10: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 11: arrow::field("int", arrow::int32(), nullable = FALSE),
# 12: arrow::field("string", arrow::string(), nullable = FALSE),
# 13: arrow::field(
# 8: arrow::field("foo", arrow::int32(), nullable = FALSE),
# 9: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 10: arrow::field("bar", arrow::float64(), nullable = FALSE),
# 11: arrow::field("baz", arrow::large_utf8(), nullable = FALSE)
# 7: arrow::field("foo", arrow::int32(), nullable = FALSE),
# 8: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 9: arrow::field("bar", arrow::float64(), nullable = FALSE),
# 10: arrow::field("baz", arrow::large_utf8(), nullable = FALSE)
# 15: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE),
# 16: arrow::field("string_not_enum", arrow::string(), nullable = FALSE),

test_that("DataFrame Factory", {
skip_if(!extended_tests())
uri <- tempfile()
if (dir.exists(uri)) unlink(uri, recursive=TRUE)

ctx <- soma_context()

sch <- arrow::schema(
arrow::field("soma_joinid", arrow::int64()),
arrow::field("int32", arrow::int32())
# arrow::field("float_column", arrow::float64(), nullable = bl),
# arrow::field("string_column", arrow::large_utf8(), nullable = bl)
# XXX MORE TO DO
)

sdf <- SOMADataFrameCreate(uri, sch, index_column_names = "soma_joinid")
expect_true(sdf$exists())
expect_true(dir.exists(uri))

tbl <- arrow::arrow_table(
soma_joinid = 1L:10L,
int32 = 101L:110L,
schema = sch)
sdf$write(tbl)
sdf$close()

sdf$reopen("READ")

cases <- list(
'soma_joinid > 5' <- function(df) {
expect_true(all(df$soma_joinid == 6:10))
expect_true(all(df$int32 == 106:110))
},
'soma_joinid == 10' <- function(df) {
expect_true(all(df$soma_joinid == 10))
expect_true(all(df$int32 == 110))
},
'soma_joinid > 4 && soma_joinid < 8' <- function(df) {
expect_true(all(df$soma_joinid == 4:7))
},
'soma_joinid < 4 || soma_joinid > 8' <- function(df) {
expect_true(all(df$soma_joinid == c(1:3, 9:10)))
}
# louvain == "T cell"
# louvain %in% c("T cell", "B cell")
# louvain %nin% c("T cell", "B cell")
# XXX MORE TO DO
)

for (query_string in names(cases)) {
parsed <- do.call(
what = tiledbsoma:::parse_query_condition_new,
args = list(expr=str2lang(query_string), schema=sch, somactx=ctx))
clib_value_filter <- parsed@ptr

sr <- sr_setup(uri = sdf$uri, ctx, qc=clib_value_filter)
iter <- TableReadIter$new(sr)
tbl <- iter$read_next()
expect_true(iter$read_complete())
df <- as.data.frame(tbl)
cases[[query_string]](df)
}

sdf$close()
})
15 changes: 5 additions & 10 deletions libtiledbsoma/test/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,12 @@ create_arrow_schema_and_index_columns(
// Create index-column info only, no schema involving the attrs
ArrowTable create_column_index_info(const std::vector<DimInfo>& dim_infos) {
for (auto info : dim_infos) {
LOG_DEBUG(fmt::format("create_column_index_info name={}", info.name));

LOG_DEBUG(fmt::format(
"create_column_index_info type={}",
tiledb::impl::to_str(info.tiledb_datatype)));

LOG_DEBUG(
fmt::format("create_column_index_info dim_max={}", info.dim_max));

LOG_DEBUG(fmt::format(
"create_column_index_info ucd={}", info.use_current_domain));
"create_column_index_info name={} type={} dim_max={} ucd={}",
info.name,
tiledb::impl::to_str(info.tiledb_datatype),
info.dim_max,
info.use_current_domain));
}

auto index_cols_info_schema = _create_index_cols_info_schema(dim_infos);
Expand Down

0 comments on commit e1788fb

Please sign in to comment.