Skip to content

Commit 574ce1c

Browse files
authored
Delay encoding nested queries (#171)
When using an inner query or a join as data source in the `from` macro, don't encode it to a JSON-like map immediately. Instead, do it when we encode the outer query. This should make it easier to modify the inner query as well as the outer query before encoding.
1 parent 14a1a7f commit 574ce1c

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/panoramix/query.ex

+6-8
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,19 @@ defmodule Panoramix.Query do
142142

143143
_ ->
144144
# Are we creating a new query from scratch, given some kind of datasource?
145-
%Panoramix.Query{data_source: Panoramix.Query.datasource(source)}
145+
%Panoramix.Query{data_source: source}
146146
end
147147

148148
Map.merge(query, Map.new(query_fields))
149149
end
150150
end
151151

152-
@doc nil
153-
# exported only so that the `from` macro can call it.
154-
def datasource(datasource) when is_binary(datasource) do
152+
defp datasource(datasource) when is_binary(datasource) do
155153
# We're using a named datasource as the source for the query
156154
datasource
157155
end
158156

159-
def datasource(%{type: :query, query: nested_query} = datasource) do
157+
defp datasource(%{type: :query, query: nested_query} = datasource) do
160158
# The datasource is a nested query. Let's convert it to JSON if needed
161159
nested_query_json =
162160
case nested_query do
@@ -171,7 +169,7 @@ defmodule Panoramix.Query do
171169
%{datasource | query: nested_query_json}
172170
end
173171

174-
def datasource(%{type: :join, left: left, right: right} = datasource) do
172+
defp datasource(%{type: :join, left: left, right: right} = datasource) do
175173
# A join between two datasources.
176174
# A named datasource and a recursive join can only appear on the
177175
# left side, but let's let Druid enforce that.
@@ -180,7 +178,7 @@ defmodule Panoramix.Query do
180178
%{datasource | left: left_datasource, right: right_datasource}
181179
end
182180

183-
def datasource(%{type: type} = datasource) when is_atom(type) do
181+
defp datasource(%{type: type} = datasource) when is_atom(type) do
184182
# Some other type of datasource. Let's include it literally.
185183
datasource
186184
end
@@ -852,7 +850,7 @@ defmodule Panoramix.Query do
852850
analysisTypes: query.analysis_types,
853851
bound: query.bound,
854852
context: query.context,
855-
dataSource: query.data_source,
853+
dataSource: datasource(query.data_source),
856854
dimension: query.dimension,
857855
dimensions: query.dimensions,
858856
filter: query.filter,

0 commit comments

Comments
 (0)