/
from.jl
259 lines (208 loc) · 6.74 KB
/
from.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
# From node.
abstract type AbstractSource
end
struct IterateSource <: AbstractSource
end
struct ValuesSource <: AbstractSource
columns::NamedTuple
end
struct FunctionSource <: AbstractSource
node::SQLNode
columns::Vector{Symbol}
end
_from_source(source::Union{SQLTable, IterateSource, FunctionSource, ValuesSource, Nothing}) =
source
_from_source(source::AbstractString) =
Symbol(source)
_from_source(source::Symbol) =
source === :^ ? IterateSource() : source
_from_source(::typeof(^)) =
IterateSource()
function _from_source(node::AbstractSQLNode;
columns::AbstractVector{<:Union{Symbol, AbstractString}})
source = FunctionSource(node,
!isa(columns, Vector{Symbol}) ?
Symbol[Symbol(col) for col in columns] :
columns)
seen = Set{Symbol}()
for col in source.columns
if col in seen
err = DuplicateLabelError(col, path = [From(source)])
throw(err)
end
push!(seen, col)
end
source
end
function _from_source(source)
columns = Tables.columntable(source)
length(columns) > 0 || throw(DomainError(source, "a table with at least one column is expected"))
ValuesSource(columns)
end
mutable struct FromNode <: TabularNode
source::Union{SQLTable, Symbol, IterateSource, ValuesSource, FunctionSource, Nothing}
FromNode(; source, kws...) =
new(_from_source(source; kws...))
end
FromNode(source; kws...) =
FromNode(source = source; kws...)
"""
From(; source)
From(tbl::SQLTable)
From(name::Symbol)
From(^)
From(df)
From(f::SQLNode; columns::Vector{Symbol})
From(::Nothing)
`From` outputs the content of a database table.
The parameter `source` could be one of:
* a [`SQLTable`](@ref) object;
* a `Symbol` value;
* a `^` object;
* a `DataFrame` or any Tables.jl-compatible dataset;
* A `SQLNode` representing a table-valued function. In this case, `From`
also requires a keyword parameter `columns` with a list of output columns
produced by the function.
* `nothing`.
When `source` is a symbol, it can refer to either a table in
[`SQLCatalog`](@ref) or an intermediate dataset defined with the [`With`](@ref)
node.
The `From` node is translated to a SQL query with a `FROM` clause:
```sql
SELECT ...
FROM \$source
```
`From(^)` must be a component of [`Iterate`](@ref). In the context of
[`Iterate`](@ref), it refers to the output of the previous iteration.
`From(::DataFrame)` is translated to a `VALUES` clause.
`From(nothing)` emits a dataset with one row and no columns and can usually
be omitted.
# Examples
*List all patients.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(person);
julia> print(render(q))
SELECT
"person_1"."person_id",
"person_1"."year_of_birth"
FROM "person" AS "person_1"
```
*List all patients.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(:person);
julia> print(render(q, tables = [person]))
SELECT
"person_1"."person_id",
"person_1"."year_of_birth"
FROM "person" AS "person_1"
```
*Show all patients diagnosed with essential hypertension.*
```jldoctest
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> condition_occurrence =
SQLTable(:condition_occurrence,
columns = [:condition_occurrence_id, :person_id, :condition_concept_id]);
julia> q = From(:person) |>
Where(Fun.in(Get.person_id, From(:essential_hypertension) |>
Select(Get.person_id))) |>
With(:essential_hypertension =>
From(:condition_occurrence) |>
Where(Get.condition_concept_id .== 320128));
julia> print(render(q, tables = [person, condition_occurrence]))
WITH "essential_hypertension_1" ("person_id") AS (
SELECT "condition_occurrence_1"."person_id"
FROM "condition_occurrence" AS "condition_occurrence_1"
WHERE ("condition_occurrence_1"."condition_concept_id" = 320128)
)
SELECT
"person_1"."person_id",
"person_1"."year_of_birth"
FROM "person" AS "person_1"
WHERE ("person_1"."person_id" IN (
SELECT "essential_hypertension_2"."person_id"
FROM "essential_hypertension_1" AS "essential_hypertension_2"
))
```
*Show the current date.*
```jldoctest
julia> q = From(nothing) |>
Select(Fun.current_date());
julia> print(render(q))
SELECT CURRENT_DATE AS "current_date"
julia> q = Select(Fun.current_date());
julia> print(render(q))
SELECT CURRENT_DATE AS "current_date"
```
Query a `DataFrame`.
```jldoctest
julia> df = DataFrame(name = ["SQL", "Julia", "FunSQL"],
year = [1974, 2012, 2021]);
julia> q = From(df) |>
Group() |>
Select(Agg.min(Get.year), Agg.max(Get.year));
julia> print(render(q))
SELECT
min("values_1"."year") AS "min",
max("values_1"."year") AS "max"
FROM (
VALUES
(1974),
(2012),
(2021)
) AS "values_1" ("year")
```
Parse comma-separated numbers.
```jldoctest
julia> q = From(Fun.regexp_matches("2,3,5,7,11", "(\\\\d+)", "g"),
columns = [:captures]) |>
Select(Fun."CAST(?[1] AS INTEGER)"(Get.captures));
julia> print(render(q, dialect = :postgresql))
SELECT CAST("regexp_matches_1"."captures"[1] AS INTEGER) AS "_"
FROM regexp_matches('2,3,5,7,11', '(\\d+)', 'g') AS "regexp_matches_1" ("captures")
```
"""
From(args...; kws...) =
FromNode(args...; kws...) |> SQLNode
const funsql_from = From
dissect(scr::Symbol, ::typeof(From), pats::Vector{Any}) =
dissect(scr, FromNode, pats)
Base.convert(::Type{AbstractSQLNode}, source::SQLTable) =
FromNode(source)
function PrettyPrinting.quoteof(n::FromNode, ctx::QuoteContext)
source = n.source
if source isa SQLTable
tex = get(ctx.vars, source, nothing)
if tex === nothing
tex = quoteof(source, limit = true)
end
Expr(:call, nameof(From), tex)
elseif source isa Symbol
Expr(:call, nameof(From), QuoteNode(source))
elseif source isa IterateSource
Expr(:call, nameof(From), :^)
elseif source isa ValuesSource
Expr(:call, nameof(From), quoteof(source.columns, ctx))
elseif source isa FunctionSource
Expr(:call, nameof(From),
quoteof(source.node, ctx),
Expr(:kw, :columns, Expr(:vect, [QuoteNode(col) for col in source.columns]...)))
else
Expr(:call, nameof(From), source)
end
end
function label(n::FromNode)
source = n.source
if source isa SQLTable
source.name
elseif source isa Symbol
source
elseif source isa ValuesSource
:values
elseif source isa FunctionSource
label(source.node)
else
:_
end
end