Skip to content

Commit

Permalink
Merge pull request #37 from mrc-ide/mrc-4392
Browse files Browse the repository at this point in the history
Allow empty queries to return everything
  • Loading branch information
weshinsley authored Jul 17, 2023
2 parents 5d703ef + 633b7bb commit 66d483a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
17 changes: 15 additions & 2 deletions R/query.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##'
##' @title Construct outpack query
##'
##' @param expr The query expression
##' @param expr The query expression. A `NULL` expression matches everything.
##'
##' @param name Optionally, the name of the packet to scope the query on. This
##' will be intersected with `scope` arg and is a shorthand way of running
Expand Down Expand Up @@ -48,6 +48,9 @@ outpack_query <- function(expr, name = NULL, scope = NULL, subquery = NULL) {


as_outpack_query <- function(expr, ...) {
if (missing(expr)) {
expr <- NULL
}
if (inherits(expr, "outpack_query")) {
if (...length() > 0) {
stop("If 'expr' is an 'outpack_query', no additional arguments allowed")
Expand All @@ -71,7 +74,7 @@ query_parse <- function(expr, context, subquery_env) {
}
expr <- expr[[1L]]
}
} else if (!is.language(expr)) {
} else if (!(is.null(expr) || is.language(expr))) {
stop("Invalid input for query")
}

Expand Down Expand Up @@ -107,6 +110,7 @@ query_component <- function(type, expr, context, args, ...) {
query_parse_expr <- function(expr, context, subquery_env) {
type <- query_parse_check_call(expr, context)
fn <- switch(type,
empty = query_parse_empty,
test = query_parse_test,
group = query_parse_group,
latest = query_parse_latest,
Expand All @@ -119,6 +123,11 @@ query_parse_expr <- function(expr, context, subquery_env) {
}


query_parse_empty <- function(expr, context, subquery_env) {
query_component("empty", expr, context, list())
}


query_parse_test <- function(expr, context, subquery_env) {
args <- lapply(expr[-1], query_parse_value, context)
name <- deparse(expr[[1]])
Expand Down Expand Up @@ -301,6 +310,10 @@ query_eval_error <- function(msg, expr, context) {


query_parse_check_call <- function(expr, context) {
if (is.null(expr)) {
return("empty")
}

if (!is.call(expr)) {
query_parse_error(sprintf(
"Invalid query '%s'; expected some sort of expression",
Expand Down
6 changes: 6 additions & 0 deletions R/query_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ query_eval <- function(query, index, parameters, subquery) {
switch(query$type,
literal = query$value,
lookup = query_eval_lookup(query, index, parameters),
empty = query_eval_empty(query, index, parameters, subquery),
group = query_eval_group(query, index, parameters, subquery),
test = query_eval_test(query, index, parameters, subquery),
latest = query_eval_latest(query, index, parameters, subquery),
Expand Down Expand Up @@ -205,6 +206,11 @@ query_eval_lookup <- function(query, index, parameters) {
}


query_eval_empty <- function(query, index, parameters, subquery) {
index$index$id
}


query_eval_group <- function(query, index, parameters, subquery) {
args <- lapply(query$args, query_eval, index, parameters, subquery)
switch(query$name,
Expand Down
2 changes: 1 addition & 1 deletion man/outpack_query.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-query-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,16 @@ test_that("allow search before query", {
ids)
expect_setequal(names(root$a$index()$metadata), ids)
})



test_that("empty search returns full set", {
root <- create_temporary_root(use_file_store = TRUE)
ids <- list(a = vcapply(1:3, function(i) create_random_packet(root, "a")),
b = vcapply(1:3, function(i) create_random_packet(root, "b")))

expect_equal(outpack_search(root = root),
c(ids$a, ids$b))
expect_equal(outpack_search(name = "a", root = root),
c(ids$a))
})
17 changes: 16 additions & 1 deletion tests/testthat/test-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,23 @@ test_that("Parse basic query", {
})


test_that("empty query is possible", {
expect_equal(
query_parse(NULL, NULL, new.env(parent = emptyenv())),
query_component("empty", expr = NULL, context = NULL, args = list()))
expect_equal(
outpack_query(NULL),
structure(
list(value = query_parse(NULL, NULL, new.env(parent = emptyenv())),
subquery = list(),
info = list(single = FALSE,
parameters = character())),
class = "outpack_query"))
})


test_that("Prevent unparseable queries", {
expect_error(query_parse(NULL, NULL, emptyenv()),
expect_error(query_parse(1, NULL, emptyenv()),
"Invalid input for query")
expect_error(query_parse("latest(); latest()", NULL, emptyenv()),
"Expected a single expression")
Expand Down

0 comments on commit 66d483a

Please sign in to comment.