Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbQuoteLiteral() should return typed NULL #357

Closed
mgirlich opened this issue Nov 19, 2021 · 2 comments · Fixed by #370
Closed

dbQuoteLiteral() should return typed NULL #357

mgirlich opened this issue Nov 19, 2021 · 2 comments · Fixed by #370
Labels
Milestone

Comments

@mgirlich
Copy link

Currently, dbQuoteLiteral() simply returns NULL for missing values

DBI::dbQuoteLiteral(con, c(1L, NA_integer_))
#> <SQL> 1::int4
#> <SQL> NULL

While this is usually okay, this doesn't work well when creating a table via a values clause

DBI::dbGetQuery(
  con,
r"{WITH tbl1 AS (
 SELECT NULL AS "country", NULL AS "date" WHERE false
), tbl2 AS (
  SELECT NULL AS "country", NULL AS "date" WHERE false
  UNION ALL
  VALUES
    ('de', '2021-11-19'::date)
)
SELECT tbl1.*
FROM tbl1
INNER JOIN tbl2
ON (tbl1.country = tbl2.country AND tbl1.date = tbl2.date)}"
)

#> Error: Failed to prepare query: ERROR:  operator does not exist: text = date
#> LINE 15: ON (tbl1.country = tbl2.country, tbl1.date = tbl2.date)

With a typed NULL this works nicely

DBI::dbGetQuery(
  con,
r"{WITH tbl1 AS (
 SELECT NULL::text AS "country", NULL::date AS "date" WHERE false
), tbl2 AS (
  SELECT NULL AS "country", NULL AS "date" WHERE false
  UNION ALL
  VALUES
    ('de', '2021-11-19'::date)
)
SELECT tbl1.*
FROM tbl1
INNER JOIN tbl2
ON (tbl1.country = tbl2.country AND tbl1.date = tbl2.date)}"
)

This is an issue for the db_values() function which hopefully soon comes to dbplyr (see tidyverse/dbplyr#678)

@krlmlr
Copy link
Member

krlmlr commented Nov 21, 2021

Thanks. This is just the tip of the iceberg, type safety with dbQuoteLiteral() is next to nowhere in the other backends. It's worth fixing here to set a standard for good behavior (and perhaps add a DBItest test).

@krlmlr krlmlr added this to the 1.4.1 milestone Nov 21, 2021
@krlmlr krlmlr added the feature label Nov 21, 2021
krlmlr added a commit that referenced this issue Dec 5, 2021
- `dbQuoteLiteral()` correctly quotes length-0 values (#355) and generates typed `NULL` expressions for `NA` values (#357).
@krlmlr krlmlr modified the milestones: 1.4.1, 1.4.2 Dec 5, 2021
@github-actions
Copy link

github-actions bot commented Dec 7, 2022

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants