I like glue, but I feel like I've missed a detail.
starwars %>%
slice(1:2) %>%
glue_data_sql("SELECT *
FROM tbl
WHERE IN {films}")
This code feeds a list column into the query. It is nearly correct, however the answer still has a proceeding c
as the films
column is a list column. It also has the contents of the list enquoted and escaped with `("item1", "item2") which doesn't work with redshift at least.
<SQL> SELECT *
FROM tbl
WHERE film IN c("Revenge of the Sith", "Return of the Jedi", "The Empire Strikes Back", "A New Hope", "The Force Awakens")
<SQL> SELECT *
FROM tbl
WHERE film IN c("Attack of the Clones", "The Phantom Menace", "Revenge of the Sith", "Return of the Jedi", "The Empire Strikes Back", "A New Hope")
Adding the *
to collapse doesn't help in this case.
I've attempted to fix it by string manipulation:
starwars %>%
slice(1:2) %>%
glue_data_sql("SELECT *
FROM tbl
WHERE film IN {films}") %>%
str_replace_all("c\\(", "(")
[1] "SELECT *\nFROM tbl\nWHERE film IN (\"Revenge of the Sith\", \"Return of the Jedi\", \"The Empire Strikes Back\", \"A New Hope\", \"The Force Awakens\")"
[2] "SELECT *\nFROM tbl\nWHERE film IN (\"Attack of the Clones\", \"The Phantom Menace\", \"Revenge of the Sith\", \"Return of the Jedi\", \"The Empire Strikes Back\", \"A New Hope\")"
I thought this might be an issue about needing to escape brackets properly, but this modification which doesn't reply on using the brackets gives the same output.
starwars %>%
slice(1:2) %>%
glue_data_sql("SELECT *
FROM tbl
WHERE film IN {films}") %>%
str_replace_all("IN c", "IN ")
But this seems to change the some details (or at least the print method) and also inputs escapes before the "
which confuses me (probably my bad regex skills). Have I missed a detail of glue/listcolumns that will make this work?