fix: The dbplyr translation of as.numeric() and as.double() uses DOUBLE instead of NUMERIC#2031
Draft
fix: The dbplyr translation of as.numeric() and as.double() uses DOUBLE instead of NUMERIC#2031
as.numeric() and as.double() uses DOUBLE instead of NUMERIC#2031Conversation
Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix dplyr conversion of as.numeric() to AS NUMERIC in duckdb
Fix as.numeric()/as.double() to use DOUBLE instead of NUMERIC
Feb 15, 2026
as.numeric() and as.double() uses DOUBLE instead of NUMERIC
Collaborator
|
@lentinj: Does this work for you? |
lentinj
approved these changes
Feb 19, 2026
lentinj
left a comment
There was a problem hiding this comment.
Looks good to me! Might be worth adding a unit test for this particular case, e.g:
diff --git a/tests/testthat/test-numeric.R b/tests/testthat/test-numeric.R
new file mode 100644
index 000000000..288948adb
--- /dev/null
+++ b/tests/testthat/test-numeric.R
@@ -0,0 +1,12 @@
+test_that("as.numeric treats values as doubles", {
+ skip_if_not_installed("dplyr")
+
+ con <- local_con()
+ df <- data.frame(value = 1.23456789123)
+ duckdb_register(con, "df", df)
+ # NB: If this results in value AS NUMERIC, the result will be fixed to 3dp, i.e. 1.235
+ res <- dplyr::tbl(con, "df") |> dplyr::mutate(x = as.numeric(value)) |> dplyr::pull(x)
+ expect_equal(res, df[1, "value"])
+})
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
as.numeric()andas.double()were being translated to SQLAS NUMERIC, which in DuckDB isDECIMAL(18,3)- a fixed-precision type with only 3 decimal places. This caused precision loss in dplyr queries.Changes
as.numericandas.doubletranslations to useDOUBLE(IEEE 754 floating-point) instead ofNUMERICExample
Before:
CAST(value AS NUMERIC)→ 1.235 (rounded to 3 decimals)After:
CAST(value AS DOUBLE)→ 1.234567 (full precision)Pattern follows dbplyr's Redshift backend.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.