Skip to content

Commit

Permalink
[r] More unit-test cases for query-condition parser (#3198)
Browse files Browse the repository at this point in the history
* [r] More unit-testing for query-condition parser

* precedence-testing
  • Loading branch information
johnkerl authored Oct 18, 2024
1 parent a34f3fa commit 90db2ba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions apis/r/R/QueryCondition.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ parse_query_condition <- function(
if (is.symbol(node)) {
stop("Unexpected symbol in expression: ", format(node))

} else if (node[[1]] == '(') {
spdl::debug("[parseqc] paren [{}]",
as.character(node[2]));
return(.parse_tree_to_qc(node[[2]]))

} else if (.is_boolean_operator(node[1])) {
spdl::debug("[parseqc] boolop [{}] [{}] [{}]",
as.character(node[2]),
Expand Down
49 changes: 48 additions & 1 deletion apis/r/tests/testthat/test-query-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ test_that("DataFrame Factory", {
expect_equal(df$int32, -310)
expect_equal(as.character(df$enum), c("green"))
},
'soma_joinid == 10.0' = function(df) {
expect_equal(df$soma_joinid, 10)
expect_equal(df$int32, -310)
expect_equal(as.character(df$enum), c("green"))
},
'soma_joinid > 4 && soma_joinid < 8' = function(df) {
expect_equal(df$soma_joinid, 5:7)
expect_equal(df$string, c("egg", "fig", "goose"))
Expand All @@ -85,13 +90,27 @@ test_that("DataFrame Factory", {
'soma_joinid < 4 || soma_joinid > 8' = function(df) {
expect_equal(df$soma_joinid, c(1:3, 9:10))
},
'(soma_joinid < 4) || (soma_joinid > 8)' = function(df) {
expect_equal(df$soma_joinid, c(1:3, 9:10))
},

'int8 == 8' = function(df) {
expect_equal(length(df$soma_joinid), 0)
},
'int8 == -12' = function(df) {
expect_equal(df$soma_joinid, c(2))
},
'uint8 == 12' = function(df) {
expect_equal(df$soma_joinid, c(2))
},
'uint8 == 268' = function(df) {
# 12+256
expect_equal(df$soma_joinid, c(2))
},
'uint8 == -244' = function(df) {
# 12-256
expect_equal(df$soma_joinid, c(2))
},
'int16 > -203' = function(df) {
expect_equal(df$soma_joinid, c(1, 2))
},
Expand Down Expand Up @@ -124,6 +143,9 @@ test_that("DataFrame Factory", {
'string == "cat" || string == "dog"' = function(df) {
expect_equal(df$soma_joinid, c(3, 4))
},
'(string == "cat") || (string == "dog")' = function(df) {
expect_equal(df$soma_joinid, c(3, 4))
},
"string == 'cat' || string == 'dog'" = function(df) {
expect_equal(df$soma_joinid, c(3, 4))
},
Expand Down Expand Up @@ -206,6 +228,26 @@ test_that("DataFrame Factory", {
expect_equal(df$soma_joinid, 1:10)
},

'uint8 <= 14 && uint16 == 202 || uint32 == 308' = function(df) {
expect_equal(df$soma_joinid, c(2, 8))
},
'(uint8 <= 14 && uint16 == 202) || uint32 == 308' = function(df) {
expect_equal(df$soma_joinid, c(2, 8))
},
'uint8 <= 14 && (uint16 == 202 || uint32 == 308)' = function(df) {
expect_equal(df$soma_joinid, c(2))
},

'uint32 == 308 || uint8 <= 14 && uint16 == 202' = function(df) {
expect_equal(df$soma_joinid, c(2, 8))
},
'uint32 == 308 || (uint8 <= 14 && uint16 == 202)' = function(df) {
expect_equal(df$soma_joinid, c(2, 8))
},
'(uint32 == 308 || uint8 <= 14) && uint16 == 202' = function(df) {
expect_equal(df$soma_joinid, c(2))
},

# TODO: for a follow-up PR
'timestamp_s < "1970-01-01 01:00:05 UTC"' = function(df) {
expect_equal(df$soma_joinid, 1:4)
Expand Down Expand Up @@ -249,8 +291,13 @@ test_that("DataFrame Factory", {
' ',
'nonesuch < 10',
'soma_joinid << 10',
'(soma_joinid < 10',
'soma_joinid',
'soma_joinid < 4 or soma_joinid > 8'
'soma_joinid ==',
'soma_joinid && int8',
'soma_joinid ==1 &&',
'soma_joinid < 4 or soma_joinid > 8',
'soma_joinid == "ten"'
)

for (query_string in names(bad_cases)) {
Expand Down

0 comments on commit 90db2ba

Please sign in to comment.