Packages
dqe
0.3.1
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.25
0.3.24
0.3.23
0.3.22
0.3.21
0.3.20
0.3.19
0.3.18
0.3.17
0.3.16
0.3.15
0.3.14
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.2.2
0.2.1
0.2.0
0.1.36
0.1.35
0.1.34
0.1.33
0.1.22
0.1.21
0.1.20
0.1.10
0.1.9
DalmatinerDB query engine
Current section
Files
Jump to
Current section
Files
src/dql_expand.erl
%%%-------------------------------------------------------------------
%%% @author Heinz Nikolaus Gies <heinz@project-fifo.net>
%%% @copyright (C) 2016, Heinz Nikolaus Gies
%%% @doc
%%% Expand query parts
%%% @end
%%% Created : 2 Aug 2016 by Heinz Nikolaus Gies <heinz@licenser.net>
%%%-------------------------------------------------------------------
-module(dql_expand).
-export([expand/1]).
expand(Qs) ->
lists:flatten([expand_grouped(Q, []) || Q <- Qs]).
expand_grouped(Q = #{op := named, args := [L, S]}, Groupings) when is_list(L) ->
Gs = [N || {dvar, N} <- L],
[Q#{args => [L, S1]} || S1 <- expand_grouped(S, Gs ++ Groupings)];
expand_grouped(Q = #{op := named, args := [N, S]}, Groupings) ->
[Q#{args => [N, S1]} || S1 <- expand_grouped(S, Groupings)];
expand_grouped(Q = #{op := timeshift, args := [T, S]}, Groupings) ->
[Q#{args => [T, S1]} || S1 <- expand_grouped(S, Groupings)];
expand_grouped({calc, Fs, Q}, Groupings) ->
[{calc, Fs, Q1} || Q1 <- expand_grouped(Q, Groupings)];
expand_grouped({combine, F, Qs}, Groupings) ->
[{combine, F, lists:flatten([expand_grouped(Q, Groupings) || Q <- Qs])}];
expand_grouped(Q = #{op := get}, _Groupings) ->
[Q];
expand_grouped(Q = #{op := lookup,
args := [Collection, Metric, Where]}, []) ->
{ok, BMs} = dqe_idx:lookup({in, Collection, Metric, Where}),
Q1 = Q#{op := get},
[Q1#{args => [Bucket, Key]} || {Bucket, Key} <- BMs];
expand_grouped(Q = #{op := lookup,
args := [Collection, Metric, Where]}, Groupings) ->
{ok, BMs} = dqe_idx:lookup({in, Collection, Metric, Where}, Groupings),
Q1 = Q#{op := get},
[Q1#{args => [Bucket, Key], groupings => lists:zip(Groupings, GVs)}
|| {Bucket, Key, GVs} <- BMs];
expand_grouped(Q = #{op := lookup,
args := [Collection, Metric]}, []) ->
{ok, BMs} = dqe_idx:lookup({in, Collection, Metric}),
Q1 = Q#{op := get},
[Q1#{args => [Bucket, Key]} || {Bucket, Key} <- BMs];
expand_grouped(Q = #{op := lookup,
args := [Collection, Metric]}, Groupings) ->
{ok, BMs} = dqe_idx:lookup({in, Collection, Metric}, Groupings),
Q1 = Q#{op := get},
[Q1#{args => [Bucket, Key], groupings => lists:zip(Groupings, GVs)}
|| {Bucket, Key, GVs} <- BMs];
expand_grouped(Q = #{op := sget,
args := [Bucket, Glob]}, _Groupings) ->
%% Glob is in an extra list since expand is build to use one or more
%% globs
{ok, {_Bucket, Ms}} = dqe_idx:expand(Bucket, [Glob]),
Q1 = Q#{op := get},
[Q1#{args => [Bucket, Key]} || Key <- Ms].