diff --git a/R/DuckDBMatrix-utils.R b/R/DuckDBMatrix-utils.R index 5a155e1..a440a29 100644 --- a/R/DuckDBMatrix-utils.R +++ b/R/DuckDBMatrix-utils.R @@ -129,6 +129,16 @@ NULL col_keycol = keycols[[dim_idx[2L]]]) } +# SQL SUM skips NULL, so a stored NA would drop out of the product and a +# number would come back. Base R keeps the NA: the sum is NULL when any +# term is NULL, and a finite sum is unchanged. +.sum_product <- function(expr) { + call("ifelse", + call("bool_or", call("is.na", expr)), + NA_real_, + call("sum", expr, na.rm = TRUE)) +} + #' @importFrom dplyr group_by left_join mutate summarize #' @importFrom stats setNames #' @importFrom DuckDBDataFrame dimtbls tblconn @@ -154,7 +164,7 @@ NULL conn <- tblconn(table, select = FALSE) y_tbl <- .duckdb_matmult_copy(conn, y_df) product_expr <- call("*", call("(", datacol), as.name("y_value")) - sum_expr <- call("sum", product_expr, na.rm = TRUE) + sum_expr <- .sum_product(product_expr) aggr <- setNames(list(sum_expr), datacol_name) result_conn <- conn |> left_join(y_tbl, by = col_key) |> @@ -204,7 +214,7 @@ NULL conn <- tblconn(table, select = FALSE) y_tbl <- .duckdb_matmult_copy(conn, y_df) product_expr <- call("*", as.name("y_value"), call("(", datacol)) - sum_expr <- call("sum", product_expr, na.rm = TRUE) + sum_expr <- .sum_product(product_expr) aggr <- setNames(list(sum_expr), datacol_name) result_conn <- conn |> left_join(y_tbl, by = row_key) |> @@ -298,8 +308,8 @@ NULL c(b_row, col_key, "value_b")) conn_a <- select(conn, !!!select_a) conn_b <- select(conn, !!!select_b) - aggr <- setNames(list(call("sum", call("*", as.name("value_a"), as.name("value_b")), - na.rm = TRUE)), datacol_name) + aggr <- setNames(list(.sum_product(call("*", as.name("value_a"), as.name("value_b")))), + datacol_name) result_conn <- conn_a |> inner_join(conn_b, by = col_key) |> group_by(!!as.name(a_row), !!as.name(b_row)) |> @@ -350,8 +360,8 @@ NULL c(row_key, b_col, "value_b")) conn_a <- select(conn, !!!select_a) conn_b <- select(conn, !!!select_b) - aggr <- setNames(list(call("sum", call("*", as.name("value_a"), as.name("value_b")), - na.rm = TRUE)), datacol_name) + aggr <- setNames(list(.sum_product(call("*", as.name("value_a"), as.name("value_b")))), + datacol_name) result_conn <- conn_a |> inner_join(conn_b, by = row_key) |> group_by(!!as.name(a_col), !!as.name(b_col)) |> diff --git a/tests/testthat/test-DuckDBMatrix.R b/tests/testthat/test-DuckDBMatrix.R index c94e289..f2a487e 100644 --- a/tests/testthat/test-DuckDBMatrix.R +++ b/tests/testthat/test-DuckDBMatrix.R @@ -278,6 +278,33 @@ test_that("matrix multiplication works for DuckDBMatrix", { checkDuckDBMatrix(object, expected) }) +test_that("matrix products keep a stored NA", { + m <- matrix(c(1, NA, 2, 4), 2, 2, byrow = TRUE, + dimnames = list(idx1 = c("r1", "r2"), idx2 = c("c1", "c2"))) + df <- data.frame(idx1 = c("r1", "r1", "r2", "r2"), + idx2 = c("c1", "c2", "c1", "c2"), + value = c(1, NA, 2, 4)) + tf <- tempfile(fileext = ".parquet") + arrow::write_parquet(df, tf) + x <- DuckDBMatrix(tf, datacol = "value", + keycols = list(idx1 = c("r1", "r2"), idx2 = c("c1", "c2"))) + + expect_equal(as.vector(x %*% c(1, 1)), as.vector(m %*% c(1, 1))) + expect_equal(as.vector(c(1, 1) %*% x), as.vector(c(1, 1) %*% m)) + expect_equal(as.matrix(crossprod(x)), crossprod(m), check.attributes = FALSE) + expect_equal(as.matrix(tcrossprod(x)), tcrossprod(m), check.attributes = FALSE) + + # a cell that is not stored is still a zero when every factor is finite + m0 <- matrix(c(1, 0, 0, 2), 2, 2, byrow = TRUE, + dimnames = list(idx1 = c("r1", "r2"), idx2 = c("c1", "c2"))) + df0 <- data.frame(idx1 = c("r1", "r2"), idx2 = c("c1", "c2"), value = c(1, 2)) + tf0 <- tempfile(fileext = ".parquet") + arrow::write_parquet(df0, tf0) + x0 <- DuckDBMatrix(tf0, datacol = "value", + keycols = list(idx1 = c("r1", "r2"), idx2 = c("c1", "c2"))) + expect_equal(as.vector(x0 %*% c(3, 4)), as.vector(m0 %*% c(3, 4))) +}) + test_that("crossprod/tcrossprod self-join has an OOM size tripwire (LKT-59; R<->Python parity with scibis)", { pqmat <- DuckDBMatrix(state_path, datacol = "value", row = "index1", col = "index2") # Under the default safeguard a small matrix is well below the limit: no trip, a real result is returned.