View Source Plausible.Stats.SQL.Fragments (Plausible v0.0.1)

Various macros and common SQL fragments used in Stats code.

Summary

Functions

Returns value of a key (usually property) under meta.value array or similar.

Returns whether a key (usually property) exists under meta.key array or similar.

Macro that helps join two Ecto queries by selecting fields from either one

Convenience Ecto macro for wrapping select_merge where each value gets in turn passed to selected_as.

Converts time or date and time to the specified timezone.

Returns the weekstart for date. If the weekstart is before the not_before boundary, not_before is returned.

Convenience Ecto macro for wrapping a map passed to select_merge_as such that each expression gets wrapped in dynamic and set as selected_as.

Functions

Link to this macro

coalesce_string(fieldA, fieldB)

View Source (macro)
Link to this macro

get_by_key(table, meta_column, key)

View Source (macro)

Returns value of a key (usually property) under meta.value array or similar.

This macro is used for operating on custom properties. Callsites should also check whether key exists first in SQL via has_key macro.

Examples

get_by_key(e, :meta, "some_property_name") expands to SQL meta.value[indexOf(meta.key, "some_property")]

Link to this macro

has_key(table, meta_column, key)

View Source (macro)

Returns whether a key (usually property) exists under meta.key array or similar.

This macro is used for operating on custom properties.

Examples

has_key(e, :meta, "some_property_name") expands to SQL has(meta.key, "some_property_name")

Link to this macro

sample_percent()

View Source (macro)
Link to this macro

select_join_fields(q, query, list, table_name)

View Source (macro)

Macro that helps join two Ecto queries by selecting fields from either one

Link to this macro

select_merge_as(q, binding, map_literal)

View Source (macro)

Convenience Ecto macro for wrapping select_merge where each value gets in turn passed to selected_as.

Examples

iex> select_merge_as(q, [t], %{ foo: t.column }) |> expand_macro_once "select_merge(q, [], ^wrap_alias([t], %{foo: t.column}))"

Link to this macro

to_timezone(date, timezone)

View Source (macro)

Converts time or date and time to the specified timezone.

Reference: https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/#totimezone

Link to this macro

visit_duration()

View Source (macro)
Link to this macro

weekstart_not_before(date, not_before)

View Source (macro)

Returns the weekstart for date. If the weekstart is before the not_before boundary, not_before is returned.

Examples

In this pseudo-code example, the fragment returns the weekstart. The not_before boundary is set to the past Saturday, which is before the weekstart, therefore the cap does not apply.

  > this_wednesday = ~D[2022-11-09]
  > past_saturday = ~D[2022-11-05]
  > weekstart_not_before(this_wednesday, past_saturday)
  ~D[2022-11-07]

In this other example, the fragment returns Tuesday and not the weekstart. The not_before boundary is set to Tuesday, which is past the weekstart, therefore the cap applies.

  > this_wednesday = ~D[2022-11-09]
  > this_tuesday = ~D[2022-11-08]
  > weekstart_not_before(this_wednesday, this_tuesday)
  ~D[2022-11-08]
Link to this macro

wrap_alias(binding, map_literal)

View Source (macro)

Convenience Ecto macro for wrapping a map passed to select_merge_as such that each expression gets wrapped in dynamic and set as selected_as.

Examples

iex> wrap_alias([t], %{ foo: t.column }) |> expand_macro_once "%{foo: dynamic([t], selected_as(t.column, :foo))}"