View Source Panoramix.Query (Panoramix v0.23.0)

Provides functions for building Druid query requests.

Summary

Functions

Use from macro to build Druid queries. See Druid documentation to learn about available fields and general query object structure.

Convert a Panoramix.Query struct into its JSON representation.

Convert a Panoramix.Query struct into a map ready to be converted to JSON.

Types

@type t() :: %Panoramix.Query{
  aggregations: term(),
  analysis_types: term(),
  bound: term(),
  context: term(),
  data_source: term(),
  dimension: term(),
  dimensions: term(),
  filter: term(),
  granularity: term(),
  intervals: term(),
  limit: term(),
  limit_spec: term(),
  merge: term(),
  metric: term(),
  post_aggregations: term(),
  query: term(),
  query_type: term(),
  search_dimensions: term(),
  sort: term(),
  subtotals_spec: term(),
  threshold: term(),
  to_include: term(),
  virtual_columns: term()
}

Functions

Link to this macro

from(source, kw)

View Source (since 1.0.0) (macro)

Use from macro to build Druid queries. See Druid documentation to learn about available fields and general query object structure.

Example

    iex(1)> use Panoramix
    Panoramix.Query
    iex(2)> q = from "my_datasource",
    ...(2)>       query_type: "timeseries",
    ...(2)>       intervals: ["2019-03-01T00:00:00+00:00/2019-03-04T00:00:00+00:00"],
    ...(2)>       granularity: :day,
    ...(2)>       filter: dimensions.foo == "bar",
    ...(2)>        aggregations: [event_count: count(),
    ...(2)>                       unique_id_count: hyperUnique(:user_unique)]
    %Panoramix.Query{
      aggregations: [
        %{name: :event_count, type: "count"},
        %{fieldName: :user_unique, name: :unique_id_count, type: :hyperUnique}
      ],
      analysis_types: nil,
      bound: nil,
      context: %{priority: 0, timeout: 120000},
      data_source: "my_datasource",
      dimension: nil,
      dimensions: nil,
      filter: %{dimension: "foo", type: "selector", value: "bar"},
      granularity: :day,
      intervals: ["2019-03-01T00:00:00+00:00/2019-03-04T00:00:00+00:00"],
      limit: nil,
      limit_spec: nil,
      merge: nil,
      metric: nil,
      post_aggregations: nil,
      query: nil,
      query_type: "timeseries",
      search_dimensions: nil,
      sort: nil,
      threshold: nil,
      to_include: nil,
      virtual_columns: nil,
      subtotals_spec: nil
    }

Some HLL aggregation names are capitalized and therefore won't play well with the macro. For such cases use their aliases as a workaround: hllSketchBuild, hllSketchMerge, hllSketchEstimate, hllSketchUnion, hllSketchToString.

The aggregation aliases will be replaced with original names when building a query.

Example

    iex(1)> use Panoramix
    Panoramix.Query
    iex(2)> query = from "my_datasource",
    ...(2)>       query_type: "timeseries",
    ...(2)>       intervals: ["2018-05-29T00:00:00+00:00/2018-06-05T00:00:00+00:00"],
    ...(2)>       granularity: :day,
    ...(2)>       aggregations: [event_count: count(),
    ...(2)>                     unique_ids: hllSketchMerge(:user_unique, round: true)]
    %Panoramix.Query{
      aggregations: [
        %{name: :event_count, type: "count"},
        %{
          fieldName: :user_unique,
          name: :unique_ids,
          round: true,
          type: "HLLSketchMerge"
        }
      ],
      ...
    }

Modifying a query

When the first argument to from is a Panoramix.Query struct, the return value is a copy of the struct where the specified fields have been changed.

For most fields, the values passed as arguments to from overwrite the existing values in the struct. For aggregations, post_aggregations and virtual_columns, the values are combined instead. Entries with the same name are overwritten, and other entries are added to the list.

In the example below, the intervals field is replaced, and the event_count aggregator is modified. The other aggregator as well as all other fields stay the same.

    iex> original_query = from "my_datasource",
    ...>       query_type: "timeseries",
    ...>       intervals: ["2018-05-29T00:00:00+00:00/2018-06-05T00:00:00+00:00"],
    ...>       aggregations: [event_count: count(),
    ...>                      unique_ids: hllSketchMerge(:user_unique, round: true)],
    ...>       post_aggregations: [event_count_per_user: aggregations.event_count / hllSketchEstimate(aggregations.unique_ids)
    %Panoramix.Query{
      intervals: ["2018-05-29T00:00:00+00:00/2018-06-05T00:00:00+00:00"],
      aggregations: [
        %{name: :event_count, type: "count"},
        %{name: :unique_ids, type: "HLLSketchMerge", fieldName: :user_unique, round: true}
      ],
      post_aggregations: [
        %{name: :event_count_per_user, type: "arithmetic", fn: :/, fields: [
          %{fieldName: :event_count, type: "fieldAccess"},
          %{type: "HLLSketchEstimate", field: %{fieldName: :unique_ids, type: "fieldAccess"}}
        ]}
      ]
    }
    iex> new_query = from original_query,
    ...>       intervals: ["2018-06-29T00:00:00+00:00/2018-07-05T00:00:00+00:00"],
    ...>       aggregations: [event_count: longSum(:count)]
    %Panoramix.Query{
      intervals: ["2018-06-29T00:00:00+00:00/2018-07-05T00:00:00+00:00"],
      aggregations: [
        %{name: :event_count, type: "longSum", fieldName: :count},
        %{fieldName: :user_unique, name: :unique_ids, round: true, type: "HLLSketchMerge"}
      ],
      post_aggregations: [
        %{name: :event_count_per_user, type: "arithmetic", fn: :/, fields: [
          %{fieldName: :event_count, type: "fieldAccess"},
          %{type: "HLLSketchEstimate", field: %{fieldName: :unique_ids, type: "fieldAccess"}}
        ]}
      ]
    }
Link to this function

merge_query_field(key, old_value, new_value)

View Source
Link to this function

post_aggregation_field_accessor(type_name, accessor_name, accessor, options \\ [])

View Source

Convert a Panoramix.Query struct into its JSON representation.

Convert a Panoramix.Query struct into a map ready to be converted to JSON.