Skip to content

glue::glue_sql() returns differently when with or without NA #115

@shrektan

Description

@shrektan

When the values contain NA, the single quotes for the character will disappear, see the reproducible example below.

Is it a bug?

Thanks.

The reproducible example

Pay attention to the last letter of each SQL. When no NA, it's 'c'. When there's NA, it's c without the single quotes.

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") 
dt <- tibble::tibble(
  COL1 = c("a", "b"),
  COL2 = c("A", "B")
)
DBI::dbWriteTable(conn, "test", dt)
sql <- "insert into test values ({col1}, {col2})"
glue::glue_sql(
  sql,
  col1 = c("c", "d"),
  col2 = c("C", "D"),
  .con = conn
)
#> <SQL> insert into test values ('c', 'C')
#> <SQL> insert into test values ('d', 'D')

glue::glue_sql(
  sql,
  col1 = c("c", "d"),
  col2 = c("C", NA_character_),
  .con = conn
)

### The expected output to me is : insert into test values ('c', 'C')
#> <SQL> insert into test values ('c', C) 
#> <SQL> insert into test values ('d', NULL)

Created on 2018-11-01 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                                              
#>  version  R version 3.4.4 (2018-03-15)                       
#>  os       Windows 7 x64 SP 1                                 
#>  system   x86_64, mingw32                                    
#>  ui       RTerm                                              
#>  language (EN)                                               
#>  collate  Chinese (Simplified)_People's Republic of China.936
#>  ctype    Chinese (Simplified)_People's Republic of China.936
#>  tz       Asia/Taipei                                        
#>  date     2018-11-01                                         
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version     date       source                            
#>  assertthat    0.2.0       2017-04-11 CRAN (R 3.4.4)                    
#>  backports     1.1.2       2017-12-13 CRAN (R 3.4.3)                    
#>  base64enc     0.1-3       2015-07-28 CRAN (R 3.4.1)                    
#>  bit           1.1-14      2018-05-29 CRAN (R 3.4.4)                    
#>  bit64         0.9-7       2017-05-08 CRAN (R 3.4.0)                    
#>  blob          1.1.1       2018-03-25 CRAN (R 3.4.4)                    
#>  callr         3.0.0       2018-08-24 CRAN (R 3.4.4)                    
#>  cli           1.0.0       2017-11-05 CRAN (R 3.4.4)                    
#>  clisymbols    1.2.0       2017-05-21 CRAN (R 3.4.4)                    
#>  crayon        1.3.4       2017-09-16 CRAN (R 3.4.4)                    
#>  DBI           1.0.0       2018-05-02 CRAN (R 3.4.4)                    
#>  debugme       1.1.0       2017-10-22 CRAN (R 3.4.3)                    
#>  desc          1.2.0       2018-05-01 CRAN (R 3.4.4)                    
#>  devtools      1.13.6.9000 2018-09-21 git (@74df201)                    
#>  digest        0.6.17      2018-09-12 CRAN (R 3.4.4)                    
#>  evaluate      0.11        2018-07-17 CRAN (R 3.4.4)                    
#>  fs            1.2.6       2018-08-23 CRAN (R 3.4.4)                    
#>  glue          1.3.0       2018-07-17 CRAN (R 3.4.4)                    
#>  htmltools     0.3.6.9000  2018-05-07 local                             
#>  knitr         1.20        2018-02-20 CRAN (R 3.4.4)                    
#>  magrittr      1.5         2014-11-22 CRAN (R 3.4.4)                    
#>  memoise       1.1.0       2017-04-21 CRAN (R 3.4.4)                    
#>  pillar        1.3.0       2018-07-14 CRAN (R 3.4.4)                    
#>  pkgbuild      1.0.1       2018-09-18 CRAN (R 3.4.4)                    
#>  pkgconfig     2.0.2       2018-08-16 CRAN (R 3.4.4)                    
#>  pkgload       1.0.0       2018-07-07 CRAN (R 3.4.4)                    
#>  processx      3.2.0       2018-08-16 CRAN (R 3.4.4)                    
#>  ps            1.1.0       2018-08-10 CRAN (R 3.4.4)                    
#>  R6            2.2.2       2017-06-17 CRAN (R 3.4.4)                    
#>  Rcpp          0.12.18     2018-07-23 CRAN (R 3.4.4)                    
#>  remotes       1.1.1.9000  2018-09-21 Github (r-lib/remotes@cd2ab59)    
#>  rlang         0.2.2       2018-08-16 CRAN (R 3.4.4)                    
#>  rmarkdown     1.10        2018-06-11 CRAN (R 3.4.4)                    
#>  rprojroot     1.3-2       2018-01-03 CRAN (R 3.4.3)                    
#>  RSQLite       2.1.1       2018-05-06 CRAN (R 3.4.4)                    
#>  sessioninfo   1.0.1.9000  2018-09-21 Github (r-lib/sessioninfo@418dc5e)
#>  stringi       1.1.7       2018-03-12 CRAN (R 3.4.4)                    
#>  stringr       1.3.1       2018-05-10 CRAN (R 3.4.4)                    
#>  testthat      2.0.0       2017-12-13 CRAN (R 3.4.4)                    
#>  tibble        1.4.2       2018-01-22 CRAN (R 3.4.3)                    
#>  usethis       1.4.0       2018-08-14 CRAN (R 3.4.4)                    
#>  withr         2.1.2       2018-03-15 CRAN (R 3.4.4)                    
#>  yaml          2.2.0       2018-07-25 CRAN (R 3.4.4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions