From 823ed4f6c4fa38b47f1fe343f6d4d3f72f6d2505 Mon Sep 17 00:00:00 2001 From: anshul23102 Date: Mon, 18 May 2026 12:08:39 +0530 Subject: [PATCH] fix(db): return data unchanged instead of NULL when no covariates found arrhenius.scaling.traits() and filter_sunleaf_traits() both set data <- NULL in their else-branch, contradicting the documented behaviour ("data with no matching covariates will be unchanged"). When no temperature covariates exist for any observation, the correct response is to return the input data unchanged (equivalent to assuming all measurements were taken at missing.temp = 25 degC, making the Arrhenius scaling a no-op). Returning NULL instead caused a hard crash in query.trait.data() at nrow(NULL) whenever Vcmax, root_respiration_rate, leaf_respiration_rate_m2, or stem_respiration_rate were queried for species with no temperature covariate recorded in BETYdb. filter_sunleaf_traits() has the identical bug: when no canopy_layer covariate is available, all measurements should pass through unfiltered, not be silently discarded. --- base/db/NEWS.md | 1 + base/db/R/covariate.functions.R | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/base/db/NEWS.md b/base/db/NEWS.md index c07db439904..e8d6ff63df1 100644 --- a/base/db/NEWS.md +++ b/base/db/NEWS.md @@ -2,6 +2,7 @@ ## Fixed +* `arrhenius.scaling.traits()` and `filter_sunleaf_traits()`: both functions returned `NULL` instead of the input `data` unchanged when no matching covariates were found. This caused a hard crash (`argument is of length zero`) in `query.trait.data()` whenever temperature-dependent traits (Vcmax, respiration rates) were queried for species where no temperature covariate was recorded in the database. The documented behaviour ("data with no matching covariates will be unchanged") is now implemented correctly. * `query.trait.data()`: the `warning()` call for missing trait data was placed after `return(NA)` and therefore never fired. Moved before the return and changed to `logger.warn()` for consistency with the rest of the codebase. * Refactored `convert.input()` internals into smaller, and hopefully more testable, chunks. No user-visible changes expected. diff --git a/base/db/R/covariate.functions.R b/base/db/R/covariate.functions.R index c40e3ca5a05..dd0c3edc119 100644 --- a/base/db/R/covariate.functions.R +++ b/base/db/R/covariate.functions.R @@ -83,7 +83,9 @@ arrhenius.scaling.traits <- function(data, covariates, temp.covariates, new.temp #remove temporary covariate column. data<-data[,colnames(data)!='temp'] } else { - data <- NULL + # No temperature covariates found for any observation; assume all were + # measured at missing.temp (default 25 degC) so scaling is a no-op. + # Return data unchanged rather than NULL, as documented. } return(data) } @@ -108,7 +110,7 @@ filter_sunleaf_traits <- function(data, covariates){ # remove temporary covariate column data <- data[,colnames(data)!='canopy_layer'] } else { - data <- NULL + # No canopy_layer covariate found; return data unchanged rather than NULL. } return(data) }