File: translate-sql-conditional.md

package info (click to toggle)
r-cran-dbplyr 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,376 kB
  • sloc: sh: 13; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download
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
# case_when converted to CASE WHEN

    Code
      translate_sql(case_when(x > 1L ~ "a"))
    Output
      <SQL> CASE WHEN (`x` > 1) THEN 'a' END

# even inside mutate

    Code
      out$select[[2]]
    Output
      [1] "CASE WHEN (`x` > 1) THEN 'a' END"

# case_when translates correctly to ELSE when TRUE ~ is used 2

    Code
      translate_sql(case_when(x == 1L ~ "yes", x == 0L ~ "no", TRUE ~ "undefined"))
    Output
      <SQL> CASE WHEN (`x` = 1) THEN 'yes' WHEN (`x` = 0) THEN 'no' ELSE 'undefined' END

# case_when uses the .default arg

    Code
      translate_sql(case_when(x == 1L ~ "yes", x == 0L ~ "no", .default = "undefined"))
    Output
      <SQL> CASE WHEN (`x` = 1) THEN 'yes' WHEN (`x` = 0) THEN 'no' ELSE 'undefined' END

---

    Code
      translate_sql(case_when(x == 1L ~ "yes", x == 0L ~ "no", .default = x + 1))
    Output
      <SQL> CASE WHEN (`x` = 1) THEN 'yes' WHEN (`x` = 0) THEN 'no' ELSE `x` + 1.0 END

---

    Code
      translate_sql(case_when(x == 1L ~ "yes", x == 0L ~ "no", TRUE ~ "true",
      .default = "undefined"))
    Output
      <SQL> CASE WHEN (`x` = 1) THEN 'yes' WHEN (`x` = 0) THEN 'no' ELSE 'true' END

# case_when does not support .ptype and .size

    Code
      (expect_error(translate_sql(case_when(x == 1L ~ "yes", .ptype = character()))))
    Output
      <error/rlang_error>
      Error in `case_when()`:
      ! `.ptype` is not supported in SQL translations.
    Code
      (expect_error(translate_sql(case_when(x == 1L ~ "yes", .size = 1))))
    Output
      <error/rlang_error>
      Error in `case_when()`:
      ! `.size` is not supported in SQL translations.

# long case_when is on multiple lines

    Code
      translate_sql(case_when(x == 1L ~ "this is long", x == 0L ~ "so it should",
      TRUE ~ "be wrapped"))
    Output
      <SQL> CASE
      WHEN (`x` = 1) THEN 'this is long'
      WHEN (`x` = 0) THEN 'so it should'
      ELSE 'be wrapped'
      END

# conditionals check arguments

    Code
      translate_sql(case_when())
    Condition
      Error in `sql_case_when()`:
      ! No cases provided

---

    Code
      translate_sql(switch(x, 1L, 2L))
    Condition
      Error in `sql_switch()`:
      ! Can only have one unnamed (ELSE) input

# LHS can handle bang bang

    Code
      translate_sql(case_match(x, !!1L ~ "x"))
    Output
      <SQL> CASE WHEN (`x` IN (1)) THEN 'x' END
    Code
      translate_sql(case_match(x, !!c(1L, 2L) ~ "x"))
    Output
      <SQL> CASE WHEN (`x` IN (1, 2)) THEN 'x' END
    Code
      translate_sql(case_match(x, !!c(NA, 1L) ~ "x"))
    Output
      <SQL> CASE WHEN (`x` IN (1) OR `x` IS NULL) THEN 'x' END

# requires at least one condition

    Code
      translate_sql(case_match(x))
    Condition
      Error in `case_match()`:
      ! No cases provided

---

    Code
      translate_sql(case_match(x, NULL))
    Condition
      Error in `case_match()`:
      ! No cases provided

# `.ptype` not supported

    Code
      (expect_error(translate_sql(case_match(x, 1 ~ 1, .ptype = integer()))))
    Output
      <error/rlang_error>
      Error in `case_match()`:
      ! `.ptype` is not supported in SQL translations.

# .x must be a symbol

    Code
      (expect_error(translate_sql(case_match(1, 1 ~ 1))))
    Output
      <error/rlang_error>
      Error in `case_match()`:
      ! `.x` must be a variable or function call, not a number.