/
function.jl
279 lines (215 loc) · 9.29 KB
/
function.jl
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
270
271
272
273
274
275
276
277
278
279
# Function and operator calls.
mutable struct FunctionNode <: AbstractSQLNode
name::Symbol
args::Vector{SQLNode}
function FunctionNode(;
name::Union{Symbol, AbstractString},
args = SQLNode[])
n = new(Symbol(name), args)
renameoperators!(n)
checkarity!(n)
n
end
end
# Rename Julia operators to SQL equivalents.
function renameoperators!(n::FunctionNode)
if n.name === :(==)
n.name = Symbol("=")
elseif n.name === :(!=)
n.name = Symbol("<>")
elseif n.name === :(||)
n.name = :or
elseif n.name === :(&&)
n.name = :and
elseif n.name === :(!)
n.name = :not
end
end
FunctionNode(name; args = SQLNode[]) =
FunctionNode(name = name, args = args)
FunctionNode(name, args...) =
FunctionNode(name = name, args = SQLNode[args...])
"""
Fun(; name, args = [])
Fun(name; args = [])
Fun(name, args...)
Fun.name(args...)
Application of a SQL function or a SQL operator.
A `Fun` node is also generated by broadcasting on `SQLNode` objects.
Names of Julia operators (`==`, `!=`, `&&`, `||`, `!`) are replaced with
their SQL equivalents (`=`, `<>`, `and`, `or`, `not`).
If `name` contains only symbols, or if `name` starts or ends with a space,
the `Fun` node is translated to a SQL operator.
If `name` contains one or more `?` characters, it serves as a template of
a SQL expression where `?` symbols are replaced with the given arguments.
Use `??` to represent a literal `?` mark. Wrap the template in parentheses
if this is necessary to make the SQL expression unambiguous.
Certain names have a customized translation in order to generate common SQL
functions and operators with irregular syntax:
| `Fun` node | SQL syntax |
|:----------------------------- |:------------------------------------------|
| `Fun.and(p₁, p₂, …)` | `p₁ AND p₂ AND …` |
| `Fun.between(x, y, z)` | `x BETWEEN y AND z` |
| `Fun.case(p, x, …)` | `CASE WHEN p THEN x … END` |
| `Fun.cast(x, "TYPE")` | `CAST(x AS TYPE)` |
| `Fun.concat(s₁, s₂, …)` | dialect-specific, e.g., `(s₁ \\|\\| s₂ \\|\\| …)` |
| `Fun.current_date()` | `CURRENT_DATE` |
| `Fun.current_timestamp()` | `CURRENT_TIMESTAMP` |
| `Fun.exists(q)` | `EXISTS q` |
| `Fun.extract("FIELD", x)` | `EXTRACT(FIELD FROM x)` |
| `Fun.in(x, q)` | `x IN q` |
| `Fun.in(x, y₁, y₂, …)` | `x IN (y₁, y₂, …)` |
| `Fun.is_not_null(x)` | `x IS NOT NULL` |
| `Fun.is_null(x)` | `x IS NULL` |
| `Fun.like(x, y)` | `x LIKE y` |
| `Fun.not(p)` | `NOT p` |
| `Fun.not_between(x, y, z)` | `x NOT BETWEEN y AND z` |
| `Fun.not_exists(q)` | `NOT EXISTS q` |
| `Fun.not_in(x, q)` | `x NOT IN q` |
| `Fun.not_in(x, y₁, y₂, …)` | `x NOT IN (y₁, y₂, …)` |
| `Fun.not_like(x, y)` | `x NOT LIKE y` |
| `Fun.or(p₁, p₂, …)` | `p₁ OR p₂ OR …` |
# Examples
*Replace missing values with N/A.*
```jldoctest
julia> location = SQLTable(:location, columns = [:location_id, :city]);
julia> q = From(:location) |>
Select(Fun.coalesce(Get.city, "N/A"));
julia> print(render(q, tables = [location]))
SELECT coalesce("location_1"."city", 'N/A') AS "coalesce"
FROM "location" AS "location_1"
```
*Find patients not born in 1980.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(:person) |>
Where(Get.year_of_birth .!= 1980);
julia> print(render(q, tables = [person]))
SELECT
"person_1"."person_id",
"person_1"."year_of_birth"
FROM "person" AS "person_1"
WHERE ("person_1"."year_of_birth" <> 1980)
```
*For each patient, show their age in 2000.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(:person) |>
Select(Fun."-"(2000, Get.year_of_birth));
julia> print(render(q, tables = [person]))
SELECT (2000 - "person_1"."year_of_birth") AS "_"
FROM "person" AS "person_1"
```
*Find invalid zip codes.*
```jldoctest
julia> location = SQLTable(:location, columns = [:location_id, :zip]);
julia> q = From(:location) |>
Select(Fun." NOT SIMILAR TO '[0-9]{5}'"(Get.zip));
julia> print(render(q, tables = [location]))
SELECT ("location_1"."zip" NOT SIMILAR TO '[0-9]{5}') AS "_"
FROM "location" AS "location_1"
```
*Extract the first 3 digits of the zip code.*
```jldoctest
julia> location = SQLTable(:location, columns = [:location_id, :zip]);
julia> q = From(:location) |>
Select(Fun."SUBSTRING(? FROM ? FOR ?)"(Get.zip, 1, 3));
julia> print(render(q, tables = [location]))
SELECT SUBSTRING("location_1"."zip" FROM 1 FOR 3) AS "_"
FROM "location" AS "location_1"
```
"""
Fun(args...; kws...) =
FunctionNode(args...; kws...) |> SQLNode
const funsql_fun = Fun
dissect(scr::Symbol, ::typeof(Fun), pats::Vector{Any}) =
dissect(scr, FunctionNode, pats)
transliterate(::typeof(Fun), name::Symbol, ctx::TransliterateContext, @nospecialize(args...)) =
Fun(name, args = [transliterate(SQLNode, arg, ctx) for arg in args])
PrettyPrinting.quoteof(n::FunctionNode, ctx::QuoteContext) =
Expr(:call,
Expr(:., nameof(Fun),
QuoteNode(Base.isidentifier(n.name) ? n.name : string(n.name))),
quoteof(n.args, ctx)...)
label(n::FunctionNode) =
Meta.isidentifier(n.name) ? n.name : :_
# Notation for making function nodes.
struct FunClosure
name::Symbol
end
FunClosure(name::AbstractString) =
FunClosure(Symbol(name))
Base.show(io::IO, f::FunClosure) =
print(io, Expr(:., nameof(Fun),
QuoteNode(Base.isidentifier(f.name) ? f.name : string(f.name))))
Base.getproperty(::typeof(Fun), name::Symbol) =
FunClosure(name)
Base.getproperty(::typeof(Fun), name::AbstractString) =
FunClosure(name)
(f::FunClosure)(args...) =
Fun(f.name, args = SQLNode[args...])
(f::FunClosure)(; args = SQLNode[]) =
Fun(f.name, args = args)
# Common SQL functions and operators.
const var"funsql_&&" = FunClosure(:and)
const var"funsql_||" = FunClosure(:or)
const var"funsql_!" = FunClosure(:not)
const var"funsql_==" = FunClosure("=")
const var"funsql_!=" = FunClosure("<>")
const var"funsql_===" = FunClosure(" IS NOT DISTINCT FROM ")
const var"funsql_!==" = FunClosure(" IS DISTINCT FROM ")
const var"funsql_>" = FunClosure(">")
const var"funsql_>=" = FunClosure(">=")
const var"funsql_<" = FunClosure("<")
const var"funsql_<=" = FunClosure("<=")
const var"funsql_+" = FunClosure("+")
const var"funsql_-" = FunClosure("-")
const var"funsql_*" = FunClosure("*")
const var"funsql_/" = FunClosure("/")
const funsql_between = FunClosure(:between)
const funsql_case = FunClosure(:case)
const funsql_cast = FunClosure(:cast)
const funsql_coalesce = FunClosure(:coalesce)
const funsql_concat = FunClosure(:concat)
const funsql_current_date = FunClosure(:current_date)
const funsql_current_timestamp = FunClosure(:current_timestamp)
const funsql_exists = FunClosure(:exists)
const funsql_extract = FunClosure(:extract)
const funsql_in = FunClosure(:in)
const funsql_is_not_null = FunClosure(:is_not_null)
const funsql_is_null = FunClosure(:is_null)
const funsql_like = FunClosure(:like)
const funsql_not_between = FunClosure(:not_between)
const funsql_not_exists = FunClosure(:not_exists)
const funsql_not_in = FunClosure(:not_in)
const funsql_not_like = FunClosure(:not_like)
# Broadcasting notation.
struct FunStyle <: Base.BroadcastStyle
end
Base.BroadcastStyle(::Type{<:AbstractSQLNode}) =
FunStyle()
Base.BroadcastStyle(::FunStyle, ::Base.Broadcast.DefaultArrayStyle{0}) =
FunStyle()
Base.broadcastable(n::AbstractSQLNode) =
n
Base.Broadcast.instantiate(bc::Base.Broadcast.Broadcasted{FunStyle}) =
bc
Base.copy(bc::Base.Broadcast.Broadcasted{FunStyle}) =
Fun(nameof(bc.f), args = SQLNode[bc.args...])
Base.convert(::Type{AbstractSQLNode}, bc::Base.Broadcast.Broadcasted{FunStyle}) =
FunctionNode(nameof(bc.f), args = SQLNode[bc.args...])
# Broadcasting over && and ||.
module DUMMY_CONNECTIVES
function var"&&" end
function var"||" end
end
if VERSION >= v"1.7"
Base.Broadcast.broadcasted(::Base.Broadcast.AndAnd,
arg1::Union{Base.Broadcast.Broadcasted{FunStyle}, AbstractSQLNode},
arg2::Union{Base.Broadcast.Broadcasted{FunStyle}, AbstractSQLNode}) =
Base.Broadcast.broadcasted(DUMMY_CONNECTIVES.var"&&", arg1, arg2)
Base.Broadcast.broadcasted(::Base.Broadcast.OrOr,
arg1::Union{Base.Broadcast.Broadcasted{FunStyle}, AbstractSQLNode},
arg2::Union{Base.Broadcast.Broadcasted{FunStyle}, AbstractSQLNode}) =
Base.Broadcast.broadcasted(DUMMY_CONNECTIVES.var"||", arg1, arg2)
end