Skip to content

Commit 532b3ad

Browse files
committed
Merge commit '60e2521'
2 parents 6e8dbab + 60e2521 commit 532b3ad

6 files changed

Lines changed: 78 additions & 41 deletions

File tree

src/QueryOperators.jl

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,79 @@ function translate_dispatch(::typeof(QueryOperators.filter), iterator, call, cal
1717
end
1818

1919
@code_instead QueryOperators.groupby SourceCode Any Expr Any Expr
20+
<<<<<<< HEAD
2021
function translate_dispatch(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; options...)
2122
SQLExpression(Symbol("GROUP BY"),
2223
translate(ungrouped; options...),
2324
translate(group_function(model_row(ungrouped)).code; options...)
25+
=======
26+
27+
struct GroupOfRows{Group, Row}
28+
group::Group
29+
row::Row
30+
end
31+
32+
function key(group_of_rows::GroupOfRows)
33+
translate(getfield(group_of_rows, :group))
34+
end
35+
36+
function getproperty(group_of_rows::GroupOfRows, column_name::Symbol)
37+
getproperty(getfield(group_of_rows, :row), column_name)
38+
end
39+
40+
function length(group_of_rows::GroupOfRows)
41+
length(first(getfield(group_of_rows, :row)))
42+
end
43+
44+
function model_row_dispatch(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; options...)
45+
model = model_row(ungrouped; options...)
46+
GroupOfRows(group_function(model), model)
47+
end
48+
49+
function translate_dispatch(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; options...)
50+
# TODO: map_selector
51+
model = model_row(ungrouped; options...)
52+
SQLExpression(Symbol("GROUP BY"),
53+
translate(ungrouped; options...),
54+
translate(group_function(model).code; options...)
55+
>>>>>>> 60e2521
2456
)
2557
end
2658

2759
@code_instead QueryOperators.join SourceCode SourceCode Any Expr Any Expr Any Expr
2860
function translate_dispatch(::typeof(QueryOperators.join), source1, source2, key1, key1_expression, key2, key2_expression, combine, combine_expression; options...)
29-
model_row_1 = model_row(source1; other = true)
30-
model_row_2 = model_row(source2; other = true)
31-
SQLExpression(:ON,
32-
SQLExpression(Symbol("INNER JOIN"),
33-
SQLExpression(:SELECT,
61+
model_row_1 = model_row(source1; other = true, options...)
62+
model_row_2 = model_row(source2; other = true, options...)
63+
SQLExpression(:SELECT,
64+
SQLExpression(:ON,
65+
SQLExpression(Symbol("INNER JOIN"),
3466
translate(source1; options...),
35-
Generator(
36-
pair -> as(pair; options...),
37-
pairs(combine(model_row_1, model_row_2))
38-
)...
67+
translate(source2; other = true, options...)
3968
),
40-
translate(source2; other = true, options...)
69+
SQLExpression(:(=),
70+
translate(key1(model_row_1).code; options...),
71+
translate(key2(model_row_2).code; other = true, options...)
72+
)
4173
),
42-
SQLExpression(:(=),
43-
translate(key1(model_row_1).code; options...),
44-
translate(key2(model_row_2).code; other = true, options...)
45-
)
74+
Generator(
75+
pair -> as(pair; options...),
76+
pairs(combine(model_row_1, model_row_2))
77+
)...
78+
)
79+
end
80+
81+
@code_instead QueryOperators.map SourceCode Any Expr
82+
function model_row_dispatch(::typeof(QueryOperators.map), iterator, call, call_expression; options...)
83+
call(model_row(iterator; options...))
84+
end
85+
86+
function translate_dispatch(::typeof(QueryOperators.map), select_table, call, call_expression; options...)
87+
SQLExpression(
88+
Symbol("SELECT"), translate(select_table; options...),
89+
Generator(
90+
pair -> as(pair; options...),
91+
pairs(call(model_row(select_table; options...)))
92+
)...
4693
)
4794
end
4895

@@ -80,21 +127,6 @@ function translate_dispatch(::typeof(QueryOperators.thenby_descending), unordere
80127
)
81128
end
82129

83-
@code_instead QueryOperators.map SourceCode Any Expr
84-
function model_row_dispatch(::typeof(QueryOperators.map), iterator, call, call_expression; options...)
85-
call(model_row(iterator; options...))
86-
end
87-
88-
function translate_dispatch(::typeof(QueryOperators.map), select_table, call, call_expression; options...)
89-
SQLExpression(
90-
Symbol("SELECT"), translate(select_table; options...),
91-
Generator(
92-
pair -> as(pair; options...),
93-
pairs(call(model_row(select_table; options...)))
94-
)...
95-
)
96-
end
97-
98130
@code_instead QueryOperators.take SourceCode Any
99131
@translate_default ::typeof(QueryOperators.take) :LIMIT
100132

src/QuerySQLite.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module QuerySQLite
22

3-
import Base: !, &, |, ==, !=, coalesce, collect, eltype, getproperty, in,
4-
isdone, isequal, isless, ismissing, iterate, IteratorSize, occursin, show,
3+
import Base: !, &, |, ==, !=, coalesce, collect, eltype, getproperty, length,
4+
in, isdone, isequal, isless, ismissing, iterate, IteratorSize, occursin, show,
55
showerror, startswith
66
using Base: Generator, NamedTuple, RefValue, SizeUnknown, tail
77
using Base.Meta: quot

src/library.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export if_else
9898
@code_instead isless Any SourceCode
9999
@code_instead isless SourceCode SourceCode
100100
@translate_default ::typeof(isless) :<
101+
<<<<<<< HEAD
102+
=======
103+
104+
@code_instead length SourceCode
105+
@translate_default ::typeof(length) :COUNT
106+
>>>>>>> 60e2521
101107

102108
@code_instead ismissing SourceCode
103109
@translate_default ::typeof(ismissing) Symbol("IS NULL")
@@ -136,10 +142,3 @@ translate_dispatch(::typeof(startswith), full, prefix::AbstractString; options..
136142
translate(full),
137143
string(prefix, '%')
138144
)
139-
140-
translate_dispatch(::typeof(startswith), full, prefix::AbstractString; options...) =
141-
SQLExpression(
142-
:LIKE,
143-
translate(full),
144-
string(prefix, '%')
145-
)

src/realize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function realize(something)
7575
end
7676

7777
function realize(sql_expression::SQLExpression)
78-
if in(sql_expression.call, (:COALESCE,))
78+
if in(sql_expression.call, (:COALESCE, :COUNT))
7979
function_call(sql_expression)
8080
elseif sql_expression.call == :IF && length(sql_expression.arguments) == 3
8181
string("CASE WHEN", realize(sql_expressions.arguments[1]),

src/translate.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ end
6868
function translate(source_row::SourceRow; options...)
6969
source_row.table_name
7070
end
71-
function translate(source_row::SourceOtherRow; options...)
72-
end
7371
function translate(node::Expr; options...)
7472
translate_dispatch(split_node(node)...; options...)
7573
end

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ database2 = Database(SQLite.DB(filename))
5252
DataTable |>
5353
length == 347
5454

55+
<<<<<<< HEAD
5556
# database.Track |>
5657
# @groupby(_.AlbumId) |>
5758
# @map({AlbumId = key(_), Count = length(_)})
59+
=======
60+
@test (database.Track |>
61+
@groupby(_.AlbumId) |>
62+
@map({AlbumId = key(_), Count = length(_.AlbumId)}) |>
63+
collect |>
64+
first).Count == 10
65+
>>>>>>> 60e2521
5866

5967
end

0 commit comments

Comments
 (0)