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 19a9dcd
Show file tree
Hide file tree
Showing 3 changed files with 141 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
128 changes: 128 additions & 0 deletions apis/r/tests/testthat/test-query-condition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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("int8", arrow::int8()),
arrow::field("int16", arrow::int16()),
arrow::field("int32", arrow::int32()),
arrow::field("int64", arrow::int64()),
arrow::field("uint8", arrow::uint8()),
arrow::field("uint16", arrow::uint16()),
arrow::field("uint32", arrow::uint32()),
arrow::field("uint64", arrow::uint64()),
arrow::field("string", arrow::string()),
arrow::field("large_utf8", arrow::large_utf8()),
arrow::field("enum",
arrow::dictionary(
index_type = arrow::int8(),
value_type = arrow::utf8(),
ordered = TRUE)),
arrow::field("float32", arrow::float32()),
arrow::field("float64", arrow::float64())
# 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,
string = c("apple", "ball", "cat", "dog", "egg", "fig", "goose", "hay", "ice", "jam"),
large_utf8 = c("APPLE", "BALL", "CAT", "DOG", "EGG", "FIG", "GOOSE", "HAY", "ICE", "JAM"),
enum = factor(
c("red", "yellow", "green", "red", "red", "red", "yellow", "green", "red", "green"),
levels = c("red", "yellow", "green")),
float32 = 1.5:10.5,
float64 = 11.5:20.5,
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))
expect_true(all(df$enum == "green"))
},
'soma_joinid > 4 && soma_joinid < 8' = function(df) {
expect_true(all(df$soma_joinid == 5:7))
expect_true(all(df$string == c("egg", "fig", "goose")))
expect_true(all(df$large_utr8 == c("EGG", "FIG", "GOOSE")))
},
'soma_joinid < 4 || soma_joinid > 8' = function(df) {
expect_true(all(df$soma_joinid == c(1:3, 9:10)))
},

'string == "dog"' = function(df) {
expect_true(df$soma_joinid == c(4))
},
'string %in% c("fig", "dog")' = function(df) {
expect_true(all(df$soma_joinid == c(4, 6)))
},
'string %nin% c("fig", "dog")' = function(df) {
expect_true(all(df$soma_joinid == c(1, 2, 3, 5, 7, 8, 9, 10)))
},

'enum == "red"' = function(df) {
expect_true(all(df$soma_joinid == c(1, 4, 5, 6, 9)))
},
'enum != "red"' = function(df) {
expect_true(all(df$soma_joinid == c(2, 3, 7, 8, 10)))
},
'enum == "orange"' = function(df) {
expect_true(all(df$soma_joinid == c(4, 6)))
},
'enum != "orange"' = function(df) {
expect_true(all(df$soma_joinid == 1:10))
},
'enum %in% c("red", "green")' = function(df) {
expect_true(all(df$soma_joinid == c(1, 3, 4, 5, 6, 8, 9, 10)))
},
'enum %nin% c("red", "green")' = function(df) {
expect_true(all(df$soma_joinid == c(2, 7)))
},
'enum %in% c("orange", "green")' = function(df) {
expect_true(all(df$soma_joinid == c(3, 8, 10)))
},
'enum %nin% c("orange", "green")' = function(df) {
expect_true(all(df$soma_joinid == c(1, 2, 4, 5, 6, 7, 9)))
},
'enum %in% c("orange", "yellow")' = function(df) {
expect_true(all(df$soma_joinid == c()))
},
'enum %nin% c("orange", "purple")' = function(df) {
expect_true(all(df$soma_joinid == 1:10))
}
)

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 19a9dcd

Please sign in to comment.