1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
# select after distinct produces subquery
Code
lf %>% distinct() %>% select(x)
Output
<SQL>
SELECT `x`
FROM (
SELECT DISTINCT `df`.*
FROM `df`
) AS `q01`
# rename/relocate after distinct is inlined #1141
Code
lf %>% distinct() %>% rename(z = y)
Output
<SQL>
SELECT DISTINCT `x`, `y` AS `z`
FROM `df`
Code
lf %>% distinct() %>% relocate(y)
Output
<SQL>
SELECT DISTINCT `y`, `x`
FROM `df`
# select preserves grouping vars
Code
out <- mf %>% select(a) %>% collect()
Message
Adding missing grouping variables: `b`
# select() after left_join() is inlined
Code
(out <- left_join(lf1, lf2, by = "x") %>% select(b, x))
Output
<SQL>
SELECT `b`, `lf1`.`x` AS `x`
FROM `lf1`
LEFT JOIN `lf2`
ON (`lf1`.`x` = `lf2`.`x`)
---
Code
(out <- left_join(lf1, lf2, by = "x") %>% relocate(b))
Output
<SQL>
SELECT `b`, `lf1`.*
FROM `lf1`
LEFT JOIN `lf2`
ON (`lf1`.`x` = `lf2`.`x`)
# select() after semi_join() is inlined
Code
(out <- semi_join(lf1, lf2, by = "x") %>% select(x, a2 = a))
Output
<SQL>
SELECT `x`, `a` AS `a2`
FROM `lf1`
WHERE EXISTS (
SELECT 1 FROM `lf2`
WHERE (`lf1`.`x` = `lf2`.`x`)
)
---
Code
(out <- anti_join(lf1, lf2, by = "x") %>% relocate(a))
Output
<SQL>
SELECT `a`, `x`
FROM `lf1`
WHERE NOT EXISTS (
SELECT 1 FROM `lf2`
WHERE (`lf1`.`x` = `lf2`.`x`)
)
# select() after join handles previous select
Code
print(lf)
Output
<SQL>
SELECT `x` AS `x2`, `y` AS `y3`, `z`
FROM `df` AS `df_LHS`
WHERE EXISTS (
SELECT 1 FROM `df` AS `df_RHS`
WHERE (`df_LHS`.`x` = `df_RHS`.`x`)
)
---
Code
print(lf2)
Output
<SQL>
SELECT `df_LHS`.`x` AS `x2`, `df_LHS`.`y` AS `y3`, `z`
FROM `df` AS `df_LHS`
LEFT JOIN `df` AS `df_RHS`
ON (`df_LHS`.`x` = `df_RHS`.`x`)
# select() produces nice error messages
Code
lf %>% select(non_existent)
Condition
Error in `select()`:
! Can't select columns that don't exist.
x Column `non_existent` doesn't exist.
Code
lf %>% select(non_existent + 1)
Condition
Error in `select()`:
i In argument: `non_existent + 1`.
Caused by error:
! object 'non_existent' not found
---
Code
lf %>% relocate(non_existent)
Condition
Error in `relocate()`:
! Can't relocate columns that don't exist.
x Column `non_existent` doesn't exist.
Code
lf %>% relocate(non_existent + 1)
Condition
Error in `relocate()`:
i In argument: `non_existent + 1`.
Caused by error:
! object 'non_existent' not found
---
Code
lf %>% rename(x)
Condition
Error in `rename()`:
! All renaming inputs must be named.
Code
lf %>% rename(y = non_existent)
Condition
Error in `rename()`:
! Can't rename columns that don't exist.
x Column `non_existent` doesn't exist.
Code
lf %>% rename(y = non_existent + 1)
Condition
Error in `rename()`:
i In argument: `non_existent + 1`.
Caused by error:
! object 'non_existent' not found
---
Code
lf %>% rename_with(toupper, .cols = non_existent)
Condition
Error in `rename_with()`:
! Can't select columns that don't exist.
x Column `non_existent` doesn't exist.
Code
lf %>% rename_with(toupper, .cols = non_existent + 1)
Condition
Error in `rename_with()`:
i In argument: `non_existent + 1`.
Caused by error:
! object 'non_existent' not found
# where() isn't suppored
Code
lf %>% select(where(is.integer))
Condition
Error in `select()`:
! This tidyselect interface doesn't support predicates.
# arranged computed columns are not inlined away
Code
lf %>% mutate(z = 1) %>% arrange(x, z) %>% select(x)
Condition
Warning:
ORDER BY is ignored in subqueries without LIMIT
i Do you need to move arrange() later in the pipeline or use window_order() instead?
Output
<SQL>
SELECT `x`
FROM (
SELECT `df`.*, 1.0 AS `z`
FROM `df`
) AS `q01`
# multiple selects are collapsed
Code
lf %>% select(2:1) %>% select(2:1)
Output
<SQL>
SELECT `df`.*
FROM `df`
---
Code
lf %>% select(2:1) %>% select(2:1) %>% select(2:1)
Output
<SQL>
SELECT `y`, `x`
FROM `df`
---
Code
lf %>% select(x1 = x) %>% select(x2 = x1)
Output
<SQL>
SELECT `x` AS `x2`
FROM `df`
# mutate collapses over nested select
Code
lf %>% mutate(a = 1, b = 2) %>% select(a)
Output
<SQL>
SELECT 1.0 AS `a`
FROM `df`
---
Code
lf %>% mutate(a = 1, b = 2) %>% select(x)
Output
<SQL>
SELECT `x`
FROM `df`
# output is styled
Code
show_query(out, sql_options = sql_options(cte = TRUE))
Output
<SQL>
[34mWITH[39m `q01` [34mAS[39m (
[34mSELECT[39m `x`, AVG(`y`) OVER (PARTITION BY `x`)[34m AS [39m`y`, `z` + 1.0[34m AS [39m`z`
[34mFROM[39m `df`
),
`q02` [34mAS[39m (
[34mSELECT[39m `q01`.*
[34mFROM[39m `q01`
[34mWHERE[39m (`z` = 1.0)
)
[34mSELECT[39m
`LHS`.`x`[34m AS [39m`x`,
`LHS`.`y`[34m AS [39m`y.x`,
`LHS`.`z`[34m AS [39m`z.x`,
`df`.`y`[34m AS [39m`y.y`,
`df`.`z`[34m AS [39m`z.y`
[34mFROM[39m `q02`[34m AS [39m`LHS`
[34mLEFT JOIN[39m `df`
[34mON[39m (`LHS`.`x` = `df`.`x`)
|