Skip to content

Commit 60e2521

Browse files
committed
unsatisfying grouping
1 parent f7208da commit 60e2521

6 files changed

Lines changed: 89 additions & 56 deletions

File tree

src/QueryOperators.jl

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function as(new_name_model_row::Pair{Symbol, <: SourceCode}; options...)
66
end
77

88
@code_instead QueryOperators.drop SourceCode Integer
9-
@translate ::typeof(QueryOperators.drop) :OFFSET
9+
@translate_default ::typeof(QueryOperators.drop) :OFFSET
1010

1111
@code_instead QueryOperators.filter SourceCode Any Expr
1212
function translate_dispatch(::typeof(QueryOperators.filter), iterator, call, call_expression; options...)
@@ -16,25 +16,73 @@ function translate_dispatch(::typeof(QueryOperators.filter), iterator, call, cal
1616
)
1717
end
1818

19+
@code_instead QueryOperators.groupby SourceCode Any Expr Any Expr
20+
21+
struct GroupOfRows{Group, Row}
22+
group::Group
23+
row::Row
24+
end
25+
26+
function key(group_of_rows::GroupOfRows)
27+
translate(getfield(group_of_rows, :group))
28+
end
29+
30+
function getproperty(group_of_rows::GroupOfRows, column_name::Symbol)
31+
getproperty(getfield(group_of_rows, :row), column_name)
32+
end
33+
34+
function length(group_of_rows::GroupOfRows)
35+
length(first(getfield(group_of_rows, :row)))
36+
end
37+
38+
function model_row_dispatch(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; options...)
39+
model = model_row(ungrouped; options...)
40+
GroupOfRows(group_function(model), model)
41+
end
42+
43+
function translate_dispatch(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; options...)
44+
# TODO: map_selector
45+
model = model_row(ungrouped; options...)
46+
SQLExpression(Symbol("GROUP BY"),
47+
translate(ungrouped; options...),
48+
translate(group_function(model).code; options...)
49+
)
50+
end
51+
1952
@code_instead QueryOperators.join SourceCode SourceCode Any Expr Any Expr Any Expr
2053
function translate_dispatch(::typeof(QueryOperators.join), source1, source2, key1, key1_expression, key2, key2_expression, combine, combine_expression; options...)
21-
model_row_1 = model_row(source1; other = true)
22-
model_row_2 = model_row(source2; other = true)
23-
SQLExpression(:ON,
24-
SQLExpression(Symbol("INNER JOIN"),
25-
SQLExpression(:SELECT,
54+
model_row_1 = model_row(source1; other = true, options...)
55+
model_row_2 = model_row(source2; other = true, options...)
56+
SQLExpression(:SELECT,
57+
SQLExpression(:ON,
58+
SQLExpression(Symbol("INNER JOIN"),
2659
translate(source1; options...),
27-
Generator(
28-
pair -> as(pair; options...),
29-
pairs(combine(model_row_1, model_row_2))
30-
)...
60+
translate(source2; other = true, options...)
3161
),
32-
translate(source2; other = true, options...)
62+
SQLExpression(:(=),
63+
translate(key1(model_row_1).code; options...),
64+
translate(key2(model_row_2).code; other = true, options...)
65+
)
3366
),
34-
SQLExpression(:(=),
35-
translate(key1(model_row_1).code; options...),
36-
translate(key2(model_row_2).code; other = true, options...)
37-
)
67+
Generator(
68+
pair -> as(pair; options...),
69+
pairs(combine(model_row_1, model_row_2))
70+
)...
71+
)
72+
end
73+
74+
@code_instead QueryOperators.map SourceCode Any Expr
75+
function model_row_dispatch(::typeof(QueryOperators.map), iterator, call, call_expression; options...)
76+
call(model_row(iterator; options...))
77+
end
78+
79+
function translate_dispatch(::typeof(QueryOperators.map), select_table, call, call_expression; options...)
80+
SQLExpression(
81+
Symbol("SELECT"), translate(select_table; options...),
82+
Generator(
83+
pair -> as(pair; options...),
84+
pairs(call(model_row(select_table; options...)))
85+
)...
3886
)
3987
end
4088

@@ -72,23 +120,8 @@ function translate_dispatch(::typeof(QueryOperators.thenby_descending), unordere
72120
)
73121
end
74122

75-
@code_instead QueryOperators.map SourceCode Any Expr
76-
function model_row_dispatch(::typeof(QueryOperators.map), iterator, call, call_expression; options...)
77-
call(model_row(iterator; options...))
78-
end
79-
80-
function translate_dispatch(::typeof(QueryOperators.map), select_table, call, call_expression; options...)
81-
SQLExpression(
82-
Symbol("SELECT"), translate(select_table; options...),
83-
Generator(
84-
pair -> as(pair; options...),
85-
pairs(call(model_row(select_table; options...)))
86-
)...
87-
)
88-
end
89-
90123
@code_instead QueryOperators.take SourceCode Any
91-
@translate ::typeof(QueryOperators.take) :LIMIT
124+
@translate_default ::typeof(QueryOperators.take) :LIMIT
92125

93126
@code_instead QueryOperators.unique SourceCode Any Expr
94127
function translate_dispatch(::typeof(QueryOperators.unique), repeated, key_function, key_function_expression; options...)

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: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
@code_instead (==) SourceCode Any
22
@code_instead (==) Any SourceCode
33
@code_instead (==) SourceCode SourceCode
4-
@translate ::typeof(==) :(=)
4+
@translate_default ::typeof(==) :(=)
55

66
@code_instead (!=) SourceCode Any
77
@code_instead (!=) Any SourceCode
88
@code_instead (!=) SourceCode SourceCode
9-
@translate ::typeof(!=) Symbol("<>")
9+
@translate_default ::typeof(!=) Symbol("<>")
1010

1111
@code_instead (!) SourceCode
12-
@translate ::typeof(!) :NOT
12+
@translate_default ::typeof(!) :NOT
1313

1414
@code_instead (&) SourceCode Any
1515
@code_instead (&) Any SourceCode
1616
@code_instead (&) SourceCode SourceCode
17-
@translate ::typeof(&) :AND
17+
@translate_default ::typeof(&) :AND
1818

1919
@code_instead (|) SourceCode Any
2020
@code_instead (|) Any SourceCode
2121
@code_instead (|) SourceCode SourceCode
22-
@translate ::typeof(|) :OR
22+
@translate_default ::typeof(|) :OR
2323

2424
@code_instead coalesce SourceCode Vararg{Any}
25-
@translate ::typeof(coalesce) :COALESCE
25+
@translate_default ::typeof(coalesce) :COALESCE
2626

2727
function get_column(source_row, column_name)
2828
SourceCode(source_row.source, Expr(:call, getproperty, source_row, column_name))
@@ -82,25 +82,28 @@ export if_else
8282
@code_instead if_else SourceCode Any SourceCode
8383
@code_instead if_else SourceCode SourceCode Any
8484
@code_instead if_else SourceCode SourceCode SourceCode
85-
@translate ::typeof(if_else) :IF
85+
@translate_default ::typeof(if_else) :IF
8686

8787
@code_instead in SourceCode Any
8888
@code_instead in Any SourceCode
8989
@code_instead in SourceCode SourceCode
90-
@translate ::typeof(in) :IN
90+
@translate_default ::typeof(in) :IN
9191

9292
@code_instead isequal SourceCode Any
9393
@code_instead isequal Any SourceCode
9494
@code_instead isequal SourceCode SourceCode
95-
@translate ::typeof(isequal) Symbol("IS NOT DISTINCT FROM")
95+
@translate_default ::typeof(isequal) Symbol("IS NOT DISTINCT FROM")
9696

9797
@code_instead isless SourceCode Any
9898
@code_instead isless Any SourceCode
9999
@code_instead isless SourceCode SourceCode
100-
@translate ::typeof(isless) :<
100+
@translate_default ::typeof(isless) :<
101+
102+
@code_instead length SourceCode
103+
@translate_default ::typeof(length) :COUNT
101104

102105
@code_instead ismissing SourceCode
103-
@translate ::typeof(ismissing) Symbol("IS NULL")
106+
@translate_default ::typeof(ismissing) Symbol("IS NULL")
104107

105108
@code_instead occursin AbstractString SourceCode
106109
@code_instead occursin Regex SourceCode
@@ -116,7 +119,7 @@ translate_dispatch(::typeof(occursin), needle::Regex, haystack; options...) =
116119
translate(haystack; options...),
117120
replace(replace(needle.pattern, r"(?<!\\)\.\*" => "%"), r"(?<!\\)\." => "_")
118121
)
119-
@translate ::typeof(occursin) :LIKE
122+
@translate_default ::typeof(occursin) :LIKE
120123

121124
@code_instead startswith SourceCode Any
122125
@code_instead startswith Any SourceCode
@@ -136,10 +139,3 @@ translate_dispatch(::typeof(startswith), full, prefix::AbstractString; options..
136139
translate(full),
137140
string(prefix, '%')
138141
)
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: 1 addition & 3 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
@@ -87,6 +85,6 @@ function translate_default(location, function_type, SQL_call)
8785
result
8886
end
8987

90-
macro translate(a_function, SQL_call)
88+
macro translate_default(a_function, SQL_call)
9189
translate_default(__source__, a_function, SQL_call) |> esc
9290
end

test/runtests.jl

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

55+
@test (database.Track |>
56+
@groupby(_.AlbumId) |>
57+
@map({AlbumId = key(_), Count = length(_.AlbumId)}) |>
58+
collect |>
59+
first).Count == 10
60+
5561
end

0 commit comments

Comments
 (0)